1. 程式人生 > >Loj 6280 數列分塊入門 4

Loj 6280 數列分塊入門 4

CI 比較 div 數列 min pan long long span sin

鏈接:https://loj.ac/problem/6280

思路:

多設置一個數組sum去存區間值的和就好了,因為數據範圍比較大,需要開long long .

實現代碼;

#include<bits/stdc++.h>
using namespace std;
#define ll long long
const ll M = 1e5+10;
ll sum[M],a[M],tag[M],bl[M],n,block;

void update(ll l,ll r,ll c){
    for(ll i = l;i <= min(bl[l]*block,r);i ++)
        a[i] 
+= c,sum[bl[l]]+=c; if(bl[l] != bl[r]){ for(ll i = (bl[r]-1)*block+1;i <= r;i ++) a[i] += c,sum[bl[r]]+=c; } for(ll i = bl[l]+1;i <= bl[r] -1;i ++) tag[i] += c; } ll query(ll l,ll r){ ll ret = 0; for(ll i = l;i <= min(bl[l]*block,r);i ++) ret
+= a[i]+tag[bl[l]]; if(bl[l] != bl[r]){ for(ll i = (bl[r]-1)*block+1;i <= r;i ++) ret += a[i] + tag[bl[r]]; } for(ll i = bl[l]+1;i <= bl[r]-1;i ++) ret += sum[i]+tag[i]*block; return ret; } int main() { ll f,l,r,c; cin>>n; block
= sqrt(n); for(ll i = 1;i <= n;i ++) cin>>a[i]; for(ll i = 1;i <= n;i ++){ bl[i] = (i-1)/block+1; sum[bl[i]]+=a[i]; } for(ll i = 1;i <= n;i ++){ cin>>f>>l>>r>>c; if(f == 0) update(l,r,c); else cout<<(query(l,r)%(c+1))<<endl; } return 0; }

Loj 6280 數列分塊入門 4