CDQ分治,然而超过内存了
CDQ分治的思想还是很有趣的。
http://www.lydsy.com/JudgeOnline/problem.php?id=2683
/************************************************************** Problem: 2683 User: 1349367067 Language: C++ Result: Accepted Time:7344 ms Memory:45028 kb ****************************************************************/ #include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #define N 800011 using namespace std; int n; struct Node { int x,y,belong,opt,no,a; }q[N],nq[N]; int num=0,T=0; bool cmp(Node a,Node 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; } int c[N]; inline int lowbit(int x) {return x&(-x);} int getsum(int x) { int ret=0; while (x) {ret+=c[x];x-=lowbit(x);} return ret; } void update(int x,int a) { while (x<=n) {c[x]+=a;x+=lowbit(x);} } int ans[N]={}; void sov(int l,int r) { if (l==r) return; int mid=(l+r)/2,l1,l2; for (int i=l;i<=r;i++) { if (q[i].no<=mid&&q[i].opt==1) update(q[i].y,q[i].a); if (q[i].no>mid&&q[i].opt==2) ans[q[i].belong]+=q[i].a*getsum(q[i].y); } for (int i=l;i<=r;i++) { if (q[i].no<=mid&&q[i].opt==1) update(q[i].y,-q[i].a); } l1=l;l2=mid+1; for (int i=l;i<=r;i++) { if (q[i].no<=mid) nq[l1++]=q[i]; else nq[l2++]=q[i]; } for (int i=l;i<=r;i++) q[i]=nq[i]; sov(l,mid);sov(mid+1,r); } int main() { scanf("%d",&n); int tem; while (scanf("%d",&tem),tem!=3) { if (tem==1) { int x,y,a;scanf("%d%d%d",&x,&y,&a); num++; q[num].opt=tem; q[num].x=x;q[num].y=y;q[num].a=a; q[num].no=num; } else { int x1,y1,x2,y2; scanf("%d%d%d%d",&x1,&y1,&x2,&y2); q[++num].opt=tem;q[num].x=x1-1;q[num].y=y1-1;q[num].a=1;q[num].no=num;q[num].belong=++T; q[++num].opt=tem;q[num].x=x1-1;q[num].y=y2;q[num].a=-1;q[num].no=num;q[num].belong=T; q[++num].opt=tem;q[num].x=x2;q[num].y=y1-1;q[num].a=-1;q[num].no=num;q[num].belong=T; q[++num].opt=tem;q[num].x=x2;q[num].y=y2;q[num].a=1;q[num].no=num;q[num].belong=T; } } sort(q+1,q+num+1,cmp); sov(1,num); for (int i=1;i<=T;i++) printf("%d\n",ans[i]); return 0; }
时间: 2024-11-05 16:05:03