• poj 3468 A Simple Problem with Integers 降维线段树


    这道题是区间更新线段树模板题

    正常线段树是倍增的

    rt<<1 rt<<1+1

    这个线段树用了getid的方法使线段树降了一维 

    虽然这道题用不上降维 但还是要记录一下

      1 #include<cstdio>
      2 #include<iostream>
      3 #include<algorithm>
      4 #include<cmath>
      5 #include<cstring>
      6 #include<string>
      7 #include<ctime>
      8 #include<map>
      9 #include<set>
     10 #include<vector>
     11 #include<queue>
     12 #include<cstdlib>
     13 #include<cassert>
     14 #include<sstream>
     15 #include<stack>
     16 #include<list>
     17 #include<bitset>
     18 #define cl(a,b) memset(a,b,sizeof(a))
     19 #define debug(x) cerr<<#x<<"=="<<(x)<<endl
     20 using namespace std;
     21 typedef long long ll;
     22 typedef long double ldb;
     23 typedef pair<int,int> pii;
     24 
     25 #define ls getid(l,l+r>>1)
     26 #define rs getid((l+r>>1)+1,r)
     27 #define lson l,m,ls
     28 #define rson m+1,r,rs
     29 
     30 typedef long long ll;
     31 
     32 const int maxn = 1e5+10;
     33 
     34 inline int getid(int x,int y)
     35 {
     36     return x+y|y!=x;
     37 }
     38 
     39 ll rm[maxn<<1],col[maxn<<1];
     40 
     41 void pushup(int l,int r,int rt)
     42 {
     43     rm[rt]=rm[ls]+rm[rs];
     44 }
     45 
     46 void pushdown(int l,int r,int rt,int k)
     47 {
     48     if (col[rt])
     49     {
     50         col[ls] += col[rt];
     51         col[rs] += col[rt];
     52         rm[ls] += col[rt]*(k-(k>>1));
     53         rm[rs] += col[rt]*(k>>1);
     54         col[rt] = 0;
     55     }
     56 }
     57 
     58 void build(int l,int r,int rt)
     59 {
     60     if(l == r)
     61     {
     62         scanf("%I64d",&rm[rt]);
     63         return ;
     64     }
     65     int m=l+r>>1;
     66     build(lson);
     67     build(rson);
     68     pushup(l,r,rt);
     69 }
     70 
     71 void update(int L,int R,int c,int l,int r,int rt)
     72 {//区间更新
     73     if(L<=l && r<=R)
     74     {
     75         col[rt]+=c;
     76         rm[rt]+=c*(r-l+1);
     77         return ;
     78     }
     79     pushdown(l,r,rt,r-l+1);
     80     int m=l+r>>1;
     81     if( L <= m ) update(L,R,c,lson);
     82     if (R > m) update(L,R,c,rson);
     83     pushup(l,r,rt);
     84 }
     85 
     86 void update(int p,int c,int l,int r,int rt)
     87 {//单点更新
     88     if(l==r)
     89     {
     90         rm[rt]+=c;
     91         return ;
     92     }
     93     int m=l+r>>1;
     94     if( p <= m ) update(p,c,lson);
     95     else update(p,c,rson);
     96     pushup(l,r,rt);
     97 }
     98 
     99 ll query(int L,int R,int l,int r,int rt)
    100 {
    101     if( L<=l && r<=R )
    102     {
    103         return rm[rt];
    104     }
    105     pushdown(l,r,rt, r-l+1);
    106     int m=l+r>>1;
    107     ll ret = 0;
    108     if( L<=m ) ret += query(L,R,lson);
    109     if( m<R ) ret += query(L,R,rson);
    110     return ret;
    111 }
    112 
    113 int main()
    114 {
    115     int n,m;
    116     scanf("%d%d",&n,&m);
    117     char str[2];
    118     build(1,n,1);
    119     while(m--)
    120     {
    121         int l,r,x;
    122         scanf("%s",str);
    123         if(str[0]=='Q')
    124         {
    125             scanf("%d%d",&l,&r);
    126             printf("%I64d
    ",query(l,r,1,n,1));
    127         }
    128         else
    129         {
    130             scanf("%d %d %d", &l, &r, &x);
    131             update(l, r, x, 1, n, 1);
    132         }
    133     }
    134     return 0;
    135 }/*
    136 
    137 5 6
    138 1 2 3 4 5
    139 Q 1 5
    140 U 3 6
    141 Q 3 4
    142 Q 4 5
    143 U 2 9
    144 Q 1 5
    145 
    146 */
  • 相关阅读:
    Xcode 增强开发效率的插件
    svn 常用指令
    初识JFinal
    企业竞争案例分析
    可行性研究5
    可行性研究习题4
    可行性研究课后题
    关于阅读软件工程和计算机科学技术区别的文章来谈谈自己看法
    关于中文编程是解决中国程序员效率的秘密武器的问题思考
    Java ForkJoinPool 用法及原理
  • 原文地址:https://www.cnblogs.com/general10/p/6953347.html
Copyright © 2020-2023  润新知