qbxt 解题能力营第二次比赛
【豆豆与地牢】 题解
sp:https://noip.ac/show_problem/3450
区间维护
最初的问题在于如何维护一个单个前缀和,其实就把前缀和当成一个普通的一个量进行线段树维护,修改什么的都是正常操作。
几个易错点:
- 结构体无用的信息尽量不操作,减少时间复杂度。
例如:
node operator +(const node &l,const node &r) { node p; p.l=l.l,p.r=r.r,p.maxx=max(l.maxx,r.maxx),p.sum=l.sum+r.sum; p.col=0; return p; }其中
p.sum=l.sum+r.sum是无用的,应该删除,否则会T一个点 - 忽略第一次直接镶嵌宝石,即一直用新手剑会更好
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cmath>
#define int long long
using namespace std;
const int B=2e5+10;
#define root 1,n,1
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
int read()
{
int x=0, ch=getchar();
while (!isdigit(ch)) ch=getchar();
while (isdigit(ch)) x=x*10+ch-'0',ch=getchar();
return x;
}
int n;
int tot[B],a[B];
namespace Seg
{
struct node
{
int sum;
int col;
int maxx; int l,r;
node() {l=r=sum=col=maxx=0;}
void init(int l_,int r_) {l=l_,r=r_,sum=tot[l],col=0,maxx=a[l]-tot[l];}
}z[B<<2];
node operator +(const node &l,const node &r)
{
node p;
p.l=l.l,p.r=r.r,p.maxx=max(l.maxx,r.maxx),p.sum=l.sum+r.sum;
p.col=0; return p;
}
void color(int rt,int v) {z[rt].col+=v;z[rt].sum+=(z[rt].r-z[rt].l+1)*v,z[rt].maxx-=v;return;}
void push(int rt) {if (z[rt].col) color(rt<<1,z[rt].col),color(rt<<1|1,z[rt].col),z[rt].col=0;}
void update(int rt) {z[rt]=z[rt<<1]+z[rt<<1|1];}
void build(int l,int r,int rt)
{
if (l==r) {z[rt].init(l,r);return;}
int m=l+r>>1;
build(lson),build(rson);
update(rt);
}
void one_modify(int l,int r,int rt,int x,int v)
{
if (l==r && l==x) {z[rt].maxx+=v;return;}
int m=l+r>>1;
push(rt);
if (x<=m) one_modify(lson,x,v);
else if (x>m) one_modify(rson,x,v);
update(rt);
}
void ALL_modify(int l,int r,int rt,int nowl,int nowr,int v)
{
if (nowl<=l && r<=nowr) {color(rt,v);return;};
push(rt);
int m=l+r>>1;
if (nowl<=m) ALL_modify(lson,nowl,nowr,v);
if (m<nowr) ALL_modify(rson,nowl,nowr,v);
update(rt);
}
int bg_query(int l,int r,int rt,int nowl,int nowr)
{
if (nowl<=l && r<=nowr) return z[rt].maxx;
push(rt);
int m=l+r>>1;
if (nowl<=m)
{
if (m<nowr) return max(bg_query(lson,nowl,nowr),bg_query(rson,nowl,nowr));
return bg_query(lson,nowl,nowr);
}
return bg_query(rson,nowl,nowr);
}
int One_query(int l,int r,int rt,int x)
{
if (l==r&&l==x) return z[rt].sum;
int m=l+r>>1;
push(rt);
if (x<=m) One_query(lson,x);
else One_query(rson,x);
}
}
int T;
int m;
int b[B];
void work1() {int x=read(),y=read();Seg::one_modify(root,x,y-a[x]); a[x]=y;}
void work2() {int x=read(),y=read();Seg::ALL_modify(root,x,n,y-b[x]); b[x]=y;}
void work3()
{
int x=read();
if (x==1) {printf("%lld\n",max(a[1],b[1]));return;}
int fir=Seg::bg_query(root,1,x);
int lat=Seg::One_query(root,x);
printf("%lld\n",fir+lat);
return;
}
void solve()
{
n=read(),m=read();
for (int i=1;i<=n;i++) a[i]=read();
for (int i=1;i<=n;i++) tot[i]=tot[i-1]+(b[i]=read());
Seg::build(root);
while (m--)
{
int opt=read();
if (opt==1) work1();
if (opt==2) work2();
if (opt==3) work3();
}
}
signed main()
{
// freopen("dd.in","r",stdin);
// freopen("bl.out","w",stdout);
T=read();
while (T--) solve();
}
/*
1
6 10
337 801 510 746 979 677
30 25 100 27 87 87
2 1 134
3 3
3 5
2 5 365
3 3
2 6 546
1 2 589
3 5
1 5 526
1 3 315
*/