Sereja has got an array, consisting of n integers, a1, a2, ..., an. Sereja is an active boy, so he is now going to complete m operations. Each operation will have one of the three forms:
- Make vi-th array element equal to xi. In other words, perform the assignment avi = xi.
- Increase each array element by yi. In other words, perform n assignments ai = ai + yi (1 ≤ i ≤ n).
- Take a piece of paper and write out the qi-th array element. That is, the element aqi.
Help Sereja, complete all his operations.
The first line contains integers n, m (1 ≤ n, m ≤ 105). The second line contains n space-separated integers a1, a2, ..., an (1 ≤ ai ≤ 109) — the original array.
Next m lines describe operations, the i-th line describes the i-th operation. The first number in the i-th line is integer ti (1 ≤ ti ≤ 3) that represents the operation type. If ti = 1, then it is followed by two integers vi and xi, (1 ≤ vi ≤ n, 1 ≤ xi ≤ 109). If ti = 2, then it is followed by integer yi (1 ≤ yi ≤ 104). And if ti = 3, then it is followed by integer qi (1 ≤ qi ≤ n).
For each third type operation print value aqi. Print the values in the order, in which the corresponding queries follow in the 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
2 9 11 20 30 40 39
题意:3个操作,1 a[i] = x, 2 所有a[i] += x, 3 输出a[i]。
当然线段树一定能写,这里用一个sum数组记录前缀和,pre数组记录上一次改变是什么时候,查询的时候输出sum[cur] - sum[ pre[pos] ] + a[pos] 即可
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <vector>
#include <string>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <iostream>
#include <algorithm>
using namespace std;
#define lson l, mid, rt << 1
#define rson mid + 1, r, rt << 1 | 1
#define ls rt << 1
#define rs rt << 1 | 1
#define pi acos(-1.0)
#define eps 1e-8
#define asd puts("sdfsdfsdfsdfsdfsdf");
typedef long long ll;
const int inf = 0x3f3f3f3f;
const int N = 111111;
ll n, m;
ll pre[N], sum[N];
ll a[N];
int main()
{
while( cin >> n >> m ) {
sum[0] = 0;
for( int i = 1; i <= n; ++i ) {
pre[i] = 0;
cin >> a[i];
}
ll op, x, pos;
for( int i = 1; i <= m; ++i ) {
cin >> op;
if( op == 1 ) {
cin >> pos >> x;
a[pos] = x;
pre[pos] = i;
sum[i] = sum[i-1];
}
else if( op == 2 ) {
cin >> x;
sum[i] = sum[i-1] + x;
}
else {
cin >> pos;
sum[i] = sum[i-1];
cout << sum[i] - sum[ pre[pos] ] + a[pos] << endl;
}
}
}
return 0;
}