zoukankan      html  css  js  c++  java
  • 线段树专题

     

    HDU 1166 敌兵布阵 (单点更新,区间求和)

    Problem Description

     

    C国的死对头A国这段时间正在进行军事演习,所以C国间谍头子Derek和他手下Tidy又开始忙乎了。A国在海岸线沿直线布置了N个工兵营地,Derek和Tidy的任务就是要监视这些工兵营地的活动情况。由于采取了某种先进的监测手段,所以每个工兵营地的人数C国都掌握的一清二楚,每个工兵营地的人数都有可能发生变动,可能增加或减少若干人手,但这些都逃不过C国的监视。
    中央情报局要研究敌人究竟演习什么战术,所以Tidy要随时向Derek汇报某一段连续的工兵营地一共有多少人,例如Derek问:“Tidy,马上汇报第3个营地到第10个营地共有多少人!”Tidy就要马上开始计算这一段的总人数并汇报。但敌兵营地的人数经常变动,而Derek每次询问的段都不一样,所以Tidy不得不每次都一个一个营地的去数,很快就精疲力尽了,Derek对Tidy的计算速度越来越不满:"你个死肥仔,算得这么慢,我炒你鱿鱼!”Tidy想:“你自己来算算看,这可真是一项累人的工作!我恨不得你炒我鱿鱼呢!”无奈之下,Tidy只好打电话向计算机专家Windbreaker求救,Windbreaker说:“死肥仔,叫你平时做多点acm题和看多点算法书,现在尝到苦果了吧!”Tidy说:"我知错了。。。"但Windbreaker已经挂掉电话了。Tidy很苦恼,这么算他真的会崩溃的,聪明的读者,你能写个程序帮他完成这项工作吗?不过如果你的程序效率不够高的话,Tidy还是会受到Derek的责骂的.

     

     


    Input

     

    第一行一个整数T,表示有T组数据。
    每组数据第一行一个正整数N(N<=50000),表示敌人有N个工兵营地,接下来有N个正整数,第i个正整数ai代表第i个工兵营地里开始时有ai个人(1<=ai<=50)。
    接下来每行有一条命令,命令有4种形式:
    (1) Add i j,i和j为正整数,表示第i个营地增加j个人(j不超过30)
    (2)Sub i j ,i和j为正整数,表示第i个营地减少j个人(j不超过30);
    (3)Query i j ,i和j为正整数,i<=j,表示询问第i到第j个营地的总人数;
    (4)End 表示结束,这条命令在每组数据最后出现;
    每组数据最多有40000条命令

     

     


    Output

     

    对第i组数据,首先输出“Case i:”和回车,
    对于每个Query询问,输出一个整数并回车,表示询问的段中的总人数,这个数保持在int以内。

     

     


    Sample Input

     

    1 10 1 2 3 4 5 6 7 8 9 10 Query 1 3 Add 3 6 Query 2 7 Sub 10 2 Add 6 3 Query 3 10 End

     

     


    Sample Output

     

    Case 1: 6 33 59

     

     
    const int Max_N = 50100 ;
    int sum[Max_N<<2] , x[Max_N] ;
    
    void push_up(int root){
         sum[root] = sum[root<<1] + sum[root<<1|1] ;
    }
    
    void make_tree(int L , int R , int root){
         if(L == R){
            sum[root] = x[L] ;
            return  ;
         }
         int mid = (L + R) >> 1 ;
         make_tree(L , mid , root<<1) ;
         make_tree(mid+1 , R , root<<1|1) ;
         push_up(root) ;
    }
    
    void update(int id , int c , int L , int R , int root){
         if(L == R){
            sum[root] += c ;
            return ;
         }
         int mid = (L + R) >> 1 ;
         if(id <= mid)
            update(id , c , L , mid ,root<<1) ;
         else
            update(id , c , mid+1 , R , root<<1|1) ;
         push_up(root) ;
    }
    
    int query(int l , int r , int L , int R ,int root){
        if(l <= L && R <= r)
           return sum[root] ;
        int mid = (L + R) >> 1 ;
        int ans = 0 ;
        if(l <= mid)
           ans += query(l , r , L , mid , root<<1) ;
        if(r > mid)
           ans += query(l , r , mid+1 , R , root<<1|1) ;
        return ans ;
    }
    
    int main(){
        int T , n , i , a , b ;
        char str[8] ;
        cin>>T ;
        for(int cas = 1 ; cas <= T ; cas++){
            printf("Case %d:
    ",cas) ;
            scanf("%d",&n) ;
            for(i = 1 ; i <= n ; i++)
                scanf("%d",&x[i]) ;
            make_tree(1,n,1) ;
            while(scanf("%s",str) && strcmp(str,"End")!=0){
                scanf("%d%d",&a,&b) ;
                if(str[0] == 'A')
                   update(a , b , 1 , n , 1) ;
                else if(str[0] == 'S')
                   update(a , -b , 1 , n , 1) ;
                else
                   printf("%d
    ",query(a , b , 1 , n , 1)) ;
            }
        }
        return 0 ;
    }
    

      

    HDU 1394 Minimum Inversion Number (逆序对 , 单点更新,区间求和)

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)

    Problem Description
    The inversion number of a given number sequence a1, a2, ..., an is the number of pairs (ai, aj) that satisfy i < j and ai > aj.

    For a given sequence of numbers a1, a2, ..., an, if we move the first m >= 0 numbers to the end of the seqence, we will obtain another sequence. There are totally n such sequences as the following:

    a1, a2, ..., an-1, an (where m = 0 - the initial seqence)
    a2, a3, ..., an, a1 (where m = 1)
    a3, a4, ..., an, a1, a2 (where m = 2)
    ...
    an, a1, a2, ..., an-1 (where m = n-1)

    You are asked to write a program to find the minimum inversion number out of the above sequences.
     

     

    Input
    The input consists of a number of test cases. Each case consists of two lines: the first line contains a positive integer n (n <= 5000); the next line contains a permutation of the n integers from 0 to n-1.
     

     

    Output
    For each case, output the minimum inversion number on a single line.
     

     

    Sample Input
    10
    1 3 6 9 0 8 5 7 4 2
    Sample Output
    16
     
    const int Max_N = 5008  ;
    int   Sum[Max_N<<1] ,x[Max_N];
    
    void  push_up(int root){
          Sum[root] = Sum[root<<1] + Sum[root<<1|1] ;
    }
    
    void  make_tree(int L , int R , int root){
          if(L == R){
               Sum[root] = 0 ;
               return ;
          }
          int mid = (L + R) >> 1 ;
          make_tree(L , mid , root<<1) ;
          make_tree(mid+1 , R , root<<1|1) ;
          push_up(root) ;
    }
    
    void  update(int id , int L , int R , int root){
          if(L == R){
               Sum[root]++ ;
               return ;
          }
          int mid = (L + R) >> 1 ;
          if(id <= mid)
             update(id , L , mid , root<<1) ;
          else
             update(id , mid+1 , R , root<<1|1) ;
          push_up(root) ;
    }
    
    int   query(int l , int r , int L , int R , int root){
          if(l <= L && R <= r)
             return Sum[root] ;
          int mid = (L + R) >> 1 ;
          int ans = 0 ;
          if(l <= mid)
              ans += query(l , r , L , mid , root<<1) ;
          if(r > mid)
              ans += query(l , r , mid+1 , R , root<<1|1) ;
          return ans ;
    }
    
    int main(){
        int n , i  , sum  , ans ;
        while(cin>>n){
            make_tree(1 , n , 1) ;  //建一颗空树
            sum = 0 ;
            for(i = 1 ; i <= n ; i++){
                scanf("%d",&x[i]) ;
                x[i]++ ;          // 序列变为[1,n]
                sum += query(x[i] , n , 1 , n , 1) ; //  [x[i] , n]出现的个数之和 , 也就是在大于x[i],且位置在起左边的个数 
                update(x[i] , 1 , n , 1) ;  //在x[i]位置插入1  
            }
            ans = sum ;   // sum即为初始序列逆序数
            for(i = 1 ; i < n ; i++){
                // 将想x[i]移到尾巴。逆序数变化情况。 x[i]在头少了(x[i] -1 ) ,x[i] 在尾多了(n-x[i])个 ,总计多了: +(n-x[i])-(x[i]-1)
                sum = sum + n - x[i] - x[i] + 1 ;  
                ans = min(ans ,sum) ;
            }
            printf("%d
    ",ans) ;
        }
        return 0 ;
    }
    

      

    HDU 1754 I Hate It (单点更新,区间最值)

    Time Limit: 9000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)


    Problem Description
    很多学校流行一种比较的习惯。老师们很喜欢询问,从某某到某某当中,分数最高的是多少。
    这让很多学生很反感。

    不管你喜不喜欢,现在需要你做的是,就是按照老师的要求,写一个程序,模拟老师的询问。当然,老师有时候需要更新某位同学的成绩。
     

     

    Input
    本题目包含多组测试,请处理到文件结束。
    在每个测试的第一行,有两个正整数 N 和 M ( 0<N<=200000,0<M<5000 ),分别代表学生的数目和操作的数目。
    学生ID编号分别从1编到N。
    第二行包含N个整数,代表这N个学生的初始成绩,其中第i个数代表ID为i的学生的成绩。
    接下来有M行。每一行有一个字符 C (只取'Q'或'U') ,和两个正整数A,B。
    当C为'Q'的时候,表示这是一条询问操作,它询问ID从A到B(包括A,B)的学生当中,成绩最高的是多少。
    当C为'U'的时候,表示这是一条更新操作,要求把ID为A的学生的成绩更改为B。
     

     

    Output
    对于每一次询问操作,在一行里面输出最高成绩。
     

     

    Sample Input
    5 6 1 2 3 4 5 Q 1 5 U 3 6 Q 3 4 Q 4 5 U 2 9 Q 1 5
     

     

    Sample Output
    5 6 5 9
    const int Max_N = 200100 ;
    int Max[Max_N<<2] , x[Max_N] ;
    
    void push_up(int root){
         Max[root] = max(Max[root<<1] , Max[root<<1|1]) ;
    }
    
    void make_tree(int L , int R , int root){
         if(L == R){
            Max[root] = x[L] ;
            return ;
         }
         int mid = (L + R) >> 1 ;
         make_tree(L , mid , root<<1) ;
         make_tree(mid+1 , R , root<<1|1) ;
         push_up(root) ;
    }
    
    void update(int id , int c , int L , int R , int root){
         if(L == R){
            Max[root] = c ;
            return ;
         }
         int mid = (L + R) >> 1 ;
         if(id <= mid)
            update(id , c , L , mid ,root<<1) ;
         else
            update(id , c , mid+1 , R ,root<<1|1) ;
         push_up(root) ;
    }
    
    int query(int l , int r , int L , int R , int root){
        if(l <= L && R <= r)
          return Max[root] ;
        int mid = (L + R) >> 1 ;
        int ans = -1 ;
        if(l <= mid)
            ans = max(ans , query(l , r , L , mid , root<<1)) ;
        if(r > mid)
            ans = max(ans , query(l , r , mid+1 , R , root<<1|1)) ;
        return ans ;
    }
    
    int main(){
        int n , m , i , a , b ;
        char str[2] ;
        while(cin>>n>>m){
            for(i = 1 ; i <= n ; i++)
                scanf("%d",&x[i]) ;
            make_tree(1 , n , 1) ;
            while(m--){
                scanf("%s%d%d",str,&a,&b) ;
                if(*str == 'U')
                   update(a , b , 1 , n , 1) ;
                else
                   printf("%d
    ",query(a , b , 1 , n , 1)) ;
            }
        }
        return 0 ;
    }
    

      

    Codeforces 91B. Queue (区间“最值”)

    There are n walruses standing in a queue in an airport. They are numbered starting from the queue's tail: the 1-st walrus stands at the end of the queue and the n-th walrus stands at the beginning of the queue. The i-th walrus has the age equal to ai.

    The i-th walrus becomes displeased if there's a younger walrus standing in front of him, that is, if exists such j (i < j), that ai > aj. Thedispleasure of the i-th walrus is equal to the number of walruses between him and the furthest walrus ahead of him, which is younger than the i-th one. That is, the further that young walrus stands from him, the stronger the displeasure is.

    The airport manager asked you to count for each of n walruses in the queue his displeasure.

    Input

    The first line contains an integer n (2 ≤ n ≤ 105) — the number of walruses in the queue. The second line contains integers ai (1 ≤ ai ≤ 109).

    Note that some walruses can have the same age but for the displeasure to emerge the walrus that is closer to the head of the queue needs to be strictly younger than the other one.

    Output

    Print n numbers: if the i-th walrus is pleased with everything, print "-1" (without the quotes). Otherwise, print the i-th walrus's displeasure: the number of other walruses that stand between him and the furthest from him younger walrus.

    Sample test(s)
     
    input
    6
    10 8 5 3 50 45
    output
    2 1 0 -1 0 -1 
    input
    7
    10 4 6 3 2 8 15
    output
    4 2 1 0 -1 -1 -1 
    input
    5
    10 3 1 10 11
    output
    1 0 -1 -1 -1 

      题意: 

      有n个数的失望值,id失望值的定义是:id右边离它最远的且比它小的数 与 它本身之间 间隔的数的数量;

    线段树,结点保存区间最小值 每次将当前的数更新为INF,然后查找整个区间内最右边的比它小的数的下标 .

    const int Max_N = 100008 ;
    const int inf = 1000000800 ;
    int   _Min[Max_N<<2] , x[Max_N]  , ans[Max_N] ;
    
    void  push_up(int root){
          _Min[root] = min(_Min[root<<1] , _Min[root<<1|1]) ;
    }
    
    void  make_tree(int L , int R , int root){
          if(L == R){
              _Min[root] = x[L] ;
              return ;
          }
          int mid = (L + R) >> 1 ;
          make_tree(L , mid , root<<1) ;
          make_tree(mid+1 , R , root<<1|1) ;
          push_up(root) ;
    }
    
    void  update(int id ,  int L , int R , int root){
          if(L == R){
              _Min[root] = inf ;
              return ;
          }
          int mid = (L + R) >> 1 ;
          if(id <= mid)
               update(id , L , mid , root<<1) ;
          else
               update(id , mid+1 , R , root<<1|1) ;
          push_up(root) ;
    }
    
    //求区间[L,R]中 比C小 且 下标最大 的下标  。 其实二叉树遍历蛮重要的。 先右->后左。{好像不是3种遍历方式哦!}
    int   query(int c , int L , int R , int root){    
          if(L == R)
             return L ;
          int mid = (L + R) >> 1 ;
          if(_Min[root<<1|1] < c)   //右孩子的最小值比C小,则在右孩子中找 。
             return query(c , mid+1 , R , root<<1|1) ;
          else
             return query(c , L , mid , root<<1) ;
    }
    
    int main(){
        int n , i ;
        cin>>n ;
        for(i = 1 ; i <= n ; i++)
            scanf("%d" , &x[i]) ;
        make_tree(1 , n , 1) ;
        for(i = 1 ; i <= n ; i++){
            update(i , 1 , n , 1) ;  //将i处更新最大,更新完之后,_Min[1] = [1,n]最小值
            if(_Min[1] < x[i])
                ans[i] = query(x[i] , 1 , n , 1) - i - 1 ;  // 为什么不能是query(x[i] , i , n , 1) ?因为[i,n]的节点编号不是1。注意这里
            else
                ans[i] = -1 ;
        }
        printf("%d" , ans[1]) ;
        for(i = 2 ; i <= n ; i++)
            printf(" %d" , ans[i]) ;
        puts("") ;
        return 0 ;
    }
    

      

    hdu 1698  (区间更新)

    Just a Hook

    Problem Description
    In the game of DotA, Pudge’s meat hook is actually the most horrible thing for most of the heroes. The hook is made up of several consecutive metallic sticks which are of the same length.



    Now Pudge wants to do some operations on the hook.

    Let us number the consecutive metallic sticks of the hook from 1 to N. For each operation, Pudge can change the consecutive metallic sticks, numbered from X to Y, into cupreous sticks, silver sticks or golden sticks.
    The total value of the hook is calculated as the sum of values of N metallic sticks. More precisely, the value for each kind of stick is calculated as follows:

    For each cupreous stick, the value is 1.
    For each silver stick, the value is 2.
    For each golden stick, the value is 3.

    Pudge wants to know the total value of the hook after performing the operations.
    You may consider the original hook is made up of cupreous sticks.
     
    Input
    The input consists of several test cases. The first line of the input is the number of the cases. There are no more than 10 cases.
    For each case, the first line contains an integer N, 1<=N<=100,000, which is the number of the sticks of Pudge’s meat hook and the second line contains an integer Q, 0<=Q<=100,000, which is the number of the operations.
    Next Q lines, each line contains three integers X, Y, 1<=X<=Y<=N, Z, 1<=Z<=3, which defines an operation: change the sticks numbered from X to Y into the metal kind Z, where Z=1 represents the cupreous kind, Z=2 represents the silver kind and Z=3 represents the golden kind.
     
    Output
    For each case, print a number in a line representing the total value of the hook after the operations. Use the format in the example.
     
    Sample Input
    1 10 2 1 5 2 5 9 3
     
    Sample Output
    Case 1: The total value of the hook is 24.
     
    typedef long long LL ;
    const int Max_N = 100008 ;
    int sum[Max_N<<2] ;  /*和*/
    int color[Max_N<<2] ;/*颜色,-1表示不是纯色*/
    
    /*向上更新,计算和*/
    void push_up(int root){
         sum[root] = sum[root<<1] + sum[root<<1|1] ;
    }
    
    /*lazy操作,向下更新时,把纯色的颜色传递下去,同时更新和,若不是纯色则孩子节点的颜色之前已经更新*/
    void push_down(int root ,int L ,int R){
         if(color[root] != -1){
             color[root<<1] = color[root<<1|1] = color[root] ;
             color[root] = -1 ;  /*向下更新就是因为root节点不再纯色*/
             int mid = (L+R)>>1 ;
             sum[root<<1] = (mid-L+1) * color[root<<1] ;
             sum[root<<1|1] = (R-mid) * color[root<<1|1] ;
         }
    }
    
    /*建树*/
    void make_tree(int L , int R , int root){
         color[root] = 1 ;
         if(L == R){
            sum[root] =  1 ;
            return ;
         }
         int mid = (L+R)>>1 ;
         make_tree(L,mid,root<<1) ;
         make_tree(mid+1,R,root<<1|1) ;
         push_up(root) ;
    }
    
    /*更新区间[l,r]颜色为c*/
    void update(int l , int r , int c ,int L ,int R ,int root){
         if(l <= L && R <= r){
            color[root] = c ;
            sum[root] = (R-L+1) * c ;
            return ;
         }
         push_down(root,L,R) ; 
         int mid = (L+R)>>1 ;
         if(l <= mid)
            update(l,r,c,L,mid,root<<1) ;
         if(r > mid)
            update(l,r,c,mid+1,R,root<<1|1) ;
         push_up(root) ;
    }
    
    int main(){
        int T ,cas , n ,q , l ,r ,c;
        scanf("%d",&T) ;
        for(cas = 1 ; cas <= T ; cas++){
            scanf("%d",&n) ;
            make_tree(1,n,1) ;
            scanf("%d",&q) ;
            while(q--){
                 scanf("%d%d%d",&l,&r,&c) ;
                 update(l,r,c,1,n,1) ;
            }
            printf("Case %d: The total value of the hook is %d.
    ",cas,sum[1]) ;
        }
        return 0 ;
    }
    

      

    POJ 3468 (区间更新)

    A Simple Problem with Integers
    Time Limit: 5000MS   Memory Limit: 131072K
         

    You have N integers, A1A2, ... , AN. You need to deal with two kinds of operations. One type of operation is to add some given number to each number in a given interval. The other is to ask for the sum of numbers in a given interval.

    Input

    The first line contains two numbers N and Q. 1 ≤ N,Q ≤ 100000.
    The second line contains N numbers, the initial values of A1A2, ... , AN. -1000000000 ≤ Ai ≤ 1000000000.
    Each of the next Q lines represents an operation.
    "C a b c" means adding c to each of AaAa+1, ... , Ab. -10000 ≤ c ≤ 10000.
    "Q a b" means querying the sum of AaAa+1, ... , Ab.

    Output

    You need to answer all Q commands in order. One answer in a line.

    Sample Input

    10 5
    1 2 3 4 5 6 7 8 9 10
    Q 4 4
    Q 1 10
    Q 2 4
    C 3 6 3
    Q 2 4
    

    Sample Output

    4
    55
    9
    15

    Hint

    The sums may exceed the range of 32-bit integers.
    typedef long long LL ;
    const int Max_N = 101000 ;
    LL sum[Max_N<<2] , add[Max_N<<2] , x[Max_N];
    
    void push_up(int root){
         sum[root] = sum[root<<1] + sum[root<<1|1] ;
    }
    
    void push_down(int L ,int R ,int root){
         if(add[root]){  //纯色才向下更新。
              add[root<<1] += add[root] ;
              add[root<<1|1] += add[root] ;
              int mid = (L + R)>>1 ;
              sum[root<<1] += add[root]*(mid-L+1) ;
              sum[root<<1|1] += add[root]*(R-mid) ;
              add[root] = 0 ; //更新完便不再是纯色了。
         }
    }
    
    void make_tree(int L ,int R,int root){
         add[root] = 0 ;
         if(L == R){
            sum[root] = x[L] ;
            return ;
         }
         int mid = (L + R)>>1 ;
         make_tree(L,mid,root<<1) ;
         make_tree(mid+1,R,root<<1|1) ;
         push_up(root) ;
    }
    
    void update(int l ,int r ,LL c ,int L ,int R ,int root){
         if(l <= L && R <= r){
             add[root] += c ;
             sum[root] += c*(R-L+1) ;
             return ;
         }
         push_down(L,R,root) ;
         int mid = (L + R)>>1 ;
         if(l <= mid)
            update(l,r,c,L,mid,root<<1) ;
         if(r > mid)
            update(l,r,c,mid+1,R,root<<1|1) ;
         push_up(root) ;
    }
    
    LL   query(int l ,int r ,int L ,int R ,int root){
         if(l <= L && R <= r)
            return sum[root] ;
         push_down(L,R,root) ; // update中push_down的时候并没有把所有该更新的更新完。
         int mid = (L + R)>>1 ;
         LL ans = 0 ;
         if(l <= mid)
            ans += query(l,r,L,mid,root<<1) ;
         if(r > mid)
            ans += query(l,r,mid+1,R,root<<1|1) ;
         return ans ;
    }
    
    int main(){
        int i , n , q ,l,r;
        LL c;
        char str[2] ;
        cin>>n>>q ;
        for(i = 1 ; i <= n ; i++)
            scanf("%lld",&x[i]) ;
        make_tree(1,n,1) ;
        while(q--){
            scanf("%s%d%d",str,&l,&r) ;
            if(str[0] == 'Q')
               printf("%lld
    ",query(l,r,1,n,1)) ;
            else{
               scanf("%lld",&c) ;
               update(l,r,c,1,n,1) ;
            }
        }
        return 0 ;
    }
    

     

    HDU 4027  Can you answer these queries?(区间更新)

    Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65768/65768 K (Java/Others)


    Problem Description
    A lot of battleships of evil are arranged in a line before the battle. Our commander decides to use our secret weapon to eliminate the battleships. Each of the battleships can be marked a value of endurance. For every attack of our secret weapon, it could decrease the endurance of a consecutive part of battleships by make their endurance to the square root of it original value of endurance. During the series of attack of our secret weapon, the commander wants to evaluate the effect of the weapon, so he asks you for help.
    You are asked to answer the queries that the sum of the endurance of a consecutive part of the battleship line.

    Notice that the square root operation should be rounded down to integer.
     
    Input
    The input contains several test cases, terminated by EOF.
      For each test case, the first line contains a single integer N, denoting there are N battleships of evil in a line. (1 <= N <= 100000)
      The second line contains N integers Ei, indicating the endurance value of each battleship from the beginning of the line to the end. You can assume that the sum of all endurance value is less than 263.
      The next line contains an integer M, denoting the number of actions and queries. (1 <= M <= 100000)
      For the following M lines, each line contains three integers T, X and Y. The T=0 denoting the action of the secret weapon, which will decrease the endurance value of the battleships between the X-th and Y-th battleship, inclusive. The T=1 denoting the query of the commander which ask for the sum of the endurance value of the battleship between X-th and Y-th, inclusive.
     
    Output
    For each test case, print the case number at the first line. Then print one line for each query. And remember follow a blank line after each test case.
     
    Sample Input
    10
    1 2 3 4 5 6 7 8 9 10
    5
    0 1 10
    1 1 10
    1 1 5
    0 5 8
    1 4 8
     Sample Output
    Case #1:
    19
    7
    6
     由于--》1很快,所以在有限的更新次数内,区间容易变为全是1. 用color来标记 。
    当color【root】 = 1 时则不用往下继续更新。
    typedef long long LL ;
    const int Max_N = 100100 ;
    LL sum[Max_N<<2] , x[Max_N] ;
    int color[Max_N<<2] ;
    void push_up(int root){
         sum[root] = sum[root<<1] + sum[root<<1|1] ;
         color[root] = color[root<<1] && color[root<<1|1] ;
    }
    
    void make_tree(int L , int R , int root){
         if(L == R){
            sum[root] = x[L] ;
            color[root]  =  sum[root] == 1 ;
            return ;
         }
         int mid = (L + R) >> 1 ;
         make_tree(L , mid , root<<1) ;
         make_tree(mid+1 , R , root<<1|1) ;
         push_up(root) ;
    }
    
    void update(int l , int r , int L , int R , int root){
         if(color[root])
            return ;
         if(L == R){
             sum[root] = LL (sqrt(sum[root])) ;
             color[root] = sum[root] == 1 ;
             return ;
         }
         int mid = (L + R) >> 1 ;
         if(l <= mid)
            update(l , r , L , mid , root<<1) ;
         if(r > mid)
            update(l , r , mid+1 , R , root<<1|1) ;
         push_up(root) ;
    }
    
    LL query(int l , int r , int L , int R , int root){
       if(l <= L && R <= r)
         return  sum[root] ;
       int mid = (L + R) >> 1 ;
       LL ans = 0 ;
       if(l <= mid)
          ans += query(l , r , L , mid , root<<1) ;
       if(r > mid)
          ans += query(l , r , mid+1 , R , root<<1|1) ;
       return ans ;
    }
    
    int main(){
        int n , i , q , k = 1 , t , l , r;
        while(cin>>n){
            printf("Case #%d:
    " , k++) ;
            for(i = 1 ; i <= n ; i++)
                scanf("%I64d",&x[i]) ;
            make_tree(1 , n , 1) ;
            scanf("%d",&q) ;
            while(q--){
                scanf("%d%d%d",&t,&l,&r) ;
                if(l > r)
                  swap(l , r) ;   
                if(t)
                  printf("%I64d
    ",query(l , r , 1 , n , 1)) ;
                else
                  update(l , r , 1 , n , 1) ;
            }
            puts("") ;
        }
        return 0 ;
    }
    

    http://codeforces.com/problemset/problem/292/E   (区间更新)  

    题目大意:两个数组,a和b,两种操作:

    1:将数组a的区间[x,x+k-1]复制给数组b的区间[y,y+k-1]。

    2:问当前b[x]的值。

    const int  Max_N = 100008 ;
    int  Pos[Max_N<<2] ; //区间左端映射到数组A下标的起点
    
    void push_down(int root , int L , int R){
         if(Pos[root]){
            int mid = (L + R) >> 1 ;
            Pos[root<<1] = Pos[root] ;
            Pos[root<<1|1] = mid + 1 + Pos[root] - L ;
            Pos[root] = 0 ;
         }
    }
    
    void  make_tree(int L , int R , int root){
          Pos[root] = 0 ;
          if(L == R)
            return ;
          int mid = (L + R) >> 1 ;
          make_tree(L , mid , root<<1) ;
          make_tree(mid+1 , R , root<<1|1) ;
    }
    
    void update(int l , int r , int start , int L , int R , int root){
         if(l == L && R == r){
              Pos[root] = start ;
              return ;
         }
         push_down(root , L , R) ;
         int mid = (L + R) >> 1 ;
         if(r <= mid)
            update(l , r , start , L , mid , root<<1) ;
         else if(l > mid)
            update(l , r , start , mid+1 , R , root<<1|1) ;
         else{
            update(l , mid , start , L , mid , root<<1) ;
            update(mid+1 , r , start+mid+1-l , mid+1 , R ,root<<1|1) ;
         }
    }
    
    int query(int id , int L , int R , int root){
        if(L == R)
          return Pos[root] ;
        push_down(root , L , R) ;
        int mid = (L + R) >> 1 ;
        if(id <= mid)
           return query(id , L , mid , root<<1) ;
        else
           return query(id , mid+1 , R , root<<1|1) ;
    }
    
    int A[Max_N] , B[Max_N] ;
    int  main(){
         int i ,t , n , m , x , y , k;
         cin>>n>>m ;
         for(i = 1 ; i <= n ; i++)
            scanf("%d" , &A[i]) ;
         for(i = 1 ; i <= n ; i++)
            scanf("%d" , &B[i]) ;
         make_tree(1 , n , 1) ;
         while(m--){
              scanf("%d%d" , &t , &x) ;
              if(t == 1){
                   scanf("%d%d" , &y , &k) ;
                   update(y , y+k-1 , x , 1 , n , 1) ;
              }
              else{
                   int id = query(x , 1 , n , 1) ;
                   printf("%d
    " , id ? A[id] : B[x]) ;
              }
         }
         return 0 ;
    }
    

      

    http://codeforces.com/problemset/problem/46/D  (区间合并)

    题目大意:
    有一条长度为L的街道,有N个操作,操作有两种:
    (1)"1 a",表示有一辆长度为a的车开进来想找停车位;
    停车位必须满足与它前面的车距离至少为b,与后面的车距离至少为f;
    如果能找到这样的停车位;输出这辆车的起始位置(且这个位置最小),否则输出-1;
    (2)"2 a",表示第a个事件里进来停车的那辆车开出去了;

    算法思想:
    建立起点为-b,终点为L+f的线段树;
    查找的时候,直接查找len+b+f的线段就可以了;

    注意这里存的是坐标点个数,而不是线段长度。

     1 //节点个数
     2 const int Max_N = 100300 ;
     3 int  L_canuse[Max_N<<2] , R_canuse[Max_N<<2] , All_canuse[Max_N<<2]  , color[Max_N<<2] ;
     4 
     5 void make_tree(int L , int R , int root){
     6      L_canuse[root] = R_canuse[root] = All_canuse[root] = R - L + 1 ;
     7      color[root] = -1 ;
     8      if(L == R)
     9         return ;
    10      int mid = (L + R) >> 1 ;
    11      make_tree(L , mid , root<<1) ;
    12      make_tree(mid+1 , R , root<<1|1) ;
    13 }
    14 
    15 void push_up(int root , int L , int R){
    16      L_canuse[root] = L_canuse[root<<1] ;
    17      R_canuse[root] = R_canuse[root<<1|1] ;
    18      int mid = (L + R) >> 1 ;
    19      if(L_canuse[root] == mid - L + 1)
    20         L_canuse[root] += L_canuse[root<<1|1] ;
    21      if(R_canuse[root] == R - mid)
    22         R_canuse[root] += R_canuse[root<<1] ;
    23      All_canuse[root] = max(max(All_canuse[root<<1] , All_canuse[root<<1|1]),
    24                             R_canuse[root<<1] + L_canuse[root<<1|1]) ;
    25 }
    26 
    27 void push_down(int root , int L , int R){
    28      if(color[root] != -1){
    29            color[root<<1] = color[root<<1|1] = color[root] ;
    30            int mid = (L + R) >> 1 ;
    31            L_canuse[root<<1] = R_canuse[root<<1] = All_canuse[root<<1] = (color[root]? mid - L + 1 : 0) ;
    32            L_canuse[root<<1|1] = R_canuse[root<<1|1] = All_canuse[root<<1|1] = (color[root]? R - mid : 0) ;
    33            color[root] = -1 ;
    34      }
    35 }
    36 
    37 void update(int l , int r , int c , int L , int R , int root){
    38      if(l <= L && R <= r){
    39            L_canuse[root] = R_canuse[root] = All_canuse[root] = (c? R - L + 1 : 0) ;
    40            color[root] = c ;
    41            return ;
    42      }
    43      push_down(root , L , R) ;
    44      int mid = (L + R) >> 1 ;
    45      if(l <= mid)
    46         update(l , r , c , L , mid , root<<1) ;
    47      if(r > mid)
    48         update(l , r , c , mid+1 , R , root<<1|1) ;
    49      push_up(root , L , R) ;
    50 }
    51 
    52 int  query(int x , int L , int R , int root){
    53      if(L == R)
    54         return L  ;
    55      push_down(root , L , R) ;
    56      int mid = (L + R) >> 1 ;
    57      if(x <= All_canuse[root<<1])
    58         return query(x , L , mid , root<<1) ;
    59      else if(x <= R_canuse[root<<1] + L_canuse[root<<1|1])
    60         return mid - R_canuse[root<<1] + 1 ;
    61      else
    62         return query(x , mid+1 , R , root<<1|1) ;
    63 }
    64 
    65 struct Car{
    66        int Start  ;
    67        int Len ;
    68 }car[108];
    69 
    70 int  main(){
    71      int n ,L , B , F , t  , x ;
    72      cin>>L>>B>>F;
    73      make_tree(-B , L+F , 1) ;
    74      cin>>n ;
    75      for(int i = 1 ; i <= n ; i++){
    76         cin>>t>>x ;
    77         if(t == 1){
    78             car[i].Len = x ;
    79             if(All_canuse[1] < x + B + F + 1)
    80                 puts("-1") ;
    81             else{
    82                 car[i].Start = query(x+B+F , -B , L+F  , 1) + B ;
    83                 update(car[i].Start , car[i].Start + car[i].Len - 1, 0 , -B , L+F , 1) ;
    84                 printf("%d
    ",car[i].Start) ;
    85              }
    86         }
    87         else
    88             update(car[x].Start , car[x].Start + car[x].Len - 1 , 1 , -B , L+F , 1) ;
    89      }
    90      return 0 ;
    91 }

    hdu 1504  (区间合并)

    Tunnel Warfare

    Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
    Total Submission(s): 3609    Accepted Submission(s): 1377


    Problem Description
    During the War of Resistance Against Japan, tunnel warfare was carried out extensively in the vast areas of north China Plain. Generally speaking, villages connected by tunnels lay in a line. Except the two at the ends, every village was directly connected with two neighboring ones.

    Frequently the invaders launched attack on some of the villages and destroyed the parts of tunnels in them. The Eighth Route Army commanders requested the latest connection state of the tunnels and villages. If some villages are severely isolated, restoration of connection must be done immediately!
     
    Input
    The first line of the input contains two positive integers n and m (n, m ≤ 50,000) indicating the number of villages and events. Each of the next m lines describes an event.

    There are three different events described in different format shown below:

    D x: The x-th village was destroyed.

    Q x: The Army commands requested the number of villages that x-th village was directly or indirectly connected with including itself.

    R: The village destroyed last was rebuilt.
     
    Output
    Output the answer to each of the Army commanders’ request in order on a separate line.
     
    Sample Input
    7 9 D 3 D 6 D 5 Q 4 Q 5 R Q 4 R Q 4
     Sample Output
    1 0 2 4
    /*区间合并*/
    typedef long long LL ;
    const int Max_N = 50100 ;
     /*Left[i] ,到节点i左端点连续村庄的个数 ,Right[i] ,到节点i右端点连续村庄的个数 */
    int Left[Max_N<<2] , Right[Max_N<<2] ;
    void push_up(int L , int R , int root){
         Left[root] = Left[root<<1] ;
         Right[root] = Right[root<<1|1] ;
         int mid = (L + R) >> 1 ;
         if(Left[root] == mid - L + 1)
            Left[root] += Left[root<<1|1] ;
         if(Right[root] == R - mid)
            Right[root] += Right[root<<1] ;
    }
    
    void make_tree(int L ,int R ,int root){
         Left[root] = Right[root] = R - L + 1 ;
         if(L == R)
           return  ;
         int mid = (L + R) >> 1 ;
         make_tree(L , mid , root<<1) ;
         make_tree(mid+1 , R , root<<1|1) ;
    }
    
    void update(int id , int type , int L , int R , int root){
         if(L == R){
            Left[root] = Right[root] = type ;
            return ;
         }
         int mid = (L + R) >> 1 ;
         if(id <= mid)
            update(id , type , L , mid , root<<1) ;
         else
            update(id , type , mid + 1 , R ,root<<1|1) ;
         push_up(L , R , root) ;
    }
    
    int query(int id , int L , int R , int root){
        if(L == R)
           return Left[root] ;
        int mid = (L + R) >> 1 ;
        if(id <= mid){
            if(Left[root<<1] == mid - L + 1)
                 return Left[root<<1] + Left[root<<1|1] ;
            else if(L + Left[root<<1] > id)
                 return Left[root<<1] ;
            else if(id > mid - Right[root<<1])
                 return Right[root<<1] + Left[root<<1|1] ;
            else
                 return query(id , L , mid , root<<1) ;
        }
        else{
            if(Right[root<<1|1] == R - mid)
                 return Right[root<<1|1] + Right[root<<1] ;
            else if(id > R - Right[root<<1|1])
                 return Right[root<<1|1] ;
            else if(mid + 1 + Left[root<<1|1] > id)
                 return Right[root<<1] + Left[root<<1|1] ;
            else
                 return query(id , mid+1 , R , root<<1|1) ;
        }
    }
    
    int main(){
        int n , m  , id ;
        stack<int> me ;
        char str[2] ;
        while(cin>>n>>m){
             while(!me.empty())
                me.pop() ;
             make_tree(1,n,1) ;
             while(m--){
                   scanf("%s" , str) ;
                   if(str[0] == 'D'){
                      scanf("%d",&id) ;
                      update(id , 0 , 1 , n , 1) ;
                      me.push(id) ;
                   }
                   else if(str[0] == 'R'){
                        if(!me.empty()){
                            update(me.top() , 1 , 1 , n , 1) ;
                            me.pop() ;
                        }
                   }
                   else{
                       scanf("%d",&id) ;
                       printf("%d
    ",query(id,1,n,1)) ;
                   }
             }
        }
        return 0 ;
    }
  • 相关阅读:
    C# 关键字总结
    C# .NET、Mono、跨平台 的简单介绍
    Leetcode---剑指Offer题15---二进制中1的个数
    Leetcode---剑指Offer题14---剪绳子
    C# string方法总结
    Unity XML的使用
    C# 文件类总结 File、Directory、FileStream、StreamWriter、StreamReader
    自定义博客园---固定推荐反对到右下角
    自定义博客园---返回顶部
    CentOS 安装Python3.x常见问题
  • 原文地址:https://www.cnblogs.com/liyangtianmen/p/3551879.html
Copyright © 2011-2022 走看看