跳到正文

个人记录

THUPC初赛 K 赛时代码

Cap1taL约 23 分钟
原文
文章信息与版本

文章信息

收录
2025年12月12日
更新
2025年12月12日
篇幅
约 23 分钟

版本

  1. v1当前

赛时鏖战三小时通过了此题,爽!

C++
//%^~
// #pragma GCC optimize(3)
// #pragma GCC optimize("Ofast")
// #include <bits/stdc++.h>
#include <cstdio>
#include <cstring>
#include <string>
#include <iostream>
#include <fstream>
#include <vector>
#include <algorithm>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <functional>
#include <limits>
#include <climits>
#include <bitset>
#include <cassert>
#include <unordered_set>
#include <unordered_map>
#include <cmath>
// #include "ext/pb_ds/assoc_container.hpp"
// #include "ext/pb_ds/tree_policy.hpp"
// #include "ext/pb_ds/priority_queue.hpp"
// #include <ext/rope>
// #define PBDS __gnu_pbds
// #include <bits/extc++.h>
#define MAXN 200005
#define eps 1e-10
#define foru(a, b, c) for (int a = (b); (a) <= (c); (a)++)
#define ford(a, b, c) for (int a = (b); (a) >= (c); (a)++)
#define uLL unsigned long long
#define LL long long
#define LXF int
#define RIN Cap1taLDebug::read()
#define RSIN Cap1taLDebug::rdstr()
#define RCIN Cap1taLDebug::rdchar()
#define HH printf("\n")
#define All(x) (x).begin(), (x).end()
#define fi first
#define se second
#define CA const auto&
using namespace std;
// const int RANDOM = time(0);
// template<class T1,class T2>
// class tr1::hash<pair<T1,T2>>{
// public:
	// size_t operator () (pair<T1,T2> x)const{
		// tr1::hash<T1> H1;
		// tr1::hash<T2> H2;
		// return H1(x.fi)^H2(x.se)^RANDOM;
	// }
// };
int ID;
class Cap1taLDebug{
	#ifdef LXF
	#define READTYPE LXF
	#else
	#define READTYPE int
	#endif
	#define DEBUGING
private:
	ostream& buf;
	#ifndef DEBUGING
	static char fbuf[1<<21],*p1,*p2;
	#define getchar() (Cap1taLDebug::p1==p2&&(p1=(p2=fbuf)+fread(fbuf,1,1<<21,stdin),p1==p2)?EOF:*p2++)
	#endif
public:
	Cap1taLDebug(ostream& out=cout):buf(out){}
	~Cap1taLDebug(){
		#ifdef DEBUGING
		buf.flush();
		#endif
	}
	static READTYPE read(){
		READTYPE x=0,w=1;char ch=0;
		while(!isdigit(ch)){
		if(ch=='-')	w=-1;
		ch=getchar();
		}
		while(isdigit(ch))	x=x*10+(ch^48),ch=getchar();
		return x*w;
	}
	static string rdstr(){
		string s;char c=getchar();
		while(c==' ' || c=='\r' || c=='\n')	c=getchar();
		while(c!=' ' && c!='\r' && c!='\n' && c!=EOF)	s+=c,c=getchar();
		return s;
	}
	static char rdchar(){
		char c=getchar();
		while(c==' ' || c=='\r' || c=='\n')	c=getchar();
		return c;
	}
	static string int128ToString(__int128 x){
		if(x==0)	return "0";
		string s="",w="";
		if(x<0)	w="-",x*=-1;
		while(x) s+=(char)('0'+(int)(x%10)),x/=10;
		reverse(All(s));
		return w+s;
	}
	Cap1taLDebug& operator<<(const string val){
		#ifdef DEBUGING
		buf<<val;
		#endif
		return *this;
	}
	template<typename T1,typename T2>
	Cap1taLDebug& operator<<(const pair<T1,T2>& val){
		#ifdef DEBUGING
		(*this)<<"("<<val.first<<","<<val.second<<")";
		#endif
		return *this;
	}
	template<typename T,template<typename,typename...>class Container,typename...Args>
	Cap1taLDebug& operator<<(const Container<T, Args...>& container){
		#ifdef DEBUGING
		buf<<"{";
		bool fst=0;
		for(const auto& val:container){
			if(!fst)	fst=true;
			else	buf<<",";
			(*this)<<val;
		}
		buf<<"}";
		#endif
		return *this;
	}
	Cap1taLDebug& operator<<(const __int128& val){
		#ifdef DEBUGING
		buf<<int128ToString(val);
		#endif
		return *this;
	}
	template<typename T>
	Cap1taLDebug& operator<<(const T& val){
		#ifdef DEBUGING
		buf<<val;
		#endif
		return *this;
	}
	Cap1taLDebug& operator<<(ostream& (*manip)(ostream&)){
		#ifdef DEBUGING
		buf<<manip;
		#endif
		return *this;
	}
};
#ifndef DEBUGING
char Cap1taLDebug::fbuf[1<<21],*Cap1taLDebug::p1=nullptr,*Cap1taLDebug::p2=nullptr;
#endif
Cap1taLDebug cein(cout);
// Cap1taLDebug cein(cerr);
ostream& operator<<(ostream& os,__int128 val){
	os<<Cap1taLDebug::int128ToString(val);
	return os;
}
template<typename T>
class Stack : public stack<T>{
public:
	void clear(){
		while(!this->empty())	this->pop();
	}
	void popuntil(const function<bool(T)>& func){
		while(!this->empty() && !func(this->top()))	this->pop();
	}
};
/*

*/
int n;
int a[1000005];
int p[1000005];
bool t1[1000005];
bool t2[1000005];
int jp_p[1000005];
int jp_q[1000005];

