BZOJ 2120 數顏色
阿新 • • 發佈:2019-05-14
bits move spa sizeof temp long long lin amp esp
帶修莫隊
因為莫隊是強行離線,所以修改的話需要加一個時間戳,每次詢問的時候暴力還原或者更新某個位置的數就行了,但是這樣只能單點修改,區間修改好像是不資瓷的。
在給詢問分塊的時候,不能再讓每塊的長度是sqrt(n)了,最佳的長度經過神牛們證明是n^2/3,具體證明就看看網上的說明吧QAQ
#include <bits/stdc++.h> #define INF 0x3f3f3f3f #define full(a, b) memset(a, b, sizeof a) using namespace std; typedef long long ll; inline int lowbit(int x){ return x & (-x); } inline int read(){ int X = 0, w = 0; char ch = 0; while(!isdigit(ch)) { w |= ch == '-'; ch = getchar(); } while(isdigit(ch)) X = (X << 3) + (X << 1) + (ch ^ 48), ch = getchar(); return w ? -X : X; } inline int gcd(int a, int b){ return b ? gcd(b, a % b) : a; } inline int lcm(int a, int b){ return a / gcd(a, b) * b; } template<typename T> inline T max(T x, T y, T z){ return max(max(x, y), z); } template<typename T> inline T min(T x, T y, T z){ return min(min(x, y), z); } template<typename A, typename B, typename C> inline A fpow(A x, B p, C lyd){ A ans = 1; for(; p; p >>= 1, x = 1LL * x * x % lyd)if(p & 1)ans = 1LL * x * ans % lyd; return ans; } const int N = 1000005; int n, m, k, q, t, a[N], pos[N], ans, freq[N], res[N]; struct Query{ int l, r, t, id; bool operator < (const Query &rhs) const { if(pos[l] != pos[rhs.l]) return l < rhs.l; if(pos[r] != pos[rhs.r]) return r < rhs.r; return t < rhs.t; } }query[N]; struct Modify{ int p, pre, suc; }modify[N]; void add(int k){ freq[a[k]] ++; if(freq[a[k]] == 1) ans ++; } void remove(int k){ freq[a[k]] --; if(freq[a[k]] == 0) ans --; } int main(){ n = read(), m = read(); for(int i = 1; i <= n; i ++) a[i] = read(); t = (int)pow(n, 2.0/3); //t = (int)sqrt(n); for(int i = 1; i <= m; i ++){ char opt[3]; scanf("%s", opt); if(opt[0] == 'R'){ k ++; modify[k].p = read(), modify[k].suc = read(); modify[k].pre = a[modify[k].p]; a[modify[k].p] = modify[k].suc; } else{ q ++; query[q].l = read(), query[q].r = read(); query[q].id = q, query[q].t = k; pos[query[q].l] = (query[q].l - 1) / t + 1; pos[query[q].r] = (query[q].r - 1) / t + 1; } } for(int i = k; i >= 1; i --) a[modify[i].p] = modify[i].pre; sort(query + 1, query + q + 1); int l = 1, r = 0, ti = 0; for(int i = 1; i <= q; i ++){ int curL = query[i].l, curR = query[i].r, curT = query[i].t; while(l < curL) remove(l ++); while(r < curR) add(++ r); while(l > curL) add(-- l); while(r > curR) remove(r --); while(ti < curT){ ti ++; int ps = modify[ti].p; if(l <= ps && ps <= r) remove(ps); a[ps] = modify[ti].suc; if(l <= ps && ps <= r) add(ps); } while(ti > curT){ int ps = modify[ti].p; if(l <= ps && ps <= r) remove(ps); a[ps] = modify[ti].pre; if(l <= ps && ps <= r) add(ps); ti --; } res[query[i].id] = ans; } for(int i = 1; i <= q; i ++){ printf("%d\n", res[i]); } return 0; }
BZOJ 2120 數顏色