AtCoder Beginner Contest 421 题解

A Misdelivery

#include<bits/stdc++.h> 
#include<atcoder/all>
using namespace std;
using namespace atcoder;

#define For(i,n) for(int i=1;i<=n;i++)
#define Fork(i,k,n) for(int i=k;i<=n;i++)
#define ForkD(i,k,n) for(int i=n;i>=k;i--)
#define Rep(i,n) for(int i=0;i<n;i++)
#define ForD(i,n) for(int i=n;i;i--)
#define RepD(i,n) for(int i=n;i>=0;i--)
#define Forp(x) for(int p=pre[x];p;p=next[p])
#define Forpiter(x) for(int &p=iter[x];p;p=next[p])  
#define Lson (o<<1)
#define Rson ((o<<1)+1)
#define MEM(a) memset(a,0,sizeof(a));
#define MEMI(a) memset(a,0x3f,sizeof(a));
#define MEMi(a) memset(a,128,sizeof(a));
#define MEMx(a,b) memset(a,b,sizeof(a));
#define INF (0x3f3f3f3f)
#define F (1000000007)
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define vi vector<int> 
#define pi pair<int,int>
#define SI(a) ((a).size())
#define Pr(kcase,ans) printf("Case #%d: %lld\n",kcase,ans);
#define PRi(a,n) For(i,n-1) cout<<a[i]<<' '; cout<<a[n]<<endl;
#define PRi2D(a,n,m) For(i,n) { \
						For(j,m-1) cout<<a[i][j]<<' ';\
						cout<<a[i][m]<<endl; \
						} 
#pragma comment(linker, "/STACK:102400000,102400000")
#define ALL(x) (x).begin(),(x).end()
#define gmax(a,b) a=max(a,b);
#define gmin(a,b) a=min(a,b);
#ifdef DEBUG
#define _GLIBCXX_DEBUG
#endif

#define DEBUG
int recur_depth = 0;
#ifdef DEBUG
#define dbg(x) {++recur_depth; auto x_=x; --recur_depth; cerr<<string(recur_depth, '\t')<<"\e[91m"<<__func__<<":"<<__LINE__<<"\t"<<#x<<" = "<<x_<<"\e[39m"<<endl;}
#else
#define dbg(x)
#endif
template<typename Ostream, typename Cont>
typename enable_if<is_same<Ostream,ostream>::value, Ostream&>::type operator<<(Ostream& os,  const Cont& v){
	os<<"[";
	for(auto& x:v){os<<x<<", ";}
	return os<<"]";
}
template<typename Ostream, typename ...Ts>
Ostream& operator<<(Ostream& os,  const pair<Ts...>& p){
	return os<<"{"<<p.first<<", "<<p.second<<"}";
}

typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
ll mul(ll a,ll b){return (a*b)%F;}
ll add(ll a,ll b){return (a+b)%F;}
ll sub(ll a,ll b){return ((a-b)%F+F)%F;}
void upd(ll &a,ll b){a=(a%F+b%F)%F;}
inline int read()
{
	int x=0,f=1; char ch=getchar();
	while(!isdigit(ch)) {if (ch=='-') f=-1; ch=getchar();}
	while(isdigit(ch)) { x=x*10+ch-'0'; ch=getchar();}
	return x*f;
} 
vi c[200000+10];
int main()
{
//	freopen("A.in","r",stdin);
//	freopen(".out","w",stdout);
	int t=read();
	vector<string> v(t+1);
	For(i,t) cin>>v[i];
	int p=read();string g;cin>>g;
	puts(v[p]==g?"Yes":"No");
	
		return 0;
}


B Fibonacci Reversed

#include<bits/stdc++.h> 
#include<atcoder/all>
using namespace std;
using namespace atcoder;

