ACM-ICPC 2018 徐州赛区网络预赛 H. Ryuji doesn't want to study(线段树)

题目链接:H. Ryuji doesn't want to study

 

#include<bits/stdc++.h>
using namespace std;
#define clr(a) memset(a,0,sizeof(a))
#define line cout<<"-------------"<<endl
 
typedef long long ll;
const ll maxn = 1e5+10;
const ll MAXN = 1e6+10;
const ll INF = 0x3f3f3f3f;
const ll MOD = 1e9+7;
const ll N = 1010;
 
ll n, q;
ll a[maxn];
struct node{
    ll l, r, val, ans;
    ll mid(){
        return (l + r) >> 1;
    }
}tree[maxn << 2];
void pushup(ll x){
    tree[x].val = tree[x<<1].val + tree[x<<1|1].val;
    tree[x].ans = tree[x<<1].ans + tree[x<<1|1].ans;
}
void build(ll x, ll l, ll r){
    tree[x].l = l; tree[x].r = r;
    if(l == r){
        tree[x].val = a[l];
        tree[x].ans = a[l] * (n-l+1);
        return ;
    }
    ll mid = tree[x].mid();
    build(x<<1, l, mid);
    build(x<<1|1, mid+1, r);
    pushup(x);
}
void update(ll x, ll pos, ll val){
    if(tree[x].l == tree[x].r){
        tree[x].val = val;
        tree[x].ans = val * (n-tree[x].l+1);
        return ;
    }
    ll mid = tree[x].mid();
    if(mid >= pos) update(x<<1, pos, val);
    else if(mid < pos) update(x<<1|1, pos, val);
    pushup(x);
}
ll ans1 = 0, ans2 = 0;
void query(ll x, ll l, ll r){
    if(tree[x].l >= l && tree[x].r <= r){
        ans1 += tree[x].ans;
        ans2 += tree[x].val;
        return ;
    }
    ll mid = tree[x].mid();
    if(mid >= l) query(x<<1, l, r);
    if(mid < r) query(x<<1|1, l, r);
}
 
int main(){
    while(scanf("%lld%lld", &n, &q) != EOF){
        for(ll i=1; i<=n; i++) scanf("%lld", &a[i]);
        build(1, 1, n);
        while(q--){
            ans1 = ans2 = 0;
            ll x, y, z;
            scanf("%lld%lld%lld", &x, &y, &z);
            if(x == 1){
                query(1, y, z);
                printf("%lld\n", ans1 - ans2*(n-z));
            }
            else{
                update(1, y, z);
            }
        }
    }
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值