zoukankan      html  css  js  c++  java
  • Counting Haybales 区间更新 多颗线段树

    Counting Haybales

    Time Limit: 10 Sec  Memory Limit: 128 MB
    Submit: 77  Solved: 31
    [Submit][Status][Discuss]

    Description

    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 NN fields in a row, conveniently numbered 1…N1…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.

    农夫约翰打算重修他的农场。他有 N 块土地,连续排列成一行,标号为 1…N。在每块土地上有任意数量的草堆。他可以发出三种指令:
    1) 对一个连续区间的土地,每块土地增加相同数量的草堆。
    2) 对一个连续区间的土地,输出其中最少的草堆数量。
    3) 对一个连续区间的土地,输出草堆数量总数。

    第一行两个正整数,N (1≤N≤200,000) 和 Q (1≤Q≤100,000)。
    下一行是N个非负整数,最大100,000,表示每块土地上有多少个草堆。
    以下Q行,每行一单个大写字母开头(M,P或S),空格后跟随两个正整数 A 和 B (1≤A≤B≤N), 或者三个正整数 A, B, 和 C (1≤A≤B≤N; 1≤C≤100,000)。当且仅当第一个字母是 P 时,是三个正整数。
    当该字母是M,输出区间A…B的最小草堆数。
    当该字母是P,在区间A…B,每块土地增加C堆草。
    当该字母是M,输出区间A…B的草堆数之和。

    每行一个数字,用于响应'M' 或 'S' 命令。

    Input

    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 AA and BB (1≤A≤B≤N), or three positive integers AA, BB, and CC (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.

    Output

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

    Sample Input

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

    Sample Output

    2
    6
    3
    8

    HINT

    Source

    Platinum鸣谢Claris提供译文

     

    每次区间更新时,都找到叶子节点在网上更新,和我之前用标志法有区别。但是这中方法比标记法好理解。

    #include <cstdio>
    #include <algorithm>
    #define ll long long
    #define maxn 800010
    #define lson l,m,rt<<1
    #define rson m+1,r,rt<< 1 | 1
    using namespace std;
    int n,m;
    ll mi;
    char p[10];
    struct node{
        ll sum,mi;
    }t[maxn];
    
    void PushUp(int rt){
        t[rt].sum=t[rt<<1].sum+t[rt<<1|1].sum;
        t[rt].mi=min(t[rt<<1].mi,t[rt<<1|1].mi);
    }
    void build(int l,int r,int rt){
        //t[rt].lazy=0;
        if(l==r){
            scanf("%lld",&t[rt].mi);
            t[rt].sum=t[rt].mi;return;
        }
        int m=(l+r)>>1;
        build(lson);
        build(rson);
        PushUp(rt);
    }
    
    void updata(int L,int R,int c,int l,int r,int rt){
        if(l==r){
            t[rt].sum+=(ll)c*(r-l+1);
            t[rt].mi+=c;return;
        }
        int m=(l+r)>>1;
        if(L<=m)    updata(L,R,c,lson);
        if(R>m)     updata(L,R,c,rson);
        PushUp(rt);
    }
    ll query(int L,int R,int l,int r,int rt){
        if(l>=L&&r<=R){  //zai
            mi=min(mi,t[rt].mi);
            return t[rt].sum;
        }
        if(l>R||r<L) {return  0;}  //buzai
        int m= (l+r)>>1;
        return query(L,R,rson)+query(L,R,lson);
    }
    int main(){
        //freopen("data.in","r",stdin);
        int i,j,a,b,c;
        scanf("%d%d",&n,&m);
        build(1,n,1);
        while(m--){
            scanf("%s",p);
            if(p[0]=='M'){
                scanf("%d%d",&a,&b);
                mi=1e18;
                query(a,b,1,n,1);
                printf("%lld
    ",mi);
            }
            else {
                if(p[0]=='S'){scanf("%d%d",&a,&b); printf("%lld
    ",query(a,b,1,n,1));}
                else{
                    scanf("%d%d%d",&a,&b,&c);
                    updata(a,b,c,1,n,1);
                }
            }
        }
    }
  • 相关阅读:
    Asp.NET Core+ABP框架+IdentityServer4+MySQL+Ext JS之验证码
    ABP前端使用阿里云angular2 UI框架NG-ZORRO分享
    ASP.NET Core之跨平台的实时性能监控(2.健康检查)
    应用程序的8个关键性能指标以及测量方法
    ASP.NET Core之跨平台的实时性能监控
    浅析Entity Framework Core2.0的日志记录与动态查询条件
    开发短网址平台的思路
    Nginx解决错误413 Request Entity Too Large
    配置Nginx实现负载均衡
    Windows下Nginx的安装及使用方法入门
  • 原文地址:https://www.cnblogs.com/acmtime/p/5758079.html
Copyright © 2011-2022 走看看