题意:左边有n个城市,右边有m个城市,现在修k条路,问会形成多少个交点
先按照x从小到大排,x相同的话,则按照y从小到大排,然后对于每一个y统计前面有多少个y比它大,它们就一定会相交
另外要用long long
1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include <cmath> 5 #include<stack> 6 #include<vector> 7 #include<map> 8 #include<set> 9 #include<queue> 10 #include<algorithm> 11 using namespace std; 12 13 typedef long long LL; 14 const int INF = (1<<30)-1; 15 const int mod=1000000007; 16 const int maxn=1000005; 17 18 int a[maxn]; 19 int c[maxn];//树状数组 20 int n,m,k; 21 22 struct node{ 23 int x,y; 24 }p[maxn]; 25 26 int cmp(node n1,node n2){ 27 if(n1.x != n2.x) return n1.x < n2.x; 28 return n1.y < n2.y; 29 } 30 31 int lowbit(int x){ return x & (-x);} 32 33 int sum(int x){ 34 int ret =0; 35 while(x > 0){ 36 ret+=c[x];x-=lowbit(x); 37 } 38 return ret; 39 } 40 41 void add(int x,int d){ 42 while(x < maxn){ 43 c[x]+=d;x+=lowbit(x); 44 } 45 } 46 47 int main(){ 48 int T; 49 scanf("%d",&T); 50 int kase=0; 51 while(T--){ 52 scanf("%d %d %d",&n,&m,&k); 53 for(int i=1;i<=k;i++) scanf("%d %d",&p[i].x,&p[i].y); 54 sort(p+1,p+k+1,cmp); 55 56 memset(c,0,sizeof(c)); 57 LL ans=0; 58 for(int i=1;i<=k;i++){ 59 add(p[i].y,1); 60 ans += i- sum(p[i].y); 61 } 62 printf("Test case %d: %I64d\n",++kase,ans); 63 } 64 return 0; 65 }
时间: 2024-10-11 22:48:08