码迷,mamicode.com
首页 > 其他好文 > 详细

离散化线段树

时间:2015-04-26 16:30:51      阅读:190      评论:0      收藏:0      [点我收藏+]

标签:

  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 }

 

离散化线段树

标签:

原文地址:http://www.cnblogs.com/mitrenick/p/4457905.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!