标签:
#include <iostream> #include <algorithm> #include <cstdio> #include <cstring> #include <map> using namespace std; typedef long long LL; LL ax[300005],ay[300005]; LL c[200005]; LL Lowbit(LL t) { return t&(t^(t-1)); } LL Sum(LL x) { LL sum = 0; while(x > 0) { sum += c[x]; x -= Lowbit(x); } return sum; } void add(LL li,LL d) { while(li<200005) { c[li]+=d; li=li+Lowbit(li); } } struct Nodex { LL x1,x2; LL y; }nodex[100005]; struct Nodey { LL y1,y2; LL x; }nodey[100005]; struct Node { LL x; LL y; }node[200005]; bool cmp1(const Nodey s1,const Nodey s2) { return s1.x<s2.x; } bool cmp2(const Node s1,const Node s2) { return s1.x<s2.x; } map<LL,LL>q1; map<LL,LL>q2; int main() { LL T,n; scanf("%I64d",&T); while(T--) { q1.clear(); q2.clear(); scanf("%I64d",&n); LL tot1=0,tot2=0; for(LL i=0;i<n;i++) { LL x1,x2,y1,y2; scanf("%I64d%I64d%I64d%I64d",&x1,&y1,&x2,&y2); if(x1==x2) { nodey[tot2].y1=min(y1,y2); nodey[tot2].y2=max(y1,y2); nodey[tot2++].x=x1; } else { nodex[tot1].x1=min(x1,x2); nodex[tot1].x2=max(x1,x2); nodex[tot1++].y=y1; } } LL num1,num2; for(LL i=0;i<tot1;i++) { ax[i*2]=nodex[i].x1; ax[i*2+1]=nodex[i].x2; } num1=2*tot1; for(LL i=0;i<tot2;i++) { ax[num1++]=nodey[i].x; } sort(ax,ax+num1); LL tot=0,pre=-1; for(LL i=0;i<num1;i++) { if(ax[i]!=pre) { pre=ax[i]; q1[pre]=++tot; } } for(LL i=0;i<tot2;i++) { ay[i*2]=nodey[i].y1; ay[i*2+1]=nodey[i].y2; } num2=2*tot2; for(LL i=0;i<tot1;i++) ay[num2++]=nodex[i].y; sort(ay,ay+num2); tot=0,pre=-1; for(LL i=0;i<num2;i++) { if(ay[i]!=pre) { pre=ay[i]; q2[pre]=++tot; } } for(LL i=0;i<tot1;i++) { nodex[i].x1=q1[nodex[i].x1]; nodex[i].x2=q1[nodex[i].x2]; nodex[i].y=q2[nodex[i].y]; } for(LL i=0;i<tot2;i++) { nodey[i].y1=q2[nodey[i].y1]; nodey[i].y2=q2[nodey[i].y2]; nodey[i].x=q1[nodey[i].x]; } //for(LL i=0;i<tot1;i++) //cout<<"x: "<<nodex[i].x1<<" "<<nodex[i].x2<<" "<<nodex[i].y<<endl; //for(LL i=0;i<tot1;i++) //cout<<"y: "<<nodey[i].y1<<" "<<nodey[i].y2<<" "<<nodey[i].x<<endl; sort(nodey,nodey+tot2,cmp1); tot=0; for(LL i=0;i<tot1;i++) { node[tot].x=nodex[i].x1; node[tot++].y=nodex[i].y; node[tot].x=nodex[i].x2+1; node[tot++].y=0-nodex[i].y; } sort(node,node+tot,cmp2); LL j=0,k=0,sum=0; memset(c,0,sizeof(c)); for(LL i=1;i<=200001;i++) { while(node[j].x==i&&j<tot) { if(node[j].y>0) add(node[j].y,1); else add(0-node[j].y,-1); j++; } while(nodey[k].x==i&&k<tot2) { sum+=Sum(nodey[k].y2)-Sum(nodey[k].y1-1); k++; } if(j>=tot) break; if(k>=tot2) break; } printf("%I64d\n",sum); } return 0; }
2016暑假多校联合---Counting Intersections
标签:
原文地址:http://www.cnblogs.com/chen9510/p/5789834.html