zoukankan      html  css  js  c++  java
  • hdu 1754 I Hate It 线段树

    #include <stdio.h>
    #include <string.h>
    #define N 200200
    int max[4*N];
    int n,m;
    int A,B;
    int L,R;
    int MAX(int a,int b)
    {
    	if(a>b) return a;
    	else return b;
    }
    int MIN(int a,int b)
    {
    	if(a<b) return a;
    	else return b;
    }
    
    void update(int o,int l,int r)
    {
    	int m=(l+r)/2;
    	if(l==r) max[o]=B;
    	else 
    	{
    		if(A<=m) update(o*2,l,m);
    		else update(o*2+1,m+1,r);
    		max[o]=MAX(max[o*2+1],max[o*2]);
    	}
    }
    int query(int o,int l,int r)
    {
    	int m=(l+r)/2;
    	int ans=-1;
    	if(L<=l&&R>=r) return max[o];
    	if(L <= m) ans = MAX ( query(2*o,l,m) , ans );
    	if(R > m) ans = MAX ( query(2*o+1,m+1,r) , ans );
    	return ans;
    }
    int main()
    {
    	while(scanf("%d%d",&n,&m)!=EOF)
    	{
    		int i;
    		char c;
    		for(i=1;i<=n;i++)
    		{
    			A=i;
    			scanf("%d",&B);
    			update(1,1,n);
    		}
    		getchar();
    		for(i=1;i<=m;i++)
    		{
    			scanf("%c",&c);
    			if(c=='Q')
    			{
    				scanf("%d%d",&L,&R);
    				printf("%d
    ",query(1,1,n));
    			}
    			else if(c=='U')
    			{
    				scanf("%d%d",&A,&B);
    				update(1,1,n);
    			}
    			getchar();
    		}
    	}
    	return 0;
    }
    
    	
    


     

  • 相关阅读:
    空类型指针实践
    参数作用域实践
    内联函数实践:有疑惑未解决
    可变参数实践
    默认参数实践
    函数指针实践
    Windows下开发环境搭建
    Test
    C++ 左值与右值
    如何打包成多个资源文件
  • 原文地址:https://www.cnblogs.com/vermouth/p/3710176.html
Copyright © 2011-2022 走看看