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

    A Simple Problem with Integers
    Time Limit: 5000MS   Memory Limit: 131072K
    Total Submissions: 94448   Accepted: 29421
    Case Time Limit: 2000MS

    Description

    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 Aa, Aa+1, ... , Ab. -10000 ≤ c ≤ 10000. "Q a b" means querying the sum of Aa, Aa+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.

    Source

     
    POJ---3264(同类型的吧)
    思路:
    线段树成段更新的入门题目。。学会使用lazy即可。还需要注意的是,lazy的时候更改是累加,而不是直接修改。。有可能连续几次进行修改操作。。注意这一点就好了。。
    #include <iostream>
    #include <string.h>
    #include <stdio.h>
    #include <algorithm>
    #include <math.h>
    #include <vector>
    using namespace std;
    typedef long long LL;
    const int oo = 0xfffffff;
    const int maxn = 110007;
    #define lson rt<<1
    #define rson rt<<1|1
    int A[maxn];
    
    struct da
    {
        int l, r;
        int len()
        {
            return r-l+1;
        }
        LL s, e;
    } tree[maxn<<2];
    
    void build(int l, int r, int rt)// 建立一颗树
    { tree[rt].l = l; tree[rt].r = r; tree[rt].e = 0; if(l == r) { tree[rt].s = A[l]; return ; } int mid = (l+r)/2; build(l, mid, rt*2); build(mid+1, r, rson); tree[rt].s = tree[lson].s + tree[rson].s; }
    void down(int rt) { tree[lson].s += tree[lson].len()*tree[rt].e; tree[rson].s += tree[rson].len()*tree[rt].e; tree[lson].e += tree[rt].e; tree[rson].e += tree[rt].e; tree[rt].e = 0; } void update(int l, int r, int val, int rt) { tree[rt].s += val*(r-l+1); if(tree[rt].l == l && r == tree[rt].r)///超时的原因****&* { tree[rt].e += val; return; } down(rt); int mid = (tree[rt].l+tree[rt].r)/2; if(r <= mid) update(l, r, val, lson); else if(l > mid)update(l, r, val, rson); else { update(l, mid, val, lson); update(mid+1, r, val,rson); } } LL query(int l, int r, int rt) { if(tree[rt].l == l && r == tree[rt].r) { return tree[rt].s; } down(rt); int mid = (tree[rt].l+tree[rt].r)/2; if(r <= mid) return query(l, r, lson); else if(l > mid) return query(l, r, rson); else { LL lsum, rsum; lsum = query(l, mid, lson); rsum = query(mid+1, r, rson); return lsum+rsum ; } } int main() { int n, q, i, a, b, c; while(scanf("%d %d", &n, &q) != EOF) { char op[20]; for(i = 1; i <= n; i++) scanf("%d", &A[i]); build(1, n, 1); while(q--) { scanf("%s %d %d", op, &a, &b); if(op[0] == 'Q') { printf("%lld ", query(a, b, 1)); continue; } scanf("%d", &c); update(a, b, c, 1); } } return 0; }
  • 相关阅读:
    xfs(dm-3):please umount the filesystem and rectify the problem(s)
    解决 pcre-8.35 make[2]: *** [aclocal.m4] Error 127
    redis-4.0.6 编译安装
    nginx出错:rewrite or internal redirection cycle
    17个技巧
    go-fastdfs/go-fastdfs-web
    Win X86 时间同步
    关闭或启动linux防火墙后,docker启动容器报错问题解决方式
    Nginx 重定向 443
    Docker 容器和宿主机相互拷贝文件
  • 原文地址:https://www.cnblogs.com/w-y-1/p/5738434.html
Copyright © 2011-2022 走看看