离散化线段树

  1 #include <iostream>
  2 #include <cstdio>
  3 #include <cstring>
  4 #include <cmath>
  5 #include <ctime>
  6 #include <climits>
  7 #include <string>
  8 #include <iterator>
  9 #include <algorithm>
 10 #include <cstdlib>
 11 #include <queue>
 12 #include <stack>
 13 #include <list>
 14 #include <map>
 15 #include <vector>
 16 #include <iterator>
 17 using namespace std;
 18 #define PI acos(-1.0)
 19 #define INF 0x3f3f3f3f
 20 #define MAXN 400005
 21 #define MAXM 400005
 22 #define MOD 10000
 23 #define EPS 1e-6
 24 #define rst(a,b) memset(a,b,sizeof(a))
 25 #define pd(a) cout << "debug:" << a << " ";
 26 #define pp(a) cout << a << " ";
 27 #define pl(a) cout << a << endl;
 28 #define FOR(a, b, c) for(int a = b; a < c; a++)
 29 typedef long long LL;
 30 typedef unsigned long long ULL;
 31 typedef pair<int,int> pii;
 32 struct Line {
 33     int num;
 34     int li;
 35 }line[MAXN];
 36 struct node {
 37     int l, r;
 38     int color;
 39 }tree[MAXN*4];
 40 int post[MAXN][2];
 41 bool cmp(Line a, Line b) {
 42     return a.li < b.li;
 43 }
 44 void build(int c, int l, int r) {
 45     tree[c].l = l;
 46     tree[c].r = r;
 47     tree[c].color = 0;
 48     if(l == r) return;
 49     else {
 50         int mid = (l + r) / 2;
 51         build(2*c, l, mid);
 52         build(2*c+1, mid+1, r);
 53     }
 54 }
 55 void updata(int c, int l, int r, int color) {
 56     if(tree[c].l == l && tree[c].r == r) {
 57         tree[c].color = color;
 58         //printf("(%d, %d) - %d\n",tree[c].l, tree[c].r, tree[c].color);
 59         return;
 60     }
 61     if(tree[c].color) {
 62         tree[2*c].color = tree[c].color;
 63         tree[2*c+1].color = tree[c].color;
 64         tree[c].color = 0;
 65     }
 66     int mid = (tree[c].l + tree[c].r) / 2;
 67     if(r <= mid) updata(2*c, l, r, color);
 68     else if(l > mid) updata(2*c+1, l, r, color);
 69     else {
 70         updata(2*c, l, mid, color);
 71         updata(2*c+1, mid+1, r, color);
 72     }
 73 }
 74 int ans;
 75 bool vis[MAXN];
 76 void query(int c) {
 77     if(tree[c].color) {
 78         if(!vis[tree[c].color]) {
 79             //printf("(%d - %d)", tree[c].l, tree[c].r);
 80             vis[tree[c].color] = 1;
 81             ans++;
 82         }
 83         return;
 84     }
 85     if(tree[c].l == tree[c].r) return;
 86     query(2*c);
 87     query(2*c+1);
 88 }
 89 int main() {
 90     //freopen("d:\\input.txt", "r", stdin);
 91     int n, l;
 92     scanf("%d %d", &n, &l);
 93     for(int i = 0; i < n; i++) {
 94         scanf("%d %d", &post[i][0], &post[i][1]);
 95         line[2*i].li = post[i][0];
 96         line[2*i].num = -(i+1);
 97         line[2*i+1].li = post[i][1];
 98         line[2*i+1].num = i+1;
 99     }
100     sort(line, line+2*n, cmp);
101     int tmp = line[0].li;
102     int tp = 1;
103     for(int i = 0; i < 2*n; i++) {
104         if(tmp != line[i].li) {
105             tp += 2;
106             tmp = line[i].li;
107         }
108         if(line[i].num < 0) post[-line[i].num-1][0] = tp;
109         else post[line[i].num-1][1] = tp;
110     }
111     build(1, 1, tp);
112     for(int i = 0; i < n; i++) {
113         //printf("(%d, %d)\n", post[i][0], post[i][1]);
114         updata(1, post[i][0], post[i][1], i+1);
115     }
116     ans = 0;
117     query(1);
118     printf("%d\n", ans);
119     return 0;
120 }
时间: 2024-08-29 15:54:48

