zoukankan      html  css  js  c++  java
  • HDU 1754

    线段树入门题,找下手感

    #include <cstdio>
    #include <iostream>
    #include <algorithm>
    #include <cstring>
    #include <queue>
    using namespace std;
    
    const int maxn= 2e5+5;
    const int maxm= 5e3+5;
    const int maxl= maxn<<2;
    
    int tree[maxl], ar[maxn];
    int a, b;
    
    inline LChild(int x)
    {
    	return x<<1;
    }
    inline RChild(int x)
    {
    	return x<<1|1;
    }
    void Build(int rt, int l, int r)
    {
    	if (l== r){
    		tree[rt]= ar[l];
    		return;
    	}
    	int lc= LChild(rt), rc= RChild(rt);
    	int mid= (l+r)>>1;
    	Build(lc, l, mid);
    	Build(rc, mid+1, r);
    
    	tree[rt]= tree[lc] > tree[rc] ? tree[lc] : tree[rc];
    }
    void Update(int rt, int l, int r)
    {
    	if (l== r){
    		tree[rt]= ar[l];
    		return;
    	}
    
    	int lc= LChild(rt), rc= RChild(rt);
    	int md= (l+r)>>1;
    	if (l<= a && a<= md){
    		Update(lc, l, md);
    	}
    	if (md+1<= a && a<= r){
    		Update(rc, md+1, r);
    	}
    
    	tree[rt]= tree[lc] > tree[rc] ? tree[lc] : tree[rc];
    }
    int Query(int rt, int l, int r)
    {
    	if (a<= l && r<= b){
    		return tree[rt];
    	}
    
    	int md= (l+r)>>1, ans= -1, t= -1;
    	if (a<= md){
    		t= Query(LChild(rt), l, md);
    		if (t> ans){
    			ans= t;
    		}
    	}
    	if (md+1<= b){
    		t= Query(RChild(rt), md+1, r);
    		if (t> ans){
    			ans= t;
    		}
    	}
    
    	return ans;
    }
    
    int main(int argc, char const *argv[])
    {
    	int n, m;
    	char op;
    
    	while (EOF!= scanf("%d %d", &n, &m)){
    		for(int i= 1; i<= n; ++i){
    			scanf("%d", ar+i);
    		}
    		memset(tree, -1, sizeof(tree));
    		Build(1, 1, n);
    
    		while (m--){
    			scanf(" %c", &op);
    			if ('U'== op){
    				scanf("%d %d", &a, &b);
    				ar[a]= b;
    				Update(1, 1, n);
    			}
    			else{
    				scanf("%d %d", &a, &b);
    				cout<<Query(1, 1, n)<<endl;
    			}
    		}
    	}
    
    	return 0;
    }
    
  • 相关阅读:
    react-router刷新页面Cannot GET 问题
    react学习之弹出层
    react学习之js-xlsx导入和导出excel表格
    c#串口通信并处理接收的多个参数
    react与微信小程序
    promise知识点小结
    汇编命令小记
    Firebase-config 在android中的使用
    python:html元素解析
    Toast实现源码解析
  • 原文地址:https://www.cnblogs.com/Idi0t-N3/p/13428460.html
Copyright © 2011-2022 走看看