【POJ2528】Mayor's Posters-線段樹+離散化
(本人本題完成於2016-7-23)
題目大意:有一面很長(10000000節)的牆,要在上面貼N張海報,對於第i張海報,它將會覆蓋牆面的第Li~Ri節。剛開始牆面是空的,求最後還有多少張海報是可見的(即未被其他海報完全覆蓋的)。
以下是本人程式碼(貌似有瑕疵,可過官方資料,有待學習):
#include <cstdio> #include <cstdlib> #include <cstring> #include <algorithm> using namespace std; int d,n,a[200010]={0},q[200010]={0},v[200010]={0},tot,ans; struct node { int l,r; int n; //n表示該區間最後被第n張海報完全覆蓋,當n為0時表示不能確定 }seg[600010]; bool cmp(int a,int b) { return a<b; } void buildtree(int no,int l,int r) { int mid=(l+r)/2; seg[no].l=l;seg[no].r=r;seg[no].n=0; if (l!=r) { buildtree(2*no,l,mid); buildtree(2*no+1,mid+1,r); } } void insert(int no,int s,int t,int i) { int mid=(seg[no].l+seg[no].r)/2; if (a[seg[no].l]>=s&&a[seg[no].r]<=t) { seg[no].n=i; return; } if (seg[no].n!=0) { seg[2*no].n=seg[2*no+1].n=seg[no].n; seg[no].n=0; } if (s<=a[mid]) insert(2*no,s,t,i); if (t>a[mid]) insert(2*no+1,s,t,i); if (seg[2*no].n==seg[2*no+1].n) seg[no].n=seg[2*no].n; else seg[no].n=0; } void query(int no) { if (seg[no].n!=0) { v[seg[no].n]=1; return; } if (seg[no].l!=seg[no].r) { query(2*no); query(2*no+1); } } int main() { scanf("%d",&d); for(int t=1;t<=d;t++) { memset(a,0,sizeof(a)); scanf("%d",&n); for(int i=1;i<=n;i++) { scanf("%d %d",&a[2*i-1],&a[2*i]); q[2*i-1]=a[2*i-1];q[2*i]=a[2*i]; } sort(a+1,a+2*n+1,cmp); tot=0; for(int i=1;i<=2*n;i++) if (a[tot]!=a[i]) {tot++;a[tot]=a[i];} //離散化 buildtree(1,1,tot); for(int i=1;i<=n;i++) insert(1,q[2*i-1],q[2*i],i); memset(v,0,sizeof(v)); query(1); ans=0; for(int i=1;i<=n;i++) if (v[i]) ans++; printf("%d\n",ans); } return 0; }
相關推薦
【POJ2528】Mayor's Posters-線段樹+離散化
(本人本題完成於2016-7-23) 題目大意:有一面很長(10000000節)的牆,要在上面貼N張海報,對於第i張海報,它將會覆蓋牆面的第Li~Ri節。剛開始牆面是空的,求最後還有多少張海報是可見的
poj2528--Mayor's posters(線段樹+離散化)
The citizens of Bytetown, AB, could not stand that the candidates in the mayoral election campaign have been placing their electoral poste
Mayor's posters 線段樹+離散化
%%% //感覺博主對於離散的解釋很好,很感謝。 %%% //博主解釋了一下本題離散的特殊性,感謝 如三張海報為:1~10 1~4 6~10 離散化時 X[ 1 ] = 1, X[ 2 ] = 4, X[ 3 ] = 6, X[ 4 ] = 10 第一張海報時
Mayor's posters (線段樹+離散化+加思維)
The citizens of Bytetown, AB, could not stand that the candidates in the mayoral election campaign have been placing their electoral poste
POJ 2528 Mayor's posters (線段樹+離散化 成段替換)
題目大意: 就是在一段區間上貼海報, 後來的區間會把前面來的區間覆蓋掉, 為貼完海報後能看到幾張海報(只看到一部分也算看到) 大致思路: 就是區間替換更新, 標記一下當前區間的所有的海報是否一致, 用懶惰標記標記一下當前區間的類別 聽說資料比較水...布吉島嚴格的資料下這
數算實習 Mayor's posters 線段樹 離散化
Mayor’s posters每個候選人都可以在牆上放置一張海報。 所有海報的高度都與牆的高度相同; 海報的寬度可以是任何整數個單位。 牆被分成10000000段,每段的寬度是一個單位。每張海報必須完全覆蓋連續數量個單位的牆壁。 你的任務是找到所有海報放置完畢後
poj2528 - Mayor's posters - 線段樹離散化(詳解)
Mayor's posters Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 79035 Accepted
D - Mayor's posters (線段樹 + 離散化)
滴答滴答---題目連結 The citizens of Bytetown, AB, could not stand that the candidates in the mayoral election campaign have been placing their electoral pos
poj 2528 Mayor's posters(線段樹+離散化)
The citizens of Bytetown, AB, could not stand that the candidates in the mayoral election campaign have been placing their electoral posters at
Mayor's posters (線段樹+離散化)
Mayor's posters Description The citizens of Bytetown, AB, could not stand that the candidates in the mayoral election campaign have been placing the
POJ 2528 Mayor's posters 線段樹(成段更新+離散化)
題意: 給出N個海報,每個海報有一個長度區間(a,b).按順序貼在牆上。 問最後可以看到幾張海報。 思路: 一想到的就是線段樹,對每個區間進行染色,最後查詢一共有多少種顏色。 第一次寫玩沒看資料大小。MLE了。。仔細一看,海報長度1QW。 然後寫了個離散化的,300MS+。
POJ 2528 Mayor's posters 線段樹成段更新+離散化
題意:很多張海報貼在牆上 求可以看到幾張海報 看那兩張圖就行了 第一張俯檢視 思路:最多2W個不同的數 離散化一下 然後成段更新 a[rt] = i代表這個區間是第i張報紙 更新玩之後一次query cover[i]=1代表可以看到第i張報紙 #include <c
【線段樹 + 離散化 + 詳細註釋】北大 poj 2528 Mayor's posters
/* THE PROGRAM IS MADE BY PYY */ /*----------------------------------------------------------------------------// Copyright (c) 2011 panyanyany All rig
Mayor's posters【線段樹+離散化】
題解:區間修改線段樹+離散化。cnt=unique(arr,arr+cnt)-arr 用於離散化時非常方便。程式碼:#include<stdio.h> #include<string.
POJ2528 Mayor's posters(線段樹+離散化)
Mayor’s posters Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 57835 Accepted: 16725 Description The ci
資料結構--線段樹&離散化可能有的問題--poj2528 Mayor's posters
給定n個區間,按順序覆蓋後,問最後幾個是可見的。 l,r,範圍1e7,需要離散化。 //但是注意簡單的離散化可能會出現錯誤,給出下面兩個簡單的例子應該能體現普通離散化的缺陷: //例子一:1-10 1-4 5-10 //例子二:1-10 1-4 6-10 //解決的
POJ 2528 Mayor's posters (線段樹 離散化+區間更新+區間求值 )
href eof 求值 給定 一個點 一個 stream 問題 void 題目鏈接:http://poj.org/problem?id=2528 題意:塗色問題,給定n個要塗色的區間(每次用的顏色不一樣,顏色覆蓋性極強),問最後能看到多少種顏色。(貼海報問題轉換) 題解
POJ--2528 Mayor's posters (線段樹)
題目連結:POJ--2528 Mayor's posters 題目大意:給你一個無限長的板子,然後依次往上面貼n張等高的海報,問你最後能看到多少張海報。 思路分析:線段樹區間更新問題,但是要注意,給的長度的可能非常大,有1e9,不加處理直接維護一個線段樹肯定會 MLE,TLE,但是我們注意到一共最多隻
POJ 2528 Mayor's posters(線段樹+離散化)
Mayor's posters Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 60691 Accepted: 17565 Description The citizens of Byteto
Mayor's posters(線段樹+離散化)
++ main splay d3d close tree pac pair lin 這道題最關鍵的點就在離散化吧。 假如有三張海報[1, 10] [10, 13][15, 20] 僅僅三個區間就得占用到20了。 但是離散化後就可以是[1, 2] [2, 3] [