江西省程序设计竞赛k题Peach Conference

这篇博客讨论了如何使用线段树解决区间加、区间求和的问题,并结合了一个具体的例子,即孙悟空的桃子分配问题。在比赛中,作者初次尝试未能正确解答,后来发现需要考虑区间内所有桃子数量小于特定值时将区间设为0的特殊情况。通过维护区间最大值和最小值,实现了正确的线段树解决方案。
摘要由CSDN通过智能技术生成


Sun Wukong was honored as the great saint of Qitian. He was very happy, so he held a peach meeting for the monkeys (ID numbered 1 to N). To make it more interesting, Sun Wukong decided to throw the dice. The conference will roll the dice Q times, forming Q instructions, each in the form of ’m a b’, where m is an integer label, and a and b are monkey’s ID. The meaning of each instruction is as follows:
1. If m > 0, send m peaches to each monkey in ID interval [a, b];
2. If m < 0, remove |m| peaches from each monkey in the ID interval [a, b] (if the number of peaches from any monkey is less than |m|, remove all peaches of the monkey);
3. If m = 0, calculate the sum of peaches of each monkey in the interval [a, b]; now you are invited to preside over the peach conference, can you complete the task according to the requirements of Sun Wukong?
输入描述:
The fifirst line contains two positive integers N and Q (1 ≤ N, Q ≤ 100000), representing N monkeys and Q instructions, respectively.
Next, there are Q lines, and each line corresponds to one instruction. Each instruction is composed of three integers m (−10000 ≤ m ≤ 10000), a and b (1 ≤ a ≤ b ≤ N), which respectively represent the label m and the ID interval of monkey.
输出描述:
Output each instruction with label m = 0 as an integer in order per line, that is, the sum of peaches of each monkey in the interval [a, b].
示例1
输入
复制
10 8
1 1 10
0 4 6
2 3 6
0 4 5
-2 5 8
0 4 7
-2 4 5
0 3 5
输出
复制
3
6
5
4

当时比赛的时候好激动,一看题目就知道是区间加,区间求和的线段树

结果到考试结束还是WA,后来补题的时候发现原来是以为由于没有看清楚题目描述说

如果这个区间的猴子的桃子都小于m就让这个区间 桃子变为0

那这也不还是线段树的裸题吗,只要维护一下区间最大值和区间最小值即可

如果区间最大值加上m小于等于0就让整个区间都变为0,如果区间最小值加上m大于等于0就让整个区间都加上m

代码

#include<bits/stdc++.h>
using namespace std;
const int N=110000;

typedef long long ll;

struct Node{
    int l,r;
    ll s,mx,mi,add,clear;
}tr[N<<2];
int n,m;

void pushup(int u)
{
    tr[u].mx=max(tr[u<<1].mx,tr[u<<1|1].mx);//求区间最大值
    tr[u].mi=min(tr[u<<1].mi,tr[u<<1|1].mi);//求区间最小值
    tr[u].s=tr[u<<1].s+tr[u<<1|1].s;//区间求和
}
 
void pushdown(int u)
{
    Node&root=tr[u],&left=tr[u<<1],&right=tr[u<<1|1];
    if(root.clear)//懒标记标记这个区间是否需要清空
    {
        left.s=left.mx=left.mi=left.add=0;//清空
        left.clear=1;//传递标记
        right.s=right.mx=right.mi=right.add=0;//清空
        right.clear=1;//传递标记
        root.clear=0;
    }
    if(root.add)
    {
        left.s+=(left.r-left.l+1)*root.add;//区间求和
        right.s+=(right.r-right.l+1)*root.add;
        left.mx+=root.add; left.mi+=root.add;
       left.add+=root.add; right.add+=root.add;//让最小和最大值还有懒标记都加上
        right.mi+=root.add; right.mx+=root.add;      
        
        root.add=0;
    }
}

void build(int u,int l,int r)
{
    tr[u]={l,r};
    if(l==r)
    {
        return;
    }
    int mid=l+r>>1;
    build(u<<1,l,mid),build(u<<1|1,mid+1,r);
    pushup(u);
}

void modify(int u,int l,int r,int d)
{
    if(l<=tr[u].l&&r>=tr[u].r)
    {
        if(tr[u].mi+d>=0)
        {
            tr[u].s+=(tr[u].r-tr[u].l+1)*d;
            tr[u].mi+=d;
            tr[u].mx+=d;
            tr[u].add+=d;
            return;
        }
        if(tr[u].mx+d<=0)
        {
            tr[u].add=tr[u].s=tr[u].mi=tr[u].mx=0;
            tr[u].clear=1;
            return;
        }
    }

        pushdown(u);
        int mid=tr[u].l+tr[u].r>>1;
        if(l<=mid)modify(u<<1,l,r,d);
        if(r>mid)modify(u<<1|1,l,r,d);
        pushup(u);

}

ll query(int u,int l,int r)
{
     if(l<=tr[u].l&&r>=tr[u].r)
          return tr[u].s;
    pushdown(u);
    int mid=tr[u].l+tr[u].r>>1;
    ll sum=0;
    if(l<=mid)sum+=query(u<<1,l,r);
    if(r>mid)sum+=query(u<<1|1,l,r);
    return sum;
}

int main()
{
    scanf("%d%d",&n,&m);
    
    build(1,1,n);
    
    while(m--)
    {
        int d,l,r;
        scanf("%d%d%d",&d,&l,&r);
        if(d!=0)modify(1,l,r,d);
        else
        {
            printf("%lld\n",query(1,l,r));
        }
    }
    
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值