【模板】线段树单点修改
2017-09-14 14:55
369 查看
基本介绍
模板题目
代码实现
给出N个数,两种操作:
1、C x y:修改第x个数的值为y;
2、P x y:求第x到第y个的最大值,注:x未必比y小
输入输出格式
输入格式:
第一行输入N和M,N表示有N个数(N<=200000,M<5000),M表示有M个操作
下来N个数
然后是M个操作。
输出格式:
遇到P操作的时候,输出结果。
输入输出样例
输入样例:
5 6
1 2 3 4 5
P 1 5
C 3 6
P 3 4
P 4 5
C 2 9
P 1 5
输出样例:
5
6
5
9
模板题目
代码实现
基本介绍
在求区间最值的基础上加了一个单点修改 也就是下面代码中的update函数 主要通过不断二分区间往下找左右子区间 直到一个子区间只包括一个节点 直接改变这个节点的值并改变所有与这个点相关的父亲节点 (摘自战友Jiang.S博客)模板题目
题目描述给出N个数,两种操作:
1、C x y:修改第x个数的值为y;
2、P x y:求第x到第y个的最大值,注:x未必比y小
输入输出格式
输入格式:
第一行输入N和M,N表示有N个数(N<=200000,M<5000),M表示有M个操作
下来N个数
然后是M个操作。
输出格式:
遇到P操作的时候,输出结果。
输入输出样例
输入样例:
5 6
1 2 3 4 5
P 1 5
C 3 6
P 3 4
P 4 5
C 2 9
P 1 5
输出样例:
5
6
5
9
代码实现
#include<iostream> #include<cstdio> #include<cctype> #include<cstring> #include<string> using namespace std; #define in = read() typedef long long ll; const ll size = 1000000 + 10000; #define left (rt<<1) #define right (rt<<1|1) #define mid ((l + r)>>1) #define lson l,mid,left #define rson mid + 1,r,right #define len (r - l + 1) ll n,m; ll tree[size]; inline ll read(){ ll num = 0 , f = 1; char ch = getchar(); while(!isdigit(ch)){ if(ch == '-') f = -1; ch = getchar(); } while(isdigit(ch)){ num = num*10 + ch - '0'; ch = getchar(); } return num*f; } inline void pushup1(ll rt){ tree[rt] = max(tree[left] , tree[right]);} void buildtree(ll l, ll r, ll rt){ if(l == r){ tree[rt] in; return;} buildtree(lson); buildtree(rson); pushup1(rt); } ll query(ll from,ll to,ll l,ll r,ll rt){ if(from <= l && to >= r) return tree[rt]; ll ans = 0; if(from <= mid) ans = max(ans , query(from,to,lson)); if(to > mid) ans = max(ans , query(from,to,rson)); return ans; } void update(ll num,ll dis,ll l,ll r,ll rt){ if(l == r){ tree[rt] = dis; return;} if(num <= mid) update(num,dis,lson); else update(num,dis,rson); pushup1(rt); } int main(){ n in; m in; buildtree(1,n,1); for(int i=1;i<=m;i++){ string s; cin>>s; ll x,y; x in; y in; if(s[0] == 'P'){ if(x <= y) printf("%lld\n",query(x,y,1,n,1)); if(y < x) printf("%lld\n",query(y,x,1,n,1)); } else if(s[0] == 'C') update(x,y,1,n,1); } } //COYG
相关文章推荐
- I Hate It HDU - 1754(线段树单点修改,区间求最大值模板)
- HDU-1754 I Hate It (线段树裸题 splay模板 单点修改 区间询问最大值)
- hdu 1166 线段树 单点修改 + 询问区间求和 (线段树模板)
- 敌兵布阵 HDU - 1166 (线段树单点修改区间求和模板)
- hdu1166 敌兵布阵+线段树模板题+单点修改
- HDU3974 线段树区间修改单点查询+树上dfs序
- 线段树 --- 单点修改 以及 区间更新中的单点更新
- hihoCoder 1077 RMQ问题再临-线段树 单点修改,求区间最小值
- HDU - 1754 I Hate It(线段树,单点修改)
- Lightoj 1348 Aladdin and the Return Journey (树链剖分)(线段树单点修改区间求和)
- HDU 1754 B I Hate It 线段树 单点更新 区间最大值 模板
- 线段树(单点修改,区间查询)
- 【模板】线段树_区间最值、区间求和、修改
- 线段树大模板(区间更新,单点更新,查询区间最值等等)
- 【codevs 1080】线段树练习(单点修改+区间和)
- POJ - 2155 Matrix (二维树状数组 + 区间修改 + 单点求值 或者 二维线段树 + 区间更新 + 单点求值)
- hdu4893 Wow! Such Sequence!,树状数组,线段树,单点修改,区间更新
- BZOJ 1036 [ZJOI2008]树的统计Count (树链剖分)(线段树单点修改)
- [模板]-线段树-区间修改 + 区间查询
- 操作格子 (线段树)(单点修改,区间求最值,区间求和)