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

hdu5124 线段树+离散化

时间:2014-11-29 22:54:57      阅读:246      评论:0      收藏:0      [点我收藏+]

标签:style   blog   io   color   sp   for   on   div   log   

题意:令a[l..r]都+1,求a[1..n]的最大值

裸的成段更新+区间最值,但是本题坐标范围很大(10^9),所以需要离散化

顺便离散化模板get

 

  1 #include <cstdio>
  2 #include <cstring>
  3 #include <cstdlib>
  4 #include <algorithm>
  5 
  6 #define lson l, m, rt << 1
  7 #define rson m + 1, r, rt << 1 | 1
  8 #define lc rt << 1
  9 #define rc rt << 1 | 1
 10 
 11 using namespace std;
 12 
 13 const int MAXN = 1111111;
 14 
 15 struct node
 16 {
 17     int l, r;
 18 };
 19 
 20 node D[MAXN];
 21 int limit, Q;
 22 int maxi[ MAXN << 2 ];
 23 int lazy[ MAXN << 2 ];
 24 int N;
 25 int X[MAXN];
 26 
 27 void build( int l, int r, int rt )
 28 {
 29     maxi[rt] = lazy[rt] = 0;
 30     if ( l == r ) return;
 31     int m = ( l + r ) >> 1;
 32     build( lson );
 33     build( rson );
 34     return;
 35 }
 36 
 37 void PushUp( int rt )
 38 {
 39     maxi[rt] = max( maxi[lc], maxi[rc] );
 40     return;
 41 }
 42 
 43 void PushDown( int rt )
 44 {
 45     if ( lazy[rt] )
 46     {
 47         lazy[lc] += lazy[rt];
 48         lazy[rc] += lazy[rt];
 49         maxi[lc] += lazy[rt];
 50         maxi[rc] += lazy[rt];
 51         lazy[rt] = 0;
 52     }
 53     return;
 54 }
 55 
 56 void update( int L, int R, int l, int r, int rt )
 57 {
 58     if ( L <= l && r <= R )
 59     {
 60         lazy[rt] += 1;
 61         maxi[rt] += 1;
 62         return;
 63     }
 64     PushDown( rt );
 65     int m = ( l + r ) >> 1;
 66     if ( L <= m ) update( L, R, lson );
 67     if ( R > m )  update( L, R, rson );
 68     PushUp( rt );
 69     return;
 70 }
 71 
 72 int query( int L, int R, int l, int r, int rt )
 73 {
 74     if ( L <= l && r <= R )
 75     {
 76         return maxi[rt];
 77     }
 78     PushDown( rt );
 79     int m = ( l + r ) >> 1;
 80 
 81     int res = -10;
 82     if ( L <= m ) res = max( res, query( L, R, lson ) );
 83     if ( R > m )  res = max( res, query( L, R, rson ) );
 84     PushUp( rt );
 85     return res;
 86 }
 87 
 88 int Bin(int key,int n,int X[]) {
 89          int l = 0 , r = n - 1;
 90          while (l <= r) {
 91                  int m = (l + r) >> 1;
 92                  if (X[m] == key) return m;
 93                  if (X[m] < key) l = m + 1;
 94                  else r = m - 1;
 95          }
 96          return -1;
 97 }
 98 
 99 int main()
100 {
101     int T;
102     scanf( "%d", &T );
103     while ( T-- )
104     {
105         scanf( "%d", &Q );
106         int nnd=0;
107         N = 0;
108         for ( int i = 0; i < Q; ++i )
109         {
110             int u, v;
111             scanf( "%d%d", &u, &v );
112             if (v>N) N=v;
113             D[i].l = u, D[i].r = v;
114             X[nnd++]=u;
115             X[nnd++]=v;
116         }
117         //build( 1, N, 1 );
118         //for (int i=0;i<Q;i++)
119         //    update( D[i].l, D[i].r, 1, N, 1 );
120 
121                  sort(X , X + nnd);
122                  int m = 1;
123                  for (int i = 1 ; i < nnd; i ++) {
124                           if (X[i] != X[i-1]) X[m ++] = X[i];
125                  }
126                  for (int i = m - 1 ; i > 0 ; i --) {
127                           if (X[i] != X[i-1] + 1) X[m ++] = X[i-1] + 1;
128                  }
129                  sort(X , X + m);
130                  build(0,m-1,1);
131                  for (int i = 0 ; i < Q ; i ++) {
132                           int l = Bin(D[i].l, m, X);
133                           int r = Bin(D[i].r, m, X);
134                           update(l, r , 0, m - 1, 1);
135                  }
136 
137         //int ans = query( 1, N , 1, N, 1 );
138         int ans=query(0,m-1,0,m-1,1);
139         printf( "%d\n", ans);
140         //update( D[i].l, D[i].r, 1, N, 1 );
141     }
142     return 0;
143 }

 

hdu5124 线段树+离散化

标签:style   blog   io   color   sp   for   on   div   log   

原文地址:http://www.cnblogs.com/pdev/p/4132068.html

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