只是简单的求逆序数,还是re一次,wa一次。
re注意的是两个岛各最多1000城市,所以路可以有1e6
wa注意的是交叉点的数量可以很多,会超int。
代码:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn = 1e6+5;
int x, y, n, tree[maxn];
struct node
{
int x, y;
bool operator < (const node &a) const
{
if(x == a.x) return y < a.y;
return x < a.x;
}
}a[maxn];
int lowbit(int x) { return x&(-x); }
void update(int pos, int val)
{
while(pos < maxn)
{
tree[pos] += val;
pos += lowbit(pos);
}
}
int query(int pos)
{
int sum = 0;
while(pos)
{
sum += tree[pos];
pos -= lowbit(pos);
}
return sum;
}
int main(void)
{
int t, ca = 1;
cin >> t;
while(t--)
{
long long ans = 0;
memset(tree, 0, sizeof(tree));
scanf("%d%d%d", &x, &y, &n);
for(int i = 0; i < n; i++)
scanf("%d%d", &a[i].x, &a[i].y);
sort(a, a+n);
for(int i = 0; i < n; i++)
{
ans += i-query(a[i].y);
update(a[i].y, 1);
}
printf("Test case %d: %lld\n", ca++, ans);
}
return 0;
}
Japan
| Time Limit: 1000MS | Memory Limit: 65536K | |
| Total Submissions: 26393 | Accepted: 7167 |
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)
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
Source
本文介绍了一个计算日本岛屿间超级高速公路交叉点数量的问题。利用离线处理和数据结构的方法,通过求逆序数来解决该问题。特别关注了可能出现的运行错误(RE)和错误答案(WA),并给出了具体的代码实现。
&spm=1001.2101.3001.5002&articleId=52834409&d=1&t=3&u=6a991cbad8d54726805aa59f9ff83305)
564

被折叠的 条评论
为什么被折叠?



