• [数据结构][线段树]Counting Haybales


    题目描述

    Farmer John is trying to hire contractors to help rearrange his farm, but so far all of them have quit when they saw the complicated sequence of instructions FJ wanted them to follow. Left to complete the project by himself, he realizes that indeed, he has made the project perhaps more complicated than necessary. Please help him follow his instructions to complete the farm upgrade.

    FJ's farm consists of N fields in a row, conveniently numbered 1…N. In each field there can be any number of haybales. Farmer John's instructions contain three types of entries:

    1) Given a contiguous interval of fields, add a new haybale to each field.

    2) Given a contiguous interval of fields, determine the minimum number of haybales in a field within that interval.

    3) Given a contiguous interval of fields, count the total number of haybales inside that interval.

    输入

    The first line contains two positive integers, N (1≤N≤200,000) and Q (1≤Q≤100,000).

    The next line contains N nonnegative integers, each at most 100,000, indicating how many haybales are initially in each field.

    Each of the next Q lines contains a single uppercase letter, either M, P or S, followed by either two positive integers A and B (1≤A≤B≤N), or three positive integers A, B, and C (1≤A≤B≤N; 1≤C≤100,000). There will be three positive integers if and only if the uppercase letter is P.

    If the letter is M, print the minimum number of haybales in the interval of fields from A…B.

    If the letter is P, put C new haybales in each field in the interval of fields from A…B.

    If the letter is S, print the total number of haybales found within interval of fields from A…B.

    输出

     A line in the output should appear in response to every 'M' or 'S' entry in FJ's instructions.

    样例输入

    4 5
    3 1 2 4
    M 3 4
    S 1 3
    P 2 3 1
    M 3 4
    S 1 3
    

    样例输出

    2
    6
    3
    8

    思路:用线段树结构存储区间信息(包括区间的最小值、区间的和),对线段树进行操作——区间更新(更新区间的两个信息)、区间查询(查询区间的两个信息)
    AC代码:
    #include <iostream>
    #include<cstdio>
    #include<algorithm>
    #define inf 0x3f3f3f3f
    using namespace std;
    
    int a[200010];
    int sum[200010*4];
    int mi[200010*4];
    
    void pushup(int rt){
      mi[rt]=min(mi[rt*2],mi[rt*2+1]);
      sum[rt]=sum[rt*2]+sum[rt*2+1];
    }
    
    void build(int l,int r,int rt){
      if(l==r){mi[rt]=sum[rt]=a[l]; return;}
      int mid=(l+r)>>1;
      build(l,mid,rt*2);
      build(mid+1,r,rt*2+1);
      pushup(rt);
    }
    
    void update(int L,int R,int val,int l,int r,int rt){//采用对区间内每个点逐个更新并pushup的方法,时间复杂度较高,可以用延迟标记优化
      if(l==r) {sum[rt]+=val; mi[rt]+=val; return;}
      int mid=(l+r)>>1;
      if(L<=mid) update(L,R,val,l,mid,rt*2);
      if(R>mid)  update(L,R,val,mid+1,r,rt*2+1);
      pushup(rt);
    }
    
    int query_mi(int L,int R,int l,int r,int rt){
      if(L<=l&&r<=R) return mi[rt];
      int mid=(l+r)>>1;
      int ret=inf;
      if(L<=mid) ret=min(ret,query_mi(L,R,l,mid,rt*2));
      if(R>mid)  ret=min(ret,query_mi(L,R,mid+1,r,rt*2+1));
      return ret;
    }
    
    int query_sum(int L,int R,int l,int r,int rt){
      if(L<=l&&r<=R) return sum[rt];
      int mid=(l+r)>>1;
      int ret=0;
      if(L<=mid) ret+=query_sum(L,R,l,mid,rt*2);
      if(R>mid)  ret+=query_sum(L,R,mid+1,r,rt*2+1);
      return ret;
    }
    
    int main()
    {
        int n,q;
        scanf("%d%d",&n,&q);
        for(int i=1;i<=n;i++) scanf("%d",&a[i]);
        build(1,n,1);
        while(q--){
            getchar();
            char op; scanf("%c",&op);
            int x,y,z;
            if(op=='M') {
                scanf("%d%d",&x,&y);
                printf("%d
    ",query_mi(x,y,1,n,1));
            }
            if(op=='S'){
                scanf("%d%d",&x,&y);
                printf("%d
    ",query_sum(x,y,1,n,1));
            }
            if(op=='P'){
                scanf("%d%d%d",&x,&y,&z);
                update(x,y,z,1,n,1);
            }
        }
        return 0;
    }
    转载请注明出处:https://www.cnblogs.com/lllxq/
  • 相关阅读:
    CUP的MESI协议
    synchronized 锁的升级
    BigDecimal/Long 前后端交互失去精度解决办法
    nested exception is java.lang.NoClassDefFoundError: Could not initialize class org.springframework.http.converter.json.MappingJackson2HttpMessageConverter
    根据文件原始名称,文件根路径按照日期生成存储路径
    异步处理MultipartFile -- No such file or directory
    select下拉框相关操作(更新中。。。)
    input清空和重置select下拉框
    sql多字段分组排序显示全部数据
    ajax发送请求下载字节流形式的excel文件
  • 原文地址:https://www.cnblogs.com/lllxq/p/8884456.html
Copyright © 2020-2023  润新知