标签:des style blog http io ar color os sp
题解:
这种题。。。按x轴排一下序,然后树状数组算一下前缀和。。。
刚开始好心离散化了一下结果慢了一倍 囧
代码:(离散化)
1 #include<cstdio> 2 #include<cstdlib> 3 #include<cmath> 4 #include<cstring> 5 #include<algorithm> 6 #include<iostream> 7 #include<vector> 8 #include<map> 9 #include<set> 10 #include<queue> 11 #include<string> 12 #define inf 1000000000 13 #define maxn 2000000+5 14 #define maxm 500+100 15 #define eps 1e-10 16 #define ll long long 17 #define pa pair<int,int> 18 #define for0(i,n) for(int i=0;i<=(n);i++) 19 #define for1(i,n) for(int i=1;i<=(n);i++) 20 #define for2(i,x,y) for(int i=(x);i<=(y);i++) 21 #define for3(i,x,y) for(int i=(x);i>=(y);i--) 22 #define mod 1000000007 23 using namespace std; 24 inline int read() 25 { 26 int x=0,f=1;char ch=getchar(); 27 while(ch<‘0‘||ch>‘9‘){if(ch==‘-‘)f=-1;ch=getchar();} 28 while(ch>=‘0‘&&ch<=‘9‘){x=10*x+ch-‘0‘;ch=getchar();} 29 return x*f; 30 } 31 int n,m,cnt,tot,b[maxn],c[maxn],s[maxn],ans[maxn]; 32 struct rec{int x,y,z,id;}a[4*maxn]; 33 inline void add(int x) 34 { 35 for(;x<=cnt;x+=x&(-x))s[x]++; 36 } 37 inline int sum(int x) 38 { 39 int t=0; 40 for(;x;x-=x&(-x))t+=s[x]; 41 return t; 42 } 43 inline bool cmp1(int x,int y){return a[x].y<a[y].y;} 44 inline bool cmp2(rec x,rec y) 45 { 46 if(x.x!=y.x)return x.x<y.x; 47 else if(x.y!=y.y)return x.y<y.y; 48 else return x.id<y.id; 49 } 50 int main() 51 { 52 freopen("input.txt","r",stdin); 53 freopen("output.txt","w",stdout); 54 n=read();m=read(); 55 for1(i,n)a[i].x=read()+1,a[i].y=read()+1;tot=n; 56 for1(i,m) 57 { 58 int x1=read()+1,y1=read()+1,x2=read()+1,y2=read()+1; 59 a[++tot]=(rec){x1-1,y1-1,1,i};a[++tot]=(rec){x2,y2,1,i}; 60 a[++tot]=(rec){x2,y1-1,-1,i};a[++tot]=(rec){x1-1,y2,-1,i}; 61 } 62 for1(i,tot)b[i]=i; 63 sort(b+1,b+tot+1,cmp1); 64 cnt=0; 65 for1(i,tot) 66 { 67 if(i==1||a[b[i]].y!=a[b[i-1]].y)cnt++; 68 c[b[i]]=cnt; 69 } 70 for1(i,tot)a[i].y=c[i]; 71 sort(a+1,a+tot+1,cmp2); 72 for1(i,tot) 73 { 74 if(!a[i].id)add(a[i].y); 75 else ans[a[i].id]+=a[i].z*sum(a[i].y); 76 } 77 for1(i,m)printf("%d\n",ans[i]); 78 return 0; 79 }
代码:(不离散化)
1 #include<cstdio> 2 #include<cstdlib> 3 #include<cmath> 4 #include<cstring> 5 #include<algorithm> 6 #include<iostream> 7 #include<vector> 8 #include<map> 9 #include<set> 10 #include<queue> 11 #include<string> 12 #define inf 1000000000 13 #define maxn 2500000+5 14 #define maxm 10000000+5 15 #define eps 1e-10 16 #define ll long long 17 #define pa pair<int,int> 18 #define for0(i,n) for(int i=0;i<=(n);i++) 19 #define for1(i,n) for(int i=1;i<=(n);i++) 20 #define for2(i,x,y) for(int i=(x);i<=(y);i++) 21 #define for3(i,x,y) for(int i=(x);i>=(y);i--) 22 #define mod 1000000007 23 using namespace std; 24 inline int read() 25 { 26 int x=0,f=1;char ch=getchar(); 27 while(ch<‘0‘||ch>‘9‘){if(ch==‘-‘)f=-1;ch=getchar();} 28 while(ch>=‘0‘&&ch<=‘9‘){x=10*x+ch-‘0‘;ch=getchar();} 29 return x*f; 30 } 31 int n,m,cnt,tot,b[maxn],c[maxn],s[maxm],ans[maxn]; 32 struct rec{int x,y,z,id;}a[maxn]; 33 inline void add(int x) 34 { 35 for(;x<maxn;x+=x&(-x))s[x]++; 36 } 37 inline int sum(int x) 38 { 39 int t=0; 40 for(;x;x-=x&(-x))t+=s[x]; 41 return t; 42 } 43 inline bool cmp(rec x,rec y) 44 { 45 if(x.x!=y.x)return x.x<y.x; 46 else if(x.y!=y.y)return x.y<y.y; 47 else return x.id<y.id; 48 } 49 int main() 50 { 51 freopen("input.txt","r",stdin); 52 freopen("output.txt","w",stdout); 53 n=read();m=read(); 54 for1(i,n)a[i].x=read()+1,a[i].y=read()+1;tot=n; 55 for1(i,m) 56 { 57 int x1=read()+1,y1=read()+1,x2=read()+1,y2=read()+1; 58 a[++tot]=(rec){x1-1,y1-1,1,i};a[++tot]=(rec){x2,y2,1,i}; 59 a[++tot]=(rec){x2,y1-1,-1,i};a[++tot]=(rec){x1-1,y2,-1,i}; 60 } 61 sort(a+1,a+tot+1,cmp); 62 for1(i,tot) 63 { 64 if(!a[i].id)add(a[i].y); 65 else ans[a[i].id]+=a[i].z*sum(a[i].y); 66 } 67 for1(i,m)printf("%d\n",ans[i]); 68 return 0; 69 }
BZOJ1935: [Shoi2007]Tree 园丁的烦恼
标签:des style blog http io ar color os sp
原文地址:http://www.cnblogs.com/zyfzyf/p/4132881.html