http://www.lydsy.com/JudgeOnline/problem.php?id=1935
二维偏序问题
排序x,离散化树状数组维护y
#include<cstdio> #include<iostream> #include<algorithm> #define lowbit(x) x&-x using namespace std; #define N 500001 struct TREE { int xi,yi; }Tree[N]; struct ASK { int ldx,ldy,rux,ruy; }Ask[N]; int hy[N*3]; struct node { int x,y,bl,mul; bool ty; }e[N*5],tmp[N*5]; int ans[N]; int c[N*3]; int toty; void read(int &x) { x=0; char c=getchar(); while(!isdigit(c)) c=getchar(); while(isdigit(c)) { x=x*10+c-'0'; c=getchar(); } } bool cmp(node p,node q) { if(p.x!=q.x) return p.x<q.x; return p.ty<q.ty; } void change(int x) { while(x<=toty) { c[x]++; x+=lowbit(x); } } int query(int x) { int sum=0; while(x) { sum+=c[x]; x-=lowbit(x); } return sum; } int main() { int n,m; read(n); read(m); for(int i=1;i<=n;++i) { read(Tree[i].xi); read(Tree[i].yi); hy[++toty]=Tree[i].yi; } for(int i=1;i<=m;++i) { read(Ask[i].ldx); read(Ask[i].ldy); hy[++toty]=Ask[i].ldy; read(Ask[i].rux); read(Ask[i].ruy); hy[++toty]=Ask[i].ruy; } sort(hy+1,hy+toty+1); toty=unique(hy+1,hy+toty+1)-hy-1; int tot=0; for(int i=1;i<=n;++i) { ++tot; e[tot].x=Tree[i].xi; e[tot].y=lower_bound(hy+1,hy+toty+1,Tree[i].yi)-hy; } int ly,ry; for(int i=1;i<=m;++i) { ly=lower_bound(hy+1,hy+toty+1,Ask[i].ldy)-hy; ry=lower_bound(hy+1,hy+toty+1,Ask[i].ruy)-hy; e[++tot].bl=i; e[tot].mul=1; e[tot].ty=true; e[tot].x=Ask[i].rux; e[tot].y=ry; e[++tot].bl=i; e[tot].mul=1; e[tot].ty=true; e[tot].x=Ask[i].ldx-1; e[tot].y=ly-1; e[++tot].bl=i; e[tot].mul=-1; e[tot].ty=true; e[tot].x=Ask[i].ldx-1; e[tot].y=ry; e[++tot].bl=i; e[tot].mul=-1; e[tot].ty=true; e[tot].x=Ask[i].rux; e[tot].y=ly-1; } sort(e+1,e+tot+1,cmp); for(int i=1;i<=tot;++i) { if(!e[i].ty) change(e[i].y); else ans[e[i].bl]+=e[i].mul*query(e[i].y); } for(int i=1;i<=m;++i) cout<<ans[i]<<'\n'; }
1935: [Shoi2007]Tree 园丁的烦恼
Time Limit: 15 Sec Memory Limit: 357 MBSubmit: 1499 Solved: 673
[Submit][Status][Discuss]
Description
Input
Output
Sample Input
0 0
0 1
1 0
0 0 1 1