#define For(i,n) for(int i=1;i<=n;i++)
#define Fork(i,k,n) for(int i=k;i<=n;i++)
#define ForkD(i,k,n) for(int i=n;i>=k;i--)
#define Rep(i,n) for(int i=0;i<n;i++)
#define ForD(i,n) for(int i=n;i;i--)
#define RepD(i,n) for(int i=n;i>=0;i--)
#define Forp(x) for(int p=pre[x];p;p=next[p])
#define Forpiter(x) for(int &p=iter[x];p;p=next[p])  
#define Lson (o<<1)
#define Rson ((o<<1)+1)
#define MEM(a) memset(a,0,sizeof(a));
#define MEMI(a) memset(a,0x3f,sizeof(a));
#define MEMi(a) memset(a,128,sizeof(a));
#define MEMx(a,b) memset(a,b,sizeof(a));
#define INF (0x3f3f3f3f)
#define F (1000000007)
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define vi vector<int> 
#define pi pair<int,int>
#define SI(a) ((a).size())
#define Pr(kcase,ans) printf("Case #%d: %lld\n",kcase,ans);
#define PRi(a,n) For(i,n-1) cout<<a[i]<<' '; cout<<a[n]<<endl;
#define PRi2D(a,n,m) For(i,n) { \
						For(j,m-1) cout<<a[i][j]<<' ';\
						cout<<a[i][m]<<endl; \
						} 
#pragma comment(linker, "/STACK:102400000,102400000")
#define ALL(x) (x).begin(),(x).end()
#define gmax(a,b) a=max(a,b);
#define gmin(a,b) a=min(a,b);
#ifdef DEBUG
#define _GLIBCXX_DEBUG
#endif

#define DEBUG
int recur_depth = 0;
#ifdef DEBUG
#define dbg(x) {++recur_depth; auto x_=x; --recur_depth; cerr<<string(recur_depth, '\t')<<"\e[91m"<<__func__<<":"<<__LINE__<<"\t"<<#x<<" = "<<x_<<"\e[39m"<<endl;}
#else
#define dbg(x)
#endif
template<typename Ostream, typename Cont>
typename enable_if<is_same<Ostream,ostream>::value, Ostream&>::type operator<<(Ostream& os,  const Cont& v){
	os<<"[";
	for(auto& x:v){os<<x<<", ";}
	return os<<"]";
}
template<typename Ostream, typename ...Ts>
Ostream& operator<<(Ostream& os,  const pair<Ts...>& p){
	return os<<"{"<<p.first<<", "<<p.second<<"}";
}

typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
ll mul(ll a,ll b){return (a*b)%F;}
ll add(ll a,ll b){return (a+b)%F;}
ll sub(ll a,ll b){return ((a-b)%F+F)%F;}
void upd(ll &a,ll b){a=(a%F+b%F)%F;}
inline int read()
{
	int x=0,f=1; char ch=getchar();
	while(!isdigit(ch)) {if (ch=='-') f=-1; ch=getchar();}
	while(isdigit(ch)) { x=x*10+ch-'0'; ch=getchar();}
	return x*f;
} 
int main()
{
//	freopen("A.in","r",stdin);
//	freopen(".out","w",stdout);
	ll x,y,z;
	cin>>x>>y;
	For(i,8) {
		z=x+y;
		stringstream ss,ss2;
		ss<<z;
		string s;
		ss>>s;
		reverse(ALL(s));
		ss2<<s;
		ss2>>z;
		x=y,y=z;
	}
	cout<<y<<endl;
	
	return 0;
}


C Alternated

#include<bits/stdc++.h> 
#include<atcoder/all>
using namespace std;
using namespace atcoder;

#define For(i,n) for(int i=1;i<=n;i++)
#define Fork(i,k,n) for(int i=k;i<=n;i++)
#define ForkD(i,k,n) for(int i=n;i>=k;i--)
#define Rep(i,n) for(int i=0;i<n;i++)
#define ForD(i,n) for(int i=n;i;i--)
#define RepD(i,n) for(int i=n;i>=0;i--)
#define Forp(x) for(int p=pre[x];p;p=next[p])
#define Forpiter(x) for(int &p=iter[x];p;p=next[p])  
#define Lson (o<<1)
#define Rson ((o<<1)+1)
#define MEM(a) memset(a,0,sizeof(a));
#define MEMI(a) memset(a,0x3f,sizeof(a));
#define MEMi(a) memset(a,128,sizeof(a));
#define MEMx(a,b) memset(a,b,sizeof(a));
#define INF (0x3f3f3f3f)
#define F (1000000007)
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define vi vector<int> 
#define pi pair<int,int>
#define SI(a) ((a).size())
#define Pr(kcase,ans) printf("Case #%d: %lld\n",kcase,ans);
#define PRi(a,n) For(i,n-1) cout<<a[i]<<' '; cout<<a[n]<<endl;
#define PRi2D(a,n,m) For(i,n) { \
						For(j,m-1) cout<<a[i][j]<<' ';\
						cout<<a[i][m]<<endl; \
						} 
