zoukankan      html  css  js  c++  java
  • 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 }
  • 相关阅读:
    写个简单的搜索引擎
    C++中的三种继承关系
    《深度探索C++对象模型》调用虚函数
    一次数据库优化的对话
    读后感:你的灯亮着吗
    Linux Shell 截取字符串
    一次关于知识储备的思考
    哈夫曼树与哈夫曼编码
    二叉查找树
    jar中没有注清单属性
  • 原文地址:https://www.cnblogs.com/Xacm/p/3865948.html
Copyright © 2011-2022 走看看