# CodeForces 295E – Yaroslav and Points 题解：又是线段树！

/ 0评 / 0

## Description

Yaroslav has n points that lie on the Ox axis. The coordinate of the first point is x_1, the coordinate of the second point is x_2, ..., the coordinate of the n-th point is — x_n. Now Yaroslav wants to execute m queries, each of them is of one of the two following types:

Move the p_j-th point from position x_{p_j} to position x_{p_j} + d_j. At that, it is guaranteed that after executing such query all coordinates of the points will be distinct.
Count the sum of distances between all pairs of points that lie on the segment [l_j, r_j] (l_j ≤ r_j). In other words, you should count the sum of: \displaystyle \sum_{l_j\leq x_p\leq x_q\leq r_j} (x_q-x_p)

Help Yaroslav.

• time limit per test: 5 seconds
• memory limit per test: 256 megabytes
• input: standard input
• output: standard output

## Input

The first line contains integer n — the number of points (1 ≤ n ≤ 10^5). The second line contains distinct integers x_1, x_2, \dots , x_n — the coordinates of points (|x_i| ≤ 10^9).

The third line contains integer m — the number of queries (1 ≤ m ≤ 10^5). The next m lines contain the queries. The j-th line first contains integer t_j (1 ≤ t_j ≤ 2) — the query type. If t_j = 1, then it is followed by two integers p_j and d_j (1 ≤ p_j ≤ n, |d_j| ≤ 1000). If t_j = 2, then it is followed by two integers l_j and r_j (- 10^9 ≤ l_j ≤ r_j ≤ 10^9).

It is guaranteed that at any moment all the points have distinct coordinates.

## Output

For each type 2 query print the answer on a single line. Print the answers in the order, in which the queries follow in the input.

Please, do not use the %lld specifier to read or write 64-bit integers in C++. It is preferred to use the cin, cout streams of the %I64d specifier.

## Examples

Input

8
36 50 28 -75 40 -60 -95 -48
20
2 -61 29
1 5 -53
1 1 429
1 5 130
2 -101 -71
2 -69 53
1 1 404
1 5 518
2 -101 53
2 50 872
1 1 -207
2 -99 -40
1 7 -389
1 6 -171
1 2 464
1 7 -707
1 1 -730
1 1 560
2 635 644
1 7 -677



Output

176
20
406
1046
1638
156
0



## Translation

- 改变某个坐标 x_i，将其增加 d（每次操作 d 不同）：
- 查询 [L_i,R_i] 区间内的 \sum_{x_i>x_j} x_i-x_j，也就是两两点对距离之和。

## Analysis

- sum_p：这个区间中所有点坐标之和；
- tree_p：这个区间中的 \sum_{x_i>x_j} x_i-x_j
- num_p：这个区间中点的数量。

tree_p=\sum_{x_i>x_j} x_i-x_j = \sum_{i=1}^n \sum_{j=1}^n x_i - \sum_{i=1}^n \sum_{j=1}^n x_j

tree_p=tree_{ls}+tree_{rs}+num_{ls}\ast sum_{rs}+num_{rs}\ast sum_{ls}

## Code

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<vector>
#include<map>

using namespace std;
#define int long long

int ret=0,f=1;char ch=getchar();
while (ch<'0'||ch>'9') {if (ch=='-') f=-1;ch=getchar();}
while (ch>='0'&&ch<='9') ret=ret*10+ch-'0',ch=getchar();
return ret*f;
}

const int maxn=2e5+5;
const pair<int,int> zero_pair=make_pair(0,0);
int n,m,N;
map<int,int> to;
int numto[maxn*2],sx[maxn];

#define ls ((p<<1))
#define rs ((p<<1)+1)
#define mid (((tr-tl)>>1)+tl)

struct sts{
int x,y,z;
}zero_sts=(sts){0,0,0};

namespace SegmentTree{
int tree[maxn*4],sum[maxn*4],num[maxn*4];

inline void change(int x,int delta,int tl,int tr,int p){
if (x==tl && tl==tr){
sum[p]+=delta*numto[x];
num[p]+=delta;
return;
}
if (x<=mid  ) change(x,delta,tl,mid,ls);
if (mid+1<=x) change(x,delta,mid+1,tr,rs);
num[p]=num[ls]+num[rs];
tree[p]=tree[ls]+tree[rs]+num[ls]*sum[rs]-num[rs]*sum[ls];
sum[p]=sum[rs]+sum[ls];
// printf("[%lld,%lld]: sum=%lld tree=%lld num=%lld\n",tl,tr,sum[p],tree[p],num[p]);
}

inline sts query(int sl,int sr,int tl,int tr,int p){
if (sl<=tl && tr<=sr){
// printf("tl=%lld tr=%lld sum=%lld tree=%lld num=%lld\n",tl,tr,sum[p],tree[p],num[p]);
return (sts){sum[p],tree[p],num[p]};
}
sts ret1=zero_sts,ret2=zero_sts;
if (sl<=mid  ) ret1=query(sl,sr,tl,mid,ls);
if (mid+1<=sr) ret2=query(sl,sr,mid+1,tr,rs);
sts ret=(sts){ret1.x+ret2.x,  ret1.y+ret2.y+ret1.z*ret2.x-ret2.z*ret1.x,  ret1.z+ret2.z};
// printf("query (%lld %lld %lld %lld) : sum=%lld tree=%lld num=%lld\n",sl,sr,tl,tr,ret.x,ret.y,ret.z);;
return ret;
}
}

struct opt{
int x,y,z;
}opts[maxn];

signed main(){
vector<int> vec;vec.clear();
for (int i=1;i<=m;i++){
if (opts[i].x==1) numto[opts[i].y]+=opts[i].z,vec.push_back(numto[opts[i].y]);
else vec.push_back(opts[i].y),vec.push_back(opts[i].z);
}
sort(vec.begin(),vec.end());unique(vec.begin(),vec.end()); N=vec.size();
for (int i=1;i<=N;i++) numto[i]=vec[i-1],to[numto[i]]=i;//,cout<<i<<" --> "<<vec[i-1]<<endl;

for (int i=1;i<=n;i++) SegmentTree::change(to[sx[i]],1,1,N,1);
for (int i=1;i<=m;i++){
if (opts[i].x==1){
SegmentTree::change(to[sx[opts[i].y]],-1,1,N,1);
sx[opts[i].y]+=opts[i].z;
SegmentTree::change(to[sx[opts[i].y]],1,1,N,1);
} else {
sts ans=SegmentTree::query(to[opts[i].y],to[opts[i].z],1,N,1);
printf("%lld\n",ans.y);
// printf(" - %lld %lld %lld\n",ans.x,ans.y,ans.z);
}
}
return 0;
}


• 默认
• 护眼
• 夜晚
• Serif
• Sans