#pragma comment(linker, "/STACK:102400000,102400000")
#define ALL(x) (x).begin(),(x).end()
#define gmax(a,b) a=max(a,b);
#define gmin(a,b) a=min(a,b);
#ifdef DEBUG
#define _GLIBCXX_DEBUG
#endif

#define DEBUG
int recur_depth = 0;
#ifdef DEBUG
#define dbg(x) {++recur_depth; auto x_=x; --recur_depth; cerr<<string(recur_depth, '\t')<<"\e[91m"<<__func__<<":"<<__LINE__<<"\t"<<#x<<" = "<<x_<<"\e[39m"<<endl;}
#else
#define dbg(x)
#endif
template<typename Ostream, typename Cont>
typename enable_if<is_same<Ostream,ostream>::value, Ostream&>::type operator<<(Ostream& os,  const Cont& v){
	os<<"[";
	for(auto& x:v){os<<x<<", ";}
	return os<<"]";
}
template<typename Ostream, typename ...Ts>
Ostream& operator<<(Ostream& os,  const pair<Ts...>& p){
	return os<<"{"<<p.first<<", "<<p.second<<"}";
}

typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
ll mul(ll a,ll b){return (a*b)%F;}
ll add(ll a,ll b){return (a+b)%F;}
ll sub(ll a,ll b){return ((a-b)%F+F)%F;}
void upd(ll &a,ll b){a=(a%F+b%F)%F;}
inline int read()
{
	int x=0,f=1; char ch=getchar();
	while(!isdigit(ch)) {if (ch=='-') f=-1; ch=getchar();}
	while(isdigit(ch)) { x=x*10+ch-'0'; ch=getchar();}
	return x*f;
} 
int main()
{
//	freopen("A.in","r",stdin);
//	freopen(".out","w",stdout);
	int n;string s;
	cin>>n>>s;
	ll ans;
	{
		int a=0,b=1;
		ll p=0;
		Rep(i,2*n){
			if(s[i]=='A') {
				p+=abs(i-a);a+=2;	
			}else {
				p+=abs(i-b);b+=2;	
			}
		}
		ans=p;
	}
	{
		int a=1,b=0;
		ll p=0;
		Rep(i,2*n){
			if(s[i]=='A') {
				p+=abs(i-a);a+=2;	
			}else {
				p+=abs(i-b);b+=2;	
			}
		}
		gmin(ans,p)
	}
cout<<ans/2;
	
	return 0;
}


D RLE Moving

#include<bits/stdc++.h> 
#include<atcoder/all>
using namespace std;
using namespace atcoder;

#define For(i,n) for(int i=1;i<=n;i++)
#define Fork(i,k,n) for(int i=k;i<=n;i++)
#define ForkD(i,k,n) for(int i=n;i>=k;i--)
#define Rep(i,n) for(int i=0;i<n;i++)
#define ForD(i,n) for(int i=n;i;i--)
#define RepD(i,n) for(int i=n;i>=0;i--)
#define Forp(x) for(int p=pre[x];p;p=next[p])
#define Forpiter(x) for(int &p=iter[x];p;p=next[p])  
#define Lson (o<<1)
#define Rson ((o<<1)+1)
#define MEM(a) memset(a,0,sizeof(a));
#define MEMI(a) memset(a,0x3f,sizeof(a));
#define MEMi(a) memset(a,128,sizeof(a));
#define MEMx(a,b) memset(a,b,sizeof(a));
#define INF (0x3f3f3f3f)
#define F (1000000007)
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define vi vector<int> 
#define pi pair<int,int>
#define SI(a) ((a).size())
#define Pr(kcase,ans) printf("Case #%d: %lld\n",kcase,ans);
#define PRi(a,n) For(i,n-1) cout<<a[i]<<' '; cout<<a[n]<<endl;
#define PRi2D(a,n,m) For(i,n) { \
						For(j,m-1) cout<<a[i][j]<<' ';\
						cout<<a[i][m]<<endl; \
						} 
#pragma comment(linker, "/STACK:102400000,102400000")
#define ALL(x) (x).begin(),(x).end()
#define gmax(a,b) a=max(a,b);
#define gmin(a,b) a=min(a,b);
#ifdef DEBUG
#define _GLIBCXX_DEBUG
#endif

