MSE06H - Japan

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

Output:
Test case 1: 5

Được gửi lên bởi:psetter
Ngày:2009-04-16
Thời gian chạy:0.209s-1s
Giới hạn mã nguồn:50000B
Memory limit:1536MB
Cluster: Cube (Intel G860)
Ngôn ngữ cho phép:Tất cả ngoại trừ: ERL GOSU JS-RHINO NODEJS PERL6 PYPY RUST SED VB.NET
Nguồn bài:Southeastern European 2006

hide comments
2020-03-27 21:21:52
dùng BIT để giới hạn 10^6 mới AC :v
2019-08-28 18:29:25
My Solution: https://vn.spoj.com/files/src/24311622/
2018-12-21 20:12:27
… kq để long long nha
frostpixel aka.How 2 AC
2018-11-20 08:24:10
share code từ thiện :v
https://pastebin.ubuntu.com/p/PYGQWyKKZN/
Ai dùng nhớ để lại cmt cho mình biết nhé <3

Last edit: 2018-11-20 08:25:09
2017-06-02 18:37:51
bài này có thể giải bằng pp tính tổng nhanh :))))
2017-02-16 18:06:45
ez mà free code cho mấy bạn nhé đang vui
#include <bits/stdc++.h>
#define forr(i,a,b) for( long i=a ; i<=b ; ++i)
#define ll long long
#define s second
#define f first

using namespace std;

const long dad=2e6;
long n,k,m,test ;
long long t[2000],ans ;
pair< long,long > a[dad] ;


ll get(long u)
{
ll res=0;
while (u>0)
{
res+=t[u];
u-=u&-u;
}
return res;
}

void update(long u)
{
while (u<=m)
{
++t[u];
u+=u&-u;
}
}

void reset()
{
memset(t,0,sizeof(t));
ans=0ll;
}

int main()
{
// cout << "Hello world!" << endl;
// freopen( "MSE06H.inp" , "r" , stdin );
scanf("%d",&test);
forr(itest,1,test)
{
reset();
scanf("%d%d%d",&n,&m,&k);
forr(i,1,k) scanf("%d%d",&a[i].f,&a[i].s);
sort(a+1,a+k+1);
forr(i,1,k)
{
ans+=i-1-get(a[i].s);
update(a[i].s);
}

printf("Test case %d: %lld\n",itest,ans);
}
return 0;
}
2015-08-10 17:42:23 N�ng D�n John
OK! K<=1000*1000 =)))
http://nhatkynghiencuu.blogspot.com/2015/08/japan-ma-mse06h-spoj_10.html
2015-06-10 10:12:43 Phong


Last edit: 2015-06-10 10:55:50
2015-06-02 22:09:49 there's no salvation for me...
qsort+ IT -----> TLE =))) chịu luôn...
2015-05-11 07:00:41 Prismatic
k<=M*N :v hèn j NZEC hoài
© Spoj.com. All Rights Reserved. Spoj uses Sphere Engine™ © by Sphere Research Labs.