跳到正文

题解

CF1887C Minimum Array 题解

tis00约 11 分钟
原文
文章信息与版本

文章信息

发布
2025年1月8日
收录
2025年12月12日
更新
2026年9月21日
篇幅
约 11 分钟
题目
CF1887C

版本

  1. v2当前
  2. v1

提供一种 O(nα(n))O(nα(n)) 的算法,跑进了 640ms640ms。

引理: “数组 aa 的字典序小于数组 bb”当且仅当“aa 的差分数组的字典序小于 bb 的差分数组”。

设 cc 是数组 bb 的差分数组,那么一次区间修改只会让 cl+1c_l+1,cr+1−1c_{r+1}-1。

于是我们创建一个 0101 序列 dd。

dj=0d_j=0 表示存在 kk,使得 ck<cjc_k<c_j。

dj=1d_j=1 表示不存在 kk。

枚举 ii 从 11 到 nn,执行以下两个操作更新 dd 数组。

一、求出 xx 为所有满足 dj=1d_j=1 的 jj 的 cj,ic_{j,i} 的最小值。

二、对于所有满足 dj=1d_j=1 且 cj,i≠xc_{j,i}\ne x 的 jj,令 dj=0d_j=0。

很经典的并查集维护 0101 序列。

输出时记得根据差分数组还原原数组。

代码:

