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

歡迎訪問 生活随笔!

生活随笔

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

编程问答

poj/OpenJ_Bailian - 2528 离散化+线段树

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

傳送門:http://bailian.openjudge.cn/practice/2528?lang=en_US

//http://poj.org/problem?id=2528

題意:

給你n長海報(bào),每張海報(bào)在墻上貼的區(qū)間范圍是l,r

問你這n張海報(bào)貼完后,可以看見的海報(bào)數(shù)量是多少

題解:

離散化+線段樹

因?yàn)閘,r的數(shù)據(jù)范圍是1e7,而題目只給了64MB的空間,直接存的話空間會(huì)炸掉,所以需用用到離散化的技巧

然后按照端點(diǎn)單點(diǎn)更新即可

現(xiàn)在重新寫這題發(fā)現(xiàn)這個(gè)題坑點(diǎn)在于每一個(gè)點(diǎn)他都代表一個(gè)長度為1的東東,所以我們普通的離散話會(huì)出問題

1-10 1-4 5-10
1-10 1-4 6-10

譬如 如上這組例子

所以我們離散化時(shí)做一下優(yōu)化, 如果相鄰間數(shù)字間距大于1時(shí)我們就在其中加上任意一個(gè)數(shù)字

這樣離散話下來后做線段樹就可以過
這組數(shù)據(jù)
3
5 6
4 5
6 8
3
1 10
1 3
6 10
5
1 4
2 6
8 10
3 4
7 10

這組數(shù)據(jù)

答案是3,3,4

代碼:

#include <set> #include <map> #include <deque> #include <queue> #include <stack> #include <cmath> #include <ctime> #include <bitset> #include <cstdio> #include <string> #include <cstdlib> #include <cstring> #include <iostream> #include <algorithm> using namespace std;typedef long long LL; typedef pair<LL, LL> pLL; typedef pair<LL, int> pLi; typedef pair<int, LL> pil;; typedef pair<int, int> pii; typedef unsigned long long uLL; #define ls rt<<1 #define rs rt<<1|1 #define lson l,mid,rt<<1 #define rson mid+1,r,rt<<1|1 #define bug printf("*********\n") #define FIN freopen("input.txt","r",stdin); #define FON freopen("output.txt","w+",stdout); #define IO ios::sync_with_stdio(false),cin.tie(0) #define debug1(x) cout<<"["<<#x<<" "<<(x)<<"]\n" #define debug2(x,y) cout<<"["<<#x<<" "<<(x)<<" "<<#y<<" "<<(y)<<"]\n"const double eps = 1e-8; const int mod = 1e9 + 7; const int maxn = 3e5 + 5; const int INF = 0x3f3f3f3f; const LL INFLL = 0x3f3f3f3f3f3f3f3; struct node {int l, r; } q[maxn]; int col[maxn]; int vis[maxn]; int ans; void push_down(int rt) {if(col[rt] != -1) {col[ls] = col[rs] = col[rt];col[rt] = -1;}return; } void update(int L, int R, int c, int l, int r, int rt) {if(L <= l && r <= R) {col[rt] = c;return;}push_down(rt);int mid = (l + r) >> 1;if(L <= mid) update(L, R, c, lson);if(R > mid) update(L, R, c, rson); } void query(int l, int r, int rt) {if(col[rt] != -1) {if(!vis[col[rt]]) ans++;vis[col[rt]] = true;return;}if(l == r) return;int mid = (l + r) >> 1;query(lson);query(rson); } int x[maxn]; int main() {int T;scanf("%d", &T);while(T--) {int cnt = 0;int n;scanf("%d", &n);for(int i = 0; i < n; i++) {scanf("%d%d", &q[i].l, &q[i].r);x[cnt++] = q[i].l;x[cnt++] = q[i].r;}sort(x, x + cnt);int m = 1;for(int i = 1; i < cnt; i++) {if(x[i] != x[i - 1]) {x[m++] = x[i];}}for(int i = m - 1; i >= 1; i--) {if(x[i] != x[i - 1] + 1) {x[m++] = x[i - 1] + 1;}}sort(x, x + m);memset(col, -1, sizeof(col));for(int i = 0; i < n; i++) {int l = lower_bound(x, x + m, q[i].l) - x;int r = lower_bound(x, x + m, q[i].r) - x;update(l, r, i, 0, m, 1);}ans = 0;memset(vis, false, sizeof(vis));query(0, m, 1);printf("%d\n",ans );} }

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

總結(jié)

以上是生活随笔為你收集整理的poj/OpenJ_Bailian - 2528 离散化+线段树的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

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