• 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)

    Sample Input

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

    Sample Output

    Test case 1: 5
    题意:日本是个小岛国,东部M个地区,每个地区编号从1-M;西部N个地区,每个地区编号从1-N。它们之间有路,问这些路有多少个交点。
    思路:树状数组;
    注意:这里要用long long,因为这个wa了好多次。
    ac代码:
     1 #include<stdio.h>
     2 #include<string.h>
     3 #include<iostream>
     4 #include<algorithm>
     5 using namespace std;
     6 int d1[2000000],n;
     7 struct node
     8 {
     9     int g,h;
    10 } t[2000000];
    11 int cmp(struct node a,struct node b)
    12 {
    13     if(a.h==b.h)
    14         return a.g<b.g;
    15     return a.h<b.h;
    16 }
    17 int lowbit(int x)
    18 {
    19     return x&(-x);
    20 }
    21 long long sum(int x)
    22 {
    23     long long res=0;
    24     while(x>0)
    25     {
    26         res+=d1[x];
    27         x-=lowbit(x);
    28     }
    29     return res;
    30 }
    31 void add(int x)
    32 {
    33     while(x<=20000)
    34     {
    35         d1[x]++;
    36         x+=lowbit(x);
    37     }
    38 }
    39 int main()
    40 {
    41     int a,c,b,i,j,k=0;
    42     scanf("%d",&a);
    43     while(a--)
    44     {
    45         memset(d1,0,sizeof(d1));
    46         scanf("%d%d%d",&n,&b,&c);
    47         for(i=0; i<c; i++)
    48         {
    49             scanf("%d%d",&t[i].g,&t[i].h);
    50         }
    51         sort(t,t+c,cmp);
    52         long long ans=0;
    53         for(i=0; i<c; i++)
    54         {
    55             ans+=sum(b)-sum(t[i].g);
    56             add(t[i].g);
    57         }
    58         printf("Test case %d: %lld
    ",++k,ans);
    59     }
    60 }
  • 相关阅读:
    【试水CAS-4.0.3】第07节_CASclient配置单点登录
    30分钟,让你彻底明白Promise原理
    【你离硅谷只差一步】网易中国创业家大赛项目火热征集中
    21分钟学会写编译器
    Android 模拟器下载、编译及调试
    GitLab 自动触发 Jenkins 构建
    微服务监控探索
    感觉要火!妹子实地采访网易猪厂程序员七夕怎么过
    延迟任务调度系统—技术选型与设计(下篇)
    使用QUIC
  • 原文地址:https://www.cnblogs.com/Xacm/p/3865948.html
Copyright © 2020-2023  润新知