zoukankan      html  css  js  c++  java
  • Japan

    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)
     

    Sample Input

    1
    3 4 4
    1 4
    2 3
    3 2
    3 1
     

    Sample Output

    Test case 1: 5
    1. #include"iostream"
    2. #include"algorithm"
    3. #include"cstring"
    4. #include"cstdio"
    5. using namespace std;
    6. structxy
    7. {
    8.     int x,y;
    9. }a[1000010];
    10. int c[1005];
    11. //long long int max;
    12. int cmp(const xy&a,const xy&b)
    13. {
    14.     if(a.x!=b.x)
    15.      return a.x<b.x;
    16.     else
    17.      return a.y<b.y;
    18. }
    19. int lowbit(int x)
    20. {
    21.     return x&(-x);
    22. }
    23. void updata(int x,int d,int max)
    24. {
    25.     while(x<=max)
    26.     {
    27.         c[x]+=d;
    28.         x+=lowbit(x);
    29.     }
    30. }
    31. long long int getsum(int x)
    32. {
    33.     long long int res=0;
    34.     while(x>0)
    35.     {
    36.         res+=c[x];
    37.         x-=lowbit(x);
    38.     }
    39.     return res;
    40. }
    41. int main()
    42. {
    43.     int i,t,p=0;
    44.     scanf("%d",&t);
    45.     while(t--)
    46.     {
    47.         int n,m,k,max;
    48.         memset(c,0,sizeof(c));
    49.         max=0;
    50.         scanf("%d%d%d",&n,&m,&k);
    51.         for(i=0;i<k;i++)
    52.         {
    53.             scanf("%d%d",&a[i].x,&a[i].y);
    54.             if(a[i].y>max)
    55.              max=a[i].y;
    56.         }
    57.         sort(a,a+k,cmp);
    58.         long long int sum=0;
    59.         updata(a[0].y,1,max);
    60.         for(i=1;i<k;i++)
    61.         {
    62.             sum+=getsum(max)-getsum(a[i].y);
    63.             updata(a[i].y,1,max);
    64.         }
    65.         printf("Test case %d: %lld ",++p,sum);
    66.     }
    67.     return 0;
    68. }
  • 相关阅读:
    【设计模式(17)】行为型模式之中介者模式
    服务器迁移--MySQL数据库迁移
    巧妙解决element-ui下拉框选项过多的问题
    关于后台返回的文件流下载方法
    关于input框只让输入数字的写法
    关于element ui 全局配置某些组件的属性方法
    关于echarts的南丁格尔玫瑰图 极值导致展示效果不好的解决方案
    mock数据的使用方法
    配置 git账号和邮箱
    vite 发布了正式版版了 用起来
  • 原文地址:https://www.cnblogs.com/767355675hutaishi/p/3683443.html
Copyright © 2011-2022 走看看