楼叉楼讲的高手题,很有体会,来写一个。
Solution
区间神秘修改单点查询,考虑换维扫描线,数据结构维护时间维。
将入栈看成左括号,出栈看成右括号,那么现在就是这样一个事情:将序列的一段前缀括号匹配完后,查第 到 个左括号的权值和。
关于括号匹配,一个经典的维护方式是维护一段右括号前缀与左括号后缀,合并时贪心地匹配。但是由于本题中括号带权,我们不能直接简单地压缩。接下来是关键 Hint:考虑楼房重建式的信息合并。
先考虑只询问线段树上一个节点的情况。首先把信息差分成两段后缀的括号权值相减的形式,接下来考虑查一段后缀的信息。分讨这段后缀的来源:
-
若这段后缀全部属于右侧:直接递归右侧即可。
-
否则,这相当于查右侧所有左括号的权值之和,与左侧一段 区间 的权值之和。
直接做不好做,但我们注意到这段区间的右端点一定是没被匹配掉的第一个左括号,所以我们在合并时记录左侧被合并掉的左括号权值是多少,查询时再减去即可。计算这个东西也是查后缀,所以是封闭的。
然后考虑没有特殊限制的情况。考虑把这个前缀在线段树上定位到的区间抠出来,然后从右往左做:最右的节点中的左括号肯定是能算到的,然后跟倒数第二个节点合并。合并完之后再计算倒数第二个节点内的贡献,之后重复类似的过程即可。
最终时间复杂度是 。
Code
bool Mst;
#include<bits/stdc++.h>
using namespace std;
using ui=unsigned int;
using ll=long long;
using ull=unsigned long long;
using i128=__int128;
using u128=__uint128_t;
using pii=pair<int,int>;
#define fi first
#define se second
constexpr int N=1e5+5;
int n,m;ll cnt[N],val[N];
vector<int> mdf[N];
vector<pair<ll,int> > qry[N];
#define lc (p<<1)
#define rc (p<<1|1)
int L[N<<2],R[N<<2],M[N<<2];
struct node{
ll c0,c1; // c0 = ')', c1 = '('
node(){c0=c1=0;}
node(ll _c0,ll _c1){c0=_c0,c1=_c1;}
friend inline node operator +(const node &A,const node &B){
return node(A.c0+max(B.c0-A.c1,0ll),B.c1+max(A.c1-B.c0,0ll));
}
};
node T[N<<2];ll Sum[N<<2],Res[N<<2];
inline ll calc(int p,ll k){
if(k<=0)return 0;
if(k>=T[p].c1)return Sum[p];
if(L[p]==R[p])return k*val[L[p]];
if(k<=T[rc].c1)return calc(rc,k);
else return calc(lc,k-T[rc].c1+min(T[lc].c1,T[rc].c0))+Sum[rc]-Res[p];
}
inline void pushup(int p){
T[p]=T[lc]+T[rc];
Res[p]=calc(lc,T[rc].c0);
Sum[p]=Sum[lc]+Sum[rc]-Res[p];
}
void build(int l,int r,int p=1){
L[p]=l,R[p]=r,M[p]=(l+r)>>1;
if(l==r){
T[p]=node(0,0);
Sum[p]=Res[p]=0;
return;
}
build(L[p],M[p],lc);
build(M[p]+1,R[p],rc);
pushup(p);
}
void update(int x,int v,int p=1){
if(L[p]==R[p]){
if(v<0){
T[p]=node(0,0);
Sum[p]=Res[p]=0;
}
else{
if(val[x]){
T[p]=node(0,cnt[x]);
Sum[p]=cnt[x]*val[x],Res[p]=0;
}
else{
T[p]=node(cnt[x],0);
Sum[p]=Res[p]=0;
}
}
return;
}
if(x<=M[p])update(x,v,lc);
else update(x,v,rc);
pushup(p);
}
inline node ask(int x,int p=1){
if(L[p]==R[p])return T[p];
if(x<=M[p])return ask(x,lc);
else return T[lc]+ask(x,rc);
}
inline ll query(int x,ll &v,node &cur,int p=1){
if(L[p]==R[p]){
ll ans=calc(p,v);
v-=min(v,T[p].c1);
cur=T[p];
return ans;
}
ll ans=0;
if(x<=M[p])ans=query(x,v,cur,lc);
else{
ans=query(x,v,cur,rc);
ll cnt=min(T[lc].c1,cur.c0);
ans+=calc(lc,v+cnt)-calc(lc,cnt);
v-=min(v,T[lc].c1-cnt);
cur=T[lc]+cur;
}
return ans;
}
#undef lc
#undef rc
ll ans[N];
bool Med;
int main(){
cerr<<abs(&Mst-&Med)/1048576.0<<endl;
ios::sync_with_stdio(false);
cin.tie(0),cout.tie(0);
cin>>n>>m;
for(int i=1;i<=m;i++){
int op;cin>>op;
if(op==1){
int l,r;cin>>l>>r>>cnt[i]>>val[i];
mdf[l].emplace_back(i);
mdf[r+1].emplace_back(-i);
}
else if(op==2){
int l,r;cin>>l>>r>>cnt[i];
mdf[l].emplace_back(i);
mdf[r+1].emplace_back(-i);
}
else{
int k;ll p,q;cin>>k>>p>>q;
qry[k].emplace_back(p,i);
qry[k].emplace_back(q+1,-i);
}
}
build(1,m);
for(int i=1;i<=n;i++){
for(const auto &o:mdf[i])
if(o>0)update(o,1);
else update(-o,-1);
for(const auto &o:qry[i]){
node cur(0,0);
if(o.se>0){
node tmp=ask(o.se);ll k=max(tmp.c1-o.fi+1,0ll);
ans[o.se]+=query(o.se,k,cur);
}
else{
node tmp=ask(-o.se);ll k=max(tmp.c1-o.fi+1,0ll);
ans[-o.se]-=query(-o.se,k,cur);
}
}
}
for(int i=1;i<=m;i++)
if(!cnt[i])
cout<<ans[i]<<'\n';
return 0;
}