codeforces 315 B.Sereja and Array(线段树区间更新+单点更新+单点询问)
来源:互联网 发布:微信群一键加好友软件 编辑:程序博客网 时间:2024/06/05 03:17
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 nspace-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 111 2 3 4 5 6 7 8 9 103 23 92 103 13 101 1 102 102 103 13 103 9
291120304039
题解:
题意:
操作1 x y表示将a[x]=y
操作2 x 表示全体数列值加上x
操作3 x 表示询问a[x]处的值
思路:
比较裸的一道线段树的题,然后这里的区间更新因为是全体更新所以可以偷懒
代码:
#include<stdio.h>#include<string.h>#include<stdlib.h>#include<queue>#include<stack>#include<math.h>#include<vector>#include<map>#include<set>#include<stdlib.h>#include<cmath>#include<string>#include<algorithm>#include<iostream>using namespace std;#define lson k*2#define rson k*2+1#define M (t[k].l+t[k].r)/2struct node{ int v; int tag; int l,r;}t[100005*4];void pushdown(int k){ if(t[k].tag) { t[lson].tag+=t[k].tag; t[rson].tag+=t[k].tag; if(t[lson].l==t[lson].r) t[lson].v+=t[k].tag; if(t[rson].l==t[rson].r) t[rson].v+=t[k].tag; t[k].tag=0; }}void Build(int l,int r,int k){ t[k].l=l; t[k].r=r; t[k].tag=0; if(l==r) { scanf("%d",&t[k].v); return; } int mid=M; Build(l,mid,lson); Build(mid+1,r,rson);}void update(int k,int v){ t[k].v+=v; t[k].tag+=v;}void change(int x,int v,int k){ if(t[k].l==x&&t[k].r==x) { t[k].v=v; return; } pushdown(k); int mid=M; if(x<=mid) change(x,v,lson); else change(x,v,rson);}int query(int x,int k){ if(t[k].l==x&&t[k].r==x) { return t[k].v; } pushdown(k); int mid=M; if(x<=mid) return query(x,lson); else return query(x,rson);}int main(){ int i,j,n,m,d,x,y; scanf("%d%d",&n,&m); Build(1,n,1); while(m--) { scanf("%d",&d); if(d==1) { scanf("%d%d",&x,&y); change(x,y,1); } else if(d==2) { scanf("%d",&x); update(1,x); } else { scanf("%d",&x); printf("%d\n",query(x,1)); } } return 0;}
- codeforces 315 B.Sereja and Array(线段树区间更新+单点更新+单点询问)
- codeforces 315B Sereja and Array(单点线段树)
- Codeforces 525B. Pasha and String【线段树 区间更新 单点查询】
- Codeforces 295A Greg and Array 线段树区间累加,单点询问
- codeforces 438D The Child and Sequence(线段树:单点更新+区间取模+区间和)
- ZOJ 3453 Doraemon's Sweet Bullet(线段树区间更新+单点更新+最值询问)
- 线段树(单点更新+区间更新)
- [HDU 5316] Magician (线段树+单点更新+区间询问+区间合并)
- HDU 1754 I Hate It(简单线段树-单点更新,区间询问)
- HDU 1754 I Hate It (线段树+单点更新+区间询问)
- CodeForce 356A Knight Tournament(线段树的区间更新+单点询问)
- CodeForces 332B Maximum Absurdity(线段树单点更新)
- codeforce 315B Sereja and Array 【水题,封顶距离统计 OR 线段树延迟更新】
- zoj (单点更新区间查询:线段树)
- 线段树求区间和(单点更新)
- 线段树单点更新和区间查询
- 线段树(单点更新,区间求和)
- hdu1166(线段树单点更新区间查询)
- 数据结构 队列入队,出队编程的例子
- 两个整数排序
- 深度强化学习与自适应在线学习的阿里实践
- fgetc统计文件的字符个数
- pip安装和使用
- codeforces 315 B.Sereja and Array(线段树区间更新+单点更新+单点询问)
- c语言的日志库
- 三个整数排序
- CorelDRAW对文字处理完美程度没有其二
- Redis常用命令
- Eclipse for MapReduce
- 持有对象
- SQL (student-score表)源代码
- JAVA系统环境相关