poj--3067 Japan(线段树+单点更新区间查询)
Description
Japan plans to welcome the ACM ICPC World Finals and a lot of roads must be built for the venue. Japan is tall island with N cities on the East coast and M cities on the West coast (M <= 1000, N <= 1000). K superhighways will be build. Cities on each coast are numbered 1, 2, ... from North to South. Each superhighway is straight line and connects city on the East coast with city of the West coast. The funding for the construction is guaranteed by ACM. A major portion of the sum is determined by the number of crossings between superhighways. At most two superhighways cross at one location. Write a program that calculates the number of the crossings between superhighways.
Input
The input file starts with T - the number of test cases. Each test case starts with three numbers – N, M, K. Each of the next K lines contains two numbers – the numbers of cities connected by the superhighway. The first one is the number of the city on the East coast and second one is the number of the city of the West coast.
Output
For each test case write one line on the standard output:
Test case (case number): (number of crossings)
Test case (case number): (number of crossings)
Sample Input
1 3 4 4 1 4 2 3 3 2 3 1
Sample Output
Test case 1: 5
题意:东边有n个城市,西边有m个城市,它们之间有k条路。然后输入k行东西城市相连的路,一个交点只能有两条路相交,问这些路有多少交点??
思路:对左边一列的数进行升序排列的前提下对右边一列数进行升序排列,可以发现对右边的每个数x,[x+1,m]上所有权值的和就是它和之前路的交点然后进行统计求和即可。
AC代码:
1 #include<cstdio> 2 #include<cstring> 3 #include<iostream> 4 #include<algorithm> 5 #include<queue> 6 #include<vector> 7 #include<map> 8 #define ll long long 9 using namespace std; 10 const int maxn=1000000+10; 11 ll ans;//最后的结果可能会爆int的 12 int t,n,m,b,kk; 13 struct point 14 { 15 int x,y; 16 }p[maxn];//对于每个x有近1000个y所以数组要开到10^6 17 int cmp(const struct point &a,const struct point &b) 18 { 19 if(a.x!=b.x) 20 return a.x<b.x; 21 else 22 return a.y<b.y; 23 } 24 struct note 25 { 26 int l,r,ans; 27 } a[4000]; 28 int build(int l,int r,int k) 29 { 30 if(l==r) 31 { 32 a[k].l=l; 33 a[k].r=r; 34 a[k].ans=0; 35 return 0; 36 } 37 a[k].l=l; 38 a[k].r=r; 39 a[k].ans=0; 40 int mid=(l+r)/2; 41 build(l,mid,k*2); 42 build(mid+1,r,2*k+1); 43 return 0; 44 } 45 int sea(int l,int r,int k) 46 { 47 if(a[k].l==l&&a[k].r==r) 48 { 49 ans=ans+a[k].ans; 50 return 0; 51 } 52 int mid=(a[k].l+a[k].r)/2; 53 if(mid>=r) sea(l,r,k*2); 54 else if(mid<l) sea(l,r,2*k+1); 55 else 56 { 57 sea(l,mid,k*2); 58 sea(mid+1,r,k*2+1); 59 } 60 return 0; 61 } 62 int ins(int d,int k) 63 { 64 if(a[k].l==d&&a[k].r==d) 65 { 66 if(d+1<=m) 67 sea(d+1,m,1); 68 a[k].ans++; 69 return 0; 70 } 71 if(d<=a[k*2].r) ins(d,k*2); 72 else ins(d,k*2+1); 73 a[k].ans=a[k*2].ans+a[k*2+1].ans; 74 return 0; 75 } 76 77 int main() 78 { 79 while(~scanf("%d",&t)) 80 { 81 for(int k=1; k<=t; k++) 82 { 83 scanf("%d%d%d",&n,&m,&kk); 84 build(1,m,1); 85 for(int i=0;i<kk;i++) 86 scanf("%d%d",&p[i].x,&p[i].y); 87 sort(p,p+kk,cmp); 88 ans=0; 89 for(int i=0; i<kk; i++) 90 { 91 ins(p[i].y,1); 92 } 93 printf("Test case %d: %lld\n",k,ans); 94 } 95 } 96 return 0; 97 }