A Tender Carpenter

给一个数列分是否存在至少2种划分方式使得划分后每个子段中的数任取(可重复取)3个都能作为三角形的3条边长。

#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 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);
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 f[202],a[202];
int main()
{
//	freopen("A.in","r",stdin);
//	freopen(".out","w",stdout);
	
	int T=read(),n;
	while(cin>>n) {
		For(i,n) cin>>a[i];
		f[0]=1;bool flag=0;
		For(i,n-1) {
			int x=a[i],y=a[i+1];
			if(x>y) swap(x,y);
			if(x*2>y) flag=1;
		}
		puts(flag?"Yes":"No");
	}
	return 0;
}

B. Outstanding Impressionist

#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 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);
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;
} 
#define MAXN (412345)
int l[MAXN],r[MAXN],ans[MAXN];
int cnt[MAXN]={},s[MAXN]={};
int main()
{
//	freopen("A.in","r",stdin);
//	freopen(".out","w",stdout);
	
	int T=read(),n;
	while(cin>>n) {
		For(i,n) l[i]=read(),r[i]=read();
		int m=2*n;
		For(i,2*n) cnt[i]=s[i]=0;
		For(i,n) if(l[i]==r[i]) cnt[l[i]]++;
		For(i,m) s[i]=s[i-1]+(cnt[i]>0);
		For(i,n) {
			if(l[i]==r[i]) putchar(cnt[l[i]]==1?'1':'0');
			else putchar((s[r[i]]-s[l[i]-1]<r[i]-l[i]+1)?'1':'0');
		}cout<<endl;
	}
	return 0;
}

C Bewitching Stargazer

#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 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);
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;
} 
#define MAXN (412345)
int f[MAXN],g[MAXN],sz=0;
int main()
{
//	freopen("A.in","r",stdin);
//	freopen(".out","w",stdout);
	
	int T=read();ll n,k;
	while(T--) {
		cin>>n>>k;
		ll ans=0,nown=n;sz=0;
		while(nown>=k) {
			int nowlen=nown/2;
			f[++sz]=nown%2;g[sz]=nown;
			ll p=1;
			if(f[sz]) {
				ll t=nowlen+1;
				for(int j=sz-1;j>=1;j--) {
					if(f[j]==0) {
						t=t*2+(g[j]/2)*p; p*=2;
					}else {
						t=t*2+(g[j]/2+1)*p;p*=2;
					}
				}
				ans+=t;
			}
			nown/=2;
		}
		cout<<ans<<endl;
 	}
	return 0;
}

D D. Refined Product Optimality

#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 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);
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;
} 
#define MAXN (412345)
ll a[MAXN],b[MAXN];

template<int MOD, int RT> struct mint {
	static const int mod = MOD;
	static constexpr mint rt() { return RT; } // primitive root for FFT
	int v; explicit operator int() const { return v; } // explicit -> don't silently convert to int
	mint():v(0) {}
	mint(ll _v) { v = int((-MOD < _v && _v < MOD) ? _v : _v % MOD);
		if (v < 0) v += MOD; }
	bool operator==(const mint& o) const {
		return v == o.v; }
	friend bool operator!=(const mint& a, const mint& b) { 
		return !(a == b); }
	friend bool operator<(const mint& a, const mint& b) { 
		return a.v < b.v; }
   
	mint& operator+=(const mint& o) { 
		if ((v += o.v) >= MOD) v -= MOD; 
		return *this; }
	mint& operator-=(const mint& o) { 
		if ((v -= o.v) < 0) v += MOD; 
		return *this; }
	mint& operator*=(const mint& o) { 
		v = int((ll)v*o.v%MOD); return *this; }
	mint& operator/=(const mint& o) { return (*this) *= inv(o); }
	friend mint pow(mint a, ll p) {
		mint ans = 1; assert(p >= 0);
		for (; p; p /= 2, a *= a) if (p&1) ans *= a;
		return ans; }
	friend mint inv(const mint& a) { assert(a.v != 0); 
		return pow(a,MOD-2); }
		
	mint operator-() const { return mint(-v); }
	mint& operator++() { return *this += 1; }
	mint& operator--() { return *this -= 1; }
	friend mint operator+(mint a, const mint& b) { return a += b; }
	friend mint operator-(mint a, const mint& b) { return a -= b; }
	friend mint operator*(mint a, const mint& b) { return a *= b; }
	friend mint operator/(mint a, const mint& b) { return a /= b; }
};
const int MOD=998244353; 
using mi = mint<MOD,5>; // 5 is primitive root for both common mods
ll na[MAXN],nb[MAXN];
void write(int x) {
  static int sta[35];
  int top = 0;
  do {
    sta[top++] = x % 10, x /= 10;
  } while (x);
  while (top) putchar(sta[--top] + 48);  // 48 是 '0'
}
int main()
{

//	freopen("d.in","r",stdin);	
	int T;cin>>T;ll n,q;
	while(T--) {
		n=read(),q=read();
		For(i,n) a[i]=read();
		For(i,n) b[i]=read();
		For(i,n) na[i]=a[i],nb[i]=b[i];
		sort(na+1,na+1+n);
		sort(nb+1,nb+1+n);
		mi ans=1;
		For(i,n) ans*=min(na[i],nb[i]);
			write(ans.v);
		For(i,q) {
			int op=read(),id=read();
			if(op==1) {
				int pos=upper_bound(na+1,na+1+n,a[id])-na;
				--pos;
				ans/=min(na[pos],nb[pos]);
				na[pos]++;
				ans*=min(na[pos],nb[pos]);
				a[id]++;
			}else {
				int pos=upper_bound(nb+1,nb+1+n,b[id])-nb;
				--pos;
				ans/=min(na[pos],nb[pos]);
				nb[pos]++;
				ans*=min(na[pos],nb[pos]);
				b[id]++;
			}
			putchar(' ');
			write(ans.v);
		}puts("");
	
 	}
	return 0;
}