#define DEBUG
int recur_depth = 0;
#ifdef DEBUG
#define dbg(x) {++recur_depth; auto x_=x; --recur_depth; cerr<<string(recur_depth, '\t')<<"\e[91m"<<__func__<<":"<<__LINE__<<"\t"<<#x<<" = "<<x_<<"\e[39m"<<endl;}
#else
#define dbg(x)
#endif
template<typename Ostream, typename Cont>
typename enable_if<is_same<Ostream,ostream>::value, Ostream&>::type operator<<(Ostream& os,  const Cont& v){
	os<<"[";
	for(auto& x:v){os<<x<<", ";}
	return os<<"]";
}
template<typename Ostream, typename ...Ts>
Ostream& operator<<(Ostream& os,  const pair<Ts...>& p){
	return os<<"{"<<p.first<<", "<<p.second<<"}";
}

typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
ll mul(ll a,ll b){return (a*b)%F;}
ll add(ll a,ll b){return (a+b)%F;}
ll sub(ll a,ll b){return ((a-b)%F+F)%F;}
void upd(ll &a,ll b){a=(a%F+b%F)%F;}
inline int read()
{
	int x=0,f=1; char ch=getchar();
	while(!isdigit(ch)) {if (ch=='-') f=-1; ch=getchar();}
	while(isdigit(ch)) { x=x*10+ch-'0'; ch=getchar();}
	return x*f;
} 

bool o(long long x, long long y, long long nx, long long ny) {
    // 排除 (x,y) == (0,0) 的情况
    if (x == 0 && y == 0) return false;

    // 检查方向并验证原点是否在直线上
    bool onLine = false;
    if (y == ny) { // 水平线
        onLine = (y == 0);
    } else if (x == nx) { // 垂直线
        onLine = (x == 0);
    } else if (nx - x == ny - y) { // 斜率为1
        onLine = (y == x);
    } else if (nx - x == -(ny - y)) { // 斜率为-1
        onLine = (y == -x);
    } else {
        return false; // 其他方向(理论上不会出现)
    }

    if (!onLine) return false;

    // 检查原点是否在区间内
    bool xCross = (x < 0 && nx >= 0) || (x > 0 && nx <= 0);
    bool yCross = (y < 0 && ny >= 0) || (y > 0 && ny <= 0);

    // 对于水平线,yCross 必须满足 ny == 0 或 y 和 ny 跨越 0
    if (y == ny) {
        return xCross && (y == 0);
    }
    // 对于垂直线,xCross 必须满足 nx == 0 或 x 和 nx 跨越 0
    if (x == nx) {
        return yCross && (x == 0);
    }
    // 对于对角线,xCross 和 yCross 必须同时满足
    return xCross && yCross;
}

int main()
{
//	freopen("A.in","r",stdin);
//	freopen(".out","w",stdout);

	ll sx,sy,tx,ty;
	cin>>sx>>sy>>tx>>ty;
	ll n,m,l;cin>>n>>m>>l;
	vector<pair<string,int> > op[2];
	Rep(i,m) {
		string p;int c;
		cin>>p>>c;
		op[0].pb(mp(p,c));	
	}
	Rep(i,l) {
		string p;int c;
		cin>>p>>c;
		op[1].pb(mp(p,c));	
	}
	if(((sx+sy)%2+2)%2!=((tx+ty)%2+2)%2) {
		puts("0");
		exit(0);
	}
	
	ll x=sx-tx,y=sy-ty;
	auto mv = [](string seq, ll steps, ll& x, ll& y)
    {
        switch (seq[0])
        {
            case 'U': x -= steps; break;
            case 'D': x += steps; break;
            case 'L': y -= steps; break;
            case 'R': y += steps; break;
        }
    };
    
    ll ans=0;
    ll i[2]={},p[2]={};
    ll elen=0;
	while(elen!=n) {
		Rep(j,2) {
			if(i[j]>=op[j][p[j]].se) i[j]=0,++p[j];
		}
//		if(p[0]==m||p[1]==l) break;
		ll nx=x,ny=y;
		ll dis=1e18;
		Rep(j,2) gmin(dis,op[j][p[j]].se-i[j])
		Rep(j,2) mv(op[j][p[j]].fi,dis*((j==1)?-1:1),nx,ny);
		Rep(j,2) i[j]+=dis;
		
		if(x==0 && y==0) {
			if(nx==0 &&ny==0) ans+=dis;
		}else {
			if(o(x,y,nx,ny)) {
				++ans;
			}
		}
		x=nx,y=ny;
		elen+=dis;
	}
	
	cout<<ans<<endl;
	
	return 0;
}

