跳到正文

题解

呃呃呃呃呃呃呃呃呃呃呃呃呃呃呃呃呃呃呃呃呃呃呃呃呃呃呃呃呃呃呃呃

Ado_qwq约 10 分钟
原文
文章信息与版本

文章信息

发布
2026年9月14日
收录
2026年9月18日
更新
2026年11月16日
篇幅
约 10 分钟
获赞
1
题目
CF2262B

版本

  1. v1当前

想不到正难则反 😅。,,

思路

钦定我们不能正难则反,考虑操作分块。把 BB 个查询分到一个块里,再把序列按照这 B 个查询删除的位置分块。

对于每个序列块,我们要么是直接把这个块全部推光,要么在这个块的某个地方输掉。

块的左右端点为 ll 和 rr,定义 sumi=∑j=liajsum_i=\sum_{j=l}^{i} a_j。

则我们能以 xx 的初值一直赢到 ii 位置的条件是 x≥max⁡j=li(aj−sumj−1)x\ge \max_{j=l}^i (a_j-sum_{j-1})。

然后能推完整个块的情况就是容易判断的了,现在考虑如果我们在某个地方输了会怎么变化。

显然在同一个地方输了的数,之后的变化都是相同的。定义 cnticnt_i 为在第 ii 个位置输了之后,一共会在这个块输多少次。valival_i 为在第 ii 个位置输了之后,在这个块遍历完之后会变成什么值。在每个块内从后往前用单调栈维护它下一次在哪里输即可。

然后考虑查询,我们的关键在于要找到当前的值 xx 在这个块里第一个输掉的位置,直接二分会多带个 O(log⁡n)O(\log n) 不太好,发现我们删数之后 xx 一定不增,对每个块维护一个指针就能做到线性了。

总共复杂度就是 O(n2B+nB)O(\frac {n^2} B+nB),取 B=nB=\sqrt n,时间复杂度 O(nn)O(n \sqrt n),可以通过。

似乎可以单侧递归线段树?

