• 线段树练习题


    1.洛谷1531

     点修改,查询区间最大值

     1 #include<cstdio>
     2 #include<iostream>
     3 #include<algorithm>
     4 #define ls (cur<<1)
     5 #define rs (cur<<1|1)
     6 #define mid ((a[cur].l+a[cur].r)>>1)
     7 #define len(x) (a[x].r-a[x].l+1)
     8 using namespace std;
     9 int n,m,k,x,y;
    10 struct tree{int l,r,max;}a[800010];
    11 void read(int &k){
    12     k=0; int f=1; char c=getchar();
    13     while(c<'0'||c>'9')c=='-'&&(f=-1),c=getchar();
    14     while('0'<=c&&c<='9')k=k*10+c-'0',c=getchar();
    15     k*=f;
    16 }
    17 void pushup(int cur){a[cur].max=max(a[ls].max,a[rs].max);}
    18 void build(int cur,int l,int r){
    19     a[cur].l=l; a[cur].r=r;
    20     if(l<r) build(ls,l,mid),build(rs,mid+1,r),pushup(cur); 
    21     else read(a[cur].max);    
    22 }
    23 void update(int cur,int pos,int x){
    24     if(len(cur)==1&&a[cur].l==pos) a[cur].max=max(a[cur].max,x);
    25     else update(pos<=mid?ls:rs,pos,x),pushup(cur);
    26 }
    27 int query(int cur,int l,int r){
    28     if(l<=a[cur].l&&a[cur].r<=r) return a[cur].max;
    29     int ret=-0X7f7f7f;
    30     if(l<=mid) ret=query(ls,l,r);
    31     if(r>mid) ret=max(ret,query(rs,l,r));
    32     return ret;
    33 }
    34 int main(){
    35     read(n); read(m); build(1,1,n); 
    36     for (int i=1;i<=m;i++){
    37         char c=getchar();
    38         while (c!='Q'&&c!='U') c=getchar();
    39         read(x); read(y);
    40         if(c=='Q') printf("%d
    ",query(1,x,y));
    41         else update(1,x,y);
    42     }
    43     return 0;
    44 }
    View Code

    2.BZOJ 1012最大数Maxnumber

     1 #include<cstdio>
     2 #include<algorithm>
     3 #define ls (cur<<1)
     4 #define rs (cur<<1|1)
     5 #define mid ((a[cur].l+a[cur].r)>>1)
     6 using namespace std;
     7 const int maxn=800010;
     8 long long n,m,x,y,z,k,d;
     9 struct tree{
    10     int l,r;
    11     long long max;
    12 }a[maxn];
    13 void read(long long &k){
    14     k=0; int f=1; char c=getchar();
    15     while (c<'0'||c>'9')c=='-'&&(f=-1),c=getchar();
    16     while ('0'<=c&&c<='9')k=k*10+c-'0',c=getchar();
    17     k*=f;
    18 }
    19 void build(int cur,int l,int r){
    20     a[cur].l=l; a[cur].r=r;
    21     if (l<r){
    22         build(ls,l,mid);
    23         build(rs,mid+1,r);
    24     }
    25 }
    26 void add(int cur,long long pos,long long del){
    27     if (a[cur].l==pos&&a[cur].r==pos) a[cur].max=(a[cur].max+del)%d;
    28     else{
    29         if (pos<=mid) add(ls,pos,del);
    30         else add(rs,pos,del);
    31         a[cur].max=max(a[ls].max,a[rs].max);
    32     }
    33 }
    34 long long query(int cur,int l,int r){
    35     if (l<=a[cur].l&&a[cur].r<=r) return a[cur].max;
    36     else{
    37         long long ret=-0X7f7f7f7f;
    38         if (l<=mid) ret=max(ret,query(ls,l,r));
    39         if (r>mid) ret=max(ret,query(rs,l,r));
    40         return ret;
    41     }
    42 }
    43 int main(){
    44     read(m);
    45     build(1,1,m);
    46     read(d);
    47     long long t=0;
    48     for (int i=1;i<=m;i++){
    49         char c=getchar();
    50         if (c=='A'){
    51             n++;
    52             read(x);
    53             add(1,n,x+t);
    54         }
    55         else{
    56             read(x);
    57             t=query(1,n-x+1,n);
    58             printf("%lld
    ",t);
    59         }
    60     }
    61     return 0;
    62 }
    View Code
  • 相关阅读:
    MyEclipse 自带的TomCat 新增部署的时候不显示 Deploy Location
    No prohects are avaliable for deployment to this server
    Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/logging/LogFactory
    Dom4j 对XMl解析 新手学习,欢迎高手指正
    电脑的技巧
    Browserify的基本使用
    bower的基本使用
    前端工程化--前端工程化技术栈
    前端工程化--架构说明
    前端工程化-前端工程化说明
  • 原文地址:https://www.cnblogs.com/DriverLao/p/7944159.html
Copyright © 2020-2023  润新知