【暑训组队 #1】 Array‘s Hash 树状数组

Vasya has invented a new hash function of an array. It is calculated as follows. While the array has at least two elements, the first two elements, call them a1 and a2, are deleted, and the new element a2−a1 is inserted to the beginning of the array. When the array has only one element, this number is a value of Vasya’s hash function of this array.

Vasya has the array a1, a2,…, an. He performs q operations of the following form: “increase all elements in the segment [lj,rj] by vj”. After each operation he wants to know the value of Vasya’s hash function of this array.

Input
The first line contains an integer n (1≤n≤500000) — the size of the array.

The second line contains n integers ai (−109≤ai≤109) — the elements of the array.

The third line contains an integer q (1≤q≤200000) — the number of operations.

Each of the next q lines contains three integers lj, rj, vj (1≤lj≤rj≤n, −109≤vj≤109) — the parameters of the j-th operation.

Output
Output q lines. In the j-th line output one integer — the value of Vasya’s hash function after the j-th operation.

Example
Input
7
4 2 -5 10 4 -2 6
4
2 4 -8
5 7 2
3 3 -1
3 7 3
Output
7
9
8
11

题意:每次对【L,R】区间+y,输出每次处理完后的奇数项和偶数项差的绝对值

思路:

题目的前一段其实就是在求奇数项和偶数项差的绝对值。这个就只需要分别统计偶数项和奇数项的和,用树状数组维护即可。

AC代码:

#include<iostream>
#include<string>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<map>
#include <queue>
#include<sstream>
#include <stack>
#include <set>
#include <bitset>
#include<vector>
#define FAST ios::sync_with_stdio(false)
#define abs(a) ((a)>=0?(a):-(a))
#define sz(x) ((int)(x).size())
#define all(x) (x).begin(),(x).end()
#define mem(a,b) memset(a,b,sizeof(a))
#define max(a,b) ((a)>(b)?(a):(b))
#define min(a,b) ((a)<(b)?(a):(b))
#define rep(i,a,n) for(int i=a;i<=n;++i)
#define per(i,n,a) for(int i=n;i>=a;--i)
#define pb push_back
#define mp make_pair
#define fi first
#define se second
using namespace std;
typedef long long ll;
typedef pair<ll,ll> PII;
const int maxn = 5e5+200;
const int inf=0x3f3f3f3f;
const double eps = 1e-7;
const double pi=acos(-1.0);
const int mod = 1e9+7;
inline int lowbit(int x){return x&(-x);}
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
void ex_gcd(ll a,ll b,ll &d,ll &x,ll &y){if(!b){d=a,x=1,y=0;}else{ex_gcd(b,a%b,d,y,x);y-=x*(a/b);}}//x=(x%(b/d)+(b/d))%(b/d);
inline ll qpow(ll a,ll b,ll MOD=mod){ll res=1;a%=MOD;while(b>0){if(b&1)res=res*a%MOD;a=a*a%MOD;b>>=1;}return res;}
inline ll inv(ll x,ll p){return qpow(x,p-2,p);}
inline ll Jos(ll n,ll k,ll s=1){ll res=0;rep(i,1,n+1) res=(res+k)%i;return (res+s)%n;}
inline ll read(){ ll f = 1; ll x = 0;char ch = getchar();while(ch>'9'|ch<'0') {if(ch=='-') f=-1; ch = getchar();}while(ch>='0'&&ch<='9') x = (x<<3) + (x<<1) + ch - '0',  ch = getchar();return x*f; }
int dir[4][2] = { {1,0}, {-1,0},{0,1},{0,-1} };

ll c[maxn];
ll a[maxn];
ll d1[maxn];
ll c1[maxn];
ll d2[maxn];
ll sum3[maxn];
ll sum4[maxn];
ll sum1[maxn];
ll sum2[maxn];



//这里开始是有区间修改的版本,用d表示前后项差,d的前i项和即是a[i],这样可以实现对d修改后,区间【L,R】内的元素也得到改变
void add_odd(ll pos, ll y, ll n)            //在pos位置+y,对d
{
        for(ll i=pos;i<=n;i+=lowbit(i))
        sum1[i] += y, sum2[i] += pos*y; //从这个位置开始,包含pos项的sum都改变。    sum1是d[i]的前缀和,sum2是d[i]*i的前缀和,sum1*pos-sum2就是a的前缀和
}

void add_range_odd(ll l, ll r,ll x, ll n)
{
     add_odd(l,x,n), add_odd(r+1,-x,n);
}

ll ask_odd(ll p, ll n)
{
        ll ans  = 0;
        for(ll i=p;i>=1;i-=lowbit(i))
        ans += ((p+1)*sum1[i] - sum2[i]);
        return ans;
}

void add_even(ll pos, ll y, ll n)            //在pos位置+y,对d
{
        for(ll i=pos;i<=n;i+=lowbit(i))
        sum3[i] += y, sum4[i] += pos*y; //从这个位置开始,包含pos项的sum都改变。    sum1是d[i]的前缀和,sum2是d[i]*i的前缀和,sum1*pos-sum2就是a的前缀和
}

void add_range_even(ll l, ll r,ll x, ll n)
{
     add_even(l,x,n), add_even(r+1,-x,n);
}

ll ask_even(ll p, ll n)
{
        ll ans  = 0;
        for(ll i=p;i>=1;i-=lowbit(i))
        ans += ((p+1)*sum3[i] - sum4[i]);
        return ans;
}
ll n,m;

void init()
{
        memset(c,0,sizeof(c));
        memset(sum1,0,sizeof(sum1));
        memset(sum2,0,sizeof(sum2));
}


int main()
{
    n = read();
    ll odd = 0, even = 0;
    rep(i,1,n)
    {
        a[i] = read();
        if(i&1)
        {
            d1[++odd] = a[i] - (i-2<=0?0:a[i-2]);
            add_odd(odd,d1[odd],n/2 + n%2);
        }
        else
        {
            d2[++even] = a[i] - (i-2<=0?0:a[i-2]);
            add_even(even,d2[even],n/2);
        }
    }
    m = read();
    rep(i,1,m)
    {
        ll l = read(), r = read(), y = read();
        ll L1, L2, R1, R2;
        if(l&1) L1 = l/2 + 1, L2 = l/2 + 1;
        else L1 = l/2+1, L2 = l/2;
        if(r&1) R1 = r/2 + 1, R2 = r/2;
        else
        R1 = r/2 , R2 = r/2;
        if(r-l+1==1)
        {
            if(l & 1) L2 = R2 = 0;
            else L1 = R1 = 0;
        }
        if(L1&&R1)
        add_range_odd(L1,R1,y,odd);
        if(L2&&R2)
        add_range_even(L2,R2,y,even);
        ll ans = 0;
        if(n&1)
        ans = ask_odd(odd,odd) - ask_even(even,even);
        else
        ans = ask_even(even,even) - ask_odd(odd,odd);
        printf("%lld\n",ans);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_45492531/article/details/107365530
今日推荐