线段树

  1 struct pointtype
  2 {
  3     long long sum;
  4     long long lazy;
  5 }tree[500001];
  6 void push_up(int root)
  7 {
  8     tree[root].sum=tree[root*2].sum+tree[root*2+1].sum;
  9     return;
 10 }
 11 void push_down(int root,int l,int r)
 12 {
 13     int len=r-l+1;
 14     tree[root*2].sum+=tree[root].lazy*(len-len/2);
 15     tree[root*2].lazy+=tree[root].lazy;
 16     tree[root*2+1].sum+=tree[root].lazy*(len/2);
 17     tree[root*2+1].lazy+=tree[root].lazy;
 18     tree[root].lazy=0;
 19     return;
 20 }
 21 void build(int root,int l,int r)
 22 {
 23     if(l==r)
 24     {
 25         scanf("%lld",&tree[root].sum);
 26         return;
 27     }
 28     tree[root].lazy=0;
 29     build(root*2,l,(l+r)/2);
 30     build(root*2+1,(l+r)/2+1,r);
 31     push_up(root);
 32     return;
 33 }
 34 void update_point(int root,int l,int r,int pos,int delta)
 35 {
 36     if(l==r)
 37     {
 38         tree[root].sum+=delta;
 39         return;
 40     }
 41     if(pos<=(l+r)/2)
 42     {
 43         update_point(root*2,l,(l+r)/2,pos,delta);
 44     }
 45     else
 46     {
 47         update_point(root*2+1,(l+r)/2+1,r,pos,delta);
 48     }
 49     push_up(root);
 50     return;
 51 }
 52 int query_point(int root,int l,int r,int pos)
 53 {
 54     if(l==r)
 55     {
 56         return tree[root].sum;
 57     }
 58     int ans=0;
 59     if(pos<=(l+r)/2)
 60     {
 61         ans+=query_point(root*2,l,(l+r)/2,pos);
 62     }
 63     if(pos>(l+r)/2)
 64     {
 65         ans+=query_point(root*2+1,(l+r)/2+1,r,pos);
 66     }
 67     return ans;
 68 }
 69 void update_seq(int root,int l,int r,int L,int R,int delta)
 70 {
 71     if(L<=l&&r<=R)
 72     {
 73         tree[root].sum+=delta*(r-l+1);
 74         tree[root].lazy+=delta;
 75         return;
 76     }
 77     if(tree[root].lazy)
 78     {
 79         push_down(root,l,r);
 80     }
 81     if(L<=(l+r)/2)
 82     {
 83         update_seq(root*2,l,(l+r)/2,L,R,delta);
 84     }
 85     if((l+r)/2<R)
 86     {
 87         update_seq(root*2+1,(l+r)/2+1,r,L,R,delta);
 88     }
 89     push_up(root);
 90     return;
 91 }
 92 long long query_seq(int root,int l,int r,int L,int R)
 93 {
 94     if(L<=l&&r<=R)
 95     {
 96         return tree[root].sum;
 97     }
 98     long long ans=0;
 99     push_down(root,l,r);
100     if(L<=(l+r)/2)
101     {
102         ans+=query_seq(root*2,l,(l+r)/2,L,R);
103     }
104     if((l+r)/2<R)
105     {
106         ans+=query_seq(root*2+1,(l+r)/2+1,r,L,R);
107     }
108     push_up(root);
109     return ans;
110 }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值