Skip to content

14117: 【原4117】操作数组

题目

题目描述

author: cat 原OJ链接:https://acm.sjtu.edu.cn/OnlineJudge-old/problem/4117

Description

有N个整数a1,a2,...,an。你要在这N个整数上进行两种操作:

1.给定一个区间,对这个区间的每一个数加上一个给定的数;

2.查询一个给定区间的数字和。

Input Format

第一行:两个数字N和Q,1 ≤ N,M ≤ 100000

第二行:N个数字a1,a2,...,an。 -1000000000 ≤ Ai ≤ 1000000000

接下来是M个操作:

每一个Q开头的行是询问给定区间的数字和,每一个C开头的行是对给定区间的每个数加上一个c,-10000 ≤ c ≤ 10000

Q a b 是对[a,b]进行询问;

C a,b,c是在[a,b]区间每个数加上c。

Output Format

回答每一次询问,每个回答占单独一行。

Sanple Input

10 5
1 2 3 4 5 6 7 8 9 10
Q 4 4
Q 1 10
Q 2 4
C 3 6 3
Q 2 4

Sample Output

4
55
9
15

Hint

注意数据规模。

FineArtz's solution

/* 操作数组 */
#include <iostream>
using namespace std;

const int MAXN = 100000;

struct Node{
    int l = 0, r = 0;
    long long lazy = 0, sum = 0;
};

int n, q;
long long t[MAXN + 5];
Node a[MAXN * 4 + 5];

inline void pushUp(int x){
    a[x].sum = a[x * 2].sum + a[x * 2 + 1].sum;
}

inline void pushDown(int x){
    if (a[x].lazy != 0){
        a[x * 2].lazy += a[x].lazy;
        a[x * 2].sum += a[x].lazy * (a[x * 2].r - a[x * 2].l + 1);
        a[x * 2 + 1].lazy += a[x].lazy;
        a[x * 2 + 1].sum += a[x].lazy * (a[x * 2 + 1].r - a[x * 2 + 1].l + 1);
        a[x].lazy = 0;
    }
}

void buildTree(int x, int l, int r){
    a[x].l = l;
    a[x].r = r;
    if (l == r){
        a[x].sum = t[l];
        return;
    }
    int mid = (l + r) / 2;
    buildTree(x * 2, l, mid);
    buildTree(x * 2 + 1, mid + 1, r);
    pushUp(x);
}

void update(int x, int l, int r, int c){
    if (a[x].l >= l && a[x].r <= r){
        a[x].sum += c * (a[x].r - a[x].l + 1);
        a[x].lazy += c;
        return;
    }
    pushDown(x);
    int mid = (a[x].l + a[x].r) / 2;
    if (l <= mid)
        update(x * 2, l, r, c);
    if (r > mid)
        update(x * 2 + 1, l, r, c);
    pushUp(x);
}

long long query(int x, int l, int r){
    if (a[x].l >= l && a[x].r <= r)
        return a[x].sum;
    pushDown(x);
    int mid = (a[x].l + a[x].r) / 2;
    long long ret = 0;
    if (l <= mid)
        ret += query(x * 2, l, r);
    if (r > mid)
        ret += query(x * 2 + 1, l, r);
    return ret;
}

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    cin >> n >> q;
    for (int i = 1; i <= n; ++i)
        cin >> t[i];
    buildTree(1, 1, n);
    while (q--){
        char op;
        int x, y, z;
        cin >> op;
        if (op == 'Q'){
            cin >> x >> y;
            cout << query(1, x, y) << '\n';
        }
        else{
            cin >> x >> y >> z;
            update(1, x, y, z);
        }
    }
    return 0;
}

ligongzzz's solution

#include "iostream"
#include "cstdio"
using namespace std;

int numData[100009] = { 0 };
long long numAns[100009] = { 0 };

int main() {
    int N, M;
    scanf("%d %d", &N, &M);

    for (int i = 1; i <= N; i++) {
        scanf("%d", &numData[i]);
        numAns[i] = numAns[i - 1] + numData[i];
    }

    for (; M > 0; M--) {
        char op;
        scanf("\n%c", &op);

        if (op == 'Q') {
            int a, b;
            scanf("%d %d", &a, &b);
            printf("%lld\n", numAns[b] - numAns[a - 1]);
        }
        else {
            int a, b, c;
            scanf("%d %d %d", &a, &b, &c);
            if (a - 1 > N - b) {
                int ans = c;
                for (; a < b; a++,ans+=c)
                    numAns[a] += ans;
                for (; a <= N; a++)
                    numAns[a] += ans;
            }
            else {
                int ans = c;
                for (b=b-1;b>=a;b--, ans += c)
                    numAns[b] -= ans;
                for (; b>=0; b--)
                    numAns[b] -= ans;
            }
        }
    }

    return 0;
}

