博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
HDU1754 I hate it 线段树
阅读量:4495 次
发布时间:2019-06-08

本文共 1646 字,大约阅读时间需要 5 分钟。

线段树,单点修改

这道题就是给你一些学生的初始成绩,然后有m个命令,

输入 “Q A B ”的时候,表示这是一条询问操作,它询问ID从A到B(包括A,B)的学生当中,成绩最高的是多少

输入为 “ U A B ”的时候,表示这是一条更新操作,要求把ID为A的学生的成绩更改为B

1 #include
2 #include
3 using namespace std; 4 #define lson l,m,rt<<1 5 #define rson m+1,r,rt<<1|1 6 const int inf=0x3f3f3f3f; 7 const int maxn=200000+10; 8 int _max[maxn<<2]; 9 void pushup(int rt)10 {11 _max[rt]=max(_max[rt<<1],_max[rt<<1|1]);12 }13 void build(int l,int r,int rt)14 {15 if(l==r){16 scanf("%d",&_max[rt]);17 return ;18 }19 int m=(l+r)>>1;20 build(lson);21 build(rson);22 pushup(rt);23 }24 void update(int p,int add,int l,int r,int rt)25 {26 if(l==r){27 _max[rt]=add;28 return ;29 }30 int m=(l+r)>>1;31 if(p<=m)32 update(p,add,lson);33 else34 update(p,add,rson);35 pushup(rt);36 }37 int query(int L,int R,int l,int r,int rt)38 {39 if(L<=l&&R>=r)40 return _max[rt];41 int m=(l+r)>>1;42 int ans=-inf;43 if(L<=m)44 ans=max(ans,query(L,R,lson));45 if(R>m)46 ans=max(ans,query(L,R,rson));47 return ans;48 }49 int main()50 {51 int n,m;52 while(scanf("%d%d",&n,&m)!=EOF){53 build(1,n,1);54 char str[5];55 int i,j;56 while(m--){57 scanf("%s",&str);58 scanf("%d%d",&i,&j);59 if(str[0]=='U'){60 update(i,j,1,n,1);61 }62 else{63 printf("%d\n",query(i,j,1,n,1));64 }65 66 }67 }68 return 0;69 }
View Code

 

转载于:https://www.cnblogs.com/-maybe/p/4319406.html

你可能感兴趣的文章
【转载】通过搜狗站长平台查看网站的搜狗流量及搜索关键字
查看>>
【转载】Visual Studio2017如何打包发布Winform窗体程序
查看>>
【转载】Visual Studio中WinForm窗体程序如何切换.NET Framework版本
查看>>
【转载】Visual Studio2017如何设置打包发布的WinForm应用程序的版本号
查看>>
【转载】通过搜狗站长平台手动向搜狗搜索提交死链
查看>>
【转载】通过搜狗站长平台手动向搜狗搜索提交文章加快收录
查看>>
【转载】通过百度站长平台提交网站死链
查看>>
【转载】通过搜狗站长平台提交网站域名变更后的文章地址
查看>>
【转载】Visual Studio2017中如何设置解决方案中的某个项目为启动项目
查看>>
Axios跨域实例
查看>>
ubuntu下安装pyaudio
查看>>
单片机 电子电路 嵌入式 毕设 课设 私活 代做
查看>>
notepad++ 安装 hex_editor 十六进制查看插件
查看>>
excel VBA 编程
查看>>
正则表达式
查看>>
Date类
查看>>
基本类型的数值转换
查看>>
集合、泛型、增强for
查看>>
Public Key Retrieval is not allowed错误
查看>>
Unable to load authentication plugin 'caching_sha2_password'.错误
查看>>