P6604 [HNOI2016] 序列 加强版

题意

给定长为 n 的数列 aq 次查询区间 [l,r] 的所有子区间的最小值之和。

普通版:n,q105

加强版:n105,q107,强制在线。

二次加强版:n,q107,强制在线。

分析

普通版有很多的单调栈+线段树/莫队等做法,在此不做赘述。

考虑回答一个区间 [l,r] 的答案,设区间最小值的下标在 pos,那么贡献会被拆分成三类:

  • min=apos,贡献为 apos×(psl+1)×(rps+1)
  • L,R[l,pos)
  • L,R(pos,R]

后两种贡献本质相同,我们仅考虑其中一种,L,R[l,pos)。下文用 [l1,r1][l2,r2] 表示 L[l1,r1],R[l2,r2]

正着不好计算,考虑正难则反,将 [l,r][l,r] 拆分成 [l,n][l,n](r,n](r,n][l,r](r,n],前两个式子比较好处理,设 fl 表示 [l,n][l,n] 的答案,gl 表示 [l,l][l,n] 的答案,则有转移 gl=grhtl+(rhtll)×al,fl=fl+1+gl,其中 rhti 表示 i 右侧第一个比 ai 小的数的下标。

考虑处理第三个式子,发现不好做,但是注意到我们要求的 [l,r](r,n]=[l,pos)[pos,n],而由于 apos 是区间最小值,所以 [l,pos) 中的元素没有比 apos 更小的,故左端点取哪都不会影响区间最小值,答案即为 (posl)×gpos。处理 rhtiO(n) 的,总时间复杂度为 O(nlogn+q),瓶颈在于求最小值位置的 RMQ,若使用 O(n)O(1) RMQ 即可通过二次加强版。

