1. HDU1166

  裸线段树点修改

  1. #include <iostream>
  2. #include <string.h>
  3. #include <cstdio>
  4. #include <queue>
  5. #include <map>
  6. #include <vector>
  7. #include <string>
  8. #include <cstring>
  9. #include <algorithm>
  10. #include <math.h>
  11.  
  12. #define SIGMA_SIZE 26
  13. #pragma warning ( disable : 4996 )
  14.  
  15. using namespace std;
  16. typedef long long LL;
  17.  
  18. inline LL LMax(LL a,LL b) { return a>b?a:b; }
  19. inline LL LMin(LL a,LL b) { return a>b?b:a; }
  20. inline int Max(int a,int b) { return a>b?a:b; }
  21. inline int Min(int a,int b) { return a>b?b:a; }
  22. inline int gcd( int a, int b ) { return b==?a:gcd(b,a%b); }
  23. inline int lcm( int a, int b ) { return a/gcd(a,b)*b; } //a*b = gcd*lcm
  24. const long long INF = 0x3f3f3f3f3f3f3f3f;
  25. const int inf = 0x3f3f3f3f;
  26. const int mod = ;
  27. const int maxk = 5e4+;
  28. const int maxn = 5e4+;
  29.  
  30. int N;
  31. int num[maxn];
  32. int sum[maxn<<];
  33. char str[];
  34.  
  35. void init()
  36. {
  37. memset( num, , sizeof(num) );
  38. memset( sum, , sizeof(sum) );
  39. }
  40.  
  41. void pushup( int rt )
  42. { sum[rt] = sum[rt<<]+sum[rt<<|]; }
  43.  
  44. void build( int l, int r, int rt )
  45. {
  46. if ( l == r )
  47. { sum[rt] = num[l]; return; }
  48. int m = (l+r)>>;
  49.  
  50. build( l, m, rt<< );
  51. build( m+, r, rt<<| );
  52. pushup(rt);
  53. }
  54.  
  55. void update(int L, int C, int l, int r, int rt)
  56. {
  57. if( l == r )
  58. { sum[rt]+=C; return; }
  59.  
  60. int m=(l+r)>>;
  61.  
  62. if(L <= m)
  63. update( L, C, l, m, rt<< );
  64. else
  65. update( L, C, m+, r, rt<<| );
  66. pushup(rt);
  67. }
  68.  
  69. int query( int L, int R, int l, int r, int rt )
  70. {
  71. if ( L <= l && R >= r )
  72. return sum[rt];
  73.  
  74. int m = (l+r)>>;
  75.  
  76. int Ans = ;
  77. if ( L <= m ) Ans += query( L, R, l, m, rt<< );
  78. if ( R > m ) Ans += query( L, R, m+, r, rt<<| );
  79. return Ans;
  80. }
  81.  
  82. int main()
  83. {
  84. int T; cin >> T;
  85. int cnt = ;
  86. while (T--)
  87. {
  88. init();
  89. scanf("%d", &N);
  90. for ( int i = ; i <= N; i++ )
  91. scanf( "%d", &num[i] );
  92.  
  93. build( , N, );
  94. printf( "Case %d:\n", cnt++ );
  95.  
  96. int x, w;
  97. while ()
  98. {
  99. scanf( "%s", str );
  100. if ( str[] == 'E' )
  101. break;
  102.  
  103. scanf( "%d %d", &x, &w );
  104. if ( str[] == 'A' )
  105. update( x, w, , N, );
  106. else if ( str[] == 'S' )
  107. update( x, -w, , N, );
  108. else if ( str[] == 'Q' )
  109. printf( "%d\n", query(x,w,,N,) );
  110. }
  111. }
  112. return ;
  113. }

  2.HDU 1754

  不知道为什么scanf读不了char,变成了“烫烫烫烫”....还是裸线段树求区间最大

  1. #include <iostream>
  2. #include <string.h>
  3. #include <cstdio>
  4. #include <queue>
  5. #include <map>
  6. #include <vector>
  7. #include <string>
  8. #include <cstring>
  9. #include <algorithm>
  10. #include <math.h>
  11.  
  12. #define SIGMA_SIZE 26
  13. #pragma warning ( disable : 4996 )
  14.  
  15. using namespace std;
  16. typedef long long LL;
  17.  
  18. inline LL LMax(LL a,LL b) { return a>b?a:b; }
  19. inline LL LMin(LL a,LL b) { return a>b?b:a; }
  20. inline int Max(int a,int b) { return a>b?a:b; }
  21. inline int Min(int a,int b) { return a>b?b:a; }
  22. inline int gcd( int a, int b ) { return b==?a:gcd(b,a%b); }
  23. inline int lcm( int a, int b ) { return a/gcd(a,b)*b; } //a*b = gcd*lcm
  24. const long long INF = 0x3f3f3f3f3f3f3f3f;
  25. const int inf = 0x3f3f3f3f;
  26. const int mod = ;
  27. const int maxk = 5e4+;
  28. const int maxn = 2e5+;
  29.  
  30. int N;
  31. int num[maxn];
  32. int mmax[maxn<<];
  33.  
  34. void pushup( int rt )
  35. { mmax[rt] = Max( mmax[rt<<], mmax[rt<<|] ); }
  36.  
  37. void build( int l, int r, int rt )
  38. {
  39. if ( l == r )
  40. {
  41. mmax[rt] = num[l];
  42. return;
  43. }
  44.  
  45. int m = (l+r)>>;
  46. build( l, m, rt<< );
  47. build( m+, r, rt<<| );
  48.  
  49. pushup(rt);
  50. }
  51.  
  52. void update( int L, int C, int l, int r, int rt )
  53. {
  54. if ( r == l )
  55. {
  56. mmax[rt] = C;
  57. return;
  58. }
  59.  
  60. int mid = (l+r)>>;
  61. if ( L <= mid ) update( L, C, l, mid, rt<< );
  62. else update( L, C, mid+, r, rt<<| );
  63.  
  64. pushup(rt);
  65. }
  66.  
  67. int query( int lhs, int rhs, int l, int r, int rt )
  68. {
  69.  
  70. if ( lhs <= l && rhs >= r )
  71. return mmax[rt];
  72.  
  73. int mid = (l+r)>>;
  74. int m = -;
  75.  
  76. if ( lhs <= mid ) m = Max( m, query(lhs, rhs, l, mid, rt<<) );
  77. if ( rhs > mid ) m = Max( m, query(lhs, rhs, mid+, r, rt<<|) );
  78.  
  79. return m;
  80. }
  81.  
  82. int main()
  83. {
  84. int N, M;
  85. char c[];
  86. char ch;
  87.  
  88. while ( ~scanf("%d %d", &N, &M) )
  89. {
  90. memset( mmax, , sizeof(mmax) );
  91.  
  92. for ( int i = ; i <= N; i++ )
  93. scanf("%d", &num[i]);
  94.  
  95. build( , N, );
  96.  
  97. int x, y;
  98. for ( int i = ; i <= M; i++ )
  99. {
  100. scanf("%s%d%d", &c, &x, &y);
  101.  
  102. if ( c[] == 'U' )
  103. update( x, y, , N, );
  104. else
  105. printf( "%d\n", query(x, y, , N, ) );
  106. }
  107. }
  108. return ;
  109. }

  3.POJ 3468

  简单的区间修改,但是容易错,数字全部都用long long 才行

  1. #include <iostream>
  2. #include <string.h>
  3. #include <cstdio>
  4. #include <queue>
  5. #include <map>
  6. #include <vector>
  7. #include <string>
  8. #include <cstring>
  9. #include <algorithm>
  10. #include <math.h>
  11.  
  12. #define SIGMA_SIZE 26
  13. #pragma warning ( disable : 4996 )
  14.  
  15. using namespace std;
  16. typedef long long LL;
  17.  
  18. inline LL LMax(LL a,LL b) { return a>b?a:b; }
  19. inline LL LMin(LL a,LL b) { return a>b?b:a; }
  20. inline int Max(int a,int b) { return a>b?a:b; }
  21. inline int Min(int a,int b) { return a>b?b:a; }
  22. inline int gcd( int a, int b ) { return b==?a:gcd(b,a%b); }
  23. inline int lcm( int a, int b ) { return a/gcd(a,b)*b; } //a*b = gcd*lcm
  24. const long long INF = 0x3f3f3f3f3f3f3f3f;
  25. const int inf = 0x3f3f3f3f;
  26. const int mod = ;
  27. const int maxk = 5e4+;
  28. const int maxn = 1e5+;
  29.  
  30. int N;
  31. LL num[maxn], Add[maxn<<];
  32. LL sum[maxn<<];
  33.  
  34. void pushup(LL rt) { sum[rt]=sum[rt<<]+sum[rt<<|]; }
  35.  
  36. //ln是左子树数字节点的数目, rn是右子树数字节点的数目
  37. void pushdown( LL rt, LL ln, LL rn )
  38. {
  39. if ( Add[rt] != )
  40. {
  41. //下推标记
  42. Add[rt<<] += Add[rt];
  43. Add[rt<<|] += Add[rt];
  44. //修改子节点的sum
  45. sum[rt<<] += Add[rt]*ln;
  46. sum[rt<<|] += Add[rt]*rn;
  47.  
  48. Add[rt] = ;
  49. }
  50. }
  51.  
  52. void build( LL l, LL r, LL rt )
  53. {
  54. if ( l == r )
  55. {
  56. sum[rt] = num[l];
  57. return;
  58. }
  59.  
  60. LL mid = (l+r)>>;
  61. build(l, mid, rt<<);
  62. build(mid+, r, rt<<|);
  63.  
  64. pushup(rt);
  65. }
  66.  
  67. void update( LL lhs, LL rhs, LL C,LL l, LL r, LL rt )
  68. {
  69. if ( lhs <= l && rhs >= r )
  70. {
  71. sum[rt] += (LL)C*(r-l+);
  72. Add[rt] += C;
  73. return;
  74. }
  75.  
  76. LL mid = (l+r)>>;
  77. pushdown( rt, mid-l+, r-mid );
  78.  
  79. if ( lhs <= mid ) update( lhs, rhs, C, l, mid, rt<< );
  80. if ( rhs > mid ) update( lhs, rhs, C, mid+, r, rt<<| );
  81. pushup(rt);
  82. }
  83.  
  84. LL query( int lhs, LL rhs, LL l, LL r, LL rt )
  85. {
  86. if ( lhs <= l && rhs >= r )
  87. return sum[rt];
  88.  
  89. LL mid = (l+r)>>;
  90. pushdown( rt, mid-l+, r-mid );
  91.  
  92. LL ans = ;
  93. if ( lhs <= mid ) ans += query( lhs, rhs, l, mid, rt<< );
  94. if ( rhs > mid ) ans += query( lhs, rhs, mid+, r, rt<<| );
  95.  
  96. return ans;
  97. }
  98.  
  99. int main()
  100. {
  101. int N, Q;
  102. char str[];
  103. cin >> N >> Q;
  104.  
  105. for ( int i = ; i <= N; i++ )
  106. scanf("%lld", &num[i]);
  107.  
  108. build( , N, );
  109.  
  110. LL lhs, rhs, x;
  111. while (Q--)
  112. {
  113. scanf( "%s", str );
  114. if ( str[] == 'C' )
  115. { scanf("%lld %lld %lld", &lhs, &rhs, &x); update(lhs, rhs, x, , N, ); }
  116. else
  117. { scanf("%lld %lld", &lhs, &rhs); printf("%lld\n", query(lhs, rhs, , N, )); }
  118. }
  119.  
  120. return ;
  121. }

  4.POJ 2528

  区间染色+离散化,好题建议多做一遍,有一个坑点在于离散化后范围,你以为一共1e4对数据,所以最多2e4个点,实际上我们离散的时候如果两个区间之间相隔大于1,离散的时候是会加一个数字的,所以极限情况每对区间之间都加了一个点,所以得有3e4

