• poj3468 A simple problem with integers


    You have N integers, A1, A2, ... , AN. You need to deal with two kinds of operations. One type of operation is to add some given number to each number in a given interval. The other is to ask for the sum of numbers in a given interval.

    Input

    The first line contains two numbers N and Q. 1 ≤ N,Q ≤ 100000.
    The second line contains N numbers, the initial values of A1, A2, ... , AN. -1000000000 ≤ Ai ≤ 1000000000.
    Each of the next Q lines represents an operation.
    "C a b c" means adding c to each of AaAa+1, ... , Ab. -10000 ≤ c ≤ 10000.
    "Q a b" means querying the sum of AaAa+1, ... , Ab.

    Output

    You need to answer all Q commands in order. One answer in a line.

    Sample 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

    The sums may exceed the range of 32-bit integers.

    简单的线段树+区间更新模板 看到最后hint已经把类型都改为longlong了

    但是还是WA了一发  发现输入的时候用的%d 改成%I64d 就AC了

    
    #include<iostream>
    #include<stdio.h>
    #include<string.h>
    #include<algorithm>
    #define inf 1e18
    using namespace std;
    
    int q, n;
    const int maxn = 100005;
    long long val[maxn], tree[maxn << 2],lazy[maxn << 2];
    
    
    void pushup(int rt)//更新
    {
        tree[rt] = tree[rt << 1] + tree[rt << 1 | 1];
    }
    
    void build(int l, int r, int rt)
    {
        if(l == r){
            tree[rt] = val[l];
            return;
        }
        int m = (l + r) >> 1;
        build(l, m, rt << 1);
        build(m + 1, r, rt << 1 | 1);
        pushup(rt);
    }
    
    void pushdown(int rt, int ln, int rn)
    {
        if(lazy[rt]){
            lazy[rt << 1] += lazy[rt];
            lazy[rt << 1 | 1] += lazy[rt];
            tree[rt << 1] += lazy[rt] * ln;
            tree[rt << 1 | 1] += lazy[rt] * rn;
            lazy[rt] = 0;
        }
    }
    
    void update(int L, int C, int l, int r, int rt)
    {
        if(l == r){
            tree[rt] += C;
            return;
        }
        int m = (l + r) >>1;
        if(L <= m) update(L, C, l, m, rt << 1);
        else update(L, C, m + 1, r, rt << 1 | 1);
        pushup(rt);
    }
    
    void update(int L, int R, int C, int l, int r, int rt)
    {
        if(L <= l && r <= R){//本区间完全在操作区间内
            tree[rt] += C * (r - l + 1);
            lazy[rt] += C;
            return;
        }
        int m = (l + r) >> 1;
        pushdown(rt, m - l + 1, r - m);
        if(L <= m) update(L, R, C, l, m, rt << 1);
        if(R > m) update(L, R, C, m + 1, r, rt << 1 | 1);
        pushup(rt);
    }
    
    long long query(int L, int R, int l, int r, int rt)
    {
        if(L <= l && r <= R){
            return tree[rt];
        }
        int m = (l + r) >> 1;
        pushdown(rt, m - l + 1, r - m);
    
        long long ans = 0;
        if(L <= m) ans += query(L, R, l, m, rt << 1);
        if(R > m) ans += query(L, R, m + 1, r, rt << 1 | 1);
        return ans;
    }
    
    int main()
    {
        while(scanf("%d%d", &n, &q)!= EOF){
            for(int i = 1; i <= n; i++){
                scanf("%I64d", &val[i]);
            }
            build(1, n, 1);
    
            while(q--){
                getchar();
                char ch;
                int a, b, c;
                scanf("%c", &ch);
                if(ch == 'Q'){
                    scanf("%d%d", &a, &b);
                    cout<<query(a, b, 1, n, 1)<<endl;
                }
                else{
                    scanf("%d%d%d", &a, &b, &c);
                    update(a, b, c, 1, n, 1);
                }
            }
        }
        return 0;
    }
    
  • 相关阅读:
    团队展示
    第二次结对编程作业
    第12组 团队展示
    第一次结对编程作业
    第一次个人编程作业
    软工第一次作业
    第十章 创建计算字段
    第九章 用正则表达式进行搜索
    第八章 用通配符进行过滤
    第七章 数据过滤
  • 原文地址:https://www.cnblogs.com/wyboooo/p/9643397.html
Copyright © 2020-2023  润新知