http://www.elijahqi.win/2018/01/12/bzoj4066-%e7%ae%80%e5%8d%95%e9%a2%98/类似这题
Description
你有一个N*N的棋盘,每个格子内有一个整数,初始时的时候全部为0,现在需要维护两种操作:
命令
参数限制
内容
1 x y A
1<=x,y<=N,A是正整数
将格子x,y里的数字加上A
2 x1 y1 x2 y2
1<=x1<= x2<=N
1<=y1<= y2<=N
输出x1 y1 x2 y2这个矩形内的数字和
3
无
终止程序
Input
输入文件第一行一个正整数N。
接下来每行一个操作。
Output
对于每个2操作,输出一个对应的答案。
Sample Input
4
1 2 3 3
2 1 1 3 3
1 2 2 2
2 2 2 3 4
3
Sample Output
3
5
HINT
1<=N<=500000,操作数不超过200000个,内存限制20M。
对于100%的数据,操作1中的A不超过2000。
bzoj4066的可离线版 可以cdq什么搞 回来再填坑的qwq
#include<cstdio>
#include<algorithm>
#define ll long long
#define N 220000
#define inf 0x3f3f3f3f
using namespace std;
inline char gc(){
static char now[1<<16],*S,*T;
if (T==S){T=(S=now)+fread(now,1,1<<16,stdin);if (T==S) return EOF;}
return *S++;
}
inline int read(){
int x=0;char ch=gc();
while(ch<'0'||ch>'9') ch=gc();
while(ch<='9'&&ch>='0') x=x*10+ch-'0',ch=gc();
return x;
}
int n,D,num,root;ll ans;
struct node1{
int d[2],v;
int& operator[](int x){return d[x];}
friend bool operator<(node1 a,node1 b){return a[D]<b[D];}
}point[N];
struct node{
node1 x;int min[2],max[2],left,right;ll sum;
}tree[N];
inline void update(int x){
int l=tree[x].left,r=tree[x].right;
tree[x].sum=tree[l].sum+tree[r].sum+tree[x].x.v;
for (int i=0;i<2;++i) tree[x].min[i]=min(tree[x].min[i],min(tree[l].min[i],tree[r].min[i]));
for (int i=0;i<2;++i) tree[x].max[i]=max(tree[x].max[i],max(tree[l].max[i],tree[r].max[i]));
}
inline void insert1(int &x,int dim){
if (!x){
x=++num;tree[x].x=point[n];tree[x].left=tree[x].right=0;tree[x].sum=point[n].v;
for (int i=0;i<2;++i) tree[x].min[i]=point[n][i],tree[x].max[i]=point[n][i];return;
}
if(point[n][dim]<=tree[x].x[dim]) insert1(tree[x].left,dim^1);else insert1(tree[x].right,dim^1);update(x);
}
inline void build(int &x,int l,int r,int dim){
x=++num;D=dim;int mid=l+r>>1;nth_element(point+l,point+mid,point+r+1);tree[x].left=tree[x].right=0;
tree[x].x=point[mid];for (int i=0;i<2;++i) tree[x].min[i]=tree[x].max[i]=point[mid][i];tree[x].sum=point[mid].v;
if (l<mid) build(tree[x].left,l,mid-1,dim^1);
if (r>mid) build(tree[x].right,mid+1,r,dim^1);update(x);
}int x1,y1,x2,y2;
inline bool judge(int x){
return tree[x].min[0]>=x1&&tree[x].max[0]<=x2&&tree[x].min[1]>=y1&&tree[x].max[1]<=y2;
}
inline bool judge1(int x){
return tree[x].min[0]>x2||tree[x].min[1]>y2||tree[x].max[0]<x1||tree[x].max[1]<y1;
}
inline bool judge2(int x){
return x1<=tree[x].x[0]&&tree[x].x[0]<=x2&&y1<=tree[x].x[1]&&tree[x].x[1]<=y2;
}
inline void query(int x,int dim){
if (!x) return;if (judge(x)) {ans+=tree[x].sum;return;}
if (judge1(x)) return;if(judge2(x)) ans+=tree[x].x.v;
query(tree[x].left,dim^1);query(tree[x].right,dim^1);
}
int main(){
freopen("bzoj4066.in","r",stdin);
n=read();n=0;tree[0].min[0]=tree[0].min[1]=inf;ll last_ans=0;
while(1){
int op=read();if (op==3) return 0;
if (op==1){
point[++n][0]=read()^last_ans;point[n][1]=read()^last_ans;point[n].v=read()^last_ans;
if (n%10000==0) num=0,build(root,1,n,0);else insert1(root,0);
}else{
x1=read()^last_ans;y1=read()^last_ans;x2=read()^last_ans;y2=read()^last_ans;
ans=0;query(root,0);printf("%lld\n",ans);
}
}
return 0;
}
自己丑陋的代码搞不定这题了 qwq菜死了 如何才能像icefox一样强 急在线等
#include<cmath>
#include<cstdio>
#include<algorithm>
#define inf 0x3f3f3f3f
#define N 220000
#define ll long long
using namespace std;
inline char gc(){
static char now[1<<16],*S,*T;
if(T==S){T=(S=now)+fread(now,1,1<<16,stdin);if (T==S) return EOF;}
return *S++;
}
inline int read(){
int x=0,f=1;char ch=gc();
while(ch<'0'||ch>'9') {if (ch=='-') f=-1;ch=gc();}
while(ch<='9'&&ch>='0'){x=x*10+ch-'0';ch=gc();}
return x*f;
}
struct node{
int left,right,x,y,minx,miny,maxx,maxy;ll sum,v;
}tree[N];
int D,n,m,op,num,root;ll ans;
struct node1{
int x,y;ll v;
friend bool operator<(node1 a,node1 b){
if (!D) return a.x<b.x;else return a.y<b.y;
}
}point[N],P;
inline void update(int x){
int l=tree[x].left,r=tree[x].right;
tree[x].sum=tree[x].v+tree[l].sum+tree[r].sum;
tree[x].minx=min(tree[x].minx,min(tree[l].minx,tree[r].minx));
tree[x].miny=min(tree[x].miny,min(tree[l].miny,tree[r].miny));
tree[x].maxx=max(tree[x].maxx,max(tree[l].maxx,tree[r].maxx));
tree[x].maxy=max(tree[x].maxy,max(tree[l].maxy,tree[r].maxy));
}
inline void build(int &x,int l,int r,int dim){
D=dim;x=++num;int mid=l+r>>1;nth_element(point+l,point+mid,point+r+1);tree[x].left=tree[x].right=0;
tree[x].x=point[mid].x;tree[x].y=point[mid].y;tree[x].v=tree[x].sum=point[mid].v;
tree[x].minx=tree[x].maxx=point[mid].x;tree[x].miny=tree[x].maxy=point[mid].y;
if (l<mid) build(tree[x].left,l,mid-1,dim^1);
if (r>mid) build(tree[x].right,mid+1,r,dim^1);update(x);
}
inline void init(int &x){
x=++num;tree[x].x=P.x;tree[x].y=P.y;tree[x].sum=tree[x].v=P.v;tree[x].left=tree[x].right=0;
tree[x].minx=tree[x].maxx=P.x;tree[x].miny=tree[x].maxy=P.y;
}
inline void insert1(int x,int dim){
if (P.x==tree[x].x&&P.y==tree[x].y) tree[x].sum+=P.v,tree[x].v+=P.v;else
if (!dim){
if (P.x<tree[x].x){
if (tree[x].left) insert1(tree[x].left,dim^1);else init(tree[x].left);
}else{
if(tree[x].right) insert1(tree[x].right,dim^1);else init(tree[x].right);
}
}else{
if (P.y<tree[x].y){
if (tree[x].left) insert1(tree[x].left,dim^1);else init(tree[x].left);
}else{
if (tree[x].right) insert1(tree[x].right,dim^1);else init(tree[x].right);
}
}update(x);
}
inline bool judge1(int x){
return (tree[x].maxx<=P.x)&&(tree[x].maxy<=P.y);
}
inline bool judge2(int x){
return (tree[x].minx<=P.x)&&(tree[x].miny<=P.y);
}
inline void query(int x,int dim){
if (tree[x].x<=P.x&&tree[x].y<=P.y) ans+=tree[x].v;
int l=tree[x].left,r=tree[x].right;
if (tree[x].left) {if (judge1(tree[x].left)) ans+=tree[l].sum;else if (judge2(tree[x].left)) query(tree[x].left,dim^1);}
if (tree[x].right) {if (judge1(tree[x].right)) ans+=tree[r].sum;else if (judge2(tree[x].right)) query(tree[x].right,dim^1);}
}
inline void query1(int x,int dim){
if (tree[x].x<=P.x&&tree[x].y<=P.y) ans-=tree[x].v;
int l=tree[x].left,r=tree[x].right;
if (tree[x].left) {if (judge1(tree[x].left)) ans-=tree[l].sum;else if (judge2(tree[x].left)) query1(tree[x].left,dim^1);}
if (tree[x].right) {if (judge1(tree[x].right)) ans-=tree[r].sum;else if (judge2(tree[x].right)) query1(tree[x].right,dim^1);}
}
int main(){
freopen("bzoj4066.in","r",stdin);
n=read();n=0;ll last_ans=0;int nm=0;tree[0].minx=tree[0].miny=inf;
while(1){
op=read();if (op==3) break;
if (op==1){
P.x=read()^last_ans;P.y=read()^last_ans;P.v=read()^last_ans;
++n;if (n==10000) {
nm=num;for (int i=1;i<=nm;++i) point[i].x=tree[i].x,point[i].y=tree[i].y,point[i].v=tree[i].v;n=0,num=0;
build(root,1,nm,0);
}
if (!root) tree[++root].x=P.x,tree[root].y=P.y,num=1,
tree[root].sum=tree[root].v=P.v,tree[root].minx=tree[root].maxx=P.x,tree[root].miny=tree[root].maxy=P.y;
else insert1(root,0);continue;
}else{
ans=0;int x1=read()^last_ans,y1=read()^last_ans,x2=read()^last_ans,y2=read()^last_ans;
if (x1>x2) swap(x1,x2),swap(y1,y2);
P.x=x2;P.y=y2;query(root,0);P.x=x1-1;P.y=y2;if (P.x>=0)query1(root,0);
P.x=x2;P.y=y1-1;if(P.y>0) query1(root,0);P.x=x1-1;P.y=y1-1;if (P.x>0&&P.y>0)query(root,0);
printf("%lld\n",ans);
}
}
return 0;
}