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);
            }
        }
    }
  • 相关阅读:
    JS & JQuery 动态处理select option
    如何在Oracle中复制表结构和表数据
    基于cxf的app文件上传接口(带回显功能)
    Jenkins的详细安装及使用--windows
    git用代码库文件完全覆盖本地/git不能提交jar的设置
    Windows平台下Git服务器搭建
    Vue脚手架之Vue-cli
    Vue的生命周期
    Vue状态管理之Vuex
    Vue路由管理之Vue-router
  • 原文地址:https://www.cnblogs.com/smallhester/p/11267225.html
Copyright © 2011-2022 走看看