E Yacht

#include<bits/stdc++.h> 
#include<atcoder/all>
using namespace std;
using namespace atcoder;

#define For(i,n) for(int i=1;i<=n;i++)
#define Fork(i,k,n) for(int i=k;i<=n;i++)
#define ForkD(i,k,n) for(int i=n;i>=k;i--)
#define Rep(i,n) for(int i=0;i<n;i++)
#define ForD(i,n) for(int i=n;i;i--)
#define RepD(i,n) for(int i=n;i>=0;i--)
#define Forp(x) for(int p=pre[x];p;p=next[p])
#define Forpiter(x) for(int &p=iter[x];p;p=next[p])  
#define Lson (o<<1)
#define Rson ((o<<1)+1)
#define MEM(a) memset(a,0,sizeof(a));
#define MEMI(a) memset(a,0x3f,sizeof(a));
#define MEMi(a) memset(a,128,sizeof(a));
#define MEMx(a,b) memset(a,b,sizeof(a));
#define INF (0x3f3f3f3f)
#define F (1000000007)
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define vi vector<int> 
#define pi pair<int,int>
#define SI(a) ((a).size())
#define Pr(kcase,ans) printf("Case #%d: %lld\n",kcase,ans);
#define PRi(a,n) For(i,n-1) cout<<a[i]<<' '; cout<<a[n]<<endl;
#define PRi2D(a,n,m) For(i,n) { \
						For(j,m-1) cout<<a[i][j]<<' ';\
						cout<<a[i][m]<<endl; \
						} 
#pragma comment(linker, "/STACK:102400000,102400000")
#define ALL(x) (x).begin(),(x).end()
#define gmax(a,b) a=max(a,b);
#define gmin(a,b) a=min(a,b);
#ifdef DEBUG
#define _GLIBCXX_DEBUG
#endif

#define DEBUG
int recur_depth = 0;
#ifdef DEBUG
#define dbg(x) {++recur_depth; auto x_=x; --recur_depth; cerr<<string(recur_depth, '\t')<<"\e[91m"<<__func__<<":"<<__LINE__<<"\t"<<#x<<" = "<<x_<<"\e[39m"<<endl;}
#else
#define dbg(x)
#endif
template<typename Ostream, typename Cont>
typename enable_if<is_same<Ostream,ostream>::value, Ostream&>::type operator<<(Ostream& os,  const Cont& v){
	os<<"[";
	for(auto& x:v){os<<x<<", ";}
	return os<<"]";
}
template<typename Ostream, typename ...Ts>
Ostream& operator<<(Ostream& os,  const pair<Ts...>& p){
	return os<<"{"<<p.first<<", "<<p.second<<"}";
}

typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
ll mul(ll a,ll b){return (a*b)%F;}
ll add(ll a,ll b){return (a+b)%F;}
ll sub(ll a,ll b){return ((a-b)%F+F)%F;}
void upd(ll &a,ll b){a=(a%F+b%F)%F;}
inline int read()
{
	int x=0,f=1; char ch=getchar();
	while(!isdigit(ch)) {if (ch=='-') f=-1; ch=getchar();}
	while(isdigit(ch)) { x=x*10+ch-'0'; ch=getchar();}
	return x*f;
} 

void dfs(vi v) {
	
}
int main()
{
//	freopen("A.in","r",stdin);
//	freopen(".out","w",stdout);
	
	int n=6;
	vi a(6);
	Rep(i,n) a[i]=read();
	map<pair<int,vi > ,double> h;
	auto dfs = [&] (auto self,int T, vi v) -> double{
		sort(ALL(v));
		if(h.count({T,v})) return h[{T,v}];
		if(T==3 || SI(v)==5){
			if(SI(v)==5){
				T=3;
				map<int,int> h1;
				for(auto p:v) h1[a[p]]++;
				double ans=0;
				for(auto [a,b]:h1) gmax(ans,(double)a*b)
				return h[{T,v}]=ans;
			}else return 0;
		}
		double ans=0;
		int lef=5-SI(v);
		int S=1;
		For(i,lef) S*=6;
		Rep(st,S) {
			vi q;
			for(int i=0,j=st;i<lef;i++) {
				q.pb(j%6);
				j/=6;
			}
			double c=0;
			Rep(i,1<<lef) {
				auto tmp=v;
				Rep(j,lef) if((i>>j)&1) tmp.pb(q[j]);
				gmax(c,self(self,T+1,tmp))
			}
			ans+=c; 
		}
		ans/=S;
		return h[{T,v}]=ans;	
	};
	cout<<dfs(dfs,3,{1,2,3,4,5})<<endl;
	cout<<setprecision(20)<<fixed<<dfs(dfs,0,{})<<endl;
	return 0;
}


