跳到正文

个人记录

Public Judge NOIP Round #4 Sol Set

OtoriEmu约 13 分钟
原文
文章信息与版本

文章信息

发布
2022年11月21日
收录
2026年7月8日
更新
2026年11月9日
篇幅
约 13 分钟
获赞
1

版本

  1. v1当前

关于为什么不写 #3:因为感觉没啥好玩的,挂的还多。

关于为什么写 #4:挂的太好玩了。


治病

对于每一种药片动态开点,区间查询只被覆盖了一次的位置,这个很好处理。新开的结点的总个数一样是 O(Vlog⁡V)O(V \log V) 的。注意不要全开 long long。

C++
/*
わんわん……わんだほーいっ☆
Wonderhoy!
*/
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef double DB;
char buf[1<<21],*p1=buf,*p2=buf;
#define getchar() (p1==p2 && (p2=(p1=buf)+fread(buf,1,1<<18,stdin),p1==p2)?EOF:*p1++)
int read()
{
	int x=0;
	char c=getchar();
	while(c<'0' || c>'9')	c=getchar();
	while(c>='0' && c<='9')	x=(x<<1)+(x<<3)+(c^'0'),c=getchar();
	return x;
}
void write(LL x)
{
	if(x>9)	write(x/10);
	putchar(x%10+'0');
}
const int C=1e6;
int n,m;
LL c[500005];
int L[500005],R[500005];
vector<int> F[500005];
LL ans[500005];
int rt,tot;
int tr[1000005*40],lc[1000005*40],rc[1000005*40];
#define Mm int mid=(l+r)>>1
void modify(int l,int r,int &now,int x,int y)
{
	if(!now)	now=++tot;
	if(x<=l && r<=y)	return ++tr[now],void();
	Mm;
	if(x<=mid)	modify(l,mid,lc[now],x,y);
	if(mid<y)	modify(mid+1,r,rc[now],x,y);
}
void clear(int l,int r,int now)
{
	if(!now)	return ;
	tr[now]=0;
	Mm;
	clear(l,mid,lc[now]),clear(mid+1,r,rc[now]);
}
LL build(int l,int r,int now,int w=0)
{
	if(!now)	return 0;
	w+=tr[now];
	if(w>1)	return clear(l,r,now),r-l+1;
	if(w==1)	tr[now]=r-l+1;
	Mm;
	if(w==0)
	{
		build(l,mid,lc[now],w),build(mid+1,r,rc[now],w);
		tr[now]=tr[lc[now]]+tr[rc[now]];
		return 0;
	}
	tr[now]-=build(l,mid,lc[now],w)+build(mid+1,r,rc[now],w);
	return r-l+1-tr[now];
}
int query(int l,int r,int now,int x,int y)
{
	if(!now)	return 0;
	if(x<=l && r<=y)	return tr[now];
	Mm,ret=0;
	if(x<=mid)	ret+=query(l,mid,lc[now],x,y);
	if(mid<y)	ret+=query(mid+1,r,rc[now],x,y);
	return ret;
}
int main(){
//	freopen("doctor.in","r",stdin);
//	freopen("doctor.out","w",stdout);
	n=read(),m=read();
	for(int i=1;i<=m;++i)	c[i]=read();
	for(int i=1;i<=n;++i)
	{
		int l=read(),r=read(),p=read();
		L[i]=l,R[i]=r;
		while(p-->0)	F[read()].push_back(i);
	}
	LL sum=0;
	for(int i=1;i<=m;++i)
	{
		for(int j=1;j<=tot;++j)	tr[j]=lc[j]=rc[j]=0;
		rt=tot=0;
		sort(F[i].begin(),F[i].end(),[&](int u,int v){return L[u]<L[v] || (L[u]==L[v] && R[u]<R[v]);});
		LL csum=0;
		int lst=-1;
		for(auto x:F[i])	if(R[x]>lst)	csum+=R[x]-max(L[x]-1,lst),lst=R[x];
		for(auto x:F[i])
		{
			int l=L[x],r=R[x];
			modify(1,C,rt,l,r);
		}
		build(1,C,rt);
		sum+=csum*c[i];
		for(auto x:F[i])
		{
			int l=L[x],r=R[x];
			ans[x]+=c[i]*query(1,C,rt,l,r);
		}
	}
	for(int i=1;i<=n;++i)	write(sum-ans[i]),putchar(i==n?'\n':' ');
	return 0;
}

拓扑序计数

定义 fS,uf_{S,u} 表示 uu 在 SS 构成的拓扑序列的前缀中在最后一个的方案数,gSg_{S} 表示后缀为 SS 构成的方案数。直接转移就好,复杂度 O(2nn2)O(2^nn^2)。

可以对答案的计算过程优化。每次将 SS 的 lowbit 拆出来,对应的 vv 答案先增加,然后将 fS\{v},u←+fS,uf_{S \backslash \{v\},u} \gets^+ f_{S,u} 就好,继续处理。道理很简单,小心 random access(-40)。

