zoukankan      html  css  js  c++  java
  • hdu 1754

    在写的时候加了判断询问位置先后的swap结果wa了快十次了,不知道为什么;

    http://acm.hdu.edu.cn/showproblem.php?pid=1754

    #include<iostream>
    #include<cstring>
    #include<algorithm>
    #include<cstdio>
    using namespace std;
    typedef long long ll;
    const int M=2e5+5;
    const int inf=0x3f3f3f3f;
    int tree[M<<2];
    int ma(int a,int b){
    	return a>b?a:b;
    }
    
    void up(int root){
    	tree[root]=ma(tree[root<<1],tree[root<<1|1]);
    }
    void build(int root,int l,int r){
    	if(l==r){
    		scanf("%d",&tree[root]);
    		return ;
    	}
    	int midd=(l+r)>>1;
    	build(root<<1,l,midd);
    	build(root<<1|1,midd+1,r);
    	up(root);
    }
    void update(int p,int x,int root,int l,int r){
    	if(l==r){
    		tree[root]=x;
    		return ;
    	}
    	int midd=(l+r)>>1;
    	if(p<=midd)
    		update(p,x,root<<1,l,midd);
    	else
    		update(p,x,root<<1|1,midd+1,r);
    	up(root);
    }
    int query(int L,int R,int root,int l,int r){
    	if(L<=l&&r<=R){
    		return tree[root];
    	}
    	int midd=(l+r)>>1;
    	int ans=0;
    	if(L<=midd)
    		ans=max(ans,query(L,R,root<<1,l,midd));
    	if(R>midd)
    		ans=max(ans,query(L,R,root<<1|1,midd+1,r));
    	return ans;
    }
    char s[2];
    int main(){
    	int m,n;
    	while(scanf("%d%d",&n,&m)!=EOF){
    		memset(tree,0,sizeof(tree));
    		build(1,1,n);
    		while(m--){
    			int x,y;
    			scanf("%s%d%d",s,&x,&y);
    
    			if(s[0]=='Q'){
    				printf("%d
    ",query(x,y,1,1,n));
    			}
    			else
    				update(x,y,1,1,n);
    		}
    		 
    	}
    	return 0;
    }
    

      

  • 相关阅读:
    leetcode 刷题日志 2018-03-26
    WebForms UnobtrusiveValidationMode 需要“jquery”ScriptResourceMapping
    sublime wrong
    SSM框架使用-wrong
    C++设计实现算法时易犯错误
    CodeBlocks wrong
    leetcode 刷题日志 2018-3-28
    CountDownLatch
    类加载器和双亲委派
    GC的一个面试题
  • 原文地址:https://www.cnblogs.com/starve/p/10822080.html
Copyright © 2011-2022 走看看