Neight99's solution

#include <iostream>

using namespace std;

const int maxN = 1e5 + 100;

long long nums[maxN];

long long question(int, int);

void plusn(int, int, long long);

int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);

    int n, m, tmp1, tmp2;
    char order;
    long long tmp3, sum;
    cin >> n >> m;

    for (int i = 1; i <= n; i++) {
        cin >> nums[i];
    }

    for (int i = 0; i < m; i++) {
        cin >> order;

        if (order == 'Q') {
            cin >> tmp1 >> tmp2;
            sum = question(tmp1, tmp2);
            cout << sum << '\n';
        } else if (order == 'C') {
            cin >> tmp1 >> tmp2 >> tmp3;
            plusn(tmp1, tmp2, tmp3);
        }
    }

    return 0;
}

long long question(int x, int y) {
    long long sum = 0;

    for (int i = x; i <= y; i++) {
        sum += nums[i];
    }

    return sum;
}

void plusn(int x, int y, long long n) {
    for (int i = x; i <= y; i++) {
        nums[i] += n;
    }

    return;
}

q4x3's solution

/**
 * 树状数组..
 * 再过不了就对lowbit打表了TATTTTTTTTTTT
 * 有空写个分块
 **/
#include <iostream>
#include <stdio.h>

using namespace std;

long long a[100010], b[100010], c[100010];
int N, M, tmp1, tmp2, tmp3;
char tmp;

void read(int &x){
    x = 0;
    char ch;
    bool f = 0; 
    while (ch = getchar(), (ch < '0' || ch > '9') && ch != '-');
    if (ch == '-') f = 1;
    else x = ch - '0';
    while(ch = getchar(), ch >= '0' && ch <= '9') x = 10 * x + ch - '0';
    x = f ? -x : x;
}

int lowbit(int x) {
    return x & (-x);
}

void add(long long *a, int pos, long long val) {
    for(;pos <= N;pos += lowbit(pos)) {
        a[pos] += val;
    }
}

long long addd(long long *a, int pos) {
    long long sum = 0;
    for(;pos != 0;pos -= lowbit(pos)) {
        sum += a[pos];
    }
    return sum;
}

int main() {
    read(N);
    read(M);
    int x;
    for(int i = 1;i <= N;++ i) {
        read(x);
        a[i] = x;
        add(b, i, a[i] - a[i - 1]);
        add(c, i, (i - 1) * (a[i] - a[i - 1]));
    }
    for(int i  = 0;i < M;++ i) {
        while (tmp = getchar(), tmp == '\n' || tmp == '\r' || tmp == ' ');
        read(tmp1);
        read(tmp2);
        if(tmp == 'Q') {
            long long s1, s2;
            s1 = (tmp1 - 1) * addd(b, tmp1 - 1) - addd(c, tmp1 - 1);
            s2 = tmp2 * addd(b, tmp2) - addd(c, tmp2);
            printf("%lld\n", s2 - s1);
        } else {
            read(tmp3);
            add(b, tmp1, tmp3);
            add(b, tmp2 + 1, - tmp3);
            add(c, tmp1, 1LL * tmp3 * (tmp1 - 1));
            add(c, tmp2 + 1, -1LL * tmp3 * tmp2);
        }
    }
    return 0;
}

victrid's solution

