zoukankan      html  css  js  c++  java
  • A Simple Problem with Integers POJ

    #include<iostream>
    #include<cstdio>
    #include<cstdlib>
    #include<cstring>
    #include<algorithm>
    #include<cmath>
    #include<queue>
    #include<list>
    #include<math.h>
    #include<vector>
    #include<stack>
    #include<string>
    #include<stdio.h>
    
    using namespace std;
    typedef long long LL;
    const int MAXN = 1e5 + 10;
    LL a[MAXN],st[MAXN << 2];
    LL add[MAXN << 2];
    void pushup(int o) {
        st[o] = st[o << 1] + st[o << 1 | 1];
    }
    void build(int o,int l,int r) {
        if (l == r) st[o] = a[l];
        else {
            int m = (l + r) >> 1;
            build(o << 1, l, m);
            build(o << 1 | 1, m + 1, r);
            pushup(o);
        }
    }
    void pushdown(int o,int l,int r) {
        if(add[o]) {
            add[o << 1] += add[o];
            add[o << 1 | 1] += add[o];
            int m = (l + r) >> 1;
            st[o << 1] += add[o] * (m - l + 1);
            st[o << 1 | 1] += add[o] * (r - m);
            add[o] = 0;
        }
    }
    
    LL query(int o,int l,int r,int ql,int qr) {
        if(ql <= l && r <= qr) return st[o];
        pushdown(o,l,r);
        int m = (l + r) >> 1;
        LL ans = 0;
        if(ql <= m) ans += query(o << 1, l, m, ql, qr);
        if(qr >= m + 1) ans += query(o << 1 | 1, m + 1, r, ql, qr);
        return ans;
    }
    
    void update(int o,int l,int r,int ql,int qr,LL addv) {
        if(ql <= l && r <= qr) {
            add[o] += addv;
            st[o] += addv * (r - l + 1);
            return;
        }
        pushdown(o,l,r);
        int m = (l + r) >> 1;
        if(ql <= m) update(o << 1,l,m,ql,qr,addv);
        if(qr >= m + 1) update(o << 1 | 1,m + 1,r,ql,qr,addv);
        pushup(o);
    }
    
    
    int main() {
        int n,q;
        scanf("%d %d",&n,&q);
        for (int i = 1; i <= n ; i++)
            scanf("%lld",&a[i]);
        build(1,1,n);
        char s[10];
        int x,y;
        LL add;
        while(q--) {
            scanf("%s",s);
            if(s[0] == 'Q') {
                scanf("%d %d",&x,&y);
                LL ans = query(1,1,n,x,y);
                printf("%lld
    ",ans);
            }
            else {
                scanf("%d %d %lld",&x,&y,&add);
                update(1,1,n,x,y,add);
            }
        }
    }
  • 相关阅读:
    docker pull 报X509错误
    Kong配置反向代理后获取原始IP
    MybatisPlus框架
    工厂模式
    Mybatis持久层框架
    linux 使用scp传输公钥时要注意事项
    docker 容器容器之间网络通信 docker-compose.yaml 配置固定ip
    Linux下执行sh文件提示权限不够解决办法
    docker-compose 编写yaml文件的注意事项
    nginx 中location url一定要带http://
  • 原文地址:https://www.cnblogs.com/smallhester/p/11267225.html
Copyright © 2011-2022 走看看