// int jr[1000005];
vector<vector<int>> jpls;
int lf[1000005];
int rf[1000005];
class Node{
public:
	int fi,se,idx;
};
vector<Node> rg;
int dat[1000005];

auto fail=[](int x=-1)->void{
	// if(x!=-1){
		// cout<<x<<endl;
	// }
	printf("-1");
	exit(0);
};
// namespace B{
// class Node{
// public:
	// int fi,se;
	// bool hv;
// };
// stack<Node> st;
// void check(){
	// for(int i=1;i<=n;i++){
		// if(jp_q[i]!=0){
			// int x=dat[jp_p[i]];
			// int y=jp_q[i];
//
			// if(st.empty()){
				// st.push({x,y,0});
			// }else	if(st.top().fi==x){
				// if(st.top().se!=y+1)	fail(i);
				// bool hv=st.top().hv;
				// st.pop();
				// st.push({x,y,hv});
				// if(y==1){
					// if(hv==0 && x>1)	fail(i);
					// st.pop();
				// }
			// }else{
				// if(st.top().fi!=x+1)	fail(i);
				// if(y==1)	fail(i);
				// st.top().hv=1;
				// st.push({x,y,0});
			// }
		// }
	// }
	// if(st.size())	fail();
// }
// }
// namespace C{
// int c[1000005];
// int to[1000005];
// bool vis[1000005];
// class Node{
// public:
	// int fi,se,idx;
// };
// stack<Node> st;
// void check(){
	// foru(i,1,n-1){
		// if(t1[i] && t2[i])	fail();
		// if(t1[i]){
			// to[i+1]=i;
		// }
		// if(t2[i]){
			// to[i]=i+1;
		// }
	// }
	// foru(i,1,n){
		// if(jp_q[i]!=0){
			// int x=dat[jp_p[i]];
			// int y=jp_q[i];
//
			// if(st.empty()){
				// st.push({x,y,i});
			// }else	if(st.top().fi==x){
				// int u=i;
				// while(1){
					// if(to[u]==0)	break;
					// if(to[u]!=u+1)	fail();
					// u++;
				// }
				// to[u]=st.top().idx;
				// st.pop();
				// st.push({x,y,i});
				// if(y==1){
					// st.pop();
				// }
			// }else{
				// st.push({x,y,i});
			// }
		// }
	// }
	// // foru(i,1,n){
		// // cout<<i<<' '<<to[i]<<endl;
	// // }
	// foru(i,1,n){
		// vis[to[i]]=1;
	// }
	// int N=0;
	// foru(i,1,n){
		// if(vis[i])	continue;
		// int j=i;
		// while(1){
			// c[j]=++N;
			// if(to[j]==0)	break;
			// j=to[j];
		// }
	// }
	// // foru(i,1,n){
		// // cout<<c[i]<<' ';
	// // }
	// // HH;
	// foru(i,1,n){
		// if(c[i]!=a[i]){
			// fail();
		// }
	// }
