zoukankan      html  css  js  c++  java
  • HDU 1754 I Hate It【线段树 单点更新】

    题意:给出n个数,每次操作修改它的第s个数,询问给定区间的数的最大值

    把前面两道题结合起来就可以了

    自己还是敲不出来-------------

      1 #include<iostream>  
      2 #include<cstdio>  
      3 #include<cstring> 
      4 #include <cmath> 
      5 #include<stack>
      6 #include<vector>
      7 #include<map> 
      8 #include<set>
      9 #include<queue> 
     10 #include<algorithm>  
     11 using namespace std;
     12 
     13 typedef long long LL;
     14 const int INF = (1<<30)-1;
     15 const int mod=1000000007;
     16 const int maxn=200005;
     17 
     18 int n,m;
     19 int a[maxn];
     20 int nmax;
     21 
     22 struct node{
     23     int l,r;
     24     int nmax;
     25 } tree[4*maxn];
     26 
     27 char s[105];
     28 
     29 
     30 void build_tree(int i,int l,int r){
     31     tree[i].l=l;
     32     tree[i].r=r;
     33     if(l==r){
     34         tree[i].nmax=a[l];
     35         return;
     36     }
     37     int mid=(l+r)/2;
     38     build_tree(2*i,l,mid);
     39     build_tree(2*i+1,mid+1,r);
     40     tree[i].nmax=max(tree[2*i].nmax,tree[2*i+1].nmax);
     41 }
     42 
     43 void update(int i,int s,int w){
     44     if(tree[i].l==tree[i].r){
     45         tree[i].nmax=w;
     46         return;
     47     }
     48     
     49       int mid=(tree[i].l + tree[i].r)/2;
     50       if(s<=mid) update(2*i,s,w);
     51       else update(2*i+1,s,w);
     52       
     53      tree[i].nmax=max(tree[2*i].nmax,tree[2*i+1].nmax);
     54 }
     55 
     56 void query(int i,int l,int r){//查询 
     57      if(tree[i].nmax<=nmax) return;
     58      if(tree[i].l==l&&tree[i].r==r){
     59          nmax = max(tree[i].nmax,nmax);
     60          return;
     61      }
     62      int mid=(tree[i].l+tree[i].r)/2;
     63      if(r<=mid) query(2*i,l,r);
     64      else if(l>mid) query(2*i+1,l,r);
     65      else{
     66          query(2*i,l,mid);
     67          query(2*i+1,mid+1,r);
     68      }
     69  }
     70 
     71 
     72 int main(){
     73     while(scanf("%d %d",&n,&m)!=EOF){
     74         memset(tree,0,sizeof(tree));
     75         for(int i=1;i<=n;i++) scanf("%d",&a[i]);
     76         
     77         build_tree(1,1,n);
     78         
     79         while(m--){
     80             scanf("%s",s);
     81             if(s[0]=='Q'){
     82                 int l,r;
     83                 scanf("%d %d",&l,&r);
     84                 
     85                 nmax=-INF;
     86                 query(1,l,r);
     87                 
     88                 printf("%d
    ",nmax);                
     89             }
     90             if(s[0]=='U'){
     91                 int u,v;
     92                 scanf("%d %d",&u,&v);
     93                 update(1,u,v);            
     94             }
     95             
     96         }            
     97     
     98     }
     99     return 0;    
    100 }
    View Code

    加油啊---gooooooooooooooooooooooo

  • 相关阅读:
    LeetCode 842. Split Array into Fibonacci Sequence
    LeetCode 1087. Brace Expansion
    LeetCode 1219. Path with Maximum Gold
    LeetCode 1079. Letter Tile Possibilities
    LeetCode 1049. Last Stone Weight II
    LeetCode 1046. Last Stone Weight
    LeetCode 1139. Largest 1-Bordered Square
    LeetCode 764. Largest Plus Sign
    LeetCode 1105. Filling Bookcase Shelves
    LeetCode 1027. Longest Arithmetic Sequence
  • 原文地址:https://www.cnblogs.com/wuyuewoniu/p/4523286.html
Copyright © 2011-2022 走看看