#include <cstdio>
#include <iostream>
using namespace std;
//The lazy tag is really confusing.
//I may just follow the tutorial.
//400005
long long seq[400005]  = {0};
long long tags[400005] = {0};
struct set {
    //segmenttree
    //Tree access helper
    int leftbound;
    int rightbound;
    int current;
    long long& value;
    long long& tag;
    set left() {
        return set{
            leftbound, (leftbound + rightbound) / 2, current << 1, seq[current << 1], tags[current << 1]};
    }
    set right() {
        return set{
            (leftbound + rightbound) / 2 + 1, rightbound, current << 1 | 1, seq[current << 1 | 1], tags[current << 1 | 1]};
    }
    int addtag() {
        return (rightbound - leftbound + 1) * tag;
    }
    bool isleaf() { return !(rightbound - leftbound); }
    int update_fathers() {
        int z = current >> 1;
        while (z != 0) {
            seq[z] = seq[z << 1] + seq[z << 1 | 1];
            z >>= 1;
        }
        return 0;
    }
};
void buildtree(set s) {
    if (s.leftbound == s.rightbound)
        cin >> s.value;
    else {
        buildtree(s.left());
        buildtree(s.right());
        s.value = s.left().value + s.right().value;
    }
}
void buildtree(int total) {
    buildtree(set{1, total, 1, seq[1], tags[1]});
}
int tagproc(set s) {
    if (!s.isleaf()) {
        if (!s.left().isleaf())
            s.left().tag += s.tag;
        s.left().value += s.tag * (s.left().rightbound - s.left().leftbound + 1);
        if (!s.right().isleaf())
            s.right().tag += s.tag;
        s.right().value += s.tag * (s.right().rightbound - s.right().leftbound + 1);
    }
    s.tag = 0;
    return 0;
}
long long getsum(int oper_left, int oper_right, set s) {
    if (oper_left <= s.leftbound && s.rightbound <= oper_right) {
        if (s.tag != 0)
            tagproc(s);
        return s.value;
    } else {
        long long sum = 0;
        int mid       = (s.leftbound + s.rightbound) / 2;
        if (s.tag != 0)
            tagproc(s);
        if (oper_left <= mid)
            sum += getsum(oper_left, oper_right, s.left());
        if (oper_right > mid)
            sum += getsum(oper_left, oper_right, s.right());
        return sum;
    }
}
long long getsum(int oper_left, int oper_right, int total) {
    return getsum(oper_left, oper_right, set{1, total, 1, seq[1], tags[1]});
}
int addsequence(int oper_left, int oper_right, set s, long long add) {
    if (oper_left <= s.leftbound && s.rightbound <= oper_right) {
        s.value += add * (s.rightbound - s.leftbound + 1);
        s.tag += add;
        s.update_fathers();
        return 0;
    } else {
        //if tags not passed, ancestors cannot get updated correctly.
        tagproc(s);
        int mid = (s.leftbound + s.rightbound) / 2;
        if (oper_left <= mid) {
            //Although no tags,
            //father's value add first.
            s.value += add * (mid - oper_left + 1);
            addsequence(oper_left, oper_right, s.left(), add);
        }
        if (oper_right > mid) {
            s.value += add * (oper_right - mid);
            addsequence(oper_left, oper_right, s.right(), add);
        }
        return 0;
    }
}
int addsequence(int oper_left, int oper_right, int total, long long add) {
    return addsequence(oper_left, oper_right, set{1, total, 1, seq[1], tags[1]}, add);
}
int main() {
    int n, m;
    scanf("%d %d", &n, &m);
    buildtree(n);
    char op;
    int oper_left, oper_right, oper_add;
    for (int i = 0; i < m; i++) {
        do {
            op = getchar();
        } while (op == ' ' || op == '\n');
        if (op == 'Q') {
            scanf("%d %d", &oper_left, &oper_right);
            printf("%lld\n", getsum(oper_left, oper_right, n));
        } else {
            scanf("%d %d %d", &oper_left, &oper_right, &oper_add);
            addsequence(oper_left, oper_right, n, oper_add);
        }
    }
    return 0;
}

WashSwang's solution

#include <iostream>
#include <cstdio>
using namespace std;
const int MAXN=100001;
typedef long long ll;
ll a[MAXN],ans[4*MAXN],tag[4*MAXN];
inline int ls(int p) {
    return p<<1;
}

inline int rs(int p) {
    return p<<1|1;
}

inline void push_up(int p) {
    ans[p]=ans[ls(p)]+ans[rs(p)];
}

void build(int l,int r,int p) {
    if (l==r)
    {
        ans[p]=a[l];
        return;
    }
    int mid=(l+r)>>1;
    build(l,mid,ls(p));
    build(mid+1,r,rs(p));
    push_up(p);
}

inline void add_tag(int p,int l,int r,int k)
{
    tag[p]+=k;
    ans[p]+=k*(r-l+1);
}