离散化线段树的相关文章

poj 2528 Mayor&#39;s posters【离散化+线段树】

题目:poj 2528 Mayor's posters 题意:给一个长度非常长的墙上贴长度为ai的海报,由于有的会覆盖掉,求最后能看见的海报个数. 分析:题目和POJ2777 一模一样,方法也一样,只不过这个要离散化,其次要数组开大一点.至少2倍. 离散化的时候用了C++的 pair 类,还是比较好用的. 代码: #include <iostream> #include <algorithm> #include <utility> #include <cstrin

POJ 2299 离散化线段树

点击打开链接 Ultra-QuickSort Time Limit: 7000MS   Memory Limit: 65536K Total Submissions: 40827   Accepted: 14752 Description In this problem, you have to analyze a particular sorting algorithm. The algorithm processes a sequence of n distinct integers by

南阳理工 题目9:posters(离散化+线段树)

posters 时间限制:1000 ms  |  内存限制:65535 KB 难度:6 描述 The citizens of Bytetown, AB, could not stand that the candidates in the mayoral election campaign have been placing their electoral posters at all places at their whim. The city council has finally deci

Mayor&#39;s posters(离散化线段树)

Mayor's posters Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 54067   Accepted: 15713 Description The citizens of Bytetown, AB, could not stand that the candidates in the mayoral election campaign have been placing their electoral post

POJ 2528 Mayor&#39;s posters(离散化线段树)

Description The citizens of Bytetown, AB, could not stand that the candidates in the mayoral election campaign have been placing their electoral posters at all places at their whim. The city council has finally decided to build an electoral wall for

bnu36905 Nested Segments 离散化+线段树

bnu36905 Nested Segments 离散化+线段树区间更新 也可以用离散化+set(或双向链表) #include <cstdio> #include <ctime> #include <cstdlib> #include <cstring> #include <queue> #include <string> #include <set> #include <stack> #include &l

POJ 2528 Mayor&amp;#39;s posters 离散化+线段树

题目大意:给出一些海报和贴在墙上的区间.问这些海报依照顺序贴完之后,最后能后看到多少种海报. 思路:区间的范围太大,然而最多仅仅会有10000张海报,所以要离散化. 之后用线段树随便搞搞就能过. 关键是离散化的方法,这个题我时隔半年才A掉,之前一直就TTT,我还以为是线段树写挂了. 当我觉得我自己的水平这样的水线段树已经基本写不挂的时候又写了这个题,竟然还是T. 后来我对照别人的代码,才发现是我的离散化写渣了. 以下附AC代码(79ms),这个离散化写的比較优雅.时间也非常快,以后就这么写了.

POJ 2528 Mayor&#39;s posters (离散化 + 线段树)

强烈不推荐在POJ做这道题!!! 强烈不推荐在POJ做这道题!!! 强烈不推荐在POJ做这道题!!! 推荐去UVA 10587 或 SCU 2249 POJ的数据比较水且可能有错,一些本来错误的数据但可以水过,以及在UVA与SCU同样题目都能AC的程序在POJ莫名WA了. 建议写完程序后跑下这组数据: 1 3 1 10 1 3 6 10 好多题解的答案是2,但答案明显是3,这是因为每个数字其实表示的是一个单位长度,并非一个点 , 这就会导致像这样的区间: 1-10 1-4 5-10 1-10 1

【POJ】2528 Mayor&#39;s posters ——离散化+线段树

Mayor's posters Time Limit: 1000MS    Memory Limit: 65536K Description The citizens of Bytetown, AB, could not stand that the candidates in the mayoral election campaign have been placing their electoral posters at all places at their whim. The city

POJ 2528 Mayor&#39;s posters(离散化 线段树)

题意  在墙上贴n张海报  输入每张海报的的左右端点坐标  问最后可以看到多少张海报  能看到一点也是能看到 先把线段树初始化为0 输入一张海报  就把那个区间变成这张海报的序号  最后判断墙上有多少个不同的序号就行了 但是海报坐标的端点值高达10000000  直接用线段树会超时   但是注意到海报最多只有10000张  也就是最多有20000个不同的坐标  于是可以利用离散化的知识   把所有坐标排序 注意所有右端点坐标+1也要加入排序(注意1,10 ; 1,3;  7,10这种情况  如果