F Erase between X and Y

#include<bits/stdc++.h>
using namespace std;
#define For(i,n) for(int i=1;i<=n;i++)
#define Fork(i,k,n) for(int i=k;i<=n;i++)
#define Rep(i,n) for(int i=0;i<n;i++)
#define ForD(i,n) for(int i=n;i;i--)
#define RepD(i,n) for(int i=n;i>=0;i--)
#define Forp(x) for(int p=pre[x];p;p=next[p])
#define Forpiter(x) for(int &p=iter[x];p;p=next[p])  
#define Lson (x<<1)
#define Rson ((x<<1)+1)
#define MEM(a) memset(a,0,sizeof(a));
#define MEMI(a) memset(a,127,sizeof(a));
#define MEMi(a) memset(a,128,sizeof(a));
#define INF (2139062143)
#define F (100000007)
#define MAXN (600000+10)
typedef long long ll;
ll mul(ll a,ll b){return (a*b)%F;}
ll add(ll a,ll b){return (a+b)%F;}
ll sub(ll a,ll b){return (a-b+(a-b)/F*F+F)%F;}
int modF(int a,int b){return (a+a/F*F+F)%F;}
void upd(ll &a,ll b){a=(a%F+b%F)%F;}

int n,m;
int a[MAXN];
class Splay
{
public:
	int father[MAXN],siz[MAXN],n;
	int ch[MAXN][2],val[MAXN];
	bool root[MAXN],rev[MAXN];	
	int addv[MAXN],minv[MAXN]; ll sumv[MAXN];
	int roo; //root
	void mem(int _n)
	{
		MEM(father) MEM(siz) MEM(root) MEM(rev)	MEM(ch) MEM(val) flag=0; MEM(addv) MEM(minv)
		n=0; 
		roo=1; 
		build(roo,1,_n,0);root[1]=1;
	}
	void newnode(int &x,int f,int v)
	{
		x=++n;
		father[x]=f;
		val[x]=minv[x]=sumv[x]=v;siz[x]=1;
	}
	
	void build(int &x,int L,int R,int f)
	{
		if (L>R) return ;
		int m=(L+R)>>1;
		newnode(x,f,a[m]); 
		build(ch[x][0],L,m-1,x);
		build(ch[x][1],m+1,R,x);
		maintain(x);
	}
	int getkth(int x,int k)
	{
		pushdown(x); 
		int t;
		if (ch[x][0]) t=siz[ch[x][0]]; else t=0;
		
		if (t==k-1) return x;
		else if (t>=k) return getkth(ch[x][0],k);
		else return getkth(ch[x][1],k-t-1);
		
	}
	
	
	void pushdown(int x)
	{
	}
	void maintain(int x)
	{
		siz[x]=siz[ch[x][0]]+siz[ch[x][1]]+1;
		sumv[x]=minv[x]=val[x];
		
		if (ch[x][0]) minv[ x ]=min(minv[x],minv[ ch[x][0] ] + addv[x] ),sumv[x]+=sumv[ch[x][0]];
		if (ch[x][1]) minv[ x ]=min(minv[x],minv[ ch[x][1] ] + addv[x] ),sumv[x]+=sumv[ch[x][1]];;
	}
	void rotate(int x)
	{
		int y=father[x],kind=ch[y][1]==x;
		
		pushdown(y); pushdown(x);
			
		ch[y][kind]=ch[x][!kind];
		if (ch[y][kind]) {
			father[ch[y][kind]]=y;
		}
		father[x]=father[y];
		father[y]=x;
		ch[x][!kind]=y;
		if (root[y])
		{
			root[x]=1;root[y]=0;roo=x;
		}
		else
		{
			ch[father[x]][ ch[father[x]][1]==y ] = x;
		}
		maintain(y);maintain(x);
	}
	void splay(int x)
	{
		while(!root[x])
		{
			int y=father[x];
			int z=father[y];
			if (root[y]) rotate(x);
			else if ( (ch[y][1]==x)^(ch[z][1]==y) )
			{
				rotate(x); rotate(x);
			} 
			else 
			{
				rotate(y); rotate(x);
			}
		}
		roo=x;
	}
	void splay(int x,int r)
	{
		while(!(father[x]==r))
		{
			int y=father[x];
			int z=father[y];
			if (father[y]==r) rotate(x);
			else if ( (ch[y][1]==x)^(ch[z][1]==y) )
			{
				rotate(x); rotate(x);
			} 
			else 
			{
				rotate(y); rotate(x);
			}
		}
	}
	
