ICPC2018徐州網路賽 H.Ryuji doesn't want to study ( 樹狀陣列

Yishui_Blog發表於2018-09-13

題意:

KaTeX parse error: No such environment: equation* at position 7: \begin{̲e̲q̲u̲a̲t̲i̲o̲n̲*̲}̲ \sum_{i=l}^ra_…

考慮化簡公式 利用字首和 化簡為KaTeX parse error: No such environment: equation* at position 7: \begin{̲e̲q̲u̲a̲t̲i̲o̲n̲*̲}̲ \sum_{i=l}^ra_… - KaTeX parse error: No such environment: equation* at position 7: \begin{̲e̲q̲u̲a̲t̲i̲o̲n̲*̲}̲ (n-r)*\sum_{i=…

這樣只需要考慮維護兩個數的字首和就可以了


#include <bits/stdc++.h>
using namespace std;

#define cpp_io() {ios::sync_with_stdio(false); cin.tie(NULL);}
#define rep(i,a,n) for (int i=a;i<n;i++)
#define repp(i,a,n) for (int i=a;i<=n;i++)
#define per(i,a,n) for (int i=n-1;i>=a;i--)
#define CLR(a,b) memset(a,(b),sizeof(a))
#define all(x) (x).begin(),(x).end()
#define SZ(x) ((int)(x).size())
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define ls o<<1
#define rs o<<1|1

typedef long long ll;
typedef vector<int> VI;
const int MAXN = (int)2e5+10;
const int INF = 0x3f3f3f3f;
const int mod = (int)998244353;

ll a[MAXN], c[MAXN], cc[MAXN], n, q;
ll lowbit(ll x) { return x&-x; }
ll query(ll p) {ll res=0; while(p) res+=c[p],p-=lowbit(p); return res;}
void add(ll x,ll p) { while(p<=n) c[p]+=x, p+=lowbit(p); }
ll query_1(ll p) {ll res=0; while(p) res+=cc[p],p-=lowbit(p); return res;}
void add_1(ll x,ll p) { while(p<=n) cc[p]+=x, p+=lowbit(p); }

int main() {
    cpp_io();
    cin>>n>>q;
    for(ll i=1;i<=n;++i) {
        cin>>a[i];
        add(a[i],i*1LL); add_1(1LL*a[i]*(n-i+1),i*1LL);
    }
    while(q--){
        ll op,l,r;
        cin>>op>>l>>r;
        if(op==1){
            cout<<1LL*(query_1(r)-query_1(l-1)-((n-r)*(query(r)-query(l-1))))<<endl;
        }
        else {
            add(r-a[l],l);
            add_1(1LL*(r-a[l])*(n-l+1),l);
            a[l]=r;
        }
    }
    return 0;
}


相關文章