• 自认为自己写的最美的线段树模板


      1 #include<bits/stdc++.h>
      2 using namespace std;
      3 const int MAX = 100005;
      4 typedef struct Node{
      5     long long val, tag;
      6     int l, r;
      7     Node *rson, *lson;
      8     int len() {return r - l;}
      9     int mid() {return (l + r) >> 1;}
     10     Node(): rson(NULL), lson(NULL), l(0), r(0), val(0), tag(0){}
     11 }node, *pointer;
     12 
     13 class Segment_Tree
     14 {
     15     public:
     16         pointer rt = new Node();
     17         long long a[MAX];
     18     
     19     void PushUp(pointer rt)
     20         {
     21             rt->val = rt->rson->val + rt->lson->val;
     22         }
     23     
     24     void PushDown(pointer rt)
     25         {
     26             if(rt->tag)
     27                 {
     28                     rt->lson->tag += rt->tag;
     29                     rt->rson->tag += rt->tag;
     30                     rt->lson->val += rt->tag * rt->lson->len();
     31                     rt->rson->val += rt->tag * rt->rson->len();
     32                     rt->tag = 0;
     33                 }
     34         }
     35     
     36     void Build(pointer rt,int l, int r)
     37         {
     38             rt->l = l;
     39             rt->r = r;
     40             if(l + 1 == r)
     41                 {
     42                     rt->val = a[l];
     43                     //rt->pos = l;
     44                     rt->rson = NULL;
     45                     rt->lson = NULL;
     46                     return ;
     47                 }
     48             rt->rson = new Node();
     49             rt->lson = new Node();
     50             Build(rt->lson, l, rt->mid());
     51             Build(rt->rson, rt->mid(), r);
     52             PushUp(rt); 
     53         }
     54         
     55     void UpData(pointer rt, int L, int R, int k)
     56         {
     57             if(L <= rt->l && rt->r <= R)
     58                 {
     59                     rt->val += k * rt->len();
     60                     rt->tag += k;
     61                 }
     62             else
     63                 {
     64                     if(rt->tag != 0)    PushDown(rt);
     65                     if(L < rt->mid())    UpData(rt->lson, L, R, k);
     66                     if(R > rt->mid())     UpData(rt->rson, L, R, k);
     67                     PushUp(rt);
     68                 }
     69         }
     70         
     71         long long Query(pointer rt, int L, int R)
     72             {
     73                 if(L <= rt->l && rt->r <= R)    return rt->val;
     74                 else
     75                     {
     76                         if(rt->tag != 0)    PushDown(rt);
     77                         long long ret = 0;
     78                         if(L < rt->mid())    ret += Query(rt->lson, L, R);
     79                         if(R > rt->mid()) ret += Query(rt->rson, L, R);
     80                         return ret;
     81                     }
     82             }
     83 };
     84 
     85 int main()
     86 {
     87     int n, m;
     88     int x, y, z, ord;
     89     Segment_Tree tree;
     90     scanf("%d%d", &n, &m);
     91     for(int i = 1; i <= n; i ++) scanf("%lld", &tree.a[i]);
     92     tree.Build(tree.rt, 1, n + 1);
     93     for(int i = 1; i <= m; i ++)
     94         {
     95             scanf("%d", &ord);
     96             if(ord == 1)
     97                 {
     98                     scanf("%d%d%d", &x, &y, &z);
     99                     tree.UpData(tree.rt, x, y + 1, z);
    100                 }
    101             if(ord ==2)
    102                 {
    103                     scanf("%d%d", &x, &y);
    104                     printf("%lld
    ", tree.Query(tree.rt, x, y + 1));
    105                 }
    106         }
    107     return 0;
    108 }
  • 相关阅读:
    @NotNull @NotBlank @NotEmpty
    springboot @valid与@validated的参数校验使用总结
    一张表多个外键指向同一主键
    关于List的remove()方法
    双数据源切换问题
    前端通过jqplot绘制折线图
    关于js与jquery中的文档加载
    Mybatis中typeAliases的使用
    项目中常见数据库知识
    html中实现倒计时功能(setInterval,clearInterval)
  • 原文地址:https://www.cnblogs.com/2020pengxiyue/p/8681634.html
Copyright © 2020-2023  润新知