个点,所以线段树数组至少得开4*3e4 = 12e4才行

  1. #include <iostream>
  2. #include <string.h>
  3. #include <cstdio>
  4. #include <queue>
  5. #include <map>
  6. #include <vector>
  7. #include <string>
  8. #include <cstring>
  9. #include <algorithm>
  10. #include <math.h>
  11.  
  12. #define SIGMA_SIZE 26
  13. #pragma warning ( disable : 4996 )
  14.  
  15. using namespace std;
  16. typedef long long LL;
  17.  
  18. inline LL LMax(LL a,LL b) { return a>b?a:b; }
  19. inline LL LMin(LL a,LL b) { return a>b?b:a; }
  20. inline int Max(int a,int b) { return a>b?a:b; }
  21. inline int Min(int a,int b) { return a>b?b:a; }
  22. inline int gcd( int a, int b ) { return b==?a:gcd(b,a%b); }
  23. inline int lcm( int a, int b ) { return a/gcd(a,b)*b; } //a*b = gcd*lcm
  24. const long long INF = 0x3f3f3f3f3f3f3f3f;
  25. const int inf = 0x3f3f3f3f;
  26. const int mod = ;
  27. const int maxk = 1e4+;
  28. const int maxn = 1e6+;
  29.  
  30. int N, ans;
  31. int num[maxk<<], lisan[maxk<<];
  32. int tree[maxk<<];
  33. int li[maxk], ri[maxk];
  34. bool has[maxk];
  35. //因为是染色问题所以没有上推
  36. void pushdown( int rt )
  37. {
  38. tree[rt<<] = tree[rt<<|] = tree[rt];
  39. tree[rt] = -;
  40. }
  41.  
  42. void update( int L, int R, int C, int l, int r, int rt )
  43. {
  44. //因为范围已经覆盖了子树,所以直接修改标记也没关系
  45. if ( L <= l && R >= r )
  46. {
  47. tree[rt] = C;
  48. return;
  49. }
  50.  
  51. //如果有标记,必须先下推标记才能修改tree
  52. if ( tree[rt] != - ) pushdown(rt);
  53. int mid = (l+r)>>;
  54. if ( L <= mid ) update( L, R, C, l, mid, rt<< );
  55. if ( R > mid ) update( L, R, C, mid+, r, rt<<| );
  56. //不用上推
  57. }
  58.  
  59. int binary( int x, int l, int r )
  60. {
  61. while ( l < r )
  62. {
  63. int mid = (l+r)>>;
  64.  
  65. if ( lisan[mid] == x )
  66. return mid;
  67. if ( lisan[mid] > x )
  68. r = mid - ;
  69. else
  70. l = mid + ;
  71. }
  72. return l;
  73. }
  74.  
  75. void query( int l, int r, int rt )
  76. {
  77. if ( tree[rt] != - )
  78. {
  79. if ( !has[tree[rt]] )
  80. {
  81. ans++;
  82. has[tree[rt]] = true;
  83. }
  84. return;
  85. }
  86.  
  87. //如果叶子节点有海报则在上一步肯定已经被计算过了,所以直接返回就行
  88. if ( l == r ) return;
  89.  
  90. int mid = (l+r)>>;
  91. query( l, mid, rt<< );
  92. query( mid+, r, rt<<| );
  93. }
  94.  
  95. int main()
  96. {
  97. int N;
  98. int T; cin >> T;
  99. while (T--)
  100. {
  101. scanf( "%d", &N );
  102. memset( tree, -, sizeof(tree) );
  103.  
  104. int cnt = ;
  105. for ( int i = ; i <= N; i++ )
  106. {
  107. scanf( "%d %d", &li[i], &ri[i] );
  108. num[cnt++] = li[i];
  109. num[cnt++] = ri[i];
  110. }
  111.  
  112. sort( num+, num+cnt );
  113. //int m = unique( num+1, num+cnt ) - num; //可以用这个去重
  114.  
  115. int tmpcnt = cnt;
  116. for ( int i = ; i < cnt; i++ )
  117. {
  118. if (num[i] - num[i - ] > )
  119. num[tmpcnt++] = num[i - ]++;
  120. }
  121.  
  122. sort( num+, num+tmpcnt );
  123.  
  124. cnt = ;
  125. lisan[] = num[];
  126. for ( int i = ; i < tmpcnt; i++ )
  127. if ( num[i] != num[i-] )
  128. lisan[cnt++] = num[i];
  129. cnt--;
  130.  
  131. for ( int i = ; i <= N; i++ )
  132. {
  133. int lhs = binary( li[i], , cnt );
  134. int rhs = binary( ri[i], , cnt );
  135.  
  136. update( lhs, rhs, i, , cnt, );
  137. }
  138.  
  139. ans = ;
  140. memset( has, , sizeof(has) );
  141. query( , cnt, );
  142.  
  143. printf( "%d\n", ans );
  144. }
  145. return ;
  146. }

  5. HDU 1698

  区间修改,和上一题差不多,不过我觉得顺序出反了...明显这题简单嘛

  1. #include <iostream>
  2. #include <string.h>
  3. #include <cstdio>
  4. #include <queue>
  5. #include <map>
  6. #include <vector>
  7. #include <string>
  8. #include <cstring>
  9. #include <algorithm>
  10. #include <math.h>
  11.  
  12. #define SIGMA_SIZE 26
  13. #pragma warning ( disable : 4996 )
  14.  
  15. using namespace std;
  16. typedef long long LL;
  17.  
  18. inline LL LMax(LL a,LL b) { return a>b?a:b; }
  19. inline LL LMin(LL a,LL b) { return a>b?b:a; }
  20. inline int Max(int a,int b) { return a>b?a:b; }
  21. inline int Min(int a,int b) { return a>b?b:a; }
  22. inline int gcd( int a, int b ) { return b==?a:gcd(b,a%b); }
  23. inline int lcm( int a, int b ) { return a/gcd(a,b)*b; } //a*b = gcd*lcm
  24. const long long INF = 0x3f3f3f3f3f3f3f3f;
  25. const int inf = 0x3f3f3f3f;
  26. const int mod = ;
  27. const int maxk = 1e5+;
  28. const int maxn = 1e5+;
  29.  
  30. int ans;
  31. int tree[maxn<<];
  32.  
  33. //void pushup( int rt ) { sum[rt] = sum[rt<<1] + sum[rt<<1|1]; }
  34.  
  35. void pushdown( int rt )
  36. {
  37. tree[rt<<] = tree[rt<<|] = tree[rt];
  38. tree[rt] = -;
  39. }
  40.  
  41. void update( int L, int R, int C, int l, int r, int rt )
  42. {
  43. if ( L <= l && R >= r )
  44. {
  45. tree[rt] = C;
  46. return;
  47. }
  48.  
  49. if ( tree[rt] != - ) pushdown(rt);
  50.  
  51. int mid = (l+r)>>;
  52. if ( L <= mid ) update( L, R, C, l, mid, rt<< );
  53. if ( R > mid ) update( L, R, C, mid+, r, rt<<| );
  54. //不用上推
  55. }
  56.  
  57. void query( int l, int r, int rt )
  58. {
  59. //特判叶子节点
  60. if ( l == r )
  61. {
  62. if (tree[rt] == -) ans++;
  63. else
  64. ans += tree[rt];
  65. return;
  66. }
  67.  
  68. if ( tree[rt]!=- )
  69. {
  70. ans += tree[rt]*(r-l+);
  71. return;
  72. }
  73.  
  74. int mid = (l+r)>>;
  75. query(l, mid, rt<<);
  76. query(mid+, r, rt<<|);
  77. }
  78.  
  79. int main()
  80. {
  81. int N, Q;
  82. int T, cnt = ; cin >> T;
  83.  
  84. while (T--)
  85. {
  86. ans = ;
  87. memset( tree, -, sizeof(tree) );
  88.  
  89. scanf( "%d", &N ); scanf( "%d", &Q );
  90.  
  91. int lhs, rhs, z;
  92. for ( int i = ; i <= Q; i++ )
  93. {
  94. scanf( "%d %d %d", &lhs, &rhs, &z );
  95. update( lhs, rhs, z, , N, );
  96. }
  97.  
  98. query( , N, );
  99. printf( "Case %d: The total value of the hook is %d.\n", cnt++, ans );
  100. }
  101. return ;
  102. }

  6. POJ 3264

  水题,求区间最大减最小

  1. #include <iostream>
  2. #include <string.h>
  3. #include <cstdio>
  4. #include <queue>
  5. #include <map>
  6. #include <vector>
  7. #include <string>
  8. #include <cstring>
  9. #include <algorithm>
  10. #include <math.h>
  11.  
  12. #define SIGMA_SIZE 26
  13. #define lson rt<<1
  14. #define rson rt<<1|1
  15. #pragma warning ( disable : 4996 )
  16.  
  17. using namespace std;
  18. typedef long long LL;
  19. inline LL LMax(LL a,LL b) { return a>b?a:b; }
  20. inline LL LMin(LL a,LL b) { return a>b?b:a; }
  21. inline int Max(int a,int b) { return a>b?a:b; }
  22. inline int Min(int a,int b) { return a>b?b:a; }
  23. inline int gcd( int a, int b ) { return b==?a:gcd(b,a%b); }
  24. inline int lcm( int a, int b ) { return a/gcd(a,b)*b; } //a*b = gcd*lcm
  25. const LL INF = 0x3f3f3f3f3f3f3f3f;
  26. const LL mod = ;
  27. const int inf = 0x3f3f3f3f;
  28. const int maxk = 1e5+;
  29. const int maxn = 2e5+;
  30.  
  31. int hei[maxn];
  32. int mmin[maxn<<], mmax[maxn<<];
  33.  
  34. void pushup( int rt )
  35. {
  36. mmin[rt] = Min( mmin[rt<<], mmin[rt<<|] );
  37. mmax[rt] = Max( mmax[rt<<], mmax[rt<<|] );
  38. }
  39.  
  40. void build( int l, int r, int rt )
  41. {
  42. if ( l == r )
  43. {
  44. mmin[rt] = hei[l];
  45. mmax[rt] = hei[l];
  46. return;
  47. }
  48.  
  49. int mid = (l+r)>>;
  50. build( l, mid, rt<< );
  51. build( mid+, r, rt<<| );
  52. pushup(rt);
  53. }
  54.  
  55. int findmin( int L, int R, int l, int r, int rt )
  56. {
  57. if ( L <= l && R >= r )
  58. return mmin[rt];
  59.  
  60. int mid = (l+r)>>;
  61. int mi = inf;
  62.  
  63. if ( L <= mid ) mi = Min( mi, findmin(L,R,l,mid,rt<<) );
  64. if ( R > mid ) mi = Min( mi, findmin(L,R,mid+,r,rt<<|) );
  65. return mi;
  66. }
  67.  
  68. int findmax( int L, int R, int l, int r, int rt )
  69. {
  70. if ( L <= l && R >= r )
  71. return mmax[rt];
  72.  
  73. int mid = (l+r)>>;
  74. int ma = -;
  75.  
  76. if ( L <= mid ) ma = Max( ma, findmax(L,R,l,mid,rt<<) );
  77. if ( R > mid ) ma = Max( ma, findmax(L,R,mid+,r,rt<<|) );
  78. return ma;
  79. }
  80.  
  81. int main()
  82. {
  83. int N, Q;
  84. cin >> N >> Q;
  85.  
  86. for ( int i = ; i <= N; i++ )
  87. scanf( "%d", &hei[i] );
  88.  
  89. build( , N, );
  90.  
  91. int l, r;
  92. while (Q--)
  93. {
  94. scanf( "%d%d", &l, &r );
  95. printf("%d\n", findmax(l,r,,N,) - findmin(l,r,,N,) );
  96. }
  97.  
  98. return ;
  99. }

  7.HDU 1540

  区间合并

  求某个点左右两边的最长连续长度

  恶心的题目,明明是多组数据写的却只有一组,各种WA的我又去看了kuangbin的代码结果又被误导了,,,看的我一愣一愣的最后发现还是自己写的好使...太坑了

  1. #include <iostream>
  2. #include <string.h>
  3. #include <cstdio>
  4. #include <queue>
  5. #include <map>
  6. #include <vector>
  7. #include <string>
  8. #include <cstring>
  9. #include <algorithm>
  10. #include <math.h>
  11.  
  12. #define SIGMA_SIZE 26
  13. #define lson rt<<1
  14. #define rson rt<<1|1
  15. #pragma warning ( disable : 4996 )
  16.  
  17. using namespace std;
  18. typedef long long LL;
  19. inline LL LMax(LL a,LL b) { return a>b?a:b; }
  20. inline LL LMin(LL a,LL b) { return a>b?b:a; }
  21. inline int Max(int a,int b) { return a>b?a:b; }
  22. inline int Min(int a,int b) { return a>b?b:a; }
  23. inline int gcd( int a, int b ) { return b==?a:gcd(b,a%b); }
  24. inline int lcm( int a, int b ) { return a/gcd(a,b)*b; } //a*b = gcd*lcm
  25. const LL INF = 0x3f3f3f3f3f3f3f3f;
  26. const LL mod = ;
  27. const int inf = 0x3f3f3f3f;
  28. const int maxk = 1e5+;
  29. const int maxn = 5e4+;
  30.  
  31. //rl代表从右到左最大连续村庄数,ll表示从左到右最大,ml表示该区间最大
  32. struct qujian {
  33. int l; //区间长度
  34. int ll, rl, ml;
  35. }qj[maxn<<];
  36. int sta[maxn], top;
  37. bool dead[maxn];
  38.  
  39. void pushup( int rt )
  40. {
  41. int m = qj[lson].rl+qj[rson].ll;
  42.  
  43. if (qj[lson].ml==qj[lson].l) qj[rt].ll = qj[lson].ll+qj[rson].ll;
  44. else qj[rt].ll = qj[lson].ll;
  45.  
  46. if (qj[rson].ml==qj[rson].l) qj[rt].rl = qj[lson].rl+qj[rson].rl;
  47. else qj[rt].rl = qj[rson].rl;
  48.  
  49. qj[rt].ml = Max( qj[rson].ml, qj[lson].ml );
  50. qj[rt].ml = Max( m, qj[rt].ml );
  51. }
  52.  
  53. void build( int l, int r, int rt )
  54. {
  55. if ( l == r )
  56. {
  57. qj[rt].l = ;
  58. qj[rt].ll = ;
  59. qj[rt].ml = ;
  60. qj[rt].rl = ;
  61. return;
  62. }
  63.  
  64. int mid = (l+r)>>;
  65. build( l, mid, rt<< );
  66. build( mid+, r, rt<<| );
  67. qj[rt].l = qj[lson].l + qj[rson].l;
  68. pushup(rt);
  69. }
  70.  
  71. //删除是false, 恢复是true
  72. void update( int L, int l, int r, int rt, bool flag )
  73. {
  74. if ( l == r )
  75. {
  76. qj[rt].ll = qj[rt].rl = qj[rt].ml = flag?:;
  77. return;
  78. }
  79.  
  80. int mid = (l+r)>>;
  81. if ( L <= mid ) update( L, l, mid, lson, flag );
  82. else update( L, mid+, r, rson, flag );
  83. pushup(rt);
  84. }
  85.  
  86. int query( int L, int l, int r, int rt )
  87. {
  88. if ( l==r || qj[rt].ml==qj[rt].l || qj[rt].ml== )
  89. return qj[rt].ml;
  90.  
  91. int mid = (l+r)>>;
  92. //如果在左子树,则lson的rl如果把查询点包括了,则要加上rson
  93. if ( L <= mid )
  94. {
  95. if ( qj[lson].rl- >= mid-L )
  96. return qj[lson].rl + qj[rson].ll;
  97. return query( L, l, mid, lson );
  98. }
  99. else //同上
  100. {
  101. if ( qj[rson].ll- >= L-mid- )
  102. return qj[rson].ll + qj[lson].rl;
  103. return query( L, mid+, r, rson );
  104. }
  105. }
  106.  
  107. int main()
  108. {
  109. int n, m;
  110. while ( ~scanf("%d%d", &n, &m) )
  111. {
  112. top = ;
  113. memset( dead, , sizeof(dead) );
  114. build(,n,);
  115.  
  116. char str[];
  117. int x;
  118. while (m--)
  119. {
  120. scanf( "%s", str );
  121. if ( str[] == 'D' )
  122. {
  123. scanf("%d",&x);
  124. sta[top++] = x;
  125. if (!dead[x])
  126. update( x, , n, , false );
  127. }
  128. else if (str[] == 'R')
  129. {
  130. if ( top )
  131. update(sta[--top], , n, , true);
  132. }
  133. else
  134. {
  135. scanf("%d",&x);
  136. if (dead[x]) printf( "0\n" );
  137. else
  138. printf( "%d\n", query(x,,n,) );
  139. }
  140. }
  141. }
  142. return ;
  143. }

  8.HDU 3974

  dfs建树,这是一道需要比较深入理解线段树操作的题目,WA了很多次是因为dfs的时候写成了start[i] = cnt++,这样如果到了叶子节点,那么start[i] 和end[i]就不等了,,,坑爹的是这种错误样例根本测不出来...

  1. #include <iostream>
  2. #include <string.h>
  3. #include <cstdio>
  4. #include <queue>
  5. #include <map>
  6. #include <vector>
  7. #include <string>
  8. #include <cstring>
  9. #include <algorithm>
  10. #include <math.h>
  11.  
  12. #define SIGMA_SIZE 26
  13. #define lson rt<<1
  14. #define rson rt<<1|1
  15. #pragma warning ( disable : 4996 )
  16.  
  17. using namespace std;
  18. typedef long long LL;
  19. inline LL LMax(LL a,LL b) { return a>b?a:b; }
  20. inline LL LMin(LL a,LL b) { return a>b?b:a; }
  21. inline int Max(int a,int b) { return a>b?a:b; }
  22. inline int Min(int a,int b) { return a>b?b:a; }
  23. inline int gcd( int a, int b ) { return b==?a:gcd(b,a%b); }
  24. inline int lcm( int a, int b ) { return a/gcd(a,b)*b; } //a*b = gcd*lcm
  25. const LL INF = 0x3f3f3f3f3f3f3f3f;
  26. const LL mod = ;
  27. const int inf = 0x3f3f3f3f;
  28. const int maxk = 1e5+;
  29. const int maxn = 5e4+;
  30.  
  31. struct edge {
  32. int to, next;
  33. }e[maxn];
  34.  
  35. int cnt;
  36. int st[maxn], ed[maxn];
  37. int linjie[maxn], tree[maxn<<], change[maxn<<];
  38. bool vis[maxn];
  39.  
  40. void addedge( int u, int v )
  41. { e[cnt].to = v; e[cnt].next = linjie[u]; linjie[u] = cnt++; }
  42.  
  43. void init()
  44. {
  45. cnt = ;
  46. memset( linjie, -, sizeof(linjie) );
  47. memset( vis, , sizeof(vis) );
  48. memset( tree, -, sizeof(tree) );
  49. memset( change, -, sizeof(change) );
  50. }
  51.  
  52. //妙,妙啊!
  53. void dfs( int u )
  54. {
  55. st[u] = ++cnt;
  56. for ( int i = linjie[u]; i+; i = e[i].next )
  57. dfs(e[i].to);
  58. ed[u] = cnt;
  59. }
  60.  
  61. void pushdown( int rt )
  62. {
  63. if ( change[rt] != - )
  64. {
  65. int tmp = change[rt];
  66. change[lson] = change[rson] = tmp;
  67. tree[lson] = tree[rson] = tmp;
  68.  
  69. change[rt] = -;
  70. }
  71. }
  72.  
  73. void update( int L, int R, int C, int l, int r, int rt )
  74. {
  75. if ( L <= l && R >= r )
  76. {
  77. tree[rt] = C;
  78. change[rt] = C;
  79. return;
  80. }
  81.  
  82. int mid = (l+r)>>;
  83. pushdown(rt);
  84.  
  85. if ( L <= mid ) update( L, R, C, l, mid, lson );
  86. if ( R > mid ) update( L, R, C, mid+, r, rson );
  87. }
  88.  
  89. int query( int L, int l, int r, int rt )
  90. {
  91. if ( L == l && L == r ) // L <= l && L >= r
  92. return tree[rt];
  93.  
  94. pushdown(rt);
  95. int tmp = -, mid = (l+r)>>;
  96.  
  97. if ( L <= mid ) tmp = query( L, l, mid, lson );
  98. else tmp = query( L, mid+, r, rson );
  99.  
  100. return tmp;
  101. }
  102.  
  103. int main()
  104. {
  105. freopen("F:\\cpp\\vs\\test\\Debug\\test.txt","r",stdin);
  106.  
  107. int T; cin >> T;
  108.  
  109. int N, Q, tot = ;
  110. char str[];
  111.  
  112. while (T--)
  113. {
  114. init();
  115. cin >> N;
  116.  
  117. int x, y;
  118. for ( int i = ; i < N; i++ )
  119. {
  120. scanf( "%d %d", &x, &y );
  121. vis[x] = true;
  122. addedge( y, x ); //从y到x
  123. }
  124.  
  125. cnt = ;
  126. for ( int i = ; i <= N; i++ )
  127. if ( !vis[i] )
  128. { dfs(i); break; }
  129.  
  130. cin >> Q;
  131. printf("Case #%d:\n", tot++);
  132. for ( int i = ; i <= Q; i++ )
  133. {
  134. scanf( "%s", str );
  135. if ( str[] == 'C' )
  136. {
  137. scanf( "%d", &x );
  138. printf( "%d\n", query(st[x], , cnt, ) );
  139. }
  140. else
  141. {
  142. scanf( "%d %d", &x, &y );
  143. update( st[x], ed[x], y, , cnt, );
  144. }
  145. }
  146.  
  147. }
  148. return ;
  149. }

  9 HDU 4614

  给N个瓶子,第一个操作,从第A个瓶子开始往里插x朵插花,如果一个瓶子里面没有花则放一朵花进去,如果有则找下一个直到最后一个瓶子,如果花没用完则舍弃掉,输出放第一朵花和最后一朵花的瓶子号

