• HDU 1166.敌兵布阵-完全版线段树(单点增减、区间求和)


    生活艰辛,且行且珍惜。

    先水一篇博客再去补题,要不然又忘记写博客了。

    计划系统的刷一遍线段树专题,自己给自己找虐(自作孽不可活),从基础的到后面的,所有的都挂了题,刷题不,大兄弟?

    线段树可真有意思,先写5道题的题解。

    数据结构,好好刷专题,真的要好好刷专题,因为害怕队友嫌我太菜不要我了(好想哭啊)

    少瓜皮,正经一点,队友就不嫌弃我了。。。

    正题:

    刷线段树主要参考模板是这两篇博客再加自己的习惯:

    传送门:1.完全版线段树2.线段树详解

    HDU1166.敌兵布阵

    基础的单点增减、区间求和操作,直接模板就可以

    代码:

      1 //A
      2 #include<iostream>
      3 #include<cstdio>
      4 #include<cstring>
      5 #include<cmath>
      6 #include<algorithm>
      7 #include<cstdlib>
      8 #include<queue>
      9 #include<stack>
     10 using namespace std;
     11 typedef long long ll;
     12 const int inf=0x3f3f3f3f;
     13 const double eps=1e-5;
     14 const int maxn=5*1e5+10;
     15 #define lson l,m,rt<<1
     16 #define rson m+1,r,rt<<1|1
     17 int sum[maxn<<2],MAX[maxn<<2],col[maxn<<2];
     18 
     19 void PushUp(int rt){
     20     sum[rt]=sum[rt<<1]+sum[rt<<1|1];
     21     //MAX[rt]=max(MAX[rt<<1],MAX[rt<<1|1]);
     22 }
     23 
     24 void build(int l,int r,int rt){
     25     if(l==r){
     26         scanf("%d",&sum[rt]);
     27         //scanf("%d",&MAX[rt]);
     28         return ;
     29     }
     30     int m=(l+r)>>1;
     31     build(lson);
     32     build(rson);
     33     PushUp(rt);
     34 }
     35 
     36 //单点增减
     37 void update(int p,int add,int l,int r,int rt){
     38     if(l==r){
     39         sum[rt]+=add;
     40         return ;
     41     }
     42     int m=(l+r)>>1;
     43     if(p<=m)update(p,add,lson);
     44     else update(p,add,rson);
     45     PushUp(rt);
     46 }
     47 /*
     48 //单点替换
     49 void update(int p,int sc,int l,int r,int rt){
     50     if(l==r){
     51         MAX[rt]=sc;
     52         return ;
     53     }
     54     int m=(l+r)>>1;
     55     if(p<=m)update(p,sc,lson);
     56     else update(p,sc,rson);
     57     PushUp(rt);
     58 }
     59 */
     60 //区间求和
     61 int query(int L,int R,int l,int r,int rt){
     62     if(L<=l&&r<=R){
     63         return sum[rt];
     64     }
     65     int m=(l+r)>>1;
     66     int ret=0;
     67     if(L<=m)ret+=query(L,R,lson);
     68     if(R>m) ret+=query(L,R,rson);
     69     return ret;
     70 }
     71 
     72 /*
     73 //区间最值
     74 int query(int L,int R,int l,int r,int rt){
     75     if(L<=l&&r<=R){
     76         return MAX[rt];
     77     }
     78     int m=(l+r)>>1;
     79     int ret=0;
     80     if(L<=m)ret=max(ret,query(L,R,lson));
     81     if(R>m) ret=max(ret,query(L,R,rson));
     82     return ret;
     83 }
     84 */
     85 
     86 int main(){
     87     int t,n;
     88     scanf("%d",&t);
     89     for(int i=1;i<=t;i++){
     90         scanf("%d",&n);
     91         build(1,n,1);
     92         char s[10];int a,b;
     93         printf("Case %d:
    ",i);
     94         while(~scanf("%s",s)){
     95             if(s[0]=='E')break;
     96             else{
     97                 scanf("%d%d",&a,&b);
     98                 if(s[0]=='Q')printf("%d
    ",query(a,b,1,n,1));
     99                 else if(s[0]=='A')update(a,b,1,n,1);
    100                 else update(a,-b,1,n,1);
    101             }
    102         }
    103     }
    104     return 0;
    105 }
  • 相关阅读:
    最短路径
    图解最小生成树
    图解最小生成树





    线索二叉树
    二叉树
  • 原文地址:https://www.cnblogs.com/ZERO-/p/9291206.html
Copyright © 2020-2023  润新知