Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 70483 | Accepted: 20331 |
Description
- Every candidate can place exactly one poster on the wall.
- All posters are of the same height equal to the height of the wall; the width of a poster can be any integer number of bytes (byte is the unit of length in Bytetown).
- The wall is divided into segments and the width of each segment is one byte.
- Each poster must completely cover a contiguous number of wall segments.
They have built a wall 10000000 bytes long (such that there is enough place for all candidates). When the electoral campaign was restarted, the candidates were placing their posters on the wall and their posters differed widely in width. Moreover, the candidates started placing their posters on wall segments already occupied by other posters. Everyone in Bytetown was curious whose posters will be visible (entirely or in part) on the last day before elections.
Your task is to find the number of visible posters when all the posters are placed given the information about posters' size, their place and order of placement on the electoral wall.
Input
Output
The picture below illustrates the case of the sample input.
Sample Input
1 5 1 4 2 6 8 10 3 4 7 10
Sample Output
4
Source
給你一個個區間
你要貼廣告
每次後面的廣告會蓋上前面的廣告
詢問最後能看到多少個廣告
線段樹的題
好像剛開始想歪了
我也不知道怎麼想歪的
其實只要到把貼廣告的順序倒過來
這樣如果每次貼的廣告能覆蓋到未被貼廣告的區間
那就說明這個廣告能被看到
然後用線段樹維護
每個節點有左節點右節點的編號
以及左右節點編號包含的區間是否被貼廣告(0爲未被貼, 1爲被貼)
每次貼廣告的時候從樹頂往下更新
如果掃到一個節點的廣告標記爲 1
那就不往下掃了
如果區間恰好重疊就把廣告標記標記爲1
同時每輪貼廣告ans只加一次
同時這道題需要離散化
然後離散化還不是一般的離散化
例如
3
1 10
1 3
8 10
答案是 3
離散化以後是
1 4
1 2
3 4
這樣答案就成2了,,
所以防止這種情況
第一次離散化以後XJB判一下就好了
剛開始忘記寫up操作了WA了
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
using namespace std;
const int N = 400100;
struct Three
{
int l;
int r;
int mark;
};
struct Quary
{
int l;
int r;
};
Quary quary[N];
Three tree[N * 4];
int n;
int mark;
int ans;
int al[N];
int ar[N];
void up(int x)
{
if(tree[x << 1].mark == 1 && tree[x << 1 | 1].mark == 1){
tree[x].mark = 1;
}
}
void build(int x, int l, int r)
{
tree[x].l = l;
tree[x].r = r;
tree[x].mark = 0;
if(l != r){
int mid = l + ((r - l) >> 1);
build(x << 1, l, mid);
build(x << 1 | 1, mid + 1, r);
up(x);
}
}
void update(int x, int l, int r)
{
int ll = tree[x].l;
int rr = tree[x].r;
if(l > r || tree[x].mark){
return ;
}
if(ll == l && rr == r){
if(!mark){
mark = 1;
ans ++;
tree[x].mark = 1;
}
else{
tree[x].mark = 1;
}
}
else{
int mid = ll + ((rr - ll) >> 1);
update(x << 1, l, min(mid, r));
update(x << 1 | 1, max(mid + 1, l), r);
up(x);
}
}
int main(int argc, char const *argv[])
{
int ncase;
scanf("%d", &ncase);
while(ncase --){
ans = 0;
scanf("%d", &n);
int rec[N], p = 0;
for(int i = 0; i < n; i ++){
scanf("%d%d", &quary[i].l, &quary[i].r);
rec[p++] = quary[i].l;
rec[p++] = quary[i].r;
}
/*小太陽寫的離散化*/
sort(rec, rec + p);
p = unique(rec, rec + p) - rec;
int tmp = p;
for(int i = 1; i < tmp; ++i)
if(rec[i] - rec[i-1] > 1)rec[p++] = rec[i-1] + 1;
sort(rec, rec + p);
p = unique(rec, rec + p) - rec;
/*小太陽寫的離散化*/
build(1, 1, p);
for(int i = n - 1; i >= 0; i --){
int lll = lower_bound(rec, rec + p, quary[i].l) - rec + 1;
int rrr = lower_bound(rec, rec + p, quary[i].r) - rec + 1;
mark = 0;
update(1, lll, rrr);
}
printf("%d\n", ans);
}
return 0;
}