codeforce 315B Sereja and Array 【水题,封顶距离统计 OR 线段树延迟更新】

time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

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:

  1. Make vi-th array element equal to xi. In other words, perform the assignment avi = xi.
  2. Increase each array element by yi. In other words, perform n assignments ai = ai + yi (1 ≤ i ≤ n).
  3. Take a piece of paper and write out the qi-th array element. That is, the element aqi.

Help Sereja, complete all his operations.

Input

The first line contains integers nm (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).

Output

For each third type operation print value aqi. Print the values in the order, in which the corresponding queries follow in the input.

Sample test(s)
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
output
2
9
11
20
30
40
39

水题。方法一:统计与封顶的距离,方法二:线段树成段更新,延迟标记。纯当练练线段树的模板吧!!
法一:

#include <vector>
#include <cstdio>
#include <string>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
typedef __int64 LL;
const int maxn = 1e5 + 5;
int N, M, t, v, x, y, q, a, H;
int arr[maxn];
int main()
{
    //freopen("input.in", "r", stdin);
    while(~scanf("%d %d", &N, &M))
    {
        for(int i = 1; i <= N; i++)
            scanf("%d", &arr[i]);
        H = 0;
        for(int i = 1; i <= M; i++)
        {
            scanf("%d", &t);
            if(t == 1)
            {
                scanf("%d %d", &v, &x);
                arr[v] = x;
                arr[v] -= H;
            }
            else if(t == 2)
            {
                scanf("%d", &y);
                H += y;
            }
            else if(t == 3)
            {
                scanf("%d", &q);
                printf("%d\n",arr[q] + H);
            }
        }
    }
    return 0;
}

法二:

#include <vector>
#include <cstdio>
#include <string>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
typedef __int64 LL;
const int maxn = 1e5 + 5;
int N, M, t, v, x, y, q, a;
int segtree[maxn<<2], tag[maxn<<2];
void Init()
{
    memset(segtree,0,sizeof(segtree));
    memset(tag,0,sizeof(tag));
}
void Build(int l,int r,int rt)
{
    if(l == r)
    {
        scanf("%d",&segtree[rt]);
        return ;
    }
    int mid = (l+r)>>1;
    Build(lson);
    Build(rson);
}
void PushDown(int rt)
{
    if(tag[rt])
    {
        tag[rt<<1] += tag[rt];
        tag[rt<<1|1] += tag[rt];
        tag[rt] = 0;
    }
}
void Update(int pos,int val,int l,int r,int rt)
{
    if(l == r)
    {
        segtree[rt] = val;
        tag[rt] = 0;
        return ;
    }
    PushDown(rt);
    int mid = (l+r)>>1;
    if(pos <= mid) Update(pos,val,lson);
    else Update(pos,val,rson);
}
int Query(int pos,int l,int r,int rt)
{
    if(l == r)
    {
        segtree[rt] += tag[rt];
        tag[rt] = 0;
        return segtree[rt];
    }
    PushDown(rt);
    int mid = (l+r)>>1,ret;
    if(pos <= mid) ret = Query(pos,lson);
    else ret = Query(pos,rson);
    return ret;
}
int main()
{
    //freopen("input.in", "r", stdin);
    while(~scanf("%d %d", &N, &M))
    {
        Build(1,N,1); //including scanf
        for(int i = 1; i <= M; i++)
        {
            scanf("%d", &t);
            if(t == 1)
            {
                scanf("%d %d", &v, &x);
                Update(v,x,1,N,1);
            }
            else if(t == 2)
            {
                scanf("%d", &y);
                tag[1] += y;
            }
            else if(t == 3)
            {
                scanf("%d", &q);
                printf("%d\n", Query(q,1,N,1));
            }
        }
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值