加强版代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
#include<cmath>
#include<map>
#include<unordered_map>
#include<vector>
#include<queue>
#include<stack>
#include<bitset>
#include<set>
#include<ctime>
#include<random>
#include<cassert>
#define x1 xx1
#define y1 yy1
#define IOS ios::sync_with_stdio(false)
#define ITIE cin.tie(0);
#define OTIE cout.tie(0);
#define PY puts("Yes")
#define PN puts("No")
#define PW puts("-1")
#define P0 puts("0")
#define P__ puts("")
#define PU puts("--------------------")
#define mp make_pair
#define fi first
#define se second
#define gc getchar
#define pc putchar
#define pb emplace_back
#define un using namespace
#define all(x) x.begin(),x.end()
#define mem(x,y) memset(x,y,sizeof x)
#define rep(a,b,c) for(int a=(b);a<=(c);++a)
#define per(a,b,c) for(int a=(b);a>=(c);--a)
#define reprange(a,b,c,d) for(int a=(b);a<=(c);a+=(d))
#define perrange(a,b,c,d) for(int a=(b);a>=(c);a-=(d))
#define graph(i,j,k,l) for(int i=k[j];i;i=l[i].nxt)
#define lowbit(x) ((x)&-(x))
#define lson(x) ((x)<<1)
#define rson(x) ((x)<<1|1)
//#define double long double
//#define int long long
//#define int __int128
using namespace std;
using i64=long long;
using u64=unsigned long long;
using pii=pair<int,int>;
template<typename T1,typename T2>inline void ckmx(T1 &x,T2 y){x=x>y?x:y;}
template<typename T1,typename T2>inline void ckmn(T1 &x,T2 y){x=x<y?x:y;}
inline auto rd(){
	int qwqx=0,qwqf=1;char ch=getchar();
	while(ch<'0'||ch>'9'){if(ch=='-')qwqf=-1;ch=getchar();}
	while(ch>='0'&&ch<='9'){qwqx=(qwqx<<1)+(qwqx<<3)+ch-48;ch=getchar();}return qwqx*qwqf;
}
template<typename T>inline void write(T qwqx,char ch='\n'){
	if(qwqx<0){qwqx=-qwqx;putchar('-');}
	int qwqy=0;char qwqz[40];
	while(qwqx||!qwqy){qwqz[qwqy++]=qwqx%10+48;qwqx/=10;}
	while(qwqy--)putchar(qwqz[qwqy]);if(ch)putchar(ch);
}
bool Mbg;
const int maxn=1e5+5,inf=0x3f3f3f3f;
const long long llinf=0x3f3f3f3f3f3f3f3f;
int n,Q,T;
int a[maxn];
namespace gen{
	typedef unsigned long long ull;
	ull s,a,b,c,lstans=0;
	ull rand(){	
		return s^=(a+b*lstans)%c;
	}
	int read(){
		return T?rand()%n+1:rd();
	}
}
using gen::lstans;
int fmx[maxn][20];
int lg[maxn];
int gmn(int x,int y){
	return a[x]<a[y]?x:y;
}
int qry(int l,int r){
	int p=lg[r-l+1];
	return gmn(fmx[l][p],fmx[r-(1<<p)+1][p]);
}
int sta[maxn],tp;
int lft[maxn],rht[maxn];
u64 pref[maxn],preg[maxn],secf[maxn],secg[maxn];
//f[i,n][i,n] g[i,i][i,n]
inline void solve_the_problem(){
	n=rd(),Q=rd(),T=rd();
	rep(i,1,n)a[i]=rd(),fmx[i][0]=i;
	rep(i,2,n)lg[i]=lg[i>>1]+1;
	const int M=lg[n];
	rep(j,1,M)rep(i,1,n-(1<<j)+1)fmx[i][j]=gmn(fmx[i][j-1],fmx[i+(1<<(j-1))][j-1]);
	
	rep(i,1,n)rht[i]=n+1;
	tp=0;rep(i,1,n){
		while(tp&&a[i]<a[sta[tp]])rht[sta[tp--]]=i;
		sta[++tp]=i;
	}
	per(i,n,1){
		secg[i]=secg[rht[i]]+1ull*a[i]*(rht[i]-i);
		secf[i]=secf[i+1]+secg[i];
	}
	
	rep(i,1,n)lft[i]=0;
	tp=0;per(i,n,1){
		while(tp&&a[i]<a[sta[tp]])lft[sta[tp--]]=i;
		sta[++tp]=i;
	}
	rep(i,1,n){
		preg[i]=preg[lft[i]]+1ull*a[i]*(i-lft[i]);
		pref[i]=pref[i-1]+preg[i];
	}
	
	u64 sum=0;
	if(T)gen::s=rd(),gen::a=rd(),gen::b=rd(),gen::c=rd();
	while(Q--){
		int l=gen::read(),r=gen::read();
		if(l>r) std::swap(l,r);
		int ps=qry(l,r);
		u64 ans=1ull*a[ps]*(ps-l+1)*(r-ps+1);
		ans+=secf[l]-secf[ps]-secg[ps]*(ps-l);
		ans+=pref[r]-pref[ps]-preg[ps]*(r-ps);
		sum^=(lstans=ans);
	}
	write(sum);
}
bool Med;
signed main(){
//	freopen(".in","r",stdin);freopen(".out","w",stdout);
	fprintf(stderr,"%.3lfMB\n",(&Mbg-&Med)/1048576.0);
	int _=1;
	while(_--)solve_the_problem();
}
/*

*/

作者:dcytrl

出处:https://www.cnblogs.com/dcytrl/p/18591430

版权:本作品采用「署名-非商业性使用-相同方式共享 4.0 国际」许可协议进行许可。

posted @   dcytrl  阅读(17)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· Manus爆火,是硬核还是营销?
· 一文读懂知识蒸馏
· 终于写完轮子一部分:tcp代理 了,记录一下
more_horiz
keyboard_arrow_up dark_mode palette
选择主题
点击右上角即可分享
微信分享提示