hihocoder 1079 离散化(线段树+区间离散化)
2014-12-17 21:26
597 查看
题意:
区间总长为L,依次给n个子区间,后面的覆盖前面的,求最后没有被完全盖住的子区间个数。
离散化:维护区间端点的相对大小关系,将其map到[1, 2n]的区间上。
注意:map之后得到的是端点,而线段树上的节点是单位长度的区间,所以在update的时候应该 update(l, r-1)。。。
区间总长为L,依次给n个子区间,后面的覆盖前面的,求最后没有被完全盖住的子区间个数。
离散化:维护区间端点的相对大小关系,将其map到[1, 2n]的区间上。
注意:map之后得到的是端点,而线段树上的节点是单位长度的区间,所以在update的时候应该 update(l, r-1)。。。
#include <cmath> #include <cstdio> #include <cstdlib> #include <climits> #include <string> #include <cstring> #include <vector> #include <list> #include <queue> #include <iostream> #include <algorithm> #include <map> #include <set> using namespace std; #define rep(i, s, t) for(int (i)=(s);(i)<=(t);++(i)) #define urep(i, s, t) for(int (i)=(s);(i)>=(t);--(i)) typedef long long LL; const int inf = 0x7fffffff; const int Maxn = (int)1e5+10; int li[Maxn], ri[Maxn]; int vis[Maxn]; int n, L; const int Maxnode = Maxn<<2; int color[Maxnode]; #define lson(x) ((x)<<1) #define rson(x) (((x)<<1)|1) struct SegmentTree { int length, qL, qR; void init(int n) { length = n; memset(color, -1, sizeof(color)); } void query(int L, int R) { qL = L; qR = R; _query(1, 1, length); } void _query(int o, int L, int R) { if (color[o] != -1) { vis[color[o]] = 1; } else { if (L == R) return; int M = (L+R)>>1; int lc = lson(o), rc = rson(o); if (qL <= M) _query(lc, L, M); if (qR > M) _query(rc, M+1, R); } } void Set(int L, int R, int v) { qL = L; qR = R; _set(1, 1, length, v); } void _set(int o, int L, int R, int v) { if (qL <= L && qR >= R) { color[o] = v; } else { pushdown(o, L, R); int M = (L+R)>>1; int lc = lson(o), rc = rson(o); if (qL <= M) _set(lc, L, M, v); if (qR > M) _set(rc, M+1, R, v); } } void pushdown(int o, int L, int R) { if (color[o] != -1) { int lc = lson(o), rc = rson(o); color[lc] = color[rc] = color[o]; color[o] = -1; } } }; SegmentTree tree; int x[Maxn+5]; int main() { #ifndef ONLINE_JUDGE freopen("input.in", "r", stdin); #endif while (scanf("%d%*d", &n) != EOF) { if (n == 0) {cout << 0 << endl;continue;} int cnt = 0; rep(i, 1, n) { scanf("%d%d", li+i, ri+i); x[cnt++] = li[i]; x[cnt++] = ri[i]; } sort(x, x+cnt); cnt = unique(x, x+cnt) - x; tree.init(cnt); rep(i, 1, n) { int a = lower_bound(x, x+cnt, li[i])-x+1, b = lower_bound(x, x+cnt, ri[i])-x; if (a <= b) tree.Set(a, b, i); //cout << "map: " << a << ' ' << b << endl; } tree.query(1, cnt); int ans = 0; rep(i, 1, n) if (vis[i]) ++ans; printf("%d\n", ans); } return 0; }
相关文章推荐
- hiho1079 线段树区间改动离散化
- hihoCoder 1079 离散化(线段树离散化)
- hihcoder #1079 离散化 线段树+区间离散化
- hihoCoder - 1079 - 离散化 (线段树 + 离散化)
- hihoCoder:#1079(线段树+离散化)
- hihocoder-1079题解(线段树+离散化)
- hiho1079 : 离散化(线段树+区间离散化)
- HihoCoder 1079(线段树,改变递归区间解决问题)
- HihoCoder 1079 线段树 + 离散化
- hiho一下第二十一周 #1079 : 离散化 【线段树储存区间状态】
- hihoCoder - 1079 - 离散化 (线段树 + 离散化)
- poj-----(2528)Mayor's posters(线段树区间更新及区间统计+离散化)
- POJ 2528 Mayor's posters (线段树区间更新+离散化)
- poj 3368 Frequent values(离散化+线段树区间求最值)
- zoj 2301 Color the Ball(区间染色,线段树+离散化)
- HihoCoder-1586 Minimum(线段树,区间最值,ACM-ICPC国际大学生程序设计竞赛北京赛区(2017)网络赛)
- poj 2528 线段树离散化+区间更新
- poj—2528Mayor's posters-线段树+区间离散化
- 离散化—区间覆盖——线段树实践POJ2528
- hihoCoder 1078 : 线段树的区间修改