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

线段树模板

时间:2015-05-15 22:49:10      阅读:128      评论:0      收藏:0      [点我收藏+]

标签:

技术分享
  1 #include<cstdio>
  2 #include<cstring>
  3 const int M = 30001;
  4 long long  sum[M << 2], a[M << 2];
  5 bool set[M << 2], flag;
  6 
  7 void init(void)
  8 {
  9     flag = 0;
 10     memset(sum, 0, sizeof(sum));
 11     memset(set, 0, sizeof(set));
 12     memset(a, 0, sizeof(a));
 13     
 14 }
 15 void add(int t, int x, int y)
 16 {
 17     if (set[t])
 18     {
 19         int m = (x + y) >> 1;
 20         set[t << 1] = set[t << 1 | 1] = set[t];
 21         a[t << 1] = a[t << 1 | 1] = a[t];
 22         sum[t << 1] = (m - x + 1) * a[t];
 23         sum[t << 1 | 1] = (y - m) * a[t];
 24         set[t] = 0;
 25     }
 26 }
 27 long long  query(int p, int q, int x, int y, int t)
 28 {
 29     if (p <= x&&y <= q)
 30         return sum[t];
 31     add(t, x, y);
 32     int m = (x + y) >> 1;
 33     long long  res = 0;
 34     if (p <= m) res += query(p, q, x, m, t << 1);
 35     if (q > m) res += query(p, q, m + 1, y, t << 1 | 1);
 36     return res;
 37 }
 38 void update(long long  op, int p, int q, int x, int y, int t)
 39 {
 40     if (p <= x&&y <= q)
 41     {
 42         set[t] = 1;
 43         a[t] = op;
 44         sum[t] = (y - x + 1)*op;
 45         return;
 46     }
 47     add(t, x, y);
 48     int m = (x + y) >> 1;
 49     if (p <= m) update(op, p, q, x, m, t << 1);
 50     if (q > m) update(op, p, q, m + 1, y, t << 1 | 1);
 51     sum[t] = sum[t << 1] + sum[t << 1 | 1];
 52 }
 53 void work(int x, int y, int t)
 54 {
 55     if (x == y)
 56     {
 57 
 58         if (flag)printf(" ");
 59         else flag = 1;
 60         printf("%lld", sum[t]);
 61         return;
 62     }
 63     add(t, x, y);
 64     int m = (x + y) >> 1;
 65     work(x, m, t << 1);
 66     work(m + 1, y, t << 1 | 1);
 67 }
 68 long long  calc(long long  t, int x, int y, bool up)
 69 {
 70     if (t >= 0)
 71     {
 72         if (up)return (t + y - x) / (y - x + 1);
 73         return t / (y - x + 1);
 74     }
 75     else
 76     {
 77         t = -t;
 78         if (!up)return -(t + y - x) / (y - x + 1);
 79         return -t / (y - x + 1);
 80     }
 81 }
 82 int main()
 83 {
 84     int n, m, x, y;
 85     long long  t, st, ori;
 86     while (~scanf("%d%d", &n, &m))
 87     {
 88         init();
 89         for (int i = 0; i < n; i++)
 90         {
 91             scanf("%lld", &t);
 92             update(t, i, i, 0, n - 1, 1);
 93         }
 94         ori = sum[1];
 95         while (m--)
 96         {
 97             scanf("%d%d", &x, &y);
 98             x--; y--;
 99             st = query(x, y, 0, n - 1, 1);
100             if (ori >= sum[1])update(calc(st, x, y, 1), x, y, 0, n - 1, 1);
101             else update(calc(st, x, y, 0), x, y, 0, n - 1, 1);
102         }
103         work(0, n - 1, 1);
104         printf("\n\n");
105     }
106     return 0;
107 }
View Code

 

线段树模板

标签:

原文地址:http://www.cnblogs.com/macinchang/p/4506956.html

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