日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

poj 3067

發(fā)布時(shí)間:2025/3/8 编程问答 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 poj 3067 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

樹狀數(shù)組求逆序數(shù)的應(yīng)用:

?

Japan
Time Limit:?1000MS?Memory Limit:?65536K
Total Submissions:?17874?Accepted:?4819

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
題意很好理解,就是對(duì)v進(jìn)行降序排列,然后求逆序數(shù)的個(gè)數(shù)就是交點(diǎn)的個(gè)數(shù):
代碼如下:

#include<iostream>
#include<string>
#include<string.h>
#include<algorithm>
#include<cmath>
#include<stdio.h>
#include<queue>
#include<map>
#include<ctype.h>
#define ll long long
#define loop1(k) for(int i=1;i<=k;i++)
#define loop2(k) for(int j=1;j<=k;j++)
#define pr(i) printf("%d\n",i);
using namespace std;
int n,m,k;
struct node{
int a;
int b;
}edge[1001000];
int cc[1001000];
bool cmp(node c,node d ){
if(c.a==d.a)
return c.b<d.b;
return c.a<d.a;
}
int low(int x){
return x&(-1*x);
}
int sum(int x){
int num=0;
while(x>=1){
num+=cc[x];
x-=low(x);
}
return num;
}
void update(int x)
{
while(x<=m)
{
cc[x]++;
x+=low(x);
}
}
int main()
{
int t;
scanf("%d",&t);
int ci=1;
while(t--){
memset(cc,0,sizeof(int)*m+15);
scanf("%d%d%d",&n,&m,&k);
memset(cc,0,sizeof(int)*m+15);
for(int i=1;i<=k;i++)
scanf("%d%d",&edge[i].a,&edge[i].b);
sort(edge+1,edge+1+k,cmp);
__int64 ans = 0;
for(int i=1;i<=k;i++){
update(edge[i].b);
ans+=sum(m)-sum(edge[i].b);
}
printf("Test case %d: %I64d\n", ci, ans);
ci++;
}
return 0;
}

?

轉(zhuǎn)載于:https://www.cnblogs.com/keephungry/p/3330441.html

總結(jié)

以上是生活随笔為你收集整理的poj 3067的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。