zoukankan      html  css  js  c++  java
  • codevs 1082 线段树练习3 (线段树)

    题目:

    题目描述 Description

    给你N个数,有两种操作:

    1:给区间[a,b]的所有数增加X

    2:询问区间[a,b]的数的和。

    输入描述 Input Description

    第一行一个正整数n,接下来n行n个整数,

    再接下来一个正整数Q,每行表示操作的个数,

    如果第一个数是1,后接3个正整数,

    表示在区间[a,b]内每个数增加X,如果是2,

    表示操作2询问区间[a,b]的和是多少。

    pascal选手请不要使用readln读入

    输出描述 Output Description

    对于每个询问输出一行一个答案

    样例输入 Sample Input

    3

    1

    2

    3

    2

    1 2 3 2

    2 2 3

    样例输出 Sample Output

    9

    数据范围及提示 Data Size & Hint

    数据范围

    1<=n<=200000

    1<=q<=200000


    思路:
    区间修改 区间修改 线段树模板题 数据范围有所提升 爆了int 要用long long
    代码:
    #include <iostream>
    #include <cstdio>
    #include <cstdlib>
    #include <cmath>
    #include <string>
    #include <cstring>
    #include <algorithm>
    
    using namespace std;
    typedef long long ll;
    typedef unsigned long long ull;
    const int inf=0x3f3f3f3f;
    const int maxn=2e5+10;
    int n,m,x,y,v,op;
    ll ans;
    ll a[maxn];
    
    struct node{
        int l,r,lazy;
        ll w;
    }tree[maxn<<2];
    
    void build(int l,int r,int rt){
        tree[rt].l=l;
        tree[rt].r=r;
        if(l==r){
            tree[rt].w=a[l];
            return;
        }
        int mid=(l+r)/2;
        build(l,mid,rt*2);
        build(mid+1,r,rt*2+1);
        tree[rt].w=tree[rt*2].w+tree[rt*2+1].w;
    }
    
    void pushdown(int rt){
        tree[rt*2].lazy+=tree[rt].lazy;
        tree[rt*2+1].lazy+=tree[rt].lazy;
        tree[rt*2].w+=1ll*tree[rt].lazy*(tree[rt*2].r-tree[rt*2].l+1);
        tree[rt*2+1].w+=1ll*tree[rt].lazy*(tree[rt*2+1].r-tree[rt*2+1].l+1);
        tree[rt].lazy=0;
    }
    
    void update(int rt){
        if(tree[rt].l>=x && tree[rt].r<=y){
            tree[rt].w+=1ll*v*(tree[rt].r-tree[rt].l+1);
            tree[rt].lazy+=v;
            return;
        }
        if(tree[rt].lazy) pushdown(rt);
        int mid=(tree[rt].l+tree[rt].r)/2;
        if(x<=mid) update(rt*2);
        if(y>mid) update(rt*2+1);
        tree[rt].w=tree[rt*2].w+tree[rt*2+1].w;
    }
    
    void query(int rt){
        if(tree[rt].l>=x && tree[rt].r<=y){
            ans+=tree[rt].w;
            return;
        }
        if(tree[rt].lazy) pushdown(rt);
        int mid=(tree[rt].l+tree[rt].r)/2;
        if(x<=mid) query(rt*2);
        if(y>mid) query(rt*2+1);
    }
    
    int main(){
        scanf("%d",&n);
        for(int i=1;i<=n;i++){
            scanf("%lld",&a[i]);
        }
        build(1,n,1);
        scanf("%d",&m);
        for(int i=1;i<=m;i++){
            scanf("%d",&op);
            if(op==1){
                scanf("%d%d%d",&x,&y,&v);
                update(1);
            }
            if(op==2){
                ans=0;
                scanf("%d%d",&x,&y);
                query(1);
                printf("%lld
    ",ans);
            }
        }
        return 0;
    }
  • 相关阅读:
    被.net郁闷的一天
    使用批处理出现奇怪的现象
    我们应该更相信ghost
    asp.net设置默认按钮的一种方法(041217更新)
    asp中access到sql server导入升级后要做的工作。
    一种简单方便的权限控制方案
    为何我的本本不能打开休眠功能?
    祝贺自己的blog开张
    sql server中分页获取数据的存储过程
    httpcompress实际效果能有多少?
  • 原文地址:https://www.cnblogs.com/whdsunny/p/10458632.html
Copyright © 2011-2022 走看看