C++
#include<bits/stdc++.h>
using namespace std;
#define dIO_USE_BUFFER
struct IO{
#ifdef dIO_USE_BUFFER
const static int BUFSIZE=1<<20;char ibuf[BUFSIZE],obuf[BUFSIZE],*p1,*p2,*pp;inline int getchar(){return(p1==p2&&(p2=(p1=ibuf)+fread(ibuf,1,BUFSIZE,stdin),p1==p2)?EOF:*p1++);}inline int putchar(char x){return((pp-obuf==BUFSIZE&&(fwrite(obuf,1,BUFSIZE,stdout),pp=obuf)),*pp=x,pp++),x;}inline IO&flush(){return fwrite(obuf,1,pp-obuf,stdout),pp=obuf,fflush(stdout),*this;}IO(){p1=p2=ibuf,pp=obuf;}~IO(){flush();}
#else
int(*getchar)()=&::getchar;int(*putchar)(int)=&::putchar;inline IO&flush(){return fflush(stdout),*this;}
#endif
string _sep=" ";int k=2;template<typename Tp,typename enable_if<is_integral<Tp>::value||is_same<Tp,__int128_t>::value>::type* =nullptr>inline int read(Tp&s){int f=1,ch=getchar();s=0;while(!isdigit(ch)&&ch!=EOF)f=(ch=='-'?-1:1),ch=getchar();if(ch==EOF)return false;while(ch=='0')ch=getchar();while(isdigit(ch))s=s*10+(ch^48),ch=getchar();s*=f;return true;}template<typename Tp,typename enable_if<is_floating_point<Tp>::value>::type* =nullptr>inline int read(Tp&s){int f=1,ch=getchar();s=0;while(!isdigit(ch)&&ch!='.'&&ch!=EOF)f=(ch=='-'?-1:1),ch=getchar();if(ch==EOF)return false;while(isdigit(ch))s=s*10+(ch^48),ch=getchar();if(ch=='.'){Tp eps=0.1;ch=getchar();while(isdigit(ch))s=s+(ch^48)*eps,ch=getchar(),eps/=10;}s*=f;return true;}inline int read(char&ch){ch=getchar();while(isspace(ch)&&ch!=EOF)ch=getchar();return ch!=EOF;}inline int read(char*c){char ch=getchar(),*s=c;while(isspace(ch)&&ch!=EOF)ch=getchar();while(!isspace(ch)&&ch!=EOF)*(c++)=ch,ch=getchar();*c='\0';return s!=c;}inline int read(string&s){s.clear();char ch=getchar();while(isspace(ch)&&ch!=EOF)ch=getchar();while(!isspace(ch)&&ch!=EOF)s+=ch,ch=getchar();return s.size()>0;}template<typename Tp=int>inline Tp read(){Tp x;read(x);return x;}template<typename Tp,typename...Ts>inline int read(Tp&x,Ts&...val){return read(x)&&read(val...);}inline int getline(char*c,const char&ed='\n'){char ch=getchar(),*s=c;while(ch!=ed&&ch!=EOF)*(c++)=ch,ch=getchar();*c='\0';return s!=c;}inline int getline(string&s,const char&ed='\n'){s.clear();char ch=getchar();while(ch!=ed&&ch!=EOF)s+=ch,ch=getchar();return s.size()>0;}template<typename Tp,typename enable_if<is_integral<Tp>::value||is_same<Tp,__int128_t>::value>::type* =nullptr>inline IO&write(Tp x){if(x<0)putchar('-'),x=-x;static char sta[41];int top=0;do sta[top++]=x%10^48,x/=10;while(x);while(top)putchar(sta[--top]);return*this;}inline IO&write(const string&str){for(char ch:str)putchar(ch);return*this;}inline IO&write(const char*str){while(*str!='\0')putchar(*(str++));return*this;}inline IO&write(char*str){return write((const char*)str);}inline IO&write(const char&ch){return putchar(ch),*this;}template<typename Tp,typename enable_if<is_floating_point<Tp>::value>::type* =nullptr>inline IO&write(Tp x){if(x>1e18||x<-1e18){write("[Floating point overflow]");throw;}if(x<0)putchar('-'),x=-x;const static long long pow10[]={1,10,100,1000,10000,100000,1000000,10000000,100000000,1000000000,10000000000,100000000000,1000000000000,10000000000000,100000000000000,1000000000000000,10000000000000000,100000000000000000,100000000000000000,100000000000000000};const auto&n=pow10[k];long long whole=x;double tmp=(x-whole)*n;long long frac=tmp;double diff=tmp-frac;if(diff>0.5){++frac;if(frac>=n)frac=0,++whole;}else if(diff==0.5&&((frac==0U)||(frac&1U)))++frac;write(whole);if(k==0U){diff=x-whole;if((!(diff<0.5)||(diff>0.5))&&(whole&1))++whole;}else{putchar('.');static char sta[21];int count=k,top=0;while(frac){sta[top++]=frac%10^48;frac/=10,count--;}while(count--)putchar('0');while(top)putchar(sta[--top]);}return*this;}template<typename Tp,typename...Ts>inline IO&write(Tp x,Ts...val){return write(x),write(_sep),write(val...),*this;}template<typename...Ts>inline IO&writeln(Ts...val){return write(val...),putchar('\n'),*this;}template<typename...Ts>inline IO&writesp(Ts...val){return write(val...),putchar(' '),*this;}inline IO&writeln(void){return putchar('\n'),*this;}inline IO&sep(const string&s=" "){return _sep=s,*this;}inline IO&prec(const int&K=2){return k=K,*this;}}io;
const int N=5e5+5;
typedef long long ll;
const ll INF=1e18;
typedef pair<int,int> pii;
int T,n,q,a[N],fa[N];
vector<pii> d[N];
int find(int x){
	if(fa[x]==x) return x;
	return fa[x]=find(fa[x]);
}
int main(){
	io.read(T);
	while(T--){
		io.read(n);
		for(int i=1;i<=n;i++) io.read(a[i]);
		io.read(q);
		for(int i=1;i<=n;i++) d[i].clear();
		for(int i=1;i<=n;i++) d[i].push_back(make_pair(1,0));
		for(int i=1;i<=q;i++){
			int l,r,x;io.read(l,r,x);
			d[l].push_back(make_pair(i+1,x));
			if(r<n) d[r+1].push_back(make_pair(i+1,-x));
		}
		for(int i=1;i<=n;i++) d[i].push_back(make_pair(q+2,0));
		for(int i=1;i<=q+2;i++) fa[i]=i;
		ll s=0;
		for(int i=1;i<=n;i++){
			ll x=INF,y=0;
			for(int j=1;j<d[i].size();j++){
				if(find(d[i][j-1].first)<=d[i][j].first-1) x=min(x,y);
				y+=d[i][j].second;
			}
			y=0;
			for(int j=1;j<d[i].size();j++){
				int l=find(d[i][j-1].first),r=d[i][j].first-1;
				if(l<=r && x!=y){
					while(l<=r){int tmp=l;l=find(l+1),fa[tmp]=r+1;}
				}
				y+=d[i][j].second;
			}
			io.writesp((s+=x)+a[i]);
		}
		io.putchar(10);
	}
	return 0;
}
原文