Japan
 
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 26544   Accepted: 7206

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
[题意]给你一个二部图及一些边,问你线段之间有多少组线段是相交的,交点在顶点的不算。
首先说一下这题数据有问题,应该是10的6次方。然后看看这题,又是统计,而且统计次数还很大,那就可以用树状数组来做了。
首先将y从大到小排序,若y相等则将x从大到小排序,这样,当统计到当前(x,y)时,在x 前面有多少x就把加多少到ans
因为对于两条线段(x1,y1),(x2,y2),若y1>y2&&x1<x2那么他们就相交。接下来就是树状数组来统计了。。
#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <string>
#include <map>
#include <stack>
#include <queue>
#include <vector>
#define inf 0x3f3f3f3f
#define met(a,b) memset(a,b,sizeof a)
#define pb push_back
typedef long long ll;
using namespace std;
const int N = 2e6+;
const int M = ;
const int mod=1e9+;
ll tree[N];
int n,m,k;
struct man{
int x,y;
bool operator< (const man &it)const{
if(y==it.y)return x>it.x;
return y>it.y;
}
}a[N];
void add(int k,int num){
while(k<=n){
tree[k]+=num;
k+=k&(-k);
}
}
ll Sum(int k){
ll sum=;
while(k>){
sum+=tree[k];
k-=k&(-k);
}
return sum;
}
int main() {
int T;
scanf("%d",&T);
for(int t=;t<=T;t++){
met(tree,);met(a,);
scanf("%d%d%d",&n,&m,&k);
for(int i=;i<=k;i++){
scanf("%d%d",&a[i].x,&a[i].y);
}
sort(a+,a++k);
ll ans=;
for(int i=;i<=k;i++){
ans+=Sum(a[i].x-);
add(a[i].x,);
}
printf("Test case %d: %lld\n",t,ans);
}
return ;
}

POJ 3067 Japan(树状数组)的更多相关文章

  1. poj 3067 - Japan(树状数组)

    先按第一个数从大到小排序,相等的情况下,第二个数按照从大到小排序..... 预处理后,照着树状数组写就行了... 注意:k的最大值应取1000*1000 代码如下: include <cstdi ...

  2. POJ 3067 Japan (树状数组求逆序对)

    POJ - 3067 题意:有(1-n)个城市自上到下在左边, 另有(1-m)个城市自上到下在右边,共有m条高速公路,现求这m条直线的交点个数,交点不包括在城市处相交. 题解:先将高速公路读入,然后按 ...

  3. POJ 3067 Japan 树状数组求逆序对

    题目大意:有两排城市,这两排城市之间有一些路相互连接着,求有多少条路相互交叉. 思路:把全部的路先依照x值从小到大排序,x值同样的依照y值从小到大排序,然后插入边的时候,先找有多少比自己y值小的,这些 ...

  4. POJ 3067 Japan (树状数组 && 控制变量)

    题意: 西海岸和东海岸有分别有n (1~n)个和m (1~m)个城市, 两个海岸的城市之间有k条公路连通, 公路会相交, 现在给出城市和公路的信息问你由这些公路组成的复杂交通有多少个交点 (如果两个条 ...

  5. POJ 3067【树状数组】

    题意: 给你两行数字,n个m个,然后给你k条线直接把两个数连起来,问有多少个交叉的 思路: 假定上一行是起点,下一行是终点. 把路按照起点从大到下排序, 然后可以直接对每条路查询,这条路目前的交叉数, ...

  6. poj3067 Japan(树状数组)

    转载请注明出处:http://blog.csdn.net/u012860063 题目链接:id=3067">http://poj.org/problem? id=3067 Descri ...

  7. poj3067 Japan 树状数组求逆序对

    题目链接:http://poj.org/problem?id=3067 题目就是让我们求连线后交点的个数 很容易想到将左端点从小到大排序,如果左端点相同则右端点从小到大排序 那么答案即为逆序对的个数 ...

  8. poj 2229 Ultra-QuickSort(树状数组求逆序数)

    题目链接:http://poj.org/problem?id=2299 题目大意:给定n个数,要求这些数构成的逆序对的个数. 可以采用归并排序,也可以使用树状数组 可以把数一个个插入到树状数组中, 每 ...

  9. POJ 2299 【树状数组 离散化】

    题目链接:POJ 2299 Ultra-QuickSort Description In this problem, you have to analyze a particular sorting ...

  10. poj 2155 Matrix (树状数组)

    Matrix Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 16797   Accepted: 6312 Descripti ...

随机推荐

  1. Java注释@Override

    @Override指定方法覆载.它可以强制一个子类必须覆盖父类的方法. package ch14; /** * Created by Jiqing on 2016/12/27. */ public c ...

  2. dev uploadcontrol 上传图片

    <script type="text/javascript"> // <![CDATA[ function Uploader_OnUploadStart() { ...

  3. linux 删除用户

    userdel可删除用户帐号与相关的文件.若不加参数,则仅删除用户帐号,而不删除相关文件命 令: userdel 功能说明:删除用户帐号. 语 法:userdel [-r][用户帐号] 补充说明:us ...

  4. html5 video标签兼容性与自定义控件

    Video不兼容IE8及之前的版本和opera mini. 格式上MPEG4/H.264兼容大部分浏览器,除低版本Firefox和低版本opera,这些可以通过用ogg格式解决,而webm是一种开放. ...

  5. android 中 webview 怎么用 localStorage?

    我在 android里面 使用html5的 localStorage 为什么存不进去也读不出来呀? 网上搜了好多都没效果 1 2 3 4 5 6 7 8 9 mainWebView = (WebVie ...

  6. jQuery实现滚动效果详解1

    声明:第一次写原创,本人初学,很多地方一知半解,本篇算是一个学习的笔记,欢迎批评指正,转载请注明. 今天要做的效果是在网上经常能看到多幅图片向左无缝滚动,鼠标滑过动画暂停,鼠标滑出动画继续的效果.网上 ...

  7. APIO2015 酱油记

    Day 0 昨天CTSC才比完,当然是要浪啦! 于是浪了一天...午饭都没吃... 晚饭...貌似也没吃... 晚上的时候觉得这样子浪不太好,还是要认真一下,打开bzoj,弃疗了...还是浪吧... ...

  8. Java使用ZXing生成二维码条形码

    一.下载Zxingjar包 本实例使用的是 zxing3.2.0的版本  下载地址 http://pan.baidu.com/s/1gdH7PzP 说明:本实例使用的3.2.0版本已经使用的java7 ...

  9. mongo

    最近一直在用mongodb,有时候会需要用到统计,在网上查了一些资料,最适合用的就是用aggregate,以下介绍一下自己运用的心得.. 别人写过的我就不过多描述了,大家一搜能搜索到N多一样的,我写一 ...

  10. 谈谈UIView的几个layout方法

    谈谈UIView的几个layout方法-layoutSubviews.layoutIfNeeded.setNeedsLayout...   最近在学习swift做动画,用到constraint的动画, ...