1. 程式人生 > >Newcoder 111 F.托米搭積木(水~)

Newcoder 111 F.托米搭積木(水~)

Description

這天,可愛的小托米得到了n堆積木,且第i堆積木初始時有ai塊積木.

小托米很快就喜歡上了玩積木.

他會作出三種操作:

1.把第 v v 堆的積木數量更改為 x x .

2.在每堆積木的上面都加上 y

y 個積木.

3.數第 q q 堆積木的積木個數.

由於這天可愛的小托米實在是太困了,所以他請你幫他完成這些操作.

Input

第一行兩個整數 n , m

n,m .
第二行 n n 個整數,第 i i 個整數代表 a
i a_i
的值.
接下來 m m 行,每行代表一個操作:
第一個整數 t t 代表操作的型別
t = 1 t=1 ,則接下來兩個整數 v , x v,x ,代表操作 1 1 .
t = 2 t=2 ,則接下來一個整數 y y ,代表操作 2 2 .
t = 3 t=3 ,則接下來一個整數 q q ,代表操作 3 3 .

( 1 n , m 1 0 5 , 1 a i 1 0 9 , 1 t 3 , 1 l e v n , 1 x 1 0 9 , 1 y 1 0 4 , 1 q n ) (1\le n,m\le 10^5,1\le a_i\le 10^9,1\le t\le 3,1le v\le n,1\le x\le 10^9,1\le y\le 10^4,1\le q\le n)

Output

對於每個操作 3 3 ,輸出其對應的答案.

Sample Input

10 11
1 2 3 4 5 6 7 8 9 10
3 2
3 9
2 10
3 1
3 10
1 1 10
2 10
2 10
3 1
3 10
3 9

Sample Output

2
9
11
20
30
40
39

Solution

維護每堆加的值 r e s res ,假設當前要把第 v v 堆從 x x 變成 y y ,相當於把 a v a_v 變成 y r e s y-res ,查詢 v v 的答案即為 a v + r e s a_v+res

Code

#include<cstdio>
using namespace std;
typedef long long ll;
const int maxn=100005;
int n,m;
ll a[maxn];
int main()
{
	scanf("%d%d",&n,&m);
	for(int i=1;i<=n;i++)scanf("%d",&a[i]);
	ll res=0;
	while(m--)
	{
		int op,x,y;
		scanf("%d%d",&op,&x);
		if(op==1)
		{
			scanf("%d",&y);
			a[x]=y-res;
		}
		else if(op==2)res+=x;
		else printf("%lld\n",a[x]+res);
	}
	return 0;
}