【2】做题心得 - 2026 NOICA #15 - T4【线段树】【Ad-hoc】
题目大意:一条数轴,每两点有一条边,带流量限制。从左往右扫描,你有一个变量
人的本质是复读机,下文参考了官方题解。
我们考虑怎么刻画这个最小代价,那么设
注意到,我们每次只需要保留当前权值
那么我们就需要找到一个办法动态维护这个
但是这样写会有一个问题就是
然后我们除了维护
- 维护区间的正数个数,最小(最大)正数及和。
-
-
- 反之类似
r 的维护一样递归去做,复杂度分析和r 是一样的,均摊\mathcal O(n\log n) 。
这样分析下来本题的总时间复杂度就是 __int128 来存。
对于这种区间加一个带左区间贡献的东西我们就可以用这样的方式来维护,学到了 qwq(上次见到这种东西还是在楼房重建)。
细节比较多,注意 tag 的实际意义以及对应的维护量。
::::success[Code]
#define FileIO
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define _l __int128
#define fi first
#define se second
#define il inline
bool stMem; double stT=clock();
const int N=3e5+10;
const ll P=998244353;
il ll qp(ll a,ll b=P-2){
ll res=1;
for(;b;b>>=1,a=(a*a)%P) if(b&1) res=(res*a)%P;
return res;
}
// ---------------------------------------------------- //
int test_id;
ll n,q,a[N],b[N];
ll R[N], W[N]; // 原始需要处理的 r 和 w
namespace tree_w{
ll cn[N<<2], mn[N<<2], mx[N<<2], dt[N<<2]; _l su[N<<2];
bool ct[N<<2];
il void pushup(int p){
cn[p]=cn[p<<1]+cn[p<<1|1];
mn[p]=min(mn[p<<1],mn[p<<1|1]);
mx[p]=max(mx[p<<1],mx[p<<1|1]);
su[p]=su[p<<1]+su[p<<1|1];
}
void build(int p,int l,int r){
dt[p]=ct[p]=0;
if(l==r){
if(W[l]) mn[p]=mx[p]=W[l];
else mn[p]=+1e10, mx[p]=-1e10;
su[p]=W[l]; cn[p]=(W[l]>0);
return;
}
int mid=(l+r)>>1;
build(p<<1,l,mid);
build(p<<1|1,mid+1,r);
pushup(p);
}
il void pushdown(int p){
if(dt[p]){ // 注意使用这个的时候 dt 一定小于最小正数,所以不对 cn 进行修改
if(cn[p<<1]){
dt[p<<1]+=dt[p]; mn[p<<1]-=dt[p]; mx[p<<1]-=dt[p];
su[p<<1]-=(_l)dt[p]*cn[p<<1];
}
if(cn[p<<1|1]){
dt[p<<1|1]+=dt[p]; mn[p<<1|1]-=dt[p]; mx[p<<1|1]-=dt[p];
su[p<<1|1]-=(_l)dt[p]*cn[p<<1|1];
}
dt[p]=0;
}
if(ct[p]){
dt[p<<1]=0; mn[p<<1]=1e10; mx[p<<1]=-1e10; su[p<<1]=0; ct[p<<1]=1; cn[p<<1]=0;
dt[p<<1|1]=0; mn[p<<1|1]=1e10; mx[p<<1|1]=-1e10; su[p<<1|1]=0; ct[p<<1|1]=1; cn[p<<1|1]=0;
ct[p]=0;
}
}
void update(int p,int l,int r,int ql,int qr,ll uv){
if(ql>qr) return;
if(ql<=l&&r<=qr){
if(uv<mn[p]){ if(cn[p]) su[p]-=(_l)cn[p]*uv, mn[p]-=uv, mx[p]-=uv, dt[p]+=uv; return; }
if(uv>=mx[p]){ su[p]=0; mn[p]=1e10; mx[p]=-1e10; dt[p]=0; ct[p]=1; cn[p]=0; return; }
}
pushdown(p);
int mid=(l+r)>>1;
if(ql<=mid) update(p<<1,l,mid,ql,qr,uv);
if(qr>mid) update(p<<1|1,mid+1,r,ql,qr,uv);
pushup(p);
}
}
namespace tree_r{
ll mn[N<<2], dt[N<<2];
il void pushup(int p){ mn[p]=min(mn[p<<1],mn[p<<1|1]); }
void build(int p,int l,int r){
dt[p]=0;
if(l==r){ mn[p]=R[l]; return; }
int mid=(l+r)>>1;
build(p<<1,l,mid);
build(p<<1|1,mid+1,r);
pushup(p);
}
il void pushdown(int p){
if(!dt[p]) return;
dt[p<<1]+=dt[p]; mn[p<<1]-=dt[p];
dt[p<<1|1]+=dt[p]; mn[p<<1|1]-=dt[p];
dt[p]=0;
}
il void sync_w(ll l,ll r,ll uv){ l=max(l,1ll), r=min(r,n-1); if(l<=r) tree_w::update(1,1,n-1,l,r,uv); }
ll update(int p,int l,int r,int ql,int qr,ll uv){
if(l==r&&mn[p]<uv){ ll now_dt=mn[p]; sync_w(l,r,mn[p]); mn[p]=0; return now_dt; }
if(ql<=l&&r<=qr&&mn[p]>=uv){ sync_w(l,r,uv); dt[p]+=uv; mn[p]-=uv; return uv; }
pushdown(p);
int mid=(l+r)>>1; ll weaken=uv;
if(qr>mid) weaken=update(p<<1|1,mid+1,r,ql,qr,uv);
if(ql<=mid&&weaken) weaken=update(p<<1,l,mid,ql,qr,weaken);
pushup(p);
return weaken;
}
ll query_one(int p,int l,int r,int ps){
if(l==r){ return mn[p]; }
pushdown(p);
int mid=(l+r)>>1;
if(ps<=mid) return query_one(p<<1,l,mid,ps);
return query_one(p<<1|1,mid+1,r,ps);
}
}
il void write(_l x){
string res="";
if(x==0){ cout<<"0\n"; return; }
while(x){ res+=char(x%10+'0'); x/=10; }
reverse(res.begin(),res.end());
cout<<res<<"\n";
}
// ---------------------------------------------------- //
il void solve(){
cin>>n>>q;
for(int i=1;i<=n;++i) cin>>a[i];
for(int i=1;i<n;++i) cin>>b[i];
R[n]=0;
for(int i=n;i;--i) R[i-1]=max(0ll,R[i]-a[i]);
for(int i=1;i<n;++i) W[i]=max(0ll,R[i]-b[i]);
tree_r::build(1,0,n);
tree_w::build(1,1,n-1);
while(q--){
ll op,x,y; cin>>op>>x;
if(op==1){
cin>>y;
ll ori_d=min(y,tree_r::query_one(1,0,n,x-1));
tree_r::update(1,0,n,0,x-1,ori_d);
}else{
if(x<tree_r::query_one(1,0,n,0)) cout<<-1<<"\n";
else write(tree_w::su[1]);
}
}
return;
}
// ---------------------------------------------------- //
bool edMem;
int main(){
#ifdef FileIO
freopen("freight.in","r",stdin);
freopen("freight.out","w",stdout);
#endif
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int T=1; cin>>test_id>>T;
while(T--) solve();
cerr<<"\n";
cerr<<"[Info] End Of Running. \n";
cerr<<" Time Used : "<<(clock()-stT)/CLOCKS_PER_SEC*1000.00<<" ms\n";
cerr<<" Memory Used : "<<(&stMem-&edMem)/1048576.00<<" Mib\n";
return 0;
}
::::