4927 线段树练习5
时间限制: 1 s 空间限制: 128000 KB 题目等级 : 黄金 Gold
题目描述 Description
有n个数和5种操作
add a b c:把区间[a,b]内的所有数都增加c
set a b c:把区间[a,b]内的所有数都设为c
sum a b:查询区间[a,b]的区间和
max a b:查询区间[a,b]的最大值
min a b:查询区间[a,b]的最小值
输入描述 Input Description
第一行两个整数n,m,第二行n个整数表示这n个数的初始值
接下来m行操作,同题目描述
输出描述 Output Description
对于所有的sum、max、min询问,一行输出一个答案
样例输入 Sample Input
10 6
3 9 2 8 1 7 5 0 4 6
add 4 9 4
set 2 6 2
add 3 8 2
sum 2 10
max 1 7
min 3 6
样例输出 Sample Output
49
11
4
首先我们来吐槽一下、TMD样例不过都能A~~
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#define LL long long
using namespace std;
struct Node{
int l,r,siz;
long long minn,maxx,sum,lazy_add,lazy_set;
bool v;// 标记set确实是0 还是覆盖成0
Node* son[2];
}*root;
int n,m;
long long ans;
void UpDate(Node* cur){
cur->minn = min(cur->son[0]->minn,cur->son[1]->minn);
cur->maxx = max(cur->son[0]->maxx,cur->son[1]->maxx);
cur->sum = cur->son[0]->sum + cur->son[1]->sum;
}
void Built(Node *&cur,int l,int r){
cur = new Node;
cur->l=l; cur->r=r; cur->siz = (r-l+1);
if(l==r){
int a; scanf("%d",&a);
cur->maxx = cur->minn = cur->sum = a;
return;
}
int mid=(l+r)>>1;
Built(cur->son[0],l,mid);
Built(cur->son[1],mid+1,r);
UpDate(cur);
}
void Down_Set(Node *cur){
cur->son[0]->lazy_add =
cur->son[1]->lazy_add =0;
cur->son[0]->lazy_set
= cur->son[1]->lazy_set = cur->lazy_set;
cur->son[0]->v = cur->son[1]->v = true;
cur->son[0]->maxx = cur->son[0]->minn =
cur->son[1]->maxx = cur->son[1]->minn = cur->lazy_set;
cur->son[0]->sum = cur->son[0]->siz * cur->lazy_set;
cur->son[1]->sum = cur->son[1]->siz * cur->lazy_set;
cur->v = cur->lazy_set =0;
}
void Down_Add(Node* cur){
cur->son[0]->maxx += cur->lazy_add;
cur->son[1]->maxx += cur->lazy_add;
cur->son[0]->minn += cur->lazy_add;
cur->son[1]->minn += cur->lazy_add;
cur->son[0]->sum += cur->son[0]->siz * cur->lazy_add;
cur->son[1]->sum += cur->son[1]->siz * cur->lazy_add;
cur->son[0]->lazy_add += cur->lazy_add;
cur->son[1]->lazy_add += cur->lazy_add;
cur->lazy_add = 0;
}
void Set(Node *cur,int l,int r,LL p){
if(l <= cur->l && cur->r <=r){
cur->maxx = cur->minn = p;
cur->lazy_set = p; cur->v=true;
cur->sum = cur->siz * p;
cur->lazy_add = 0;
return;
}
if(cur->v) Down_Set(cur);
if(cur->lazy_add) Down_Add(cur);
int mid=(cur->l + cur->r)>>1;
if(l<=mid) Set(cur->son[0],l,r,p);
if(r>mid) Set(cur->son[1],l,r,p);
UpDate(cur);
}
void Add(Node *cur,int l,int r,LL p){
if(l <= cur->l&&cur->r <= r){
cur->lazy_add += p;
cur->maxx += p;
cur->minn += p;
cur->sum += cur->siz * p;
return;
}
if(cur->v) Down_Set(cur);
if(cur->lazy_add) Down_Add(cur);
int mid =(cur->l + cur->r)>>1;
if(l<=mid) Add(cur->son[0],l,r,p);
if(r>mid) Add(cur->son[1],l,r,p);
UpDate(cur);
}
void Query(Node *cur,int l,int r,int w){
if(l <= cur->l && cur->r <= r){
if(w==1) ans += cur->sum;
else if(w==2) ans=max(ans,cur->maxx);
else ans=min(ans,cur->minn);
return ;
}
if(cur->v) Down_Set(cur);
if(cur->lazy_add) Down_Add(cur);
int mid=(cur->l + cur->r) >>1;
if(l<=mid) Query(cur->son[0],l,r,w);
if(r>mid) Query(cur->son[1],l,r,w);
}
int main(){
scanf("%d%d",&n,&m);
Built(root,1,n);
char s[50];
for(int i=1,x,y;i<=m;i++){
LL z;
scanf("%s",s);
if(s[0]=='a'){
scanf("%d%d%lld",&x,&y,&z);
Add(root,x,y,z);
}
else if(s[1]=='e'){
scanf("%d%d%lld",&x,&y,&z);
Set(root,x,y,z);
}
else if(s[1]=='u'){
scanf("%d%d",&x,&y);
ans=0;
Query(root,x,y,1);
printf("%lld\n",ans);
}
else if(s[1]=='a'){
scanf("%d%d",&x,&y);
ans=-1;
Query(root,x,y,2);
printf("%lld\n",ans);
}
else {
ans=1e17; scanf("%d%d",&x,&y);
Query(root,x,y,3);
printf("%lld\n",ans);
}
}
return 0;
}