C++
/*
わんわん……わんだほーいっ☆
Wonderhoy!
*/
#include<bits/stdc++.h>
//#pragma GCC target("popcnt")
using namespace std;
typedef unsigned long long LL;
typedef double DB;
char buf[1<<21],*p1=buf,*p2=buf;
#define getchar() (p1==p2 && (p2=(p1=buf)+fread(buf,1,1<<18,stdin),p1==p2)?EOF:*p1++)
LL read()
{
	LL x=0;
	char c=getchar();
	while(c<'0' || c>'9')	c=getchar();
	while(c>='0' && c<='9')	x=(x<<1)+(x<<3)+(c^'0'),c=getchar();
	return x;
}
void write(LL x)
{
	if(x>9)	write(x/10);
	putchar(x%10+'0');
}
void Solve();
LL id[1<<20];
int main(){
//	freopen("topo.in","r",stdin);
//	freopen("topo.out","w",stdout);
	for(LL i=0;i<20;++i)	id[1<<i]=i;
	LL T=read();
	while(T-->0)	Solve();
	return 0;
}
LL f[1<<20],g[1<<20];
LL ef[1<<20][25];
LL n,m,eu[405],ev[405],pref[25],preg[25];
LL ans[25][25],U,fans[1<<20][25];
void Solve()
{
	n=read(),m=read();
	U=1llu<<n;
	for(LL i=0;i<n;++i)	pref[i]=preg[i]=0;
	for(LL i=1;i<=m;++i)	eu[i]=read()-1,ev[i]=read()-1,pref[ev[i]]|=1<<eu[i],preg[eu[i]]|=1<<ev[i];
	for(LL i=0;i<n;++i)	for(LL j=0;j<n;++j)	if((pref[i]>>j)&1)	pref[i]|=pref[j];
	for(LL i=n-1;~i;--i)	for(LL j=0;j<n;++j)	if((preg[i]>>j)&1)	preg[i]|=preg[j];
	for(LL S=0;S<U;++S)	f[S]=g[S]=0;
	for(LL S=0;S<U;++S)	for(LL i=0;i<n;++i)	ef[S][i]=0;
	for(LL i=0;i<n;++i)	if(!pref[i])	f[1<<i]=ef[1<<i][i]=1;
	for(LL i=0;i<n;++i)	if(!preg[i])	g[1<<i]=1;
	for(LL i=0;i<n;++i)	for(LL j=0;j<n;++j)	ans[i][j]=0;
	for(LL S=0;S<U;++S)
	{
		for(LL i=0;i<n;++i)
		{
			if((S>>i)&1)	continue;
			if((S&pref[i])!=pref[i])	continue;
			f[S|(1<<i)]+=f[S];
			ef[S|(1<<i)][i]+=f[S];
		}
	}
	for(LL S=0;S<U;++S)
	{
		for(LL i=0;i<n;++i)
		{
			if((S>>i)&1)	continue;
			if((S&preg[i])!=preg[i])	continue;
			g[S|(1<<i)]+=g[S];
		}
	}
	for(LL S=0;S<U;++S)	for(LL u=0;u<n;++u)	fans[(U-1)^S][u]=ef[S][u]*g[S^(U-1)];
	for(LL S=U-1;S;--S)
	{
		for(LL u=0;u<n;++u)
		{
			LL p=S&(-S),v=id[p];
			fans[S^p][u]+=fans[S][u];
			ans[v][u]+=fans[S][u];
		}
	}
	for(LL i=0;i<n;++i)	for(LL j=0;j<n;++j)	write(ans[i][j]),putchar(j==n-1?'\n':' ');
}

序列

傻逼题,直接二分。

可以通过单调栈+并查集之类的东西把复杂度均摊掉,也可以用单调栈直接做到线性,反正是个没啥意思的题。带 log 反而比线性跑得快。贴个带 log 的算了。

C++
/*
わんわん……わんだほーいっ☆
Wonderhoy!
*/
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef double DB;
char buf[1<<21],*p1=buf,*p2=buf;
#define getchar() (p1==p2 && (p2=(p1=buf)+fread(buf,1,1<<18,stdin),p1==p2)?EOF:*p1++)
int read()
{
	int x=0,f=1;
	char c=getchar();
	while(c<'0' || c>'9')	f=(c=='-'?-1:f),c=getchar();
	while(c>='0' && c<='9')	x=(x<<1)+(x<<3)+(c^'0'),c=getchar();
	return x*f;
}
void write(int x)
{
	if(x>9)	write(x/10);
	putchar(x%10+'0');
}
int n,m,a[1000005];
int st1[20][1000005],st2[20][1000005];
inline int querymin(int l,int r)
{
	int d=__lg(r-l+1);
	return min(st1[d][l],st1[d][r-(1<<d)+1]);
}
inline int querymax(int l,int r)
{
	int d=__lg(r-l+1);
	return max(st2[d][l],st2[d][r-(1<<d)+1]);
}
inline bool check(int l,int r){return querymax(l,r)+querymin(l,r)>r-l+1;}
int calc()
{
	for(int i=1;i<=n;++i)	st1[0][i]=st2[0][i]=a[i];
	for(int i=1;(1<<i)<=n;++i)	for(int j=1;j+(1<<i)-1<=n;++j)	st1[i][j]=min(st1[i-1][j],st1[i-1][j+(1<<(i-1))]),st2[i][j]=max(st2[i-1][j],st2[i-1][j+(1<<(i-1))]);
	int ans=0;
	for(int i=1;i<=n;++i)	while(i-ans>=1 && check(i-ans,i))	++ans;
	return ans;
}
int main(){
//	freopen("loose.in","r",stdin);
//	freopen("loose.out","w",stdout);
	n=read(),m=read();
	for(int i=1;i<=n;++i)	a[i]=read();
	write(calc()),puts("");
	for(int i=1;i<=m;++i)
	{
		int c=read();
		while(c-->0)
		{
			int x=read(),y=read();
			swap(a[x],a[y]);
		}
		write(calc()),puts("");
	}
	return 0;
}

水果

这个题好像有点高,具体的思路是写一个显然的 O(n2)O(n^2) DP,然后线段树优化转移。maybe very tricky,有时间补吧。

原文
存档评论1 条抓取于