2018 icpc徐州站網路賽 H Ryuji doesn't want to study

Self-Discipline發表於2018-09-13

題目:點選開啟連結

題意:給定一個數列,1操作求一個這樣的區間[ L , R ]和:a[ R ]+a[ R-1 ]*2+a[ R-2 ]*3+...+a[ L ]*( R - L +1  ),2操作修改一個a[ i ]的值。

分析:藉助兩顆線段樹,線段樹s1維護正常區間和,線段樹s2維護數列:a[ n ],a[ n-1 ]*2,a[ n-2 ]*3,,,,a[ 1 ]*n 的區間和,假如題目求區間[  l ,r ]的的和,先算出 r 到 n 的差 len ,那麼s2[ l , r] - s1[ l , r ]*len就是答案。

程式碼:

#pragma GCC optimize(2)
#pragma GCC optimize(3)
#pragma GCC optimize(4)
#pragma comment(linker, "/STACK:102400000,102400000")
#include<unordered_map>
#include<unordered_set>
#include<algorithm>
#include<iostream>
#include<fstream>
#include<complex>
#include<cstdlib>
#include<cstring>
#include<cassert>
#include<iomanip>
#include<string>
#include<cstdio>
#include<bitset>
#include<vector>
#include<cctype>
#include<cmath>
#include<ctime>
#include<stack>
#include<queue>
#include<deque>
#include<list>
#include<set>
#include<map>
using namespace std;
#define pt(a) cout<<a<<endl
#define debug test
#define mst(ss,b) memset((ss),(b),sizeof(ss))
#define rep(i,a,n) for (ll i=a;i<=n;i++)
#define per(i,a,n) for (ll i=n-1;i>=a;i--)
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
#define SZ(x) ((ll)(x).size())
#define ll long long
#define ull unsigned long long
#define pb push_back
#define mp make_pair
#define inf 0x3f3f3f3f
#define eps 1e-10
#define PI acos(-1.0)
typedef pair<ll,ll> PII;
const ll mod = 1e9+7;
const ll N = 1e5+10;

ll gcd(ll p,ll q){return q==0?p:gcd(q,p%q);}
ll qp(ll a,ll b) {ll res=1;a%=mod; assert(b>=0); for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;}
ll to[4][2]={{-1,0},{1,0},{0,-1},{0,1}};

ll n,q,a[N],b[N],s1[4*N],s2[4*N];

void bd(ll l,ll r,ll id) {
    if(l==r) {
        s1[id]=a[l],s2[id]=b[l];
        return ;
    }
    ll m=(l+r)/2;
    bd(l,m,2*id);
    bd(m+1,r,2*id+1);
    s1[id]=s1[id*2]+s1[id*2+1];
    s2[id]=s2[id*2]+s2[id*2+1];
}

ll res=0;
void qy(ll l,ll r,ll ql,ll qr,ll id,ll tp[]) {
    if(ql<=l&&qr>=r) {
        res+=tp[id];
        return ;
    }
    ll m=(l+r)/2;
    if(ql<=m) qy(l,m,ql,qr,2*id,tp);
    if(qr>m) qy(m+1,r,ql,qr,2*id+1,tp);
}

void up(ll l,ll r,ll id,ll x,ll val,ll tp[]) {
    if(l==r) {
        tp[id]=val;
        return ;
    }
    ll m=(l+r)/2;
    if(x<=m) up(l,m,2*id,x,val,tp);
    else up(m+1,r,2*id+1,x,val,tp);
    tp[id]=tp[id*2]+tp[2*id+1];
}

int main() {
    ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);

    while(cin>>n>>q) {
        rep(i,1,n) cin>>a[i],b[i]=(n-i+1)*a[i];
        bd(1,n,1);
        rep(i,1,q) {
            ll op,id,val,l,r;
            cin>>op;
            if(op==1) {
                res=0;
                cin>>l>>r;
                qy(1,n,l,r,1,s2);
                ll tp=res;
                res=0;
                qy(1,n,l,r,1,s1);
                cout<<tp-res*(n-r)<<endl;
            }else {
                cin>>id>>val;
                up(1,n,1,id,val,s1);
                up(1,n,1,id,id*val,s2);
            }
        }
    }
    return 0;
}

 

相關文章