标签:树状数组
Description
Input
Output
Sample Input
1 3 4 4 1 4 2 3 3 2 3 1
Sample Output
Test case 1: 5
这题和star,cows差不多,把读入的坐标看做x,y,那么先按x进行升序排序,如果x相同的话就按照y升序排序。然后依次循环,加上比a[i].y大的线段的个数。注意用__int64
#include<iostream> #include<stdio.h> #include<string.h> #include<math.h> #include<vector> #include<map> #include<queue> #include<stack> #include<string> #include<algorithm> using namespace std; #define maxn 3005 int b[maxn]; struct node{ int x,y; }a[maxn*maxn]; bool cmp(node a,node b){ if(a.x==b.x)return a.y<b.y; return a.x<b.x; } int lowbit(int x){ return x&(-x); } void update(int pos,int num) { while(pos<=maxn){ b[pos]+=num;pos+=lowbit(pos); } } int getsum(int pos) { int num=0; while(pos>0){ num+=b[pos];pos-=lowbit(pos); } return num; } int main() { int n,m,i,j,T,k,num1=0; __int64 sum; scanf("%d",&T); while(T--) { scanf("%d%d%d",&n,&m,&k); for(i=1;i<=k;i++){ scanf("%d%d",&a[i].x,&a[i].y); a[i].x++;a[i].y++; } sort(a+1,a+1+k,cmp); memset(b,0,sizeof(b)); sum=0; for(i=1;i<=k;i++){ sum+=i-1-getsum(a[i].y); update(a[i].y,1); } num1++; printf("Test case %d: %I64d\n",num1,sum); } return 0; }
标签:树状数组
原文地址:http://blog.csdn.net/kirito_acmer/article/details/46350723