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

歡迎訪問 生活随笔!

生活随笔

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

编程问答

POJ-3067 Japan---树状数组逆序对变形

發(fā)布時(shí)間:2023/12/6 编程问答 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 POJ-3067 Japan---树状数组逆序对变形 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

題目鏈接:

https://vjudge.net/problem/POJ-3067

題目大意:

日本島東海岸與西海岸分別有N和M個(gè)城市,現(xiàn)在修高速公路連接?xùn)|西海岸的城市,求交點(diǎn)個(gè)數(shù)。

解題思路:

記每條告訴公路為(x,y), 即東岸的第x個(gè)城市與西岸的第y個(gè)城市修一條路。當(dāng)兩條路有交點(diǎn)時(shí),滿足(x1-x2)*(y1-y2) < 0。所以,將每條路按x從小到達(dá)排序,若x相同,按y從小到大排序。 然后按排序后的公路用樹狀數(shù)組在線更新,求y的逆序數(shù)之和 即為交點(diǎn)個(gè)數(shù)。

比如樣例

1 4
2 3
3 2
3 1

排完序后

1 4
2 3
3 1
3 2

1、加入1 4,此時(shí)比4大的元素為0,交點(diǎn)數(shù)目為0

2、加入2 3,此時(shí)比3大的元素?cái)?shù)目為1,交點(diǎn)數(shù)目為1

3、加入3 1,此時(shí)比1大的數(shù)字有兩個(gè),交點(diǎn)數(shù)目為3

4、加入3 2,此時(shí)比2大的數(shù)字有2個(gè),交點(diǎn)數(shù)目為5

如果先加人3 2,再加入3 1的話會(huì)導(dǎo)致3 1這條計(jì)算交點(diǎn)的時(shí)候吧3 2算進(jìn)去,但實(shí)際上并沒有交點(diǎn),所以排序順序在x相同的y從小到大排序

1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<string> 5 #include<map> 6 #include<set> 7 #include<cmath> 8 #include<algorithm> 9 using namespace std; 10 typedef long long ll; 11 const int maxn = 1000000 + 10; 12 int n, m, T, k, cases; 13 struct node 14 { 15 int x, y; 16 bool operator <(const node& a)const 17 { 18 return x < a.x || x == a.x && y < a.y; 19 } 20 }; 21 node a[maxn]; 22 int tree[10000]; 23 int lowbit(int x) 24 { 25 return x & (-x); 26 } 27 ll sum(int x) 28 { 29 ll ans = 0; 30 while(x) 31 { 32 ans += tree[x]; 33 x -= lowbit(x); 34 } 35 return ans; 36 } 37 void add(int x, int d) 38 { 39 while(x <= m) 40 { 41 tree[x] += d; 42 x += lowbit(x); 43 } 44 } 45 int main() 46 { 47 cin >> T; 48 while(T--) 49 { 50 cin >> n >> m >> k; 51 memset(tree, 0, sizeof(tree)); 52 for(int i = 1; i <= k; i++) 53 { 54 scanf("%d%d", &a[i].x, &a[i].y); 55 } 56 sort(a + 1, a + 1 + k); 57 ll ans = 0; 58 for(ll i = 1; i <= k; i++) 59 { 60 add(a[i].y, 1); 61 ans += (i - sum(a[i].y)); 62 } 63 printf("Test case %d: %lld\n", ++cases, ans); 64 } 65 return 0; 66 }

?

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

總結(jié)

以上是生活随笔為你收集整理的POJ-3067 Japan---树状数组逆序对变形的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

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