zoukankan      html  css  js  c++  java
  • HDU

    题目大意:主席树的加减区间更新。

    思路:lazy标记搞一搞,注意不需要push_down,写的时候push_up忘了加上自己的lazy疯狂WA。。。

      1 #include<bits/stdc++.h>
      2 #define fi first
      3 #define se second
      4 #define pb push_back
      5 #define mk make_pair
      6 #define pii pair<int,int>
      7 #define read(x) scanf("%d",&x)
      8 #define sread(x) scanf("%s",x)
      9 #define lread(x) scanf("%lld",&x)
     10 using namespace std;
     11 
     12 typedef long long ll;
     13 const int N=1e5+7;
     14 const int M=1e5+1;
     15 const int inf=0x3f3f3f3f;
     16 const ll INF=0x3f3f3f3f3f3f3f3f;
     17 
     18 int n,m,root[N],T;
     19 struct seg_tree
     20 {
     21     int cnt=0;
     22     struct node
     23     {
     24         int l,r;
     25         ll sum,lazy;
     26     }a[N*25];
     27     void build(int l,int r,int &x)
     28     {
     29         x=++cnt;
     30         if(l==r)
     31         {
     32             lread(a[x].sum);
     33             return;
     34         }
     35         int mid=(l+r)>>1;
     36         build(l,mid,a[x].l);
     37         build(mid+1,r,a[x].r);
     38         a[x].sum=a[a[x].l].sum+a[a[x].r].sum;
     39     }
     40     void updata(int l,int r,int L,int R,int &x,int y,ll v)
     41     {
     42         a[++cnt]=a[y]; x=cnt;
     43         if(l>=L && r<=R)
     44         {
     45             a[x].sum+=(r-l+1)*v;
     46             a[x].lazy+=v;
     47             return;
     48         }
     49         int mid=(l+r)>>1;
     50         if(L<=mid)
     51             updata(l,mid,L,R,a[x].l,a[y].l,v);
     52         if(R>mid)
     53             updata(mid+1,r,L,R,a[x].r,a[y].r,v);
     54         a[x].sum=a[a[x].l].sum+a[a[x].r].sum+a[x].lazy*(r-l+1);
     55     }
     56     ll query(int l,int r,int L,int R,int x,ll add)
     57     {
     58         if(l>=L && r<=R)
     59             return a[x].sum+add*(r-l+1);
     60         int mid=(l+r)>>1;
     61         ll ans=0;
     62         if(L<=mid)
     63             ans+=query(l,mid,L,R,a[x].l,add+a[x].lazy);
     64         if(R>mid)
     65             ans+=query(mid+1,r,L,R,a[x].r,add+a[x].lazy);
     66         return ans;
     67     }
     68 }seg;
     69 void init(){
     70     seg.cnt=0; T=0;
     71 }
     72 int main()
     73 {
     74     int cas=0;
     75     while(read(n)!=EOF)
     76     {
     77         read(m);
     78         init();
     79         seg.build(1,n,root[0]);
     80         while(m--)
     81         {
     82             char s[3];
     83             sread(s);
     84             if(s[0]=='C')
     85             {
     86                 int l,r; ll d; T++;
     87                 read(l); read(r); lread(d);
     88                 seg.updata(1,n,l,r,root[T],root[T-1],d);
     89             }
     90             else if(s[0]=='Q')
     91             {
     92                 int l,r;
     93                 read(l); read(r);
     94                 ll ans=seg.query(1,n,l,r,root[T],0);
     95                 printf("%I64d
    ",ans);
     96             }
     97             else if(s[0]=='H')
     98             {
     99                 int l,r,t;
    100                 read(l); read(r); read(t);
    101                 ll ans=seg.query(1,n,l,r,root[t],0);
    102                 printf("%I64d
    ",ans);
    103             }
    104             else
    105             {
    106                 int t; read(t);
    107                 T=t; seg.cnt=root[T+1]-1;
    108             }
    109         }
    110     }
    111     return 0;
    112 }
    113 /*  
    114 */
  • 相关阅读:
    按钮常用
    MySQL常用Json函数
    MySQL所有函数及操作符
    MySQL常用时间函数
    MySQL常用聚合函数
    Shiro整合Spring
    Shiro集成Web
    Shrio授权验证详解
    Shrio认证详解+自定义Realm
    Shiro入门
  • 原文地址:https://www.cnblogs.com/CJLHY/p/8469322.html
Copyright © 2011-2022 走看看