POJ2528 - Mayor's posters (线段树,模板)
思路
板子题,需要离散化。然后从最后一张海报往前枚举看看有没有被完全覆盖。
#include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>
#include <vector>
using namespace std;
typedef long long ll;
const ll maxn = 100010;
ll sum[maxn << 2];
ll lazy[maxn << 2];
vector<int> numb;
void pushup(int rt) {
sum[rt] = sum[rt << 1] + sum[rt << 1 | 1];
}
void pushdown(int rt, int len) {
if(lazy[rt]) {
lazy[rt << 1] = lazy[rt];
lazy[rt << 1 | 1] = lazy[rt];
sum[rt << 1] = lazy[rt] * (len - (len >> 1));
sum[rt << 1 | 1] = lazy[rt] * (len >> 1);
lazy[rt] = 0;
}
}
void update(int L, int R, int lef, int rig, int rt, ll val) { //将区间[L,R]全设置为1
if(lef >= L && rig <= R) {
sum[rt] = val * (rig - lef + 1);
lazy[rt] = val;
return ;
}
pushdown(rt, rig - lef + 1);
int mid = (lef + rig) / 2;
if(L <= mid) update(L, R, lef, mid, rt << 1, val);
if(R > mid) update(L, R, mid + 1, rig, rt << 1 | 1, val);
pushup(rt);
}
void build(int lef, int rig, int rt) {
if(lef > rig) return ;
lazy[rt] = 0;
if(lef == rig) {
sum[rt] = 0;
return ;
}
int mid = (lef + rig) / 2;
build(lef, mid, rt << 1);
build(mid + 1, rig, rt << 1 | 1);
pushup(rt);
}
ll query(int L, int R, int lef, int rig, int rt) {
if(lef >= L && rig <= R) return sum[rt];
pushdown(rt, rig - lef + 1);
int mid = (lef + rig) / 2;
ll res = 0;
if(L <= mid) res += query(L, R, lef, mid, rt << 1);
if(R > mid) res += query(L, R, mid + 1, rig, rt << 1 | 1);
return res;
}
struct sub {
int l, r;
}post[20000];
int main() {
int T;
scanf("%d", &T);
while(T--) {
numb.clear();
int n;
scanf("%d", &n);
for(int i = 0; i < n; i++) {
int l, r;
scanf("%d%d", &l, &r);
numb.push_back(l);
numb.push_back(r);
post[i] = sub{l ,r};
}
sort(numb.begin(), numb.end());
unique(numb.begin(), numb.end());
for(int i = 0; i < n; i++) {
post[i].l = lower_bound(numb.begin(), numb.end(), post[i].l) - numb.begin() + 1;
post[i].r = lower_bound(numb.begin(), numb.end(), post[i].r) - numb.begin() + 1;
}
int ans = 0;
int maxn = numb.size() + 2;
build(1, maxn, 1);
for(int i = n - 1; i >= 0; i--) {
if(query(post[i].l, post[i].r, 1, maxn, 1) < post[i].r - post[i].l + 1) ans++;
update(post[i].l, post[i].r, 1, maxn, 1, 1);
}
printf("%d\n", ans);
}
}