HDU 1754 I Hate It
来源:互联网 发布:淘宝限时抢购怎么弄 编辑:程序博客网 时间:2024/06/07 04:48
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1754
题意:给出n个数字m次操作,操作分为2种,更新某一个数的大小和询问某一区间的最大值
思路:很经典的线段树,单点更新以及查询区间,刚学习线段树按照别人的思路已经写过一次了,这次写的应该更贴近自己的想法
#include <iostream>#include <cstdio>#include <algorithm>#include <cstring>#define Maxn 200030using namespace std;int s[Maxn];char op[10];struct Tree{ int l,r,date;}tree[Maxn*3];void build(int root,int l,int r){ tree[root].l=l; tree[root].r=r; if (l==r) { tree[root].date=s[l]; return; } int mid=(l+r)>>1; build(root<<1,l,mid); build(root<<1|1,mid+1,r); tree[root].date=max(tree[root<<1].date,tree[root<<1|1].date);}void update(int root,int tar,int val){ if (tar>=tree[root].l && tar<=tree[root].r) { if (tree[root].l==tree[root].r) { tree[root].date=val; return; } int mid=(tree[root].l+tree[root].r)>>1; update(root<<1,tar,val); update(root<<1|1,tar,val); tree[root].date=max(tree[root<<1].date,tree[root<<1|1].date); } return;}int que(int root,int l,int r){ int mid=(tree[root].l+tree[root].r)>>1; if (l==tree[root].l && r==tree[root].r) return tree[root].date; else if (r<=mid) return que(root<<1,l,r); else if (l>mid) return que(root<<1|1,l,r); else return max(que(root<<1,l,mid),que(root<<1|1,mid+1,r));}int main(){ int n,m; while (scanf("%d%d",&n,&m)!=EOF) { for (int i=1;i<=n;i++) { scanf("%d",&s[i]); } build(1,1,n); for (int i=0;i<m;i++) { int a,b; scanf("%s%d%d",&op,&a,&b); if (op[0]=='Q') { printf("%d\n",que(1,a,b)); } else { update(1,a,b); } } }}
0 0
- hdu 1754 I Hate It
- HDU 1754 I Hate It
- hdu 1754 I Hate It
- HDU 1754 I Hate It
- hdu 1754 I Hate It
- HDU 1754 I Hate It .
- HDU 1754 i hate it
- hdu 1754 I Hate It
- hdu 1754 I Hate It
- hdu 1754 I Hate It
- HDU 1754(I Hate It )
- hdu 1754 I Hate It
- hdu 1754 I Hate It
- HDU-1754-I Hate It
- hdu 1754 I Hate It
- hdu 1754 I Hate It
- hdu 1754 I hate it
- HDU 1754 I Hate It
- Linux Kernel boot sequence
- exfat文件系统(六)------exfat_get_block详解
- Android Preference的使用
- GDI绘制图形
- 美团外卖支付过程分析
- HDU 1754 I Hate It
- 需要掌握的知识点
- WPF 自定义等待对话框、多线程等待的问题
- poj 3469 Dual Core CPU (最大流)
- Windows+Nginx+IIS做图片分布式存储详细步骤
- 出现error LNK2019: 无法解析的外部符号错误的诊断
- Android开发-对bitmap的一些的操作
- android 自定义View SpinnerLoader使用解析,让你摆脱系统难看的进度条
- CentOS7 运行Python Anaconda