BZOJ 5016: [Snoi2017]一个简单的询问

询问具有可减性,可以差分

\[\begin{aligned} & g(l_1, r_1,x)\times g(l_2,r_2, x) \\=& (g(0,r_1,x)-g(0,l_1-1,x)) \times (g(0, r_2, x)-g(0, l_2-1, x))\\=& g(r_1,x)\times g(r_2,x)+g(l_1 - 1,x)\times g(l_2-1,x) - g(r_1,x)\times g(l_2-1,x) - g(r_2,x)\times g(l_1-1,x) \end{aligned} \]

一个询问拆成四个询问,这样形式的询问就可以莫队了
维护两个数组分别表示 \((0,l)\) 之间的所有数的出现情况及 \((0,r)\) 之间所有数的出现情况。
假设当前修改的数第一个数组的值为 \(a\),第二个数组的值为 \(b\),每次修改就是这样的形式 \(ab \to (a+1)b\)

#include <bits/stdc++.h>
#define pb push_back
#define fi first
#define se second
#define pii pair<int, int>
#define lp p << 1
#define rp p << 1 | 1
#define mid ((l + r) >> 1)
#define ll long long
#define db double
#define rep(i,a,b) for(int i=a;i<b;i++)
#define per(i,a,b) for(int i=b-1;i>=a;i--)
#define Edg int ccnt=1,head[N],to[N*2],ne[N*2];void addd(int u,int v){to[++ccnt]=v;ne[ccnt]=head[u];head[u]=ccnt;}void add(int u,int v){addd(u,v);addd(v,u);}
#define Edgc int ccnt=1,head[N],to[N*2],ne[N*2],c[N*2];void addd(int u,int v,int w){to[++ccnt]=v;ne[ccnt]=head[u];c[ccnt]=w;head[u]=ccnt;}void add(int u,int v,int w){addd(u,v,w);addd(v,u,w);}
#define es(u,i,v) for(int i=head[u],v=to[i];i;i=ne[i],v=to[i])
const int MOD = 1e9 + 7;
void M(int &x) {if (x >= MOD)x -= MOD; if (x < 0)x += MOD;}
int qp(int a, int b = MOD - 2) {int ans = 1; for (; b; a = 1LL * a * a % MOD, b >>= 1)if (b & 1)ans = 1LL * ans * a % MOD; return ans % MOD;}
int gcd(int a, int b) { while (b) { a %= b; std::swap(a, b); } return a; }
char buf[1 << 21], *p1 = buf, *p2 = buf;
inline char getc() {
	return p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1 << 21, stdin), p1 == p2) ? EOF : *p1++;
}
inline int _() {
	int x = 0, f = 1; char ch = getc();
	while (ch < '0' || ch > '9') { if (ch == '-') f = -1; ch = getc(); }
	while (ch >= '0' && ch <= '9') { x = x * 10ll + ch - 48; ch = getc(); }
	return x * f;
}

const int N = 5e4 + 7;
int belong[N], B, a[N], q, cnt, n;
ll ans[N], sum;
int cnt1[N], cnt2[N];

inline void update(int x, int *A, int *B, int w) {
	sum += w * B[x]; A[x] += w;
}

struct QUERY {
	int l, r, opt, id;
	QUERY(int lll = 0, int rr = 0, int oo = 0, int ii = 0) {
		l = std::min(lll, rr); r = std::max(lll, rr);
		opt = oo; id = ii;
	}
	bool operator < (const QUERY &p) const {
		return belong[l] ^ belong[p.l] ? belong[l] < belong[p.l] : belong[l] & 1 ? r < p.r : r > p.r;
	}	
} query[N * 4];

int main() {
	n = _();
	B = sqrt(n) + 1;
	rep (i, 1, n + 1) a[i] = _(), belong[i] = (i - 1) / B + 1;
	q = _();
	rep (i, 1, q + 1) {
		int l1 = _() - 1, r1 = _(), l2 = _() - 1, r2 = _();
		query[++cnt] = QUERY(r1, r2, 1, i);
		query[++cnt] = QUERY(l1, l2, 1, i);
		query[++cnt] = QUERY(l1, r2, -1, i);
		query[++cnt] = QUERY(l2, r1, -1, i);
	}
	std::sort(query + 1, query + 1 + cnt);
	int l = 0, r = 0;
	rep (i, 1, cnt + 1) {
		while (l < query[i].l) update(a[++l], cnt1, cnt2, 1);
		while (l > query[i].l) update(a[l--], cnt1, cnt2, -1);
		while (r < query[i].r) update(a[++r], cnt2, cnt1, 1);
		while (r > query[i].r) update(a[r--], cnt2, cnt1, -1);
		ans[query[i].id] += query[i].opt * sum;
	}
	rep (i, 1, q + 1) printf("%lld\n", ans[i]);
	return 0;
}
posted @ 2020-02-24 16:15  Mrzdtz220  阅读(78)  评论(0编辑  收藏  举报