zoukankan      html  css  js  c++  java
  • [ACM-ICPC 2018 徐州赛区网络预赛] Ryuji doesn't want to study [线段树]

    Problem Describe

    Ryuji is not a good student, and he doesn’t want to study. But there are n books he should learn, each book has its knowledge a[i]a[i].
    Unfortunately, the longer he learns, the fewer he gets.
    That means, if he reads books from l to r, he will get a[l]×L+a[l+1]×(L1)++a[r1]×2+a[r]a[l]×L+a[l+1]×(L−1)+⋯+a[r−1]×2+a[r] (L is the length of [ l, r ] that equals to r - l + 1).
    Now Ryuji has qq questions, you should answer him:
    11. If the question type is 1, you should answer how much knowledge he will get after he reads books [ l, r ].
    22. If the question type is 2, Ryuji will change the ith book’s knowledge to a new value.

    Input

    First line contains two integers (n,q100000).(n,q≤100000).
    The next line contains n integers represent a[i](a[i]1e9)a[i](a[i]≤1e9) .
    Then in next qq line each line contains three integers aa, bb, cc, if a = 1a=1, it means question type is 11, and bb, cc represents [ l , r ]. if a = 2a=2 , it means question type is 22 , and b, c means Ryuji changes the bth book’ knowledge to cc

    Output

    For each question, output one line with one integer represent the answer.

    样例输入

    5 3
    1 2 3 4 5
    1 1 3
    2 5 0
    1 4 5

    样例输出

    10
    8

    AC code:

    #include <cstdio>
    #include <cstring>
    #include <iostream>
    #include <algorithm>
    
    #define lson rt<<1
    #define rson rt<<1|1
    
    using namespace std;
    
    typedef long long ll;
    
    const int maxn = 1e5+50;
    
    struct segtree{
        int l,r;
        int lazy;
        ll sum,val;
    }ss[maxn<<2];
    
    int n,m;
    ll v[maxn],bit[maxn];
    
    void pushUp(int rt) {
        ss[rt].sum = ss[lson].sum + ss[rson].sum;
    }
    
    void pushDown(int rt) {
        if ( ss[rt].lazy ) {
            ss[lson].lazy = ss[rson].lazy = 1;
            ss[rt].lazy = 0;
            ss[lson].sum += ss[rt].val * ( ss[lson].r - ss[lson].l + 1 );
            ss[rson].sum += ss[rt].val * ( ss[rson].r - ss[rson].l + 1 );
            ss[lson].val += ss[rt].val; ss[rson].val += ss[rt].val;
            ss[rt].val = 0;
        }
    }
    
    void build(int l,int r,int rt) {
        ss[rt].l = l; ss[rt].r = r;
        ss[rt].sum = 0; ss[rt].lazy = 0; ss[rt].val = 0;
        if ( l == r ) return;
        int mid = (l+r)>>1;
        build(l,mid,lson); build(mid+1,r,rson);
    }
    
    void update(int l,int r,int rt,ll val) {
        if ( l == ss[rt].l && ss[rt].r == r ) {
            ss[rt].lazy = 1; ss[rt].sum += val * (ss[rt].r -ss[rt].l + 1);
            ss[rt].val += val ;
            return;
        }
        pushDown(rt);
        int mid = (ss[rt].l + ss[rt].r) >> 1;
        if ( mid >= r ) {
            update(l,r,lson,val);
        } else if ( l > mid ) {
            update(l,r,rson,val);
        } else {
            update(l,mid,lson,val); update(mid+1,r,rson,val);
        }
        pushUp(rt);
    }
    
    ll query(int l,int r,int rt) {
        if ( l == ss[rt].l && r == ss[rt].r ) {
            return ss[rt].sum;
        }
        pushDown(rt);
        int mid = (ss[rt].l + ss[rt].r ) >> 1;
        if ( mid >= r ) return query(l,r,lson); 
        else if ( l > mid ) return query(l,r,rson);
        else return query(l,mid,lson) + query(mid+1,r,rson);
    } 
    
    void Print(int l,int r,int rt) {
        if(l == r) {
            printf("%lld ",ss[rt].sum);
            return;
        }
        pushDown(rt) ;
        int mid = (l + r) >> 1;
        Print(l,mid,lson) ; Print(mid+1,r,rson);
    } 
    
    ll Query(int pos) {
        ll ret = 0;
        while(pos > 0) {
            ret += bit[pos];
            pos -= (pos & (-pos));
        }
        return ret;
    }
    
    void Update(int pos,ll val) {
        while(pos <= n) {
            bit[pos] += val;
            pos += (pos & (-pos));
        }
    }
    
    int main() {
        scanf("%d %d",&n,&m);
        memset(bit ,0 ,sizeof(bit) );
        build(1,n,1);
        for (int i = 1;i<=n;i++) {
            scanf("%lld",&v[i]);
            update(i,n,1,v[i]);
            Update(i,v[i]);
        }
        //Print(1,n,1);  puts("");
        int op ,l ,r ;
        while(m--) {
            scanf("%d %d %d",&op ,&l ,&r );
            if(op == 1) {
                printf("%lld
    ",query(l,r,1) - (r - l + 1) * Query(l-1));
            } else if ( op == 2 ) {
                update(l,n,1,r - v[l]); Update(l,r - v[l]);
                v[l] = r;
                //Print(1,n,1); puts("");
            }
        }
        return 0;
    }
  • 相关阅读:
    nginx系列11:负载均衡哈希算法ip_hash与hash模块
    nginx系列10:通过upstream模块选择上游服务器和负载均衡策略round-robin
    nginx系列9:HTTP反向代理请求处理流程
    css 滚动条样式
    Vue优化:常见会导致内存泄漏问题及优化
    vue自定义指令导致的内存泄漏问题解决
    vue动态绑定class的几种方式
    前端知识点回顾之重点篇——CSS中flex布局
    Javascript数组操作
    pc端常用电脑屏幕 ((响应式PC端媒体查询)电脑屏幕分辨率尺寸大全)
  • 原文地址:https://www.cnblogs.com/Nlifea/p/11745943.html
Copyright © 2011-2022 走看看