Code
C++
#include<bits/stdc++.h>
#define int long long
#define pb emplace_back
#define pii  pair<int,int>  
namespace fast_IO{
	#define IOSIZE 100000
		char ibuf[IOSIZE], obuf[IOSIZE], *P1 = ibuf, *P2 = ibuf, *P3 = obuf;
	#define getchar() ((P1==P2)and(P2=(P1=ibuf)+fread(ibuf,1,IOSIZE,stdin),P1==P2)?(EOF):(*P1++))
	#define putchar(x) ((P3==obuf+IOSIZE)&&(fwrite(obuf,P3-obuf,1,stdout),P3=obuf),*P3++=x)
	#define isdigit(ch) (ch>47&&ch<58)
	#define isspace(ch) (ch<33)
		template<typename T> inline T read() { T s = 0; int w = 1; char ch; while (ch = getchar(), !isdigit(ch) and (ch != EOF)) if (ch == '-') w = -1; if (ch == EOF) return false; while (isdigit(ch)) s = s * 10 + ch - 48, ch = getchar(); return s * w; }
		template<typename T> inline bool read(T &s) { s = 0; int w = 1; char ch; while (ch = getchar(), !isdigit(ch) and (ch != EOF)) if (ch == '-') w = -1; if (ch == EOF) return false; while (isdigit(ch)) s = s * 10 + ch - 48, ch = getchar(); return s *= w, true; }
		template<typename T> inline void print(T x) { if (x < 0) putchar('-'), x = -x; if (x > 9) print(x / 10); putchar(x % 10 + 48); }
		inline bool read(char &s) { while (s = getchar(), isspace(s)); return true; }
		inline bool read(char *s) { char ch; while (ch = getchar(), isspace(ch)); if (ch == EOF) return false; while (!isspace(ch)) *s++ = ch, ch = getchar(); *s = '\000'; return true; }
		inline void print(char x) { putchar(x); }
		inline void print(char *x) { while (*x) putchar(*x++); }
		inline void print(const char *x) { for (int i = 0; x[i]; i++) putchar(x[i]); }
		inline bool read(std::string& s) { s = ""; char ch; while (ch = getchar(), isspace(ch)); if (ch == EOF) return false; while (!isspace(ch)) s += ch, ch = getchar(); return true; }
		inline void print(std::string x) { for (int i = 0, n = x.size(); i < n; i++) putchar(x[i]); }
		inline bool read(bool &b) { char ch; while(ch=getchar(), isspace(ch)); b=ch^48; return true; }
		inline void print(bool b) { putchar(b+48); }
		template<typename T, typename... T1> inline int read(T& a, T1&... other) { return read(a) + read(other...); }
		template<typename T, typename... T1> inline void print(T a, T1... other) { print(a), print(other...); }
		struct Fast_IO { ~Fast_IO() { fwrite(obuf, P3 - obuf, 1, stdout); } } io;
		template<typename T> Fast_IO& operator >> (Fast_IO &io, T &b) { return read(b), io; }
		template<typename T> Fast_IO& operator << (Fast_IO &io, T b) { return print(b), io; }
	#define cout io
	#define cin io
}using namespace fast_IO;
using namespace std;
const int N=2e5+10;
int n,a[N],p[N],L[N],R[N],bl[N],cnt[N],val[N],limit[N],to[N],pos[N];
bitset<N>no;
inline void check(int l,int r){
	if(l>r)return;
	int sum=0;
	for(int i=l;i<=r;++i){
	  if(no[i]){
		limit[i]=0;
		continue;
	  }
	  limit[i]=max(0ll,a[i]-sum);
	  sum+=a[i];
	}
	for(int i=l+1;i<=r;++i)limit[i]=max(limit[i],limit[i-1]);
	sum=0;
	vector<pii>vec;
	for(int i=r;i>=l;--i){
	  if(no[i])continue;
	  sum+=a[i];
	  while(!vec.empty()&&sum>=vec.back().first)vec.pop_back();
	  if(vec.empty()){
		cnt[i]=0;
		val[i]=sum;
		vec.pb(a[i]+sum,i);
	  }
	  else{
		cnt[i]=cnt[vec.back().second]+1;
		val[i]=val[vec.back().second];
		vec.pb(a[i]+sum,i);
	  }
	}
	to[l]=sum;
	pos[l]=r;
}
inline void solve(){
	cin>>n;
	for(int i=1;i<=n;++i)cin>>a[i],no[i]=0;
	for(int i=1;i<=n;++i)cin>>p[i];
	int B=sqrt(n)+1;
	for(int i=1;i<=n;++i)bl[i]=(i-1)/B+1;
	for(int i=1;i<=bl[n];++i)L[i]=(i-1)*B+1,R[i]=i*B;
	R[bl[n]]=n;
	for(int id=1;id<=bl[n];++id){
	  vector<int>vec;
	  for(int i=L[id];i<=R[id];++i)vec.pb(p[i]);
	  sort(vec.begin(),vec.end());
	  int last=1;
	  for(int x:vec){
		check(last,x-1);
		last=x+1;
	  }
	  check(last,n);
	  for(int k=L[id];k<=R[id];++k){
		last=1;
		int ans=0,now=0;
		for(int x:vec){
		  if(last<x){
			int lef=last,rig=x-1;
			if(now>=limit[rig])now+=to[lef];
			else{
			  while(pos[lef]>=lef&&now<limit[pos[lef]])--pos[lef];
			  now=val[pos[lef]+1];
			  ans+=cnt[pos[lef]+1]+1;
			}
		  }
		  if(!no[x]){
			if(now>=a[x])now+=a[x];
			else{
			  ++ans;
			  now=a[x];
			}
		  }
		  last=x+1;
		}
		if(last<=n){
		  int lef=last,rig=n;
		  if(now>=limit[rig])now+=to[lef];
		  else{
			while(pos[lef]>=lef&&now<limit[pos[lef]])--pos[lef];
			now=val[pos[lef]+1];
			ans+=cnt[pos[lef]+1]+1;
		  }
		}
		cout<<ans-1<<" ";
		no[p[k]]=1;
	  }
	}
	cout<<"\n";
}
signed main(){
	int T;
	cin>>T;
	while(T--)solve();
	return 0;
}
原文