	void Cut(int a,int b,int c)
	{
		int x=getkth(roo,a),y=getkth(roo,b);
		splay(x);
		splay(y,roo);
		pushdown(x);pushdown(y);
		int z=ch[y][0];
		ch[y][0]=0; maintain(y); maintain(x);
		
		int u=getkth(roo,c),v=getkth(roo,c+1);
		splay(u);
		splay(v,roo);
		pushdown(u);pushdown(v);
		ch[v][0]=z;father[z]=v;
		maintain(v);maintain(u);
		
	}
	
	void Flip(int a,int b)
	{
		int x=getkth(roo,a),y=getkth(roo,b);
		splay(x);
		splay(y,roo);
		pushdown(x);pushdown(y);
		int z=ch[y][0];
		rev[z]^=1;
		maintain(y); maintain(x);
	} 
	
	void Add(int a,int b,int c)
	{
		int x=getkth(roo,a),y=getkth(roo,b);
		splay(x);
		splay(y,roo);
		pushdown(x);pushdown(y);
		int z=ch[y][0];
		addv[z]+=c; val[z]+=c; minv[z]+=c;
		maintain(y); maintain(x);
	} 
	
	int queryMin(int a,int b)
	{
		int x=getkth(roo,a),y=getkth(roo,b);
		splay(x);
		splay(y,roo);
		pushdown(x);pushdown(y);
		int z=ch[y][0];		
		maintain(y); maintain(x);
		return minv[z];
	} 
	int get_kth(int x) {
		splay(x);
		int t;
		if (ch[x][0]) t=siz[ch[x][0]]; else t=0;
		return 1+t;
	}
	
	int insert(int a,int P)
	{
		int x=getkth(roo,a),y=getkth(roo,a+1);
		splay(x);
		splay(y,roo);
		pushdown(x);pushdown(y);
		newnode(ch[y][0],y,P);
		int temp=n;
		maintain(y); maintain(x);
		return temp;
	}
	int insert2(int a,int P)
	{
		int x=a;
		splay(x);
		int cc=siz[ch[x][0]]+2;
		int y=getkth(roo,cc);
		splay(y,roo);
		pushdown(x);pushdown(y);
		newnode(ch[y][0],y,P);
		int temp=n;
		maintain(y); maintain(x);
		return temp;
	}
	
	ll Delete(int a,int b)
	{
		int x=getkth(roo,a),y=getkth(roo,b);
		splay(x);
		splay(y,roo);
		pushdown(x);pushdown(y);
		ll p=sumv[ch[y][0]];
		int z=ch[y][0];
		ch[y][0]=0; father[z]=0; maintain(y); maintain(x);
		return p;
	}
	ll Delete2(int a,int b)
	{
		int x=a,y=b;
		splay(x);
		splay(y,roo);
		pushdown(x);pushdown(y);
		ll p=sumv[ch[y][0]];
		int z=ch[y][0];
		ch[y][0]=0; father[z]=0; maintain(y); maintain(x);
		return p;
	}
	
	bool flag;
	void print(int x)
	{
		if (x==0) return ;
		pushdown(x);
		print(ch[x][0]);
		printf("%d ",val[x]);
		print(ch[x][1]);	
	} 
}S;

int main()
{
//	freopen(".in","r",stdin);
//	freopen(".out","w",stdout);
	
	a[1]=a[2]=1e9;
	S.mem(2);
	
	map<int,int> h;
	h[0]=S.insert(1,0);
	int q;cin>>q;
	For(i,q) {
		int op;cin>>op;
		if(op==1) {
			int x;cin>>x;
			int id=h[x];
			int tmp=S.insert2(id,i);
			h[i]=tmp;
			
			
		}else {
			int x,y;cin>>x>>y;
			int id1=h[x],id2=h[y];
			int k1=S.get_kth(id1),k2=S.get_kth(id2);
			if(k1>k2) swap(k1,k2);
			cout<<S.Delete(k1,k2)<<endl;
		}
//		S.print(S.roo);cout<<endl;		
	}
	
	
	return 0;
}


