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.
#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
using namespace std;
const int M = 2e5 + 10;
int a[M] , b[M] , c[2 * M] , d[2 * M] , e[4 * M];
struct TnT {
int l , r , num , add;
}T[M << 2];
int re;
void push(int p) {
if(T[p].add) {
T[p << 1].num = (T[p << 1].r - T[p << 1].l + 1);
T[(p << 1) | 1].num = (T[(p << 1) | 1].r - T[(p << 1) | 1].l + 1);
T[p << 1].add = T[p].add;
T[(p << 1) | 1].add = T[p].add;
T[p].add = 0;
}
}
void build(int l , int r , int p) {
int mid = (l + r) >> 1;
T[p].l = l , T[p].r = r , T[p].num = 0 , T[p].add = 0;
if(T[p].l == T[p].r) {
return ;
}
build(l , mid , p << 1);
build(mid + 1 , r , (p << 1) | 1);
T[p].num = T[p << 1].num + T[(p << 1) | 1].num;
}
void updata(int l , int r , int p) {
int mid = (T[p].l + T[p].r) >> 1;
if(T[p].l == l && T[p].r == r) {
T[p].add = 1;
T[p].num = (r - l + 1);
return ;
}
push(p);
if(mid < l) {
updata(l , r , (p << 1) | 1);
}
else if(mid >= r) {
updata(l , r , p << 1);
}
else {
updata(l , mid , p << 1);
updata(mid + 1 , r , (p << 1) | 1);
}
T[p].num = T[p << 1].num + T[(p << 1) | 1].num;
}
int query(int l , int r , int p) {
int mid = (T[p].l + T[p].r) >> 1;
if(T[p].l == l && T[p].r == r) {
return T[p].num;
}
push(p);
T[p].num = T[p << 1].num + T[(p << 1) | 1].num;
if(mid < l) {
return query(l , r , (p << 1) | 1);
}
else if(mid >= r) {
return query(l , r , p << 1);
}
else {
return query(l , mid , p << 1) + query(mid + 1 , r , (p << 1) | 1);
}
}
int search(int ll, int hh, int xx) {
int mm;
while (ll <= hh) {
mm = (ll + hh) >> 1;
if (e[mm] == xx) return mm;
else if (e[mm] > xx) hh = mm - 1;
else ll = mm + 1;
}
return -1;
}
int main()
{
int t;
scanf("%d" , &t);
while(t--) {
int n;
scanf("%d" , &n);
int gg = 0;
for(int i = 1 ; i <= n ; i++) {
scanf("%d%d" , &a[i] , &b[i]);
c[++gg] = a[i];
c[++gg] = b[i];
}
sort(c + 1 , c + gg + 1);
int mm = 0;
c[gg + 1] = -1;
for(int i = 1 ; i <= gg ; i++) {
if(c[i] != c[i + 1]) {
d[++mm] = c[i];
}
}
e[1] = d[1];
int mt = 1;
for(int i = 2 ; i <= mm ; i++) {
if(d[i] - d[i - 1] > 1) {
e[++mt] = d[i - 1] + 1;
e[++mt] = d[i];
}
else {
e[++mt] = d[i];
}
}
// for(int i = 1 ; i <= mt ; i++) {
// cout << e[i] << ' ';
// }
build(1 , mt + 1 , 1);
int count = 0;
for(int i = n ; i >= 1 ; i--) {
int r = search(1 , mt , b[i]);
int l = search(1 , mt , a[i]);
re = query(l , r , 1);
//cout << re << endl;
if(re < r - l + 1) {
count++;
}
updata(l , r , 1);
}
printf("%d\n" , count);
}
return 0;
}