Hdu 1754 I Hate It 线段树 解题报告
来源:互联网 发布:发哥 知乎 编辑:程序博客网 时间:2024/06/05 13:35
Problem Description
很多学校流行一种比较的习惯。老师们很喜欢询问,从某某到某某当中,分数最高的是多少。
这让很多学生很反感。
不管你喜不喜欢,现在需要你做的是,就是按照老师的要求,写一个程序,模拟老师的询问。当然,老师有时候需要更新某位同学的成绩。
Input
本题目包含多组测试,请处理到文件结束。
在每个测试的第一行,有两个正整数 N 和 M ( 0
Output
对于每一次询问操作,在一行里面输出最高成绩。
Sample Input
5 6
1 2 3 4 5
Q 1 5
U 3 6
Q 3 4
Q 4 5
U 2 9
Q 1 5
Sample Output
5
6
5
9
思路
很裸的线段树,但是好像卡时间很坑,一直TLE。。。又不擅长卡常数。。。
好像ZKW线段树可以吧。。
传说中的ZKW线段树
代码
#include<cstring>#include<cstdio>#include<algorithm>#include<iostream>#include<cmath>using namespace std;const int INF=0x3f3f3f3f;const int N=200000+5; int m,n,a[N];inline int read(){ int x=0,f=1;char ch=getchar(); while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();} while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();} return x*f;}struct node{ int vmax; node *lson,*rson;};struct node dizhi[N],*tail=dizhi,*root;int max(int x,int y){ if (x>=y) return x; else return y;}int min(int x,int y){ if (x<=y) return x; else return y;}node *build(int lf,int rt){ node *nd=++tail; if (lf==rt) nd->vmax=a[lf]; else { int mid=(lf+rt)/2; nd->lson=build(lf,mid); nd->rson=build(mid+1,rt); nd->vmax=max(nd->lson->vmax,nd->rson->vmax); } return nd;}void modify(node *nd,int lf,int rg,int pos,int val ){ if (lf==rg) {nd->vmax=val;return ;} int mid=(lf+rg)>>1; if (pos<=mid) modify(nd->lson,lf,mid,pos,val); else modify(nd->rson,mid+1,rg,pos,val); nd->vmax=max(nd->lson->vmax,nd->rson->vmax);}int query(node *nd,int lf,int rg,int L,int R){ if (L<=lf&&R>=rg) return nd->vmax; int mid=(lf+rg)>>1; int res=-INF; if (L<=mid) res=max(res,query(nd->lson,lf,mid,L,R)); if (R>mid) res=max(res,query(nd->rson,mid+1,rg,L,R)); return res;}int main(){ n=read();m=read(); for (register int i=1;i<=n;i++) a[i]=read(); root=build(1,n); for (int i=1;i<=m;i++) { char s[100]; scanf("%s",s); if (s[0]=='U') { int a,b; a=read();b=read(); modify(root,1,n,a,b); } else { int a,b; a=read();b=read(); printf("%d\n",query(root,1,n,a,b)); } } return 0;}
阅读全文
1 0
- Hdu 1754 I Hate It 线段树 解题报告
- hdu 1754 解题报告 I Hate It
- 【解题报告】 HDU 1754 I Hate It 线段树 单点更新
- hdu 1754 I Hate It 解题报告(线段树 代码+注释)
- HDU1754 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 线段树
- POJ 1611 The Suspects
- 电机控制学习
- java爬虫
- 递归神经网络预测股票好文章
- COWCHECKS
- Hdu 1754 I Hate It 线段树 解题报告
- 如何给随机数加密
- Java中的第一个程序
- 在安卓上运行TensorFlow
- POJ 2763 Housewife Wind (LCA+线段树)
- CentOS 上 Jenkins 安装
- JSP 测试使用 Junit 比较方便
- 1044. Shopping in Mars (25)
- 153. Find Minimum in Rotated Sorted Array--在旋转数组中找最小值