zoukankan      html  css  js  c++  java
  • 牛客网 牛客小白月赛5 I.区间 (interval)-线段树 or 差分数组?

    牛客小白月赛5

    I.区间 (interval)


    休闲的时候写的,但是写的心情有点挫,都是完全版线段树,我的一个队友直接就水过去了,为啥我的就超内存呢???

    试了一晚上,找出来了,多初始化了add标记数组或者将add标记数组定义为long long型就会超内存,并不是自己的线段树写的有问题,而是出题人故意想卡线段树,就是不想让人家用线段树过这道题,但是还是有很多人用线段树过了,我最后删了add标记数组的初始化就过了,mdzz。。。

    这道题还要记得开long long,其他的就没了,差分数组的没写,线段树的水过去就过去吧。

     只是水一下博客。

    代码:

     1 #include<iostream>
     2 #include<cstdio>
     3 #include<cstring>
     4 #include<cmath>
     5 #include<cstdlib>
     6 #include<algorithm>
     7 #include<queue>
     8 #include<map>
     9 using namespace std;
    10 typedef long long ll;
    11 const int maxn=1e6+10;
    12 const int inf=0x3f3f3f3f;
    13 #define lson l,m,rt<<1
    14 #define rson m+1,r,rt<<1|1
    15 ll tree[maxn<<2],add[maxn<<2];
    16 
    17 void pushup(int rt)
    18 {
    19     tree[rt]=tree[rt<<1]+tree[rt<<1|1];
    20 }
    21 void pushdown(int rt,int m)
    22 {
    23     if(add[rt]){
    24         add[rt<<1]+=add[rt];
    25         add[rt<<1|1]+=add[rt];
    26         tree[rt<<1]+=(1ll)*(m-(m>>1))*add[rt];
    27         tree[rt<<1|1]+=(1ll)*(m>>1)*add[rt];
    28         add[rt]=0;
    29     }
    30 }
    31 void build(int l,int r,int rt)
    32 {
    33     if(l==r){
    34         scanf("%lld",&tree[rt]);
    35         return ;
    36     }
    37 
    38     int m=(l+r)>>1;
    39     build(lson);
    40     build(rson);
    41     pushup(rt);
    42 }
    43 void update(int L,int R,int c,int l,int r,int rt)
    44 {
    45     if(L<=l&&r<=R){
    46         add[rt]+=c;
    47         tree[rt]+=(1ll)*c*(r-l+1);
    48         return ;
    49     }
    50 
    51     pushdown(rt,r-l+1);
    52     int m=(l+r)>>1;
    53     if(L<=m) update(L,R,c,lson);
    54     if(R> m) update(L,R,c,rson);
    55     pushup(rt);
    56 }
    57 ll query(int L,int R,int l,int r,int rt)
    58 {
    59     if(L<=l&&r<=R){
    60         return tree[rt];
    61     }
    62 
    63     pushdown(rt,r-l+1);
    64     int m=(l+r)>>1;
    65     ll ret=0;
    66     if(L<=m)ret+=query(L,R,lson);
    67     if(R> m)ret+=query(L,R,rson);
    68     return ret;
    69 }
    70 int main()
    71 {
    72     int n,m;
    73     scanf("%d%d",&n,&m);
    74     build(1,n,1);
    75     int op,l,r,v;
    76     for(int i=0;i<m;i++){
    77         scanf("%d%d%d%d",&op,&l,&r,&v);
    78         if(op==1){
    79             update(l,r,-v,1,n,1);
    80         }
    81         else{
    82             update(l,r,v,1,n,1);
    83         }
    84     }
    85     int L,R;
    86     scanf("%d%d",&L,&R);
    87     printf("%lld
    ",query(L,R,1,n,1));
    88     return 0;
    89 }

    溜了。。。

  • 相关阅读:
    how to uninstall devkit
    asp.net中bin目录下的 dll.refresh文件
    查找2个分支的共同父节点
    Three ways to do WCF instance management
    WCF Concurrency (Single, Multiple, and Reentrant) and Throttling
    检查string是否为double
    How to hide TabPage from TabControl
    获取当前系统中的时区
    git svn cygwin_exception
    lodoop打印控制具体解释
  • 原文地址:https://www.cnblogs.com/ZERO-/p/9362320.html
Copyright © 2011-2022 走看看