// }
// }
// bool fx[1000005];
namespace Z{
int x[1000005];
vector<int> qr[1000005];
class SegTree{
public:
	int l,r,mx;
}tr[1000005<<2];
inline int lc(int x){return x<<1;}
inline int rc(int x){return x<<1|1;}
void push_up(int p){
	tr[p].mx=max(tr[lc(p)].mx,tr[rc(p)].mx);
}
void Build(int p,int l,int r){
	tr[p].l=l,tr[p].r=r;
	if(l==r){
		return ;
	}
	int mid=(l+r)>>1;
	Build(lc(p),l,mid);
	Build(rc(p),mid+1,r);
}
void Modify(int p,int pos,int k){
	if(tr[p].l==tr[p].r){
		tr[p].mx=max(tr[p].mx,k);
		return ;
	}
	int mid=(tr[p].l+tr[p].r)>>1;
	if(pos<=mid)	Modify(lc(p),pos,k);
	else	Modify(rc(p),pos,k);
	push_up(p);
}
int Query(int p,int l,int r){
	if(l<=tr[p].l && tr[p].r<=r)	return tr[p].mx;
	int mid=(tr[p].l+tr[p].r)>>1;
	int ret=0;
	if(l<=mid)	ret=max(ret,Query(lc(p),l,r));
	if(r>mid)	ret=max(ret,Query(rc(p),l,r));
	return ret;
}
void work(){
	for(int o=0;o<rg.size();o++){
		auto [L,R,idx]=rg[o];
		qr[R].push_back(o);
	}
	Build(1,1,n);
	foru(i,1,n){
		for(auto o:qr[i]){
			auto [L,R,idx]=rg[o];
			if(L+1<=R-1){
				dat[idx]=Query(1,L+1,R-1);
			}
			// foru(j,L+1,R-1){
				// dat[idx]=max(dat[idx],x[j]);
			// }
			dat[idx]++;
		}
		for(auto o:qr[i]){
			auto [L,R,idx]=rg[o];
			Modify(1,L,dat[idx]);
			// x[L]=max(x[L],dat[idx]);
		}
	}
	// sort(All(rg),[&](Node x,Node y)->bool{
		// return x.<y.se-y.fi;
	// });
	// sort(All(rg),[&](Node x,Node y)->bool{
		// return x.se-x.fi<y.se-y.fi;
	// });
	// for(int o=0;o<rg.size();o++){
		// // auto& ls=jpls[o];
		// auto [L,R,idx]=rg[o];
		// // if(ls.size()==1)	continue;
//
		// // int N=0;
		// for(auto& [l,r,nidx]:rg){
			// if(L<l && r<R){
				// // N=max(N,dat[nidx]);
				// e[nidx].push_back(idx);
				// d[idx]++;
			// }
		// }
//
		// // dat[idx]=N+1;
	// }
	// for(int o=0;o<jpls.size();o++){
		// if(jpls[o].size()==1)	continue;
		// if(d[o]==0){
			// q.push(o);
			// dat[o]=1;
		// }
	// }
	// while(!q.empty()){
		// int u=q.front();
		// // cout<<u<<endl;
		// // cout<<dat[u]<<endl;
		// q.pop();
		// for(auto v:e[u]){
			// d[v]--;
			// dat[v]=max(dat[v],dat[u]+1);
			// if(d[v]==0)	q.push(v);
		// }
	// }
}
}
inline int lb(int x){return x&-x;}
class Tree{
	private:
	public:
		int c[1000005];
		void add(int x,int k){
			for(;x<=n;x+=lb(x)){
				c[x]+=k;
			}
		}
		int ask(int x){
			int ret=0;
			for(;x;x-=lb(x)){
				ret+=c[x];
			}
			return ret;
		}
}tr;
void solve(bool SPE){
	n=RIN;
	foru(i,1,n){
		a[i]=RIN;
	}
	foru(i,1,n){
		p[a[i]]=i;
	}

	//check -1
	// A::check();


	//plant *
	foru(i,1,n-1){
		if(p[i+1]==p[i]-1){
			t1[p[i+1]]=1;
		}
	}

	//get # possibility
	// auto getPossibility=[&]()->void{
		// foru(i,1,n){
			// if(p[i+1]<p[i]){
				// for(int j=i-1;j>=1;j--){
					// if(p[j+1]==p[j]+1){
						// jr[j]=i;
					// }else{
						// break;
					// }
				// }
			// }
		// }
	// };
	// getPossibility();
	// foru(i,1,n){
		// cout<<jr[i]<<' ';
	// }
	// HH;

	//get some blocks
	auto calcblock=[&]()->void{
		foru(i,1,n){
			tr.add(i,1);
		}
		int i=1;
		while(i<=n){
			int j=i;

			int bg=i;

			bool fst=1;
			vector<int> ls;
			while(1){
				ls.push_back(j);
				// fx[p[j]]=1;
				tr.add(p[j],-1);
				// cout<<j<<' ';
				if(j==n){
					if(tr.ask(p[bg]-1)-tr.ask(p[j])!=0)	fail();
					// foru(o,p[j]+1,p[bg]-1){
						// if(fx[o]==0){
							// // cout<<p[j]<<' '<<p[bg]<<' '<<o<<endl;
							// fail();
						// }
					// }
					break;
				}
				if(p[j+1]>p[j]+1){
					if(tr.ask(p[bg]-1)-tr.ask(p[j])!=0)	fail();
					// foru(o,p[j]+1,p[bg]-1){
						// if(fx[o]==0){
							// // cout<<p[j]<<' '<<p[bg]<<' '<<o<<endl;
							// fail();
						// }
					// }
					break;
				}
				if(p[j+1]==p[j]-1){
					if(tr.ask(p[bg]-1)-tr.ask(p[j])!=0)	fail();
					// foru(o,p[j]+1,p[bg]-1){
						// if(fx[o]==0){
							// // cout<<p[j]<<' '<<p[bg]<<' '<<o<<endl;
							// fail();
						// }
					// }
					break;
				}
				if(p[j+1]<p[j]){
					if(tr.ask(p[bg]-1)-tr.ask(p[j])!=0)	fail();
					// foru(o,p[j]+1,p[bg]-1){
						// if(fx[o]==0){
							// // cout<<p[j]<<' '<<p[bg]<<' '<<o<<endl;
							// fail();
						// }
					// }
					fst=0;
					j++;
					continue;
				}
				//sta: p[j+1]==p[j]+1;
				//check if it needs #
				// if(jr[j]==0)	break;
				if(fst)	break;
				j++;
				continue;
			}
			// HH;
			jpls.push_back(ls);
			i=j+1;
		}
	};
	calcblock();

	// printf("Far jump number: %d\n",(int)jpls.size());
	// for(auto& ls:jpls){
		// cein<<ls<<endl;
	// }

	//work with jp_q
	//tmp fill jp_p with jpls index
	for(int o=0;o<jpls.size();o++){
		auto& ls=jpls[o];
		if(ls.size()==1)	continue;
		int N=0;
		for(int i=0;i<ls.size();i++){
			if(i==0 || p[ls[i-1]]>p[ls[i]]){
				N++;
				jp_q[p[ls[i]]]=N;
				jp_p[p[ls[i]]]=o;
			}
			if(i+1<ls.size() && p[ls[i+1]]==p[ls[i]]+1){
				t2[p[ls[i]]]=1;
			}
		}
	}

	//calc LR for each jpls
	for(int o=0;o<jpls.size();o++){
		auto& ls=jpls[o];
		if(ls.size()==1)	continue;
		lf[o]=n+1;
		rf[o]=0;
		for(auto& x:ls){
			lf[o]=min(lf[o],p[x]);
			rf[o]=max(rf[o],p[x]);
		}
		rg.push_back({lf[o],rf[o],o});
	}

	//update jp_p
	Z::work();

	// -1 check
	// B::check();
	// C::check();

	foru(i,1,n){
		printf(".");
		if(jp_q[i]!=0){
			// if(jp_p[i]==0)	exit(1);
			printf("%d-%d",dat[jp_p[i]],jp_q[i]);
		}
		if(t1[i])	printf("*");
		if(t2[i])	printf("#");
	}


	#ifdef DEBUGING
	if(SPE){

	}
	#endif
	return ;
}
/*
检查文件读写
检查多测清空
检查数组大小
*/
signed main()
{
	// #define RFILE
	// #define MULTITEST
	// #define TESTCASEID

	#ifdef RFILE
	freopen(".in","r",stdin);
	freopen(".out","w",stdout);
	#endif
	ww
	#ifdef MULTITEST
	int T=RIN;
	#else
	int T=1;
	#endif

	#ifdef TESTCASEID
	ID=RIN;
	#endif

	for(int i=1;i<=T;i++) solve(i==0);
	return 0;
}
原文