第二个操作,舍弃l~r区间内瓶子里的花,输出舍弃的花的数目

我是用数组1代表瓶子内无花,0代表有花,则区间和就代表没有花的瓶子个数,然后第一个操作就二分出起始瓶子和结尾瓶子,第二个操作直接区间长度-区间和

PS:十个二分九个错

  1. #include <iostream>
  2. #include <string.h>
  3. #include <cstdio>
  4. #include <queue>
  5. #include <map>
  6. #include <vector>
  7. #include <string>
  8. #include <cstring>
  9. #include <algorithm>
  10. #include <math.h>
  11.  
  12. #define SIGMA_SIZE 26
  13. #define lson rt<<1
  14. #define rson rt<<1|1
  15. #pragma warning ( disable : 4996 )
  16.  
  17. using namespace std;
  18. typedef long long LL;
  19. inline LL LMax(LL a,LL b) { return a>b?a:b; }
  20. inline LL LMin(LL a,LL b) { return a>b?b:a; }
  21. inline int Max(int a,int b) { return a>b?a:b; }
  22. inline int Min(int a,int b) { return a>b?b:a; }
  23. inline int gcd( int a, int b ) { return b==?a:gcd(b,a%b); }
  24. inline int lcm( int a, int b ) { return a/gcd(a,b)*b; } //a*b = gcd*lcm
  25. const LL INF = 0x3f3f3f3f3f3f3f3f;
  26. const LL mod = ;
  27. const int inf = 0x3f3f3f3f;
  28. const int maxk = 1e5+;
  29. const int maxn = 5e4+;
  30.  
  31. int sum[maxn<<];
  32. int chag[maxn<<];
  33.  
  34. void init()
  35. {
  36. memset( sum, , sizeof(sum) );
  37. memset( chag, -, sizeof(chag) );
  38. }
  39.  
  40. void pushup( int rt ) { sum[rt] = sum[lson] + sum[rson]; }
  41.  
  42. void build( int l, int r, int rt )
  43. {
  44. if ( l == r )
  45. {
  46. sum[rt] = ;
  47. return;
  48. }
  49.  
  50. int mid = (l+r)>>;
  51. build( l, mid, lson );
  52. build( mid+, r, rson );
  53. pushup(rt);
  54. }
  55.  
  56. //注意这个下推,只有两种操作,一个全部置0,一个全部置1
  57. //置1代表这个瓶子是空的,置0表示有花
  58. void pushdown( int rt, int ln, int rn )
  59. {
  60. if ( chag[rt] != - )
  61. {
  62. chag[lson] = chag[rson] = chag[rt];
  63. sum[lson] = ln*chag[lson];
  64. sum[rson] = rn*chag[rson];
  65. chag[rt] = -;
  66. }
  67. }
  68.  
  69. void update( int L, int R, int C, int l, int r, int rt )
  70. {
  71. if ( L <= l && R >= r )
  72. {
  73. sum[rt] = (r-l+)*C;
  74. chag[rt] = C;
  75. return;
  76. }
  77.  
  78. int mid = (l+r)>>;
  79. pushdown( rt, mid-l+, r-mid );
  80.  
  81. if ( L <= mid ) update( L, R, C, l, mid, lson );
  82. if ( R > mid ) update( L, R, C, mid+, r, rson );
  83. pushup(rt);
  84. }
  85.  
  86. int query( int L, int R, int l, int r, int rt )
  87. {
  88. if ( L <= l && R >= r )
  89. return sum[rt];
  90.  
  91. int mid = (l+r)>>;
  92. pushdown( rt, mid-l+, r-mid );
  93.  
  94. int ans = ;
  95. if ( L <= mid ) ans += query( L, R, l, mid, lson );
  96. if ( R > mid ) ans += query( L, R, mid+, r, rson );
  97.  
  98. return ans;
  99. }
  100.  
  101. int bin( int st, int ed, int val, bool ok )
  102. {
  103. int mid, tmp;
  104. int lhs = st, rhs = ed;
  105. while ( lhs <= rhs )
  106. {
  107. mid = (lhs+rhs)>>;
  108. tmp = query( st, mid, , ed, );
  109. if ( (ok?(val <= tmp):(val < tmp)) )
  110. rhs = mid-;
  111. else
  112. lhs = mid+;
  113. }
  114. return lhs;
  115. }
  116.  
  117. int main()
  118. {
  119. //freopen("F:\\cpp\\test.txt","r",stdin);
  120.  
  121. int T; cin >> T;
  122.  
  123. int N, Q, k, x, y;
  124. while (T--)
  125. {
  126. init();
  127. cin >> N >> Q;
  128. build(, N, );
  129.  
  130. int lpos, rpos;
  131. while (Q--)
  132. {
  133. scanf("%d%d%d", &k, &x, &y);
  134. if ( k == )
  135. {
  136. //t代表空瓶数量
  137. int t = query(x + , N, , N, );
  138. if ( t == )
  139. {
  140. printf("Can not put any one.\n");
  141. continue;
  142. }
  143. else if ( t < y ) //如果该段空花盆数目不够放
  144. rpos = bin( x+, N, t, true ); //找出第一个空瓶数量等于t的位置
  145. else
  146. rpos = bin( x+, N, y, true ); //如果够放直接二分终止位置
  147.  
  148. //二分起始位置,查找第一个不等于0的数
  149. lpos = bin( x+, N, , false );
  150.  
  151. printf( "%d %d\n", lpos-, rpos- ); //注意对齐位置
  152. update( lpos, rpos, , , N, );
  153. }
  154. else
  155. {
  156. int len = y-x+, tmp = query(x+, y+, , N, );
  157. printf("%d\n", len-tmp);
  158. update( x+, y+, , , N, );
  159. }
  160. }
  161. printf("\n");
  162. }
  163. return ;
  164. }

