• 线段树模板


      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("
    
    ");
    105     }
    106     return 0;
    107 }
    View Code
  • 相关阅读:
    刘翔那点事
    网站建站模板
    搞笑!from 饮水思源
    我de虚拟经济学系列第一章 经济危机拼命建桥
    IT民工系列——c#操作Microsoft IE,实现自动登录吧!
    商业智能的发展及其应用
    我de虚拟经济学系列第三章 常见的致富之路
    IT民工系列——c#操作EditGrid,自己做一个在线Excel数据库吧!
    Asp.net下的Singleton模式
    asp.net 控件功能小结
  • 原文地址:https://www.cnblogs.com/macinchang/p/4506956.html
Copyright © 2020-2023  润新知