https://www.lydsy.com/JudgeOnline/problem.php?id=2683
知识点:1.以操作的顺序进行分治
2.cdq分治维护矩阵
3.计算比mid小的给比mid大的的贡献
4.容斥原理
code:
#include <bits/stdc++.h>
#define inf 1000000002
#define ll long long
using namespace std;
int s,w,m;
int ans[];
int t[];
struct que
{
int x,y;
int val,pos;
int id,opt;
}q[],tmp[];
bool operator < (que a,que b)
{
if(a.x == b.x && a.y == b.y)return a.opt < b.opt;
if(a.x == b.x)return a.y < b.y;
return a.x < b.x;
}
void addquery()
{
int x1,y1,x2,y2;
scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
int pos = ++ans[];
q[++m].pos = pos;q[m].id = m;q[m].x = x1 - ;q[m].y=y1 - ;q[m].val = ;q[m].opt = ;
q[++m].pos = pos;q[m].id = m;q[m].x = x2;q[m].y = y2;q[m].val = ;q[m].opt = ;
q[++m].pos = pos;q[m].id = m;q[m].x = x1 - ;q[m].y = y2;q[m].val = -;q[m].opt = ;
q[++m].pos = pos;q[m].id = m;q[m].x = x2;q[m].y = y1 - ;q[m].val = -;q[m].opt = ;
}
int lowbit(int x)
{
return x&(-x);
}
void add(int x,int val)
{
for(int i=x;i<=w;i+=i&-i)t[i]+=val;
}
int query(int x)
{
int tmp = ;
for(int i = x;i;i -= lowbit(i))tmp += t[i];
return tmp;
}
void cdq(int l,int r)
{
if(l == r)return;
int mid = (l + r) >> ,l1 = l,l2 = mid + ;
for(int i = l;i <= r;i++)
{
if(q[i].id <= mid&&!q[i].opt)add(q[i].y,q[i].val);
if(q[i].id > mid && q[i].opt)ans[q[i].pos] += q[i].val * query(q[i].y);
}
for(int i = l;i <= r;i++)
if(q[i].id <= mid && !q[i].opt)add(q[i].y,-q[i].val);
for(int i = l;i <= r;i++)
if(q[i].id <= mid)tmp[l1++] = q[i];
else tmp[l2++] = q[i];
for(int i = l;i <= r;i++)
q[i] = tmp[i];
cdq(l,mid);cdq(mid + ,r);
}
int main()
{
scanf("%d%d",&s,&w);
while()
{
int opt;
scanf("%d",&opt);
if(opt == )
{
m++;
scanf("%d%d%d",&q[m].x,&q[m].y,&q[m].val);
q[m].id = m;
}
else if(opt == )
addquery();
else break;
}
sort(q + ,q + m + );
cdq(,m);
for(int i = ;i <= ans[];i++)
printf("%d\n",ans[i]);
return ;
}