kuangbin带我飞QAQ 线段树的更多相关文章

  1. kuangbin带我飞QAQ 并查集

    1. POJ 2236 给出N个点,一开始图是空白的,两个操作,一个是增加一个点(给出坐标),一个是查询两个点间是否相通,当两点间的距离小于D或者两点通过其他点间接相连时说这两个点相通.并查集维护,每 ...

  2. kuangbin带我飞QAQ DLX之一脸懵逼

    1. hust 1017 DLX精确覆盖 模板题 勉强写了注释,但还是一脸懵逼,感觉插入方式明显有问题但又不知道哪里不对而且好像能得出正确结果真是奇了怪了 #include <iostream& ...

  3. kuangbin带我飞QAQ 最短路

    1. poj 1502 Mathches Game 裸最短路 #include <iostream> #include <string.h> #include <cstd ...

  4. [kuangbin带你飞]专题七 线段树

            ID Origin Title 228 / 440 Problem A HDU 1166 敌兵布阵   207 / 438 Problem B HDU 1754 I Hate It   ...

  5. 【算法系列学习】线段树vs树状数组 单点修改,区间查询 [kuangbin带你飞]专题七 线段树 A - 敌兵布阵

    https://vjudge.net/contest/66989#problem/A 单点修改,区间查询 方法一:线段树 http://www.cnblogs.com/kuangbin/archive ...

  6. 培训补坑(day7:线段树的区间修改与运用)(day6是测试,测试题解以后补坑QAQ)

    补坑咯~ 今天围绕的是一个神奇的数据结构:线段树.(感觉叫做区间树也挺科学的.) 线段树,顾名思义就是用来查找一段区间内的最大值,最小值,区间和等等元素. 那么这个线段树有什么优势呢? 比如我们要多次 ...

  7. Kuangbin 带你飞-线段树专题 题解

    HDU 1166 敌兵布阵 单调更新区间查询和 #include <map> #include <set> #include <list> #include < ...

  8. kuangbin专题七 HDU1540 Tunnel Warfare (前缀后缀线段树)

    During the War of Resistance Against Japan, tunnel warfare was carried out extensively in the vast a ...

  9. kuangbin专题七 POJ3264 Balanced Lineup (线段树最大最小)

    For the daily milking, Farmer John's N cows (1 ≤ N ≤ 50,000) always line up in the same order. One d ...

