Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 23937 | Accepted: 6473 |
Description
Input
Output
Sample Input
1 3 4 4 1 4 2 3 3 2 3 1
Sample Output
Test case 1: 5
Source
给定k条线段,求交点个数(不包括端点)
这是一道树状数组裸题…不过自己提交了15次才过了...QAQ
#include<iostream> #include<cstdio> #include<algorithm> #include<cstdlib> #include<cmath> #include<cstring> #define F(i,j,n) for(int i=j;i<=n;i++) #define D(i,j,n) for(int i=j;i.=n;i--) #define MAXN 2005 #define LL long long #define pa pair<int,int> using namespace std; struct data { int x,y; }a[MAXN*MAXN]; int t,n,m,k,f[MAXN]; LL sum; int read() { int ret=0,flag=1;char ch=getchar(); while (ch<'0'||ch>'9'){if (ch=='-') flag=-1;ch=getchar();} while (ch>='0'&&ch<='9'){ret=ret*10+ch-'0';ch=getchar();} return ret*flag; } bool cmp(data d1,data d2) { if (d1.x!=d2.x) return d1.x>d2.x; else return d1.y>d2.y; } void add(int k) { for(int i=k;i<=m;i+=i&(-i)) f[i]++; } LL getsum(int k) { LL ret=0; for(int i=k;i>0;i-=i&(-i)) ret+=f[i]; return ret; } int main() { t=read(); F(ii,1,t) { memset(f,0,sizeof(f)); n=read();m=read();k=read(); sum=0; F(i,1,k) a[i].x=read(),a[i].y=read(); sort(a+1,a+k+1,cmp); F(i,1,k) { sum+=getsum(a[i].y-1); add(a[i].y); } printf("Test case %d: %lld\n",ii,sum); } }