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;
}