随机推荐

  1. 大半宿,封装了一个MP3播放器的类,写了个简陋的播放器

    用 winmm.lib 写的 封装不是很好,而且没有优化,效率可能有问题,但是现在几乎没有什么大问题 我用我封装的类,写了一个小播放器,界面上的所有功能都实现了,包括双击列表中的文件名,直接播放文件 ...

  2. java笔试之求最大连续bit数

    功能: 求一个byte数字对应的二进制数字中1的最大连续数,例如3的二进制为00000011,最大连续2个1    输入: 一个byte型的数字    输出: 无     返回: 对应的二进制数字中1 ...

  3. 【核心核心】10.Spring事务管理【TX】XML+注解方式

    转账案例环境搭建 1.引入JAR包 IOC的6个包 AOP的4个包 C3P0的1个包 MySQL的1个驱动包 JDBC的2个目标包 整合JUnit测试1个包 2.引入配置文件 log4j.proper ...

  4. Android 开发 MediaRecorder视频录制入门

    前言 MediaRecorder是Android SDK提供用于录制音视频,关于音频的录制在我另一篇博客里已经介绍.传送门: https://www.cnblogs.com/guanxinjing/p ...

  5. Oracle Database 18c数据库安装步骤

    1.Oracle官网登录下载https://login.oracle.com/mysso/signon.jsp WINDOWS.X64_180000_db_home.zip 2.D盘根目录新建文件夹: ...

  6. 关于Synthesis

    1,当追求面积最小时 会以牺牲Fmax为代价,可以使用一下setting: fit_pack_for_density=on fit_report_lab_usage_stats=on 可在 .qsf ...

  7. 12DUILib经典教程(实例)

    Duilib经典实例教程:1基本框架:一个简单的Duilib程序一般是下面这个样子的:://Duilib使用设置部分:#pragmaonce:#defineWIN32_LEAN_AND_ME:#def ...

  8. 【BZOJ 1257】[CQOI2007]余数之和sum

    一道LLJ说他吃*的题. 我实在是太愚蠢了. 传送门

  9. golang的表格驱动测试

    一.leetcode的算法题 package main import ( "fmt" "strings" ) func lengthOfNonRepeating ...

  10. php中Sessions

    PHP Sessions  Session 中文译名叫做“会话”,其本来的含义是指有始有终的一系列动作/消息. PHP session 变量用于存储关于用户会话(session)的信息,或者更改用户会 ...