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. iOS - C 基本语法

    1.数据类型 2.常量 1)凡是整数数字常量一律是 int 型. 2)typedef 和 define. typedef 定义类型, #define 只是字符串替换. 3)在编写整形常量时,可以在常量 ...

  2. JavaScript基础(慕课)

    1. JavaScript函数: function 函数名() { 函数代码;//完成特定功能的代码. }PS:函数定义好后,是不能自动执行的,所以需调用它,只需直接在需要的位置写函数就ok了 2.  ...

  3. 转AjaxControlToolkit的安装与使用详解

    AjaxControlToolkit的安装与使用详解 AjaxControlToolkit下载http://ajax.asp.net/downloads/default.aspx?tabid=47ht ...

  4. Octopus系列之价格计算公式

    百分比:在原价的基础上优惠5%,相当于95%折销售实际价格:最后实际销售的价格[基准价格:如果有特价就基于特价,没有特价基于原价]优惠金额:减少的金额 批量优惠 百分比: 原价*(1-5%)的算法实际 ...

  5. git中ssh配置方法

    前提:必须先安装好Git for windows(即msysGit)和TortoiseGit 一,检查本地是否已存在ssh $ cd ~/.ssh $ ls 如果存在id_rsa.pub或者id_ds ...

  6. 支持GPS的核心API

    Android为GPS功能支持专门提供了一个LocationManager类,它的作用于TelephonyManager.AudioManager等服务类的作用相似,所有GPS定位相关的服务.对象都将 ...

  7. NSRunLoop概述和原理

    1.什么是NSRunLoop?我们会经常看到这样的代码: - (IBAction)start:(id)sender{pageStillLoading = YES;[NSThread detachNew ...

  8. Codeforces Round #370 (Div. 2) E. Memory and Casinos 线段树

    E. Memory and Casinos 题目连接: http://codeforces.com/contest/712/problem/E Description There are n casi ...

  9. SparkSQL之旅

    1.准备数据employee.txt ,Gong Shaocheng, ,Li Dachao, ,Qiu Xin, ,Cheng Jiangzhong, ,Wo Binggang, 将数据放入hdfs ...

  10. powershell脚本闪电输入神器

    如图所示: 用autohotkey,把下列常用按键互唤.来达到快速输入目的.并且只对powershell ise,power gui,visual studio code,powershell stu ...