bnu36905 Nested Segments 离散化+线段树
来源:互联网 发布:去掉数组中重复的元素 编辑:程序博客网 时间:2024/06/05 05:26
bnu36905 Nested Segments
离散化+线段树区间更新
也可以用离散化+set(或双向链表)
#include <cstdio>#include <ctime>#include <cstdlib>#include <cstring>#include <queue>#include <string>#include <set>#include <stack>#include <map>#include <cmath>#include <vector>#include <iostream>#include <algorithm>#include <bitset>#include <fstream>using namespace std;//LOOP#define FF(i, a, b) for(int i = (a); i < (b); ++i)#define FE(i, a, b) for(int i = (a); i <= (b); ++i)#define FED(i, b, a) for(int i = (b); i>= (a); --i)#define REP(i, N) for(int i = 0; i < (N); ++i)#define CLR(A,value) memset(A,value,sizeof(A))#define FC(it, c) for(__typeof((c).begin()) it = (c).begin(); it != (c).end(); it++)//OTHER#define SZ(V) (int)V.size()#define PB push_back#define MP make_pair#define all(x) (x).begin(),(x).end()//INPUT#define RI(n) scanf("%d", &n)#define RII(n, m) scanf("%d%d", &n, &m)#define RIII(n, m, k) scanf("%d%d%d", &n, &m, &k)#define RIV(n, m, k, p) scanf("%d%d%d%d", &n, &m, &k, &p)#define RV(n, m, k, p, q) scanf("%d%d%d%d%d", &n, &m, &k, &p, &q)#define RS(s) scanf("%s", s)#define sqr(x) (x) * (x)typedef long long LL;typedef unsigned long long ULL;typedef vector <int> VI;const int INF = 0x3f3f3f3f;const double EPS = 1e-9;const int MOD = 1000000007;const double PI = acos(-1.0);const int maxn = 410000;#define ll rt << 1#define rr rt << 1 | 1struct Node{ int l, r, m; int val;}st[maxn << 2];void build(int l, int r, int rt){ st[rt].l = l; st[rt].r = r; int m = (l + r) >> 1; st[rt].m = m; st[rt].val = -1; if (l == r) return ; build(l, m, ll); build(m + 1, r, rr);}inline void segVal(int rt, int x){ st[rt].val = x;}inline void down(int rt){ if (st[rt].val != -1) { segVal(ll, st[rt].val); segVal(rr, st[rt].val); st[rt].val = -1; }}void update(int L, int R, int x, int rt){ if (L <= st[rt].l && st[rt].r <= R) { segVal(rt, x); return ; } down(rt); if (L <= st[rt].m) update(L, R, x, ll); if (R > st[rt].m) update(L, R, x, rr);}int query(int p, int rt){ if (st[rt].l == st[rt].r) return st[rt].val; down(rt); if (p <= st[rt].m) return query(p, ll); else return query(p, rr);}int n;struct Seg{ int l, r; int id; bool operator<(const Seg &rhs) const{ return r - l > rhs.r - rhs.l; }}seg[100010];int a[maxn];int asz;int main(){ while (~RI(n)) { asz = 0; REP(i, n) { scanf("%d%d", &seg[i].l, &seg[i].r); a[asz++] = seg[i].l; a[asz++] = seg[i].r; seg[i].id = i + 1; } sort(seg, seg + n); ///离散化start a[asz++] = 0; a[asz++] = 1e9 + 1; sort(a, a + asz); asz = unique(a, a + asz) - a; int atot = asz; for (int i = 1; i < asz; i++) { if (a[i] - a[i - 1] > 1) a[atot++] = a[i] - 1;/// } asz = atot; sort(a, a + asz); ///离散化end build(1, asz, 1); for (int i = 0; i < n; i++) { int L = lower_bound(a, a + asz, seg[i].l) - a; int R = lower_bound(a, a + asz, seg[i].r) - a; update(L + 1, R + 1, seg[i].id, 1); } int q; RI(q); while (q--) { int x; RI(x); int xx = lower_bound(a, a + asz, x) - a; printf("%d\n", query(xx + 1, 1)); } } return 0;}
0 0
- bnu36905 Nested Segments 离散化+线段树
- ural 1987. Nested Segments【离散化+贪心+线段树】
- Open Ural FU Championship 2013 G. Nested Segments(线段树&离散化)
- 【BNU Summer Training 2014.08.11】 Nested Segments (线段树,离散化)
- URAL1987. Nested Segments 线段树
- [线段树+离散化]LightOJ 1089 - Points in Segments (II)
- Codeforces 652D Nested Segments 【树状数组 + 离散化】
- CodeForces 652D Nested Segments(树状数组+离散化)
- CodeForces 652D Nested Segments(离散化,树状数组)
- 【Codeforces 652 D Nested Segments】+ 树状数组 + 离散化
- Codeforces 652D Nested Segments【离散化+思维+树状数组】
- Nested Segments codeforces 652D 树状数组 +离散化
- CodeForces 652D Nested Segments(树状数组 离散化)
- Nested Segments CodeForces 652D 树状数组+离散化
- URAL 1987. Nested Segments(数学 & 线段树)
- Codeforces-652D : Nested Segments(线段树)
- Codeforces 612 D. The Union of k-Segments (非递归线段树+离散化)
- Code Forces 652D Nested Segments(离散化+树状数组)
- JMM介绍
- Runtime类
- [LeetCode]Longest Substring Without Repeating Characters
- Labwindows/CVI下调用openCV实现多摄像头切换
- 母函数之Big Event in HDU hdoj 1171
- bnu36905 Nested Segments 离散化+线段树
- Android开发周围wifi热点和属性的获取
- 一些有用的数论知识。(莫比乌斯反演)
- 陈老师的多校联合2009年武汉站D题 ||hdu 3033
- POJ 1113 Wall 二维凸包
- hdu 2092 求整数解是否存在
- HDOJ题目2120 Ice_cream's world I(并查集)
- PHP语法自动检查的Vim插件
- UVA524- Prime Ring Problem