inline void push_down(int p,int l,int r)
{
    int mid=(l+r)>>1;
    add_tag(ls(p),l,mid,tag[p]);
    add_tag(rs(p),mid+1,r,tag[p]);
    tag[p]=0;
}

void update(int nl,int nr,int l,int r,int p,int k)
{
    if (nl<=l&&r<=nr)
    {
        add_tag(p,l,r,k);
        return;
    }
    push_down(p,l,r);
    int mid=(l+r)>>1;
    if (nl<=mid) update(nl,nr,l,mid,ls(p),k);
    if (nr>mid) update(nl,nr,mid+1,r,rs(p),k);
    push_up(p);
}

ll query(int nl,int nr,int l,int r,int p)
{
    if (nl<=l&&r<=nr) return ans[p];
    push_down(p,l,r);
    int mid=(l+r)>>1;
    ll sum=0;
    if (nl<=mid) sum+=query(nl,nr,l,mid,ls(p));
    if (nr>mid) sum+=query(nl,nr,mid+1,r,rs(p));
    return sum;
}
int m,n,x,y,k;
char c;
int main()
{
    scanf("%d%d",&n,&m);
    for (int i=1;i<=n;i++) scanf("%lld",&a[i]);
    build(1,n,1);
    for (int i=0;i<m;++i){
        c=' ';
        while (c!='C'&&c!='Q') c=getchar();
        if (c=='C')
        {
            scanf("%d%d%d",&x,&y,&k);
            update(x,y,1,n,1,k);
        }
        if (c=='Q')
        {
            scanf("%d%d",&x,&y);
            printf("%lld\n",query(x,y,1,n,1));
        }
    }
    return 0;
}

yyong119's solution

#include <iostream>
#include <cstdio>
#define MAX_N 100010
using namespace std;

long long tree[MAX_N << 2];
int flag[MAX_N << 2];

void buildtree(int idx, int l, int r) {
    if (l == r) {
        scanf("%lld", &tree[idx]);
        return;
    }
    int mid = (l + r) >> 1;
    buildtree(idx << 1, l, mid);
    buildtree(idx << 1 | 1, mid + 1, r);
    tree[idx] = tree[idx << 1] + tree[idx << 1 | 1];
}

void pushdown(int idx, int l, int r) {
    int mid = (l + r) >> 1;
    tree[idx << 1] += (long long) flag[idx] * (mid - l + 1);
    tree[idx << 1 | 1] += (long long) flag[idx] * (r - mid);
    flag[idx << 1] += flag[idx];
    flag[idx << 1 | 1] += flag[idx];
    flag[idx] = 0;
}

void add(int idx, int l, int r, int a, int b, int c) {
    if (l == a && b == r) {
        tree[idx] += (long long)c * (b - a + 1);
        flag[idx] += c;
        return;
    }
    if (flag[idx])
        pushdown(idx, l, r);
    int mid = (l + r) >> 1;
    if (b <= mid)
        add(idx << 1, l, mid, a, b, c);
    else if (a > mid) 
        add(idx << 1 | 1, mid + 1, r, a, b, c);
    else {
        add(idx << 1, l, mid, a, mid, c);
        add(idx << 1 | 1, mid + 1, r, mid + 1, b, c);
    }
    tree[idx] = tree[idx << 1] + tree[idx << 1 | 1];
}

long long query(int idx, int l, int r, int a, int b) {
    if (l == a && r == b)
        return tree[idx];
    int mid = (l + r) >> 1;
    if (flag[idx])
        pushdown(idx, l, r);
    if (b <= mid)
        return query(idx << 1, l, mid, a, b);
    else if (a > mid)
        return query(idx << 1 | 1, mid + 1, r, a, b);
    else
        return query(idx << 1, l, mid, a, mid) + query(idx << 1 | 1, mid + 1, r, mid + 1, b);
}

int main() {

    int n, m;
    scanf("%d%d", &n, &m);
    buildtree(1, 1, n);
    while (m--) {
        char op[2]; scanf("%s", op);
        if (op[0] == 'Q') {
            int a, b; scanf("%d%d", &a, &b);
            printf("%lld\n", query(1, 1, n, a, b));
        }
        else {
            int a, b, c; scanf("%d%d%d", &a, &b, &c);
            add(1, 1, n, a, b, c);
        }
    }
    return 0;
}