hdu 1754 I hate it
来源:互联网 发布:java支持linux 编辑:程序博客网 时间:2024/06/06 21:41
这题又学到点东西,宏定义的max跑起来要比自己定义的max函数慢得多,就因为这个,原本应该1A的题贡献2次超时。(大家不信的可以将我代码里的max换成宏,试着提交下,算是给大家提供点经验吧。)
题意:略。
思路:只要把更新换成最大值的便可以了。
#include<cstdio>#include<cstring>#include<iostream>#define L(u) (u<<1)#define R(u) (u<<1|1)using namespace std;const int N = 200001;int arr[N];struct Node{int l,r,ma;}node[N<<2];int max (int a,int b){ return a > b ? a : b;}void pushUp(int u){ node[u].ma = max(node[L(u)].ma , node[R(u)].ma);}void build(int u,int left,int right){ node[u].l = left,node[u].r = right; if(node[u].l==node[u].r) { node[u].ma = arr[left]; return; } int mid = (node[u].l+node[u].r)>>1; build(L(u),left,mid); build(R(u),mid+1,right); node[u].ma = max(node[L(u)].ma , node[R(u)].ma);//因为在上面只给叶子赋值了,所以需要进行向上的回溯}void upDate(int u,int p,int val){ if(node[u].l==node[u].r&&node[u].l==p) { node[u].ma = val; return; } int mid = (node[u].l+node[u].r)>>1; if(p<=mid) { upDate(L(u),p,val); } else { upDate(R(u),p,val); } node[u].ma = max(node[L(u)].ma , node[R(u)].ma);}int query(int u,int left,int right){ if(left<=node[u].l&&node[u].r<=right) return node[u].ma; int mid = (node[u].l+node[u].r)>>1; if(right<=mid) return query(L(u),left,right); else if(left>mid) { return query(R(u),left,right); } else { return max(query(L(u),left,mid),query(R(u),mid+1,right)); }}int main(void){ int n,m; char str[2]; while(cin>>n>>m) { for(int i=1;i<=n;++i) scanf("%d",arr+i); build(1,1,n); while(m--) { int a,b; scanf("%s %d %d",str,&a,&b); if(str[0]=='Q') { printf("%d\n",query(1,a,b)); } else { upDate(1,a,b); } } } return 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
- Wikioi P1099 字串变换
- hdu 1717 小数化分数
- hdu 4003 树形DP
- Linux上程序调试的基石(1)--ptrace
- 【JavaScript等比例缩放图片】
- hdu 1754 I hate it
- java LDAP userPassword
- 关于嵌入式操作系统几个主流方向的比较
- [Python] subprocess module
- iOS 各种传值方式
- 计算机图形学与多媒体 推荐会议和期刊
- Checkbox 对象
- DropDownList的级联绑定省份和城市
- 自己做的笔试题