3 条题解

  • 8
    @ 2026-9-21 14:17:19

    来一发可持久化李超树。

    先看看答案长什么样,假设我们找到的子段为 [l,r][l, r] 其中 l≤x≤rl \leq x \leq r,那么设之前所有 22 操作的 ∑x=S\sum x = S,答案即为 sumr−suml−1+S×(r−l+1)sum_r - sum_{l - 1} + S \times (r - l + 1),稍微整理下变成 sumr+S×r−(suml−1+S(l−1))sum_r + S \times r - (sum_{l - 1} + S(l - 1)),我们设 ti=sumi+S×it_i = sum_i + S \times i,那么我们将这个东西看作一个一次函数,SS 可变我们将其看作横坐标,使用两棵可持久化李超线段树维护最值即可。

    上面这一段话来自 cxm,拜谢 cxm!

    代码:

    #include<bits/stdc++.h>
    // #define int long long
    using namespace std;
    using ll=long long;
    const int N=2e5+5;
    const ll inf=1e18,V=2e12;
    int n,m;
    ll a[N];
    struct func{
    	int k;
    	ll b;
    	func():k(0),b(-inf){}
    	func(int _k,ll _b):k(_k),b(_b){}
    	ll f(ll x){return k*x+b;}
    };
    struct Li_Tree{
    	struct tree{
    		func v;
    		int ls,rs;
    		tree():v(),ls(0),rs(0){}
    		ll f(ll x){return v.f(x);}
    	}t[N*43];
    	int cnt;
    	int rt[N];
    	#define ls(x) t[x].ls
    	#define rs(x) t[x].rs
    	#define d ((l+r)>>1)
    	void up(int&c,int p,ll l,ll r,func v){
    		c=++cnt;
    		t[c]=t[p];
    		if(l==r){
    			if(v.f(l)>t[c].f(l))swap(v,t[c].v);
    			return;
    		}
    		if(v.f(d)>t[c].f(d))swap(t[c].v,v);
    		if(v.f(l)>t[c].f(l))up(ls(c),ls(p),l,d,v);
    		if(v.f(r)>t[c].f(r))up(rs(c),rs(p),d+1,r,v);
    	}
    	ll qy(int c,ll l,ll r,ll x){
    		if(!c)return-inf;
    		if(l==r)return t[c].f(x);
    		if(x<=d)return max(t[c].f(x),qy(ls(c),l,d,x));
    		return max(t[c].f(x),qy(rs(c),d+1,r,x));
    	}
    }T1,T2;
    signed main(){
    	ios::sync_with_stdio(0),cin.tie(0);
    	freopen("subarray.in","r",stdin);
    	freopen("subarray.out","w",stdout);
    	cin>>n>>m;
    	for(int i=1;i<=n;i++)cin>>a[i],a[i]+=a[i-1];
    	for(int i=n;i;i--)
    		T1.up(T1.rt[i],T1.rt[i+1],-V,V,func(i,a[i]));
    	for(int i=1;i<=n;i++)
    		T2.up(T2.rt[i],T2.rt[i-1],-V,V,func(1-i,-a[i-1]));
    	ll X=0;
    	while(m--){
    		int op,x;cin>>op>>x;
    		if(op==2)X+=x;
    		else
    			cout<<T1.qy(T1.rt[x],-V,V,X)+T2.qy(T2.rt[x],-V,V,X)<<'\n';
    	}
    	return 0;
    }
    
  • -1
    @ 2026-9-21 19:36:38

    提供不同码风的实现。

    其实观察到可持久化的性质是很好的,直接往一棵里面塞就行了。

    
    #include <bits/stdc++.h>
    
    using namespace std;
    using ll = long long;
    
    const int N = 8e5 + 3;
    const ll V = 1e12;
    
    struct Snode {
      int ls, rs, sid;
    } t[N << 4];
    
    struct SgeMent {
      ll k, b;
    } S[N];
    
    int n, m, rt[N], cnt, ct, tr[N], ID[N];
    ll a[N];
    
    ll Y (int sid, ll x) {
      return S[sid].k * x + S[sid].b;
    }
    
    bool C (int x, int y, ll X) {
      return Y (x, X) > Y (y, X);
    }
     
    int adsg (ll k, ll b) {
      return S[++cnt] = {k, b}, cnt;
    }
    
    int cpnd (int x) {
      return t[++ct] = t[x], ct;
    }
    
    int updt (int np, ll l, ll r, int p, int sid) {
      np = cpnd (p);
      if (t[np].sid && C (t[np].sid, sid, l) && C (t[np].sid, sid, r)) {
        return np;
      } else if (!t[np].sid || !C (t[np].sid, sid, l) && !C (t[np].sid, sid, r)) {
        return t[np].sid = sid, np;
      }
      ll mid = (l + r) >> 1;
      if (C (sid, t[np].sid, mid)) swap (t[np].sid, sid);
      if (C (sid, t[np].sid, l)) {
        t[np].ls = updt (0, l, mid, t[p].ls, sid);
      }
      if (C (sid, t[np].sid, r)) {
        t[np].rs = updt (0, mid + 1, r, t[p].rs, sid);
      }
      return np;
    }
    
    ll qury (ll l, ll r, int p, ll X) {
      if (!p || l > X || r < X) {
        return -1e18;
      }
      ll res = -1e18;
      if (t[p].sid) {
        res = Y (t[p].sid, X);
      }
      ll mid = (l + r) >> 1;
      return max ({res, qury (l, mid, t[p].ls, X), qury (mid + 1, r, t[p].rs, X)});
    }
    
    int main () {
      freopen ("subarray.in", "r", stdin);
      freopen ("subarray.out", "w", stdout);
      ios :: sync_with_stdio (0), cin.tie (0);
      cin >> n >> m;
      for (int i = 0; i <= n; ++i) {
        if (i) cin >> a[i], a[i] += a[i - 1];
        int sid1 = adsg (-i, -a[i]);
        rt[i] = updt (rt[i], -V, V, (i ? rt[i - 1] : 0), sid1);
      }
      for (int i = n; i; --i) {
        int sid2 = adsg (i, a[i]);
        tr[i] = updt (tr[i], -V, V, tr[i + 1], sid2);
      }
      for (ll sum = 0, op, x; m--; ) {
        cin >> op >> x;
        if (op == 2) {
          sum += x;
        } else {
          ll l = qury (-V, V, rt[x - 1], sum);
          ll r = qury (-V, V, tr[x], sum);
          cout << r + l << '\n';
        }
      }
      return 0 ^ 0;
    }
    
    • @ 2026-9-22 9:40:00

      VV 要开到 2×10122 \times 10^{12} 吧。

    • @ 2026-9-22 18:42:14

      yyfisgay.

  • -6
    @ 2026-9-21 9:35:15

    • 1

    信息

    ID
    42
    时间
    1000ms
    内存
    512MiB
    难度
    8
    标签
    递交数
    100
    已通过
    14
    上传者