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: 135904   Accepted: 42113
    Case Time Limit: 2000MS

    Description

    You have N integers, A1A2, ... , 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 A1A2, ... , 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

     

    思路:线段树水题,建议手写线段树,熟悉模板,注意数据需要使用long long

    》》点击进入原题测试《《

    #include<string>
    #include<iostream>
    #include<algorithm>
    
    #define lson l,m,rt<<1
    #define rson m+1,r,rt<<1|1
    #define ll long long
    
    using namespace std;
    
    const int maxn = 1e5 + 5;
    
    struct Tree{
        ll l, r, w, f;
    }tree[maxn << 2];
    ll ans;
    
    inline void PushDown(int rt)
    {
        tree[rt << 1].f += tree[rt].f;
        tree[rt << 1 | 1].f += tree[rt].f;
        tree[rt << 1].w += tree[rt].f*(tree[rt << 1].r - tree[rt << 1].l + 1);
        tree[rt << 1 | 1].w += tree[rt].f*(tree[rt << 1 | 1].r - tree[rt << 1 | 1].l + 1);
        tree[rt].f = 0;
    }
    void build(int l, int r, int rt)
    {
        tree[rt].l = l; 
        tree[rt].r = r;
        tree[rt].f = 0;
        if (l == r){
            cin >> tree[rt].w;
            return;
        }
        int m = (l + r) >> 1;
        build(lson);
        build(rson);
        tree[rt].w = tree[rt << 1].w + tree[rt << 1 | 1].w;
    }
    void update(int L, int R, int l, int r, int rt)
    {
        if (L <= l&& r <= R){
            tree[rt].w += ans*(r - l + 1);
            tree[rt].f += ans;
            return;
        }
        if (tree[rt].f)PushDown(rt);
        ll m = (l + r) >> 1;
        if (L <= m)update(L, R, lson);
        if (R > m) update(L, R, rson);
        tree[rt].w = tree[rt << 1].w + tree[rt << 1 | 1].w;
    }
    ll query(ll L, ll R, ll l, ll r, ll rt)
    {
        if (L <= l&&r <= R){
            return tree[rt].w;
        }
        if (tree[rt].f)PushDown(rt);
        ll m = (l + r) >> 1, cnt = 0;
        if (L <= m)cnt +=  query(L, R, lson);
        if (R > m)cnt += query(L, R, rson);
        return cnt;
    }
    void Print(int l, int r, int rt)
    {
        if (l == r){
            cout << rt << " = " << tree[rt].w << endl;
            return;
        }
        //cout << rt << " = " << tree[rt].w << endl;
        int m = (l + r) >> 1;
        if (l <= m)Print(lson);
        if (r > m)Print(rson);
    }
    int main()
    {
        std::ios::sync_with_stdio(false);
        
        ll n, q; 
        cin >> n >> q;
            
        build(1, n, 1);
        string flag; ll a, b;
        while (q--){
            cin >> flag >> a >> b;
            if (flag == "C"){
                cin >> ans;;
                update(a, b, 1, n, 1);
                //Print(1, n, 1);
            }
            else if (flag == "Q"){
                cout << query(a, b, 1, n, 1) << endl;
            }
        }
        
    
        return 0;
    }
  • 相关阅读:
    Bootstrap JS插件使用实例(2)-模态对话框
    Java常用正则表达式验证工具类RegexUtils.java
    程序员不是砌砖工人,他们是作家【转】
    js中继承的几种用法总结(apply,call,prototype)
    javascript中apply()方法解析-简单易懂!
    javascript方法 call()和apply()的用法
    高性能优化Web前端
    Javascript类继承-机制-代码Demo【原创】
    初识WEB:输入URL之后的故事【转】
    博客园添加网站统计访问量-操作简单很实用哦!
  • 原文地址:https://www.cnblogs.com/zengguoqiang/p/9383927.html
Copyright © 2011-2022 走看看