E Resourceful Caterpillar Sequence

给一个数,2人玩一个游戏,选定树上一条简单路径,双方各拽一边,每次可以把整条路径整体移动一条边。谁的端点最先到达叶子谁赢(同时或永远到不了算平)。问后手胜利的合法方案数。
树dp
Good Bye 2024: 2025 is NEAR 题解_#define是路径包括u到父亲的那条边,则u所在的方向有多少点不是叶子且向下拉一次到不了叶子。
Good Bye 2024: 2025 is NEAR 题解_#define_02是路径包括u到父亲的那条边,则父亲所在的方向有多少点不是叶子且能拉1次到不了叶子。

#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 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>0;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,cal) printf("Case #%d: %lld\n",kcase,cal);
#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);
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
#define add(a,b) ((a+b)%F)
ll mul(ll a,ll b){return (a*b)%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;
} 

#define MAXN (212345)

vi v[MAXN];
ll is_lef[MAXN],is_lef_neb[MAXN];
ll sz[MAXN],md[MAXN],mu[MAXN],maxd[MAXN];
ll ans=0;
ll f[MAXN];
ll n,md2[MAXN];
void dfs(int x,int fa) {
	sz[x]=1;is_lef_neb[x]=0;
	f[x]=fa;
	for (auto u:v[x])
		if (u!=fa) {
			dfs(u,x);
			gmax(maxd[x],1+maxd[u])
			md[x]+=md[u];sz[x]+=sz[u];
			if(sz[u]==1) is_lef_neb[x]++;
	}
	is_lef[x]=sz[x]==1;
	md2[x]=md[x];
	if(sz[x]>1 &&!is_lef_neb[x]) ++md[x]; 
}
void dfs3(int x,int fa) {
	for (auto u:v[x])
		if (u!=fa) {
			mu[u]=md2[x]-md[u];
			mu[u]+=mu[x];
			if(SI(v[x])!=1 && !is_lef_neb[x] && (fa==-1 || SI(v[fa])!=1 )) ++mu[u];
			if(!is_lef[u] && is_lef_neb[x]) ans+=mu[u];
			dfs3(u,x);

	}
	

}
void dfs2(int x,int fa) {
	for (auto u:v[x])
		if (u!=fa) {
			dfs2(u,x);
			if(is_lef_neb[u]) {
				ans+=md[u];
			}
	}
}
int main()
{
//	freopen("e.in","r",stdin);
//	freopen(".out",w",stdout);
	int T=read();
	while(T--) {
		n=read();
		For(i,n-1) {
			int x=read(),y=read();
			v[x].pb(y); v[y].pb(x);
		}
		For(i,n) is_lef[i]=is_lef_neb[i]=sz[i]=md[i]=mu[i]=maxd[i]=f[i]=md2[i]=0;
		int rot=1;
		For(i,n) if(SI(v[i])!=1) rot=i;
		if(SI(v[rot])==1) {
			cout<<0<<endl;
			
		}else{
			ans=0;
			ll id0=0;
			For(i,n) id0+=SI(v[i])==1;
			ans=id0*(n-id0); //0 path;
			dfs(rot,-1);
			dfs2(rot,-1);
			dfs3(rot,-1);
			cout<<ans<<endl;
		}
		For(i,n) v[i].resize(0);
	}
	
	
	return 0;
}