比赛 |
树形数据结构拔高 |
评测结果 |
AAAAAAAAAA |
题目名称 |
聪聪的世界 |
最终得分 |
100 |
用户昵称 |
dream |
运行时间 |
21.178 s |
代码语言 |
C++ |
内存使用 |
59.35 MiB |
提交时间 |
2025-04-17 20:49:42 |
显示代码纯文本
#include<bits/stdc++.h>
#define int long long
#define ls p*2
#define rs p*2+1
using namespace std;
typedef long long ll;
const int N=1000005;
ll n,m;
struct node{
int l,r;
ll mx,mn,add;
}tr[N*4];
ll a[N];
void pushup(int p){
tr[p].mx=max(tr[ls].mx,tr[rs].mx);
tr[p].mn=min(tr[ls].mn,tr[rs].mn);
}
void build(int p,int l,int r){
tr[p]={l,r,0,1000000005};
if(l==r){
tr[p].mx=tr[p].mn=a[l];
return;
}
int mid=(l+r)/2;
build(ls,l,mid);
build(rs,mid+1,r);
pushup(p);
}
ll ans;
void pushdown(int p){
if(tr[ls].l){
tr[ls].mn+=tr[p].add;
tr[ls].mx+=tr[p].add;
tr[ls].add+=tr[p].add;
}
if(tr[rs].l){
tr[rs].mn+=tr[p].add;
tr[rs].mx+=tr[p].add;
tr[rs].add+=tr[p].add;
}
tr[p].add=0;
}
ll query5(int p,int x){
if(tr[p].l==tr[p].r&&tr[p].l==x){
return tr[p].mx;
}
pushdown(p);
int mid=(tr[p].l+tr[p].r)/2;
if(x<=mid){
return query5(ls,x);
}
else{
return query5(rs,x);
}
pushup(p);
}
bool query1(int p,int x){
ll tt=query5(1,x);
if(tr[p].l==0&&tr[p].r==0) return 0;
if(tr[p].l==tr[p].r&&tr[p].mn<tt&&tr[p].r<x){
ans=tr[p].mn;
// cout<<tr[p].l<<" "<<tr[p].r<<"\n";
return 1;
}
else if(tr[p].l!=tr[p].r)
if(tr[rs].l>=x){
pushdown(p);
if(query1(ls,x)) return 1;
}
else{
pushdown(p);
if(tr[rs].mn<tt&&query1(rs,x)){
return 1;
}
else if(tr[ls].mn<tt&&query1(ls,x)){
return 1;
}
}
return 0;
}
bool query2(int p,int x){
ll tt=query5(1,x);
if(tr[p].l==0&&tr[p].r==0) return 0;
if(tr[p].l==tr[p].r&&tr[p].mx>tt&&tr[p].r<x){
ans=tr[p].mx;
return 1;
}
else if(tr[p].l!=tr[p].r)
if(tr[rs].l>=x){
pushdown(p);
if(query2(ls,x)) return 1;
}
else{
pushdown(p);
if(tr[rs].mx>tt&&query2(rs,x)){
return 1;
}
else if(tr[ls].mx>tt&&query2(ls,x)){
return 1;
}
}
return 0;
}
bool query3(int p,int x){
ll tt=query5(1,x);
if(tr[p].l==0&&tr[p].r==0) return 0;
if(tr[p].l==tr[p].r&&tr[p].mn<tt&&tr[p].l>x){
ans=tr[p].mn;
// cout<<tr[p].l<<" "<<tr[p].r<<"\n";
return 1;
}
else if(tr[p].l!=tr[p].r)
if(tr[ls].r<=x){
pushdown(p);
if(query3(rs,x)) return 1;
}
else{
pushdown(p);
if(tr[ls].mn<tt&&query3(ls,x)){
return 1;
}
else if(tr[rs].mn<tt&&query3(rs,x)){
return 1;
}
}
return 0;
}
bool query4(int p,int x){
ll tt=query5(1,x);
if(tr[p].l==0&&tr[p].r==0) return 0;
if(tr[p].l==tr[p].r&&tr[p].mx>tt&&tr[p].l>x){
ans=tr[p].mx;
return 1;
}
else if(tr[p].l!=tr[p].r)
if(tr[ls].l&&tr[ls].r<=x){
pushdown(p);
if(query4(rs,x)) return 1;
}
else{
pushdown(p);
if(tr[ls].l&&tr[ls].mx>tt&&query4(ls,x)){
return 1;
}
else if(tr[rs].l&&tr[rs].mx>tt&&query4(rs,x)){
return 1;
}
}
return 0;
}
void update1(int p,int l,int r,int v){
if(l<=tr[p].l&&tr[p].r<=r){
tr[p].mx+=v;
tr[p].mn+=v;
tr[p].add+=v;
return;
}
pushdown(p);
int mid=(tr[p].l+tr[p].r)/2;
if(l<=mid){
update1(ls,l,r,v);
}
if(r>mid){
update1(rs,l,r,v);
}
pushup(p);
}
void update2(int p,int x,int v){
if(tr[p].l==tr[p].r&&tr[p].l==x){
tr[p].mx=tr[p].mn=v;
return;
}
pushdown(p);
int mid=(tr[p].l+tr[p].r)/2;
if(x<=mid){
update2(ls,x,v);
}
else{
update2(rs,x,v);
}
pushup(p);
}
void swp(int x,int y){
int tx=query5(1,x),ty=query5(1,y);
update2(1,x,ty);
update2(1,y,tx);
}
void read(ll &x){
char c;
ll sum=0;
c=getchar();
while(c<'0'||c>'9') c=getchar();
while(c>='0'&&c<='9'){
sum=sum*10+c-'0';
c=getchar();
}
x=sum;
}
signed main(){
ios::sync_with_stdio(0);
freopen("ccsworld.in","r",stdin);
freopen("ccsworld.out","w",stdout);
read(n),read(m);
for(int i=1;i<=n;i++){
read(a[i]);
}
build(1,1,n);
while(m--){
ll op,x,y,w;
read(op);
if(op==1){
read(x);
if(query1(1,x)){
cout<<ans<<"\n";
}
else{
cout<<"-1\n";
}
}
if(op==2){
read(x);
if(query2(1,x)){
cout<<ans<<"\n";
}
else{
cout<<"-1\n";
}
}
if(op==3){
read(x);
if(query3(1,x)){
cout<<ans<<"\n";
}
else{
cout<<"-1\n";
}
}
if(op==4){
read(x);
if(query4(1,x)){
cout<<ans<<"\n";
}
else{
cout<<"-1\n";
}
}
if(op==5){
read(x),read(y);
swp(x,y);
}
if(op==6){
read(x),read(y),read(w);
update1(1,x,y,w);
}
if(op==7){
read(x),read(y),read(w);
update1(1,x,y,-w);
}
}
return 0;
}