题意
一个长为 n 的序列 a。有 m 个询问,每次询问三个区间,把三个区间中同时出现的数一个一个删掉,问最后三个区间剩下的数的个数和,询问独立。注意这里删掉指的是一个一个删,不是把等于这个值的数直接删完,比如三个区间是 [1,2,2,3,3,3,3] , [1,2,2,3,3,3,3] 与 [1,1,2,3,3],就一起扔掉了 1 个 1,1 个 2,2 个 3。
Sol
设$cnt[i]$表示第$i$个数在询问区间中的出现次数
那么第$i$个询问的答案为$r1 - l1 + r2 - l2 + r3 - l3 + 3 - min(cnt1[x], cnt2[x], cnt3[x])$
后面的那一坨可以用莫队处理
具体做法是对于每个询问维护一个bitset
将所有数离散化之后维护出每个数的出现次数即可
但是这样显然是会超空间的,于是我们把每25000个询问一起做就可以了
#include#include #include #include #include #include #define LL long long// #define int long long using namespace std;const int MAXN = 1e5 + 10, B = 25000;inline int read() { char c = getchar(); int x = 0, f = 1; while(c < '0' || c > '9') { if(c == '-') f = -1; c = getchar();} while(c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar(); return x * f;}int N, M;int a[MAXN], date[MAXN], L[MAXN][5], R[MAXN][5], belong[MAXN], base, cnt = 0, flag[MAXN], tim[MAXN], ans[MAXN];struct Query { int l, r, opt, id; bool operator < (const Query &rhs) const { return belong[l] == belong[rhs.l] ? belong[r] < belong[rhs.r] : belong[l] < belong[rhs.l]; }}Q[MAXN * 3 + 1];bitset bit[B + 50], now;void Add(int x) { tim[a[x]]++; now.set(a[x] + tim[a[x]] - 1);}void Delet(int x) { now.reset(a[x] + tim[a[x]] - 1); tim[a[x]]--;}void solve(int ll, int rr) { memset(flag, 0, sizeof(flag)); memset(tim, 0, sizeof(tim)); memset(ans, 0, sizeof(ans)); now.reset(); for(int i = 0; i <= B; i++) bit[i].reset(); cnt = 0; for(int ti = ll; ti <= rr; ti++) { for(int i = 1; i <= 3; i++) Q[++cnt] = (Query){L[ti][i], R[ti][i], i, ti - ll + 1}, ans[ti - ll + 1] += (R[ti][i] - L[ti][i] + 1); } sort(Q + 1, Q + cnt + 1); int l = 1, r = 0; for(int i = 1; i <= cnt; i++) { while(l > Q[i].l) Add(--l); while(r < Q[i].r) Add(++r); while(l < Q[i].l) Delet(l++); while(r > Q[i].r) Delet(r--); if(!flag[Q[i].id]) bit[Q[i].id] = now, flag[Q[i].id] = 1; else bit[Q[i].id] &= now; } // sort(Q + 1, Q + cnt + 1, comp); for(int i = ll; i <= rr; i++) printf("%d\n", ans[i - ll + 1] - bit[i - ll + 1].count() * 3);}main() {/// freopen("xp1.in", "r", stdin);// freopen("ans.out", "w", stdout); N = read(); M = read(); base = sqrt(N); for(int i = 1; i <= N; i++) a[i] = read(), date[i] = a[i], belong[i] = (i - 1) / base + 1; sort(date + 1, date + N + 1); for(int i = 1; i <= N; i++) a[i] = lower_bound(date + 1, date + N + 1, a[i]) - date; for(int i = 1; i <= M; i++) { //L1[i] = read(); R1[i] = read(); L2[i] = read(); R2[i] = read(); L3[i] = read(); R3[i] = read(); for(int j = 1; j <= 3; j++) L[i][j] = read(), R[i][j] = read(); } for(int i = 1; i <= N; i += B + 1) solve(i, min(i + B, M)); return 0;}/**/