Cancer Data card common MO team!!
This problem is similar to the ordinary team, but one more modification operation.
So with the repair team was born.
The normal team records the left and right end points, then a timeline is recorded here, indicating that several modifications have been performed at that time.
After that, the MO team template has six while, the first four are the same.
The last two relationships that determine the current number of changes and the number of targets modified. This is the soul.
Focus on the changes on this timeline.
void change(int idx, int i){ if(q[i].l <= c[idx].p && c[idx].p <= q[i].r)// 当修改点在询问区间内才需要维护答案 { del(a[c[idx].p]); add(c[idx].col); } std::swap(a[c[idx].p], c[idx].col);// 很巧妙的一个操作,这样操作答案不变,程序更简洁}
Full code:
#include <cstdio> #include <cmath> #include <algorithm>const int maxn = 50005;struct query{int L, R, ID, Pre;} Q[maxn];int q_tot;struct change{int p, col;} c[maxn];int c_tot;int belong[maxn], block;int a[maxn];int out[maxn];int N , M;int L, R, Res, changed;int cnt[1000005];bool cmp (query x, query y)//Sort by the block of left and right endpoints, then sort by time {if (BELONG[X.L]! = Belong[y . l]) return BELONG[X.L] < BELONG[Y.L]; if (BELONG[X.R]! = BELONG[Y.R]) return BELONG[X.R] < BELONG[Y.R]; return X.pre < Y.pre;} int read () {int ans = 0, s = 1; char ch = getchar (); while (Ch > ' 9 ' | | ch < ' 0 ') {if (ch = = '-') s =-1; ch = GetChar ();} while (Ch >= ' 0 ' && ch <= ' 9 ') ans = (ans << 3) + (ans << 1) + CH-' 0 ', ch = getchar (); return s * ans;} void Add (int x) {if (++cnt[x] = = 1) res++;} void del (int x) {if (--cnt[x] = = 0) res--;} void change (int idx, int i) {if (q[i].l <= c[idx].p && c[idx].p <= q[i].r) {del (A[C[IDX].P]); Add (C[idx].col); } std::swap (A[C[IDX].P], c[idx].col);} void Moqueue () {block = POW (n, 0.6666667);//block size to be replaced by this for (int i = 1; I <= n; i++) belong[i] = (i-1)/block + 1 ; Std::sort (q + 1, q + q_tot + 1, CMP); L = 1, r = 0, res = 0, changed = 0; for (int i = 1, i <= q_tot; i++) {while (R < Q[I].R) Add (A[++r]); while (R > Q[i].r) del (a[r--]); while (L > Q[i].l) Add (A[--l]); while (L < Q[I].L) del (a[l++]); while (changed < Q[i].pre) changes (++changed, i);//change (Changed > Q[i].pre) changed (changed--, i);//Change more Out[q[i].id] = res; }}int Main () {n = read (), M = Read (); for (int i = 1; I <= n; i++) A[i] = read (); for (int i = 1; I <= m; i++) {char opt[3]; scanf ("%s", opt); if (opt[0] = = ' Q ') {int x = read (), y = Read (); Q[++q_tot] = (Query) {x, Y, Q_tot, c_tot}; } else if (opt[0] = = ' R ') {int p = read (), col = read (); C[++c_tot] = (change) {p, col}; }} moqueue (); for (int i = 1; I <= q_tot; i++) printf ("%d\n", Out[i]); return 0;}
P1903 [Country Training Team] number color/maintenance queue