水题。方法一:统计与封顶的距离,方法二:线段树成段更新,延迟标记。纯当练练线段树的模板吧!!
法一:
#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;
}