G Increase to make it Increasing


```cpp
#include<bits/stdc++.h> 
#include<atcoder/all>
using namespace std;
using namespace atcoder;

#define For(i,n) for(int i=1;i<=n;i++)
#define Fork(i,k,n) for(int i=k;i<=n;i++)
#define ForkD(i,k,n) for(int i=n;i>=k;i--)
#define Rep(i,n) for(int i=0;i<n;i++)
#define ForD(i,n) for(int i=n;i;i--)
#define RepD(i,n) for(int i=n;i>=0;i--)
#define Forp(x) for(int p=pre[x];p;p=next[p])
#define Forpiter(x) for(int &p=iter[x];p;p=next[p])  
#define Lson (o<<1)
#define Rson ((o<<1)+1)
#define MEM(a) memset(a,0,sizeof(a));
#define MEMI(a) memset(a,0x3f,sizeof(a));
#define MEMi(a) memset(a,128,sizeof(a));
#define MEMx(a,b) memset(a,b,sizeof(a));
#define INF (0x3f3f3f3f)
#define F (1000000007)
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define vi vector<int> 
#define pi pair<int,int>
#define SI(a) ((a).size())
#define Pr(kcase,ans) printf("Case #%d: %lld\n",kcase,ans);
#define PRi(a,n) For(i,n-1) cout<<a[i]<<' '; cout<<a[n]<<endl;
#define PRi2D(a,n,m) For(i,n) { \
						For(j,m-1) cout<<a[i][j]<<' ';\
						cout<<a[i][m]<<endl; \
						} 
#pragma comment(linker, "/STACK:102400000,102400000")
#define ALL(x) (x).begin(),(x).end()
#define gmax(a,b) a=max(a,b);
#define gmin(a,b) a=min(a,b);
#ifdef DEBUG
#define _GLIBCXX_DEBUG
#endif

#define DEBUG
int recur_depth = 0;
#ifdef DEBUG
#define dbg(x) {++recur_depth; auto x_=x; --recur_depth; cerr<<string(recur_depth, '\t')<<"\e[91m"<<__func__<<":"<<__LINE__<<"\t"<<#x<<" = "<<x_<<"\e[39m"<<endl;}
#else
#define dbg(x)
#endif
template<typename Ostream, typename Cont>
typename enable_if<is_same<Ostream,ostream>::value, Ostream&>::type operator<<(Ostream& os,  const Cont& v){
	os<<"[";
	for(auto& x:v){os<<x<<", ";}
	return os<<"]";
}
template<typename Ostream, typename ...Ts>
Ostream& operator<<(Ostream& os,  const pair<Ts...>& p){
	return os<<"{"<<p.first<<", "<<p.second<<"}";
}

typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
ll mul(ll a,ll b){return (a*b)%F;}
ll add(ll a,ll b){return (a+b)%F;}
ll sub(ll a,ll b){return ((a-b)%F+F)%F;}
void upd(ll &a,ll b){a=(a%F+b%F)%F;}
inline int read()
{
	int x=0,f=1; char ch=getchar();
	while(!isdigit(ch)) {if (ch=='-') f=-1; ch=getchar();}
	while(isdigit(ch)) { x=x*10+ch-'0'; ch=getchar();}
	return x*f;
} 
int main()
{
//	freopen("A.in","r",stdin);
//	freopen(".out","w",stdout);
    int n, m;
    cin >> n >> m;
	vector<int> a(n);
	Rep(i,n) cin>>a[i];
	ForD(i,n-1) a[i]=a[i]-a[i-1];
	
    mcf_graph<int, long long> g(n + 2);
    int s = n, t = n + 1;

	ll p=0;
    for (int i = 0; i < n; i++) {
		if(a[i]<0)
        	g.add_edge(s, i, -a[i], 0),p+=-a[i];
        else g.add_edge(i, t, a[i], 0);
    }
    dbg(a)
	Rep(i,m) {
		ll a,b;cin>>a>>b;
		a--;
		if(b<n)g.add_edge(a,b,INF,1);
		else g.add_edge(a,t,INF,1);
	}
	

    auto result = g.flow(s, t);
    if(result.fi!=p) {
    	puts("-1");
	}
    else cout << result.se << endl;


	return 0;
}


评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值