Demo entry 6657902

none

   

Submitted by anonymous on Nov 05, 2017 at 14:05
Language: C++. Code size: 30.9 kB.

ÃàÑôÖÐѧʵÑéѧУ
2
0
1
7
N
O
I
P
¿¼²é֪ʶµãÄ£°å





ͼÂÛƪ
Astar:
SpfaÔ¤´¦Àí³ötµãµ½ÆäËûµãµÄ¹À¼Ûº¯Êý£º
void spfa(){
	memset(h,0x3f,sizeof(h));
	queue<int>q;q.push(t);h[t]=0;vis[s]=true;
	while(!q.empty()){
		int now=q.front();q.pop();vis[now]=false;
		for(int i=head[now];i;i=e[i].next){
			if(h[e[i].to]>h[now]+e[i].w)
¡¡¡¡¡¡¡¡¡¡¡¡¡¡{
				h[e[i].to]=h[now]+e[i].w;
				if(!vis[e[i].to])
				q.push(e[i].to);vis[e[i].to]=true;
			}
		}
	}
}
void Astar()
{
	priority_queue<node>q;
	node a;a.g=0;a.x=s;
	q.push(a);
	while(!q.empty()){
		node last=q.top();q.pop();
		if(last.g+h[last.x]>ans)continue;
		if(last.x==t){
			if(last.g<ans) ans=last.g;
			if(last.g==ans) tot++;
			continue;
		}
		for(int i=head[last.x];i;i=e[i].next){
			node now=last;now.x=e[i].to;now.g+=e[i].w;
			if(now.g+h[now.x]>ans) continue;q.push(now);
		}
	}
}
ÍøÂçÁ÷dinic£º
void adde(int u,int v,int w){
	e[++k].next=head[u];e[k].to=v;head[u]=k;e[k].w=w;
	e[++k].next=head[v];e[k].to=u;head[v]=k;e[k].w=0;
}
bool bfs(int s,int t){
	memset(dis,-1,sizeof(dis));
	queue<int>q;dis[s]=0;
	q.push(s);
	while(!q.empty()){
		int now=q.front();q.pop();
		for(int i=head[now];i;i=e[i].next)
		{
			if(dis[e[i].to]==-1 && e[i].w)
			{
				dis[e[i].to]=dis[now]+1;
				if(e[i].to==t)return true;
				q.push(e[i].to);
			}
		}
	}return false;
}
int dfs(int x,int f)
{
	if(x==t) return f;
	int use=f;
	for(int i=head[x];i;i=e[i].next)
	{
		if(dis[e[i].to]==dis[x]+1 && use && e[i].w)
		{
			int flow=dfs(e[i].to,min(e[i].w,use));
			use-=flow;e[i].w-=flow;e[i^1].w+=flow;
		}
	}return f-use;
}
Tarjan_LCA:
void adde(int u,int v){
	e[++k].next=head[u];
	e[k].to=v;head[u]=k;
}
void qadd(int u,int v,int num){
	q[++k].next=qhead[u];q[k].num=num;
	q[k].to=v;qhead[u]=k;
}
int find(int x){
	if(fa[x]==x) return x;
	return fa[x]=find(fa[x]);
}
void dfs(int x,int f){
	for(int i=head[x];i;i=e[i].next){
		if(e[i].to==f) continue;
		dfs(e[i].to,x);
		int fx=find(x),fy=find(e[i].to);
		if(fx!=fy) fa[fy]=fx;
	}
	for(int i=qhead[x];i;i=q[i].next){
		if(vis[q[i].to])
		ans[q[i].num]=find(q[i].to);
	}vis[x]=true;
}
int main()
{
	scanf("%d%d%d",&n,&m,&Q);
	for(int i=1;i<=m;i++)
	{ fa[i]=i;
		int u,v;
		scanf("%d%d%d",&u,&v);adde(u,v);adde(v,u);
	}k=1;
	for(int i=1;i<=Q;i++){
		int u,v;
		scanf("%d%d",&u,&v);qadd(u,v,i);qadd(v,u,i);
	}
	dfs(1,1);
	for(int i=1;i<=Q;i++) printf("%d",ans[i]);
}
±¶Ôö_LCA(ÕâÀïÖ»¸ø³öÔ¤´¦ÀíºÍ²éѯ):
void dfs(int x,int f)
{
	fa[x][0]=f;dep[x]=dep[f]+1;
	for(int i=head[x];i;i=e[i].next){
		if(e[i].to==f)continue;
		dfs(e[i].to,x);
	}
}
for(int i=1;i<=20;i++){
	for(int j=1;j<=n;j++){
		fa[j][i]=fa[fa[j][i-1]][i-1];
	}
}
int LCA(int x,int y)
{
	if(dep[x]<dep[y])swap(x,y);
	for(int i=20;i>=0;i--){
		if(dep[fa[x][i]]>=dep[y])x=fa[x][i];
	}if(x==y) return x;
	for(int i=20;i>=0;i--){
		if(fa[x][i]!=fa[y][i])x=fa[x][i],y=fa[y][i];
	}return fa[x][0];
}



´ÎСÉú³ÉÊ÷£º
bool cmp(node a,node b) return a.w<b.w;
int find(int x){
	if(father[x]==x) return x;
	return father[x]=find(father[x]);
}
void adde(int u,int v,long long w){
	e[++k].next=head[u];e[k].w=w;
	e[k].to=v;head[u]=k;
}
void dfs(int x,int f){
	 for(int i=head[x];i;i=e[i].next){
	   if(e[i].to==f)continue;
	   mx[e[i].to][0]=e[i].w;dep[e[i].to]=dep[x]+1;fa[e[i].to][0]=x;
	   dfs(e[i].to,x);
	 }
}
long long get(int x,int y,long long w)
{
	long long ans1=0,ans2=0;
	if(dep[x]<dep[y]) swap(x,y);
	for(int i=23;i>=0;i--)
		if(dep[fa[x][i]]>=dep[y])
		{
	        if(mx[x][i]>ans1)
¡¡¡¡¡¡¡¡¡¡¡¡¡¡ans2=max(ans1,ms[x][i]),ans1=max(ans1,mx[x][i]);
		   if(mx[x][i]<ans1)ans2=max(ans2,mx[x][i]);
		   if(mx[x][i]==ans1)ans2=max(ans2,ms[x][i]);
		   x=fa[x][i];
		}
	if(x==y) return ans1==w?ans2:ans1;
	for(int i=23;i>=0;i--){
		if(fa[x][i]!=fa[y][i]){
	if(mx[x][i]>ans1) ans2=max(ans1,ms[x][i]),ans1=mx[x][i];
			if(mx[x][i]<ans1) ans2=max(ans2,mx[x][i]);
			if(mx[x][i]==ans1)ans2=max(ans2,ms[x][i]);
			if(mx[y][i]>ans1) ans2=max(ans1,ms[y][i]),ans1=mx[y][i];
			if(mx[y][i]<ans1) ans2=max(ans2,mx[y][i]);
			if(mx[y][i]==ans1)ans2=max(ans2,ms[y][i]);
			y=fa[y][i];x=fa[x][i];
		}
	}
	if(mx[x][0]>ans1) ans2=max(ans1,ms[x][0]),ans1=mx[x][0];
	if(mx[x][0]<ans1) ans2=max(ans2,mx[x][0]);
	if(mx[y][0]>ans1) ans2=max(ans1,ms[y][0]),ans1=mx[y][0];
	if(mx[y][0]<ans1) ans2=max(ans2,mx[y][0]);
	return ans1==w?ans2:ans1;
}
int main(){
	scanf("%lld%lld",&n,&m);
	for(int i=1;i<=m;i++){
   father[i]=i;
		scanf("%d%d%lld",&s[i].u,&s[i].v,&s[i].w);
	}sort(s+1,s+m+1,cmp);
	for(int i=1;i<=m;i++)
   {
		int fx=find(s[i].u),fy=find(s[i].v);
		if(fx!=fy)
		{
			adde(s[i].u,s[i].v,s[i].w);adde(s[i].v,s[i].u,s[i].w);
			father[fx]=fy;tot++;visit[i]=true;sum+=s[i].w;
		}
		if(tot==n-1) break;
	}dfs(1,1);
	for(int i=1;i<=23;i++)
		for(int j=1;j<=n;j++)
		{
			fa[j][i]=fa[fa[j][i-1]][i-1];
			mx[j][i]=max(mx[j][i-1],mx[fa[j][i-1]][i-1]);
	if(mx[j][i-1]<mx[fa[j][i-1]][i-1]) ms[j][i]=max(ms[fa[j][i-1]][i-1],mx[j][i-1]);
	if(mx[j][i-1]>mx[fa[j][i-1]][i-1]) ms[j][i]=max(mx[fa[j][i-1]][i-1],ms[j][i-1]);	    if(mx[j][i-1]==mx[fa[j][i-1]][i-1])
     ms[j][i]=max(ms[fa[j][i-1]][i-1],ms[j][i-1]);
		}
	long long ans=0x7f7f7f7f7f7f;
	for(int i=1;i<=m;i++)
	{
		if(!visit[i])
		{
			int g=get(s[i].u,s[i].v,s[i].w);
			ans=min(ans,sum-g+s[i].w);
		}
	}
	printf("%lld\n",ans);
}

¶þ·Öͼ£º
bool dfs(int x){
	for(int i=head[x];i;i=e[i].next){
		if(!vis[e[i].to])
		{
			vis[e[i].to]=true;
			if(link[e[i].to] || dfs(link[e[i].to]))
¡¡¡¡¡¡¡¡¡¡¡¡link[e[i].to]=x;return true;
		}
	}return false;
}
int main(){
	scanf("%d%d",&n,&m);
	for(int i=1;i<=m;i++)
	{
		int u,v;
		scanf("%d%d",&u,&v);adde(u,v);
	}
	for(int i=1;i<=n;i++)
	{
		memset(vis,false,sizeof(vis));
		if(dfs(i)) ans++;
	}
}
tarjanËõµã£º
void dfs(int x)
{
	dfn[x]=low[x]=++clock;
	s[++top]=x;vis[x]=true;
	for(int i=head[x];i;i=e[i].next)
	{
		if(!dfn[e[i].to])
		{
			dfs(e[i].to);
			low[x]=min(low[x],low[e[i].to]);
		}
		else if(vis[e[i].to]) low[x]=min(low[x],dfn[e[i].to]);
	}
	if(dfn[x]==low[x])
	{
		c[x]=++cnt;vis[x]=false;
		while(s[top]!=x)
		{
			c[s[top]]=cnt;vis[s[top--]]=false;
		}top--;
	}
}
int main()
{
	scanf("%d%d",&n,&m);
	for(int i=1;i<=m;i++)
	{
		scanf("%d%d%d",&sh[i].u,&sh[i].v,&sh[i].w);
		adde(sh[i].u,sh[i].v,sh[i].w);
	}
	for(int i=1;i<=n;i++)if(!dfn[i])dfs(i);//Ëõµã 
	memset(head,0,sizeof(head));k=1;//Öع¹Í¼ 
	for(int i=1;i<=m;i++)
	{
		if(c[sh[i].u]!=c[sh[i].v])
		{
			adde(sh[i].u,sh[i].v,sh[i].w);
		}
	}
}

ÊýÂÛƪ
PS: llΪ long long ÀàÐÍ
GCD(×î´ó¹«ÒòÊý):
inline?ll?gcd(ll?a,ll?b)??
{??
????return?b==0?a:gcd(b,a%b);??
}??
inline?ll?exgcd(ll?a,ll?b,ll?&x,ll?&y)??
{??
????if(!b)?{x=1,y=0;return?a;}??
????ll?r=exgcd(b,a%b,x,y);??
????ll?tmp=x;??
????x=y;??
????y=tmp-a/b*y;??
????return?r;??
}??
LCM(×îС¹«±¶Êý):
´óÊý·­±¶·¨£º
inline?ll?fanbei(ll?a,ll?b){??
????if(b>a)?swap(a,b);??
????ll?ans=a;??
????while(ans%b!=0)?ans+=a;??
????return?ans;??
}?	??
LCMÐÔÖÊÇó·¨:
inline?ll?lcm(ll?a,ll?b)??
{??
¡¡return?a*b/gcd(a,b);??
}
EXGCD(À©Õ¹Å·¼¸ÀïµÃ,ÇóͬÓà·½³Ì) 
×îСÕýÕûÊý½âΪ£ºans=(x+mod)%mod
inline?ll?exgcd(ll?a,ll?b,ll?&x,ll?&y)??{??
????if(!b)?{x=1,y=0;return?a;}??
????ll?r=exgcd(b,a%b,x,y);??
????ll?tmp=x;??
????x=y;??
????y=tmp-a/b*y;??
????return?r;??
}??
ÄæÔª£º£¨ÇóXÔÚmod PÒâÒåϵÄÄæÔª£©
Èç¹ûPΪÖÊÊý¿ÉÓ÷ÑÂíС¶¨Àí£º
a^(p-1)¡Ô1(modp)?a*a^(p-2)¡Ô1(modp)?a^(p-2)¡Ô1/a(modp)
inline?ll?qpow(ll?x,ll?k,ll?p)??{??
????ll?ret=1;??
????while(k)?{??
????????if(k&1)?ret=ret*x%p;??
????????x=x*x%p;??
????????k>>=1;??
????}??  
????return?ret;??
}??
inline?ll?inv(ll?x,ll?p)?{?return?qpow(x,p-2,p);?}??
Ò²¿ÉÒÔÓÃÏßÐÔµÝÍƹ«Ê½ÇóÄæÔª
inline?void?getinv(ll?p)??
{??
????inv[0]=inv[1]=1;??
????for(int?i=2;i<=n;++i)?inv[i]=(p-p/i)*inv[p%i]%p;??
}??
ÏßÐÔɸËØÊý£º
 inline?void?getprime(){??
????for(int?i=2;i<=n;++i)??{??
????????if(!not_prime[i])??
????????????prime[++top]=i;??
????????for(int?j=1;j<=top?&&?i*prime[j]<=n;++j)?{??
????????????not_prime[i*prime[j]]=true;??
????????????if(!(i%prime[j]))?break;??
????????}??
????}??
}??
Ψһ·Ö½â¶¨Àí(ÐèÒªÏÈËØÊýɸ£¬°´ÓÃ;·ÖΪÁ½ÖÖ´ò·¨)£º
¡¡·½·¨Ò»£º
inline?void?fenjie1(ll?x)??
{??
????for(int?i=1;i<=top;++i)??
????{??
????????if(x<prime[i])?break;??
????????if(x%prime[i]==0)?cnt[++cnt[0]]=prime[i];??
????????while(x%prime[i]==0)?x/=prime[i];??
????}??
????if(x>1)?cnt[++cnt[0]]=x;??
}??
·½·¨¶þ£º
inline?void?fenjie2(ll?x)??
{??
????for(int?i=1;i<=top;++i)??
????????while(x%prime[i]==0)?x/=prime[i],cnt[i]++;??
}??
Å·À­º¯Êý£º
¡¡1¡¢Ö±½ÓÇ󷨣º
inline?int?euler_phi(int?x)??
{??
????int?ans=x;??
????for(int?i=2;i*i<=x;++i)??
????????if(x%i==0){??
????????????ans=ans/i*(i-1);??
????????????while(x%i==0)?x/=i;??
????????}??
????if(x>1)?ans=ans/x*(x-1);??
????return?ans;??
}??
¡¡2¡¢µÝÍÆ·¨£º
inline?void?phi_table()??
{??
????for(int?i=1;i<=n;++i)?phi[i]=i;??
????for(int?i=2;i<=n;++i)??
????????if(phi[i]==i)??
????????????for(int?j=i;j<=n;j+=i)??
????????????????phi[j]=phi[j]/i*(i-1);??
}??
¾ØÕó¿ìËÙÃÝ£º
?#include?<bits/stdc++.h>??
using?namespace?std;??
typedef?long?long?ll;??
struct?Matrix{??
????long?long?num[101][101];??
????int?height,width;??
????Matrix(int?x=0,int?y=0)?{
     height=x,width=y;
     memset(num,0,sizeof(num));
  }?
????friend?Matrix?operator?*(const?Matrix?&a,const?Matrix?&b)??{??
????????if(a.width!=b.height)?return?Matrix();??
????????Matrix?ans(a.height,b.width);??
????????for(int?i=1;i<=ans.height;++i)??
????????????for(int?j=1;j<=ans.width;++j)??
????????????????for(int?k=1;k<=a.width;++k)??
????????????????????ans.num[i][j]+=a.num[i][k]*b.num[k][j];??
????????return?ans;??
????}??
};??
Matrix?pow(Matrix?x,long?long?k)??{??
????Matrix?ans(x.height,x.width);??
????for(int?i=1;i<=x.height;++i)?ans.num[i][i]=1;??
????while(k)?{??
????????if(k&1)?ans=ans*x;??
????????x=x*x;??
????????k>>=1;??
????}??
????return?ans;?
}?
¸ÅÂÊÓëÊýѧÆÚÍû
ÀýÌ⣺ÀñÎïgift
¡¡¡¡ÏÄ´¨µÄÉúÈÕ¾ÍÒªµ½ÁË¡£×÷ΪÏÄ´¨ÐÎʽÉϵÄÄÐÅóÓÑ,¼¾ÌôòËã¸øÏÄ´¨ÂòһЩÉúÈÕÀñÎï¡£É̵êÀïÒ»¹²ÓÐÖÖÀñÎï¡£
¡¡¡¡ÏÄ´¨Ã¿µÃµ½Ò»ÖÖÀñÎï,¾Í»á»ñµÃÏàӦϲÔÃÖµWi(ÿÖÖÀñÎïµÄϲÔÃÖµ²»ÄÜÖظ´»ñµÃ)¡£Ã¿´Î,µêÔ±»á°´ÕÕÒ»¶¨µÄ¸ÅÂÊPi(»òÕß²»ÄóöÀñÎï),½«µÚiÖÖÀñÎïÄóöÀ´¡£¼¾ÌÃÿ´Î¶¼»á½«µêÔ±ÄóöÀ´µÄÀñÎïÂòÏÂÀ´¡£Ã»ÓÐÄóöÀ´ÊÓΪʲô¶¼Ã»ÓÐÂòµ½,Ò²ËãÒ»´Î¹ºÂò¡£ÖÚËùÖÜÖª,°×ëÇпª¶¼ÊǺڵġ£ËùÒÔ¼¾ÌÃÏ£Íû×îºóÏÄ´¨µÄϲÔÃÖµ¾¡¿ÉÄܵظߡ£
¡¡¡¡ÇóÏÄ´¨×îºó×î´óµÄϲÔÃÖµÊǶàÉÙ,²¢Çó³öʹÏÄ´¨µÃµ½Õâ¸öϲÔÃÖµ,¼¾ÌõÄÆÚÍû¹ºÂò´ÎÊý¡£N¡Ü20?,?0<Wi¡Ü109?,?0<Pi¡Ü1ÇÒ?¡ÆPi¡Ü1
Source
N¡Ü20Ê×ÏÈÓ¦¸Ã¿¼ÂÇÓÃ״ѹ(ÆäʵÕâ²»ÊÇ×îÄѵÄÒ»¸öµØ·½)
¹Ø¼üÔÚÓÚÍƹ«Ê½,ÓÃkÊÇÒ»¸ö¶þ½øÖÆÊý±íʾÂòµ½ÁËÄÄЩÎïÆ·
ÀýÈç:k=101101±íʾÂòµ½Á˵Ú1,3,4,6¸öÎïÆ·
f±íʾÆÚÍûÖµ:¼ÙÉèÒѾ­Çóµ½ÁËËùÓбÈkÉÙÒ»¸öÎïÆ·µÄÆÚÍû£¬k'±íʾ±ÈkÉÙÒ»¸öÎïÆ·µÄ¶þ½øÖÆÊý£¬pΪÿ¸öÎïÆ·ÄóöÀ´µÄ¸ÅÂÊ£¬i±íʾ´Ók'Õâ¸ö״̬»¹ÐèÒªÂòµÚi¸öÎïÆ·¾Í¿ÉÒÔ±ä³Ék£¬ÄÇô¹«Ê½¿ÉÒÔд³É:
f_k= ¡Æ?¡¼P_i?¡½ f_k'+(1-¡Æ?P_i ) f_k+1 
 ¡Æ?¡¼P_i?¡½ f_k' ¾­Âòµ½ÁËk'ÏÖÔÚÔÙÂòµÚi¸öÎïÆ·¾Í¿ÉÒÔ´ïµ½kµÄÆÚÍû
 (1-¡Æ?P_i ) f_k ֮ǰkÀïÃæ°üº¬µÄÎïÆ·Ò»¸ö¶¼Ã»ÓÐÂòµ½£¬ÄÇôÐèÒªÔÙÂòfk´Î²ÅÄÜÂôÍêk¸ö£¬×îºó¼ÓÒ»¸ö1±íʾµ±Ç°ÔÙÂòÒ»¸ö
ÒÆÏ½â¿ÉµÃ£º
¡Æ?P_i ?f_k= ¡Æ?P_i ? f_(k^' )+1 
f_k=  (¡Æ?P_i ? f_(k^' )+1)/(¡Æ?P_i )
using namespace std;
typedef long long ll;
const int maxn=21;
double dp[1<<22],p[maxn];
int n,w[maxn];

int main()
{
    scanf("%d",&n);
    for(int i=1;i<=n;++i) scanf("%lf%d",p+i,w+i);
    ll ret=0;
    for(int i=1;i<=n;++i) ret+=1LL*w[i];
    printf("%lld\n",ret);
    double sigma=0;
    for(int k=1;k<(1<<n);++k)
    {
        sigma=0;
        for(int i=1;i<=n;++i)
            if(((1<<(i-1))&k)) {
                sigma+=p[i]*1.0;
                dp[k]+=p[i]*dp[k^(1<<(i-1))]*1.0;
            }
        if(sigma==0) continue;
        dp[k]=((dp[k]+1.0)*1.0)/(sigma*1.0);
    }
    printf("%.3lf\n",dp[(1<<n)-1]);
    return 0;
}
[bzoj1426] ÊÕ¼¯ÓÊƱ
¡¡¡¡Description
¡¡¡¡ÓÐnÖÖ²»Í¬µÄÓÊƱ£¬Æ¤Æ¤ÏëÊÕ¼¯ËùÓÐÖÖÀàµÄÓÊƱ¡£Î¨Ò»µÄÊÕ¼¯·½·¨Êǵ½Í¬Ñ§·²·²ÄÇÀﹺÂò£¬Ã¿´ÎÖ»ÄÜÂòÒ»ÕÅ£¬²¢ÇÒÂòµ½µÄÓÊƱ¾¿¾¹ÊÇnÖÖÓÊƱÖеÄÄÄÒ»ÖÖÊǵȸÅÂʵģ¬¸ÅÂʾùΪ1/n¡£µ«ÊÇÓÉÓÚ·²·²Ò²ºÜϲ»¶ÓÊƱ£¬ËùÒÔƤƤ¹ºÂòµÚkÕÅÓÊƱÐèÒªÖ§¸¶kԪǮ¡£ ÏÖÔÚƤƤÊÖÖÐûÓÐÓÊƱ£¬Æ¤Æ¤ÏëÖªµÀ×Ô¼ºµÃµ½ËùÓÐÖÖÀàµÄÓÊƱÐèÒª»¨·ÑµÄÇ®ÊýÄ¿µÄÆÚÍû¡£
Source
¡¡¡¡fi±íʾÒѾ­ÓµÓÐÁËiÕÅÓÊƱ£¬ÔòÆÚÍû»¹ÐèÒª¹ºÂòµÄÓÊƱÊý£¬(f_n=0)
¡¡¡¡f_i=f_i?i/n+f_(i+1)?(n-i)/n+1= f_(i+1)+n/(n-i) 
g_i±íʾ»¹ÐèÒªµÄÇ® g_i=(n-i)/n?(g_(i+1)+f_(i+1) )+i/n?(g_i+f_i )+1
ÒòΪ¿ÉÒÔÊÓΪÕâÕÅƱÊÇ1ÔªÂòµÄ£¬¶øºóÃæµÄÿÕÅƱ¶¼¹óÁË1Ôª
#include<iostream>
#include<cstdio>
#include<cstring>
#define N 10001
using namespace std;
double n,f[N],g[N];
int main()
{
	scanf("%lf",&n);
	for(int i=n-1;i>=0;i--)
	   f[i]=f[i+1]+n/(n-i);
	for(int i=n-1;i>=0;i--)
	   g[i]=g[i+1]+f[i+1]+(n*i)/((n-i)*n)*f[i]+n/(n-i);
	printf("%.2lf",g[0]);
    return 0;
}
Áé»ê»­Ê¦
¡¡¡¡ËäÈ»²»ÖªÎªÊ²Ã´£¬µ«ÄãÒ»Ö±ÏëÓÃÒ»ÖÖÉñÆæµÄ·½Ê½Íê³ÉÒ»·ù»­×÷¡£
¡¡¡¡Äã°ÑnÕÅ»­Ö½Æ̳ÉÒ»ÅÅ£¬²¢½«ËüÃÇ´Ó1µ½n±àºÅ¡£ÄãÒ»¹²ÓÐcÖÖÑÕÉ«¿ÉÓã¬ÕâЩÑÕÉ«¿ÉÒÔÓÃ0µ½c-1À´±àºÅ¡£³õʼʱ£¬ËùÓл­Ö½µÄÑÕÉ«¶¼Îª1¡£ÄãÒ»¹²Ïë½øÐÐk´Î×÷»­£¬µÚi´Î×÷»­Ê±£¬Äã»áµÈ¸ÅÂÊËæ»úµØÑ¡±ÕÇø¼ä[Li,Ri]ÄڵĻ­Ö½µÄÒ»¸ö×Ó¼¯£¨¿ÉÒÔΪ¿Õ£©£¬ÔÙËæ»úÌôÒ»ÖÖÑÕÉ«bi£¬²¢°ÑÌô³öÀ´µÄ»­Ö½¶¼Í¿ÉϸÃÑÕÉ«¡£Ô­ÓÐÑÕÉ«aµÄ»­Ö½ÔÚÍ¿ÉÏÑÕÉ«bºó£¬ÑÕÉ«»á±ä³É(a*b) mod c£¬ÕâÊÇÕâ¸öÊÀ½çµÄ¹æÂÉ¡£
¡¡¡¡ÒòΪÄ㽫ÑÕÉ«ÓÃÊý×ÖÀ´ÃüÃûÁË£¬Òò´ËÄã¿ÉÒÔÇó³öÔÚk´Î×÷»­½áÊøºó£¬Ã¿ÕÅ»­Ö½ÉϵÄÑÕÉ«¶ÔÓ¦µÄÊý×ÖÏà¼ÓÖ®ºÍµÄÆÚÍû¡£ÏÖÔÚÇëÄã±à³ÌÇóÒ»ÏÂÕâ¸öÖµ¡£ÒÔ·ÀÍòÒ»Äã²»ÖªµÀʲôÊÇÆÚÍû£ºÈç¹ûÒ»¸öÁ¿X£¬ËüÓÐp1µÄ¸ÅÂÊֵΪv1£¬ÓÐp2µÄ¸ÅÂÊֵΪv2¡­¡­pnµÄ¸ÅÂÊֵΪvn¡£
¡¡¡¡ÔòXµÄÆÚÍûÖµµÈÓÚ£ºp1v1+p2v2+¡­+pnvn¡£
#include<cstring>
#include<cmath>
#include<cstdio>
#include<algorithm>
using namespace std;
const int maxn=100+10;
double p[maxn][maxn][maxn],ans;
int n,c,K;
int main()
{
	freopen("paint.in","r",stdin);
	freopen("paint.out","w",stdout);
	scanf("%d%d%d",&n,&c,&K);
	for(int i=1;i<=n;i++){
		p[0][i][1]=1.0;
	}
	for(int l=1;l<=K;l++){
		int L,R;
		scanf("%d%d",&L,&R);
		for(int i=1;i<=n;i++){
			for(int j=0;j<c;j++){
				if(L<=i && i<=R)p[l][i][j]=p[l-1][i][j]*0.5;
				else p[l][i][j]=p[l-1][i][j];
			}
		}
		for(int i=0;i<c;i++){
			for(int j=L;j<=R;j++){
				for(int k=0;k<c;k++)if(p[l-1][j][k]!=0){
					p[l][j][(i*k)%c]+=p[l-1][j][k]*0.5*1/(double)c;
				}
			}
		}
	}
	for(int i=1;i<=n;i++){
		for(int j=0;j<c;j++){
			ans+=p[K][i][j]*j;
		}
	}
	printf("%.3lf\n",ans);
}
¶¯Ì¬¹æ»®Æª
ÊýλDP£¬//ÒÔÄÇÌ쿼ÊÔ µÄÄǵÀÊýλDPΪÀý
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int N=1000;
int a[N],dp[N][N];
long long dfs(int pos,int sta,int limit){
	if(pos==0)return sta;
	if(!limit&&dp[pos][sta]!=-1)return dp[pos][sta];
	int up=limit?a[pos]:9;
	long long tmp=0;
	for(int i=0;i<=up;i++)
     {
     tmp+=dfs(pos-1,sta&&(!(i%2)&&!i)||(i%2)),limit&&i==up);
	}
	if(!limit)dp[pos][sta]=tmp;
	return tmp;
}
long long solve(long long x)
{
	memset(dp,-1,sizeof(dp));
	int pos=0;
	while(x)
	{
		a[++pos]=x%10;
		x/=10;
	}
	return dfs(pos,1,1);
}
int main()
{
	long long r;
	scanf("%lld",&r);
	printf("%lld",solve(r));
¡¡//ÈôÓÐl, printf("%lld",solve(r)-solve(l-1));
}
״ѹģ°å
/*Ô¼º²¼ÒÓÐNÍ·ÄÌÅ££¬µÚiÍ·ÄÌÅ£µÄ±àºÅÊÇSi£¬Ã¿Í·ÄÌÅ£µÄ±àºÅ¶¼ÊÇΨһµÄ¡£ÕâЩÄÌÅ£×î½ü ÔÚÄÖÆ¢Æø£¬Îª±í´ï²»ÂúµÄÇéÐ÷£¬ËýÃÇÔÚ¼·Ä̵ÄʱºòÒ»¶¨ÒªÅųɻìÂҵĶÓÎé¡£ÔÚÒ»Ö»»ìÂÒµÄ¶Ó ÎéÖУ¬ÏàÁÚÄÌÅ£µÄ±àºÅÖ®²î¾ù³¬¹ýK¡£±ÈÈçµ±K = 1ʱ£¬1, 3, 5, 2, 6, 4¾ÍÊÇÒ»Ö§»ìÂҵĶÓÎ飬¶ø1, 3, 6, 5, 2, 4²»ÊÇ£¬ÒòΪ6ºÍ5Ö»²î1¡£ÇëÊýÒ»Êý£¬ÓжàÉÙÖÖ¶ÓÐÎÊÇ»ìÂÒµÄÄØ£¿*/
#include<cstdio>
#include<cstdio>
#include<cmath>
#include<algorithm>
using namespace std;
const int maxn=100000+10;
long long s[maxn],f[20][1<<17];
long long n,p,ans;
int main()
{
	scanf("%lld%lld",&n,&p);
	for(int i=0;i<n;i++) scanf("%lld",&s[i]);
	for(int i=0;i<n;i++) f[i][(1<<i)]=1;
	for(int i=0;i<(1<<n);i++)
		for(int j=0;j<n;j++)if(i&(1<<j))
			for(int k=0;k<n;k++)
				if(!(i&(1<<k)) && abs(s[j]-s[k])>p)
					f[k][i|(1<<k)]+=f[j][i]; 
	for(int i=0;i<n;i++)
	ans=f[i][(1<<n)-1]+ans;
	printf("%lld\n",ans);
}



״ѹÉý¼¶
/*ÅÚ±øÕóµØ£º¸ø³ön*mµÄ¾ØÕó£¬ÔÚ¾ØÕóÀï·ÅÅÚ±ø£¬Ò»¸öÅÚ±ø¿ÉÒÔ¹¥»÷Á½¸ö·½¸ñ£¨ÏòÉÏ£¬Ï£¬×ó£¬ÓÒ£©²»ÄÜб׏¥»÷£¬Ñ¯ÎÊ×î¶àÄܷŶàÉÙ¸öÅÚ±ø ʹµÃÅÚ±øÁ½Á½Ö®¼ä²»ÄÜ»¥Ï๥»÷*/
#include<cstdio>
#include<cmath>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
const int maxn=100+10;
int dp[maxn][maxn][maxn];
int map[maxn*maxn],f[maxn*maxn],num[maxn*maxn];
int n,m,cnt,ans;
int main()
{
	scanf("%d%d",&n,&m);
	for(int i=1;i<=n;i++){
		for(int j=1;j<=m;j++){
			char c;
			cin>>c;map[i]=map[i]*2;
			if(c=='H')map[i]+=1;
		}
	}
	for(int i=0;i<(1<<m);i++) {
		int g=i;
		if(!(i&(i<<1)) && !(i&(i<<2))) f[++cnt]=i;
		else continue;
		while(g){
			if(g&1) num[cnt]++;g>>=1;
		}
	}
	for(int i=1;i<=cnt;i++) {
		if(!(map[1]&f[i]))
		dp[1][1][i]=num[i];
	}
	for(int i=1;i<=cnt;i++){
		if(!(f[i]&map[1]))
		for(int j=1;j<=cnt;j++){
			if(!(f[i]&f[j]) && !(f[j]&map[2]))
			dp[2][i][j]=num[i]+num[j];
		}
	}
	for(int i=3;i<=n;i++){
		for(int j=1;j<=cnt;j++)
		if(!(f[j]&map[i-2])){
			for(int k=1;k<=cnt;k++)
			if(!(f[k]&map[i-1]) && !(f[k]&f[j])){
				for(int l=1;l<=cnt;l++)
				if(!(f[l]&map[i]) && !(f[l]&f[j]) && !(f[l]&f[k])){
					dp[i][k][l]=max(dp[i][k][l],dp[i-1][j][k]+num[l]);
				}
			}
		}
	}
	for(int i=1;i<=cnt;i++)
	if(!(f[i]&map[n-1])){
		for(int j=1;j<=cnt;j++)
		if(!(f[j]&f[i]) && !(f[j]&map[n])){
			ans=max(ans,dp[n][i][j]);
		}
	}printf("%d\n",ans);
}
Çø¼äDP
for(int i=1;i<=n;i++){//ö¾Ù³¤¶È 
	for(int l=1;l+i-1<=n;l++){//ö¾Ù×ó¶Ëµã 
		int r=l+i-1;//ÓҶ˵ã 
		for(int k=l;k<r;i++){//ö¾Ù¶Ïµã 
			dp[l][r]=min(dp[l][r],dp[l][k]+dp[k+1][r]);
		} 
	}
}
×Ö·û´®Æª
TrieÊ÷(×ÖµäÊ÷)
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int N=1000;
char str[N];
int ch[N][26],n,m,tot=0;
void insert(int id,char *s){
	for(int i=0;s[i];i++){
		int j=s[i]-'a';
		if(ch[id][j]) id=ch[id][j];
		else id=ch[id][j]=++tot;
	}
}
int serch(int id,char *s){
	for(int i=0;s[i];i++){
		int j=s[i]-'a';
		if(!ch[id][j])return 0;
	}return 1;
}
int main(){
	scanf("%d%d",&n,&m);
	for(int i=1;i<=n;i++){
		scanf("%s",str);insert(0,str);
	}
	for(int i=1;i<=m;i++){
		scanf("%s",str);
		if(serch(0,str)==0) printf("ORZ!!!\n");
		else printf("233\n");
	}
}
KMP
//ÒÔluogu3375ΪÀý
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
const int N=1000000+5;
const int M=1000000+5;
int nex[M];
char s[N],t[M];
void makelove(char *t,int lent){
	nex[0]=0;
	for(int i=1,j=0;i<=lent;i++){
		j=nex[i-1];
		while(j>=1&&t[i]!=t[j]) j=nex[j-1];
		if(t[i]==t[j]) nex[i]=j+1;
	}
}
void kmp(char *s,int lens,char *t,int lent){
	int si=0,ti=0;
	while(si<lens){
		if(s[si]==t[ti]) si++,ti++;
		else {
			if(ti==0)si++;
			else ti=nex[ti-1];
		}
		if(ti==lent) printf("%d\n",si-ti+1);
	}
}
int main(){
	scanf("%s",s);
	scanf("%s",t);
	int lens=strlen(s);
	int lent=strlen(t);
	makelove(t,lent);
	kmp(s,lens,t,lent);
	for(int i=0;i<lent;i++)
	printf("%d ",nex[i]);
}
AC×Ô¶¯»ú//ÒÔluogu3808ΪÀý
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
using namespace std;
const int N=2000000+10;
char str[N];
struct node{
	int fail,cnt;
	int next[26];
}trie[N];
int n,k=0,ans=0;
queue<int> q;
void built_trie(int id,char *s){
	int j=0,len=strlen(s);
	for(int i=0;i<len;i++)
	{
		j=s[i]-'a';
		if(trie[id].next[j]==0) trie[id].next[j]=++k;
		id=trie[id].next[j];
	}
	trie[id].cnt++;
}
void built_fail(int id)
{
	while(!q.empty())q.pop();
	for(int i=0;i<26;i++)
	if(trie[id].next[i]!=0) q.push(trie[id].next[i]);
	while(!q.empty()){
		int now=q.front();q.pop();
		for(int i=0;i<26;i++)
		{
			if(trie[now].next[i]==0)
			{
				trie[now].next[i]=trie[trie[now].fail].next[i];
				continue;
			}
			trie[trie[now].next[i]].fail=trie[trie[now].fail].next[i];
			q.push(trie[now].next[i]);
		}
	}
}
void calc(int id,char *s){
	int len=strlen(s),j=0;
	for(int i=0;i<len;i++)
	{
		j=trie[id].next[s[i]-'a'];
		while(j!=0&&trie[j].cnt!=-1)
		{
			ans+=trie[j].cnt;
			trie[j].cnt=-1;
			j=trie[j].fail;
		}
		id=trie[id].next[s[i]-'a'];
	}
}
int main()
{
	scanf("%d",&n);
	for(int i=1;i<=n;i++)
	{
		scanf("%s",str);
		built_trie(0,str);
	}
	built_fail(0);
	scanf("%s",str);
	calc(0,str);
	printf("%d\n",ans);
	return 0;
}


Êý¾Ý½á¹¹Æª
¶Ñ:¼òµ¥µ«Óô¦²»¶à
struct Heap{
    int f[200005],siz;
    Heap(){memset(f,0,sizeof(f));siz=0;}
    bool com(int i,int j){return f[i]<=f[j];}
    void ins(int x) {
       f[++siz]=x; int now=siz;
while(now!=1&&com(now,now>>1)) swap(f[now],f[now>>1]),now>>=1;
    }
    int getop() {
        int ret=f[1],now=1,son; f[1]=f[siz--];
        while((now<<1)<=siz){
            if((now<<1|1)>siz||com(now<<1,now<<1|1))
¡¡son=now<<1; else son=now<<1|1;
            if(com(son,now)) swap(f[now],f[son]); 
       else break;
            now=son;
        }return ret;
    }
}H;
Ê÷×´Êý×é:¼òµ¥ÊµÓõÄÊý¾Ý½á¹¹£¬Í¨³£ÓÃÀ´Î¬»¤Ç°×ººÍ´úÂëÁ½ÐУº
Void cadd(int x,int k) 
{ 
¡¡while(x<=n){c[x]+=k; x+=x&-x;} 
¡¡} 
int cget(int x)
{ 
¡¡int y=0; 
¡¡for(;x;y+=c[x],x-=x&-x); 
¡¡return y;
¡¡}
Ï߶ÎÊ÷:Ê®·ÖÁé»îµÄ³£ÓÃÊý¾Ý½á¹¹£¬ÒÔά»¤´øÐÞ¸Ä×î´ó×ֶκÍΪÀý£º
void pushup(int id){
sum[id]=sum[id<<1]+sum[id<<1|1];
maxl[id]=max(maxl[id<<1],sum[id<<1]+maxl[id<<1|1]);
maxr[id]=max(maxr[id<<1|1],maxr[id<<1]+sum[id<<1|1]);
maxx[id]=max(max(maxx[id<<1],maxx[id<<1|1]),maxl[id<<1|1]+maxr[id<<1]);
}
void build(int id,int l,int r){
    if(l==r){
        sum[id]=maxx[id]=maxl[id]=maxr[id]=a[l];
        return;
    }int mid=(l+r)>>1;
    build(id<<1,l,mid); build(id<<1|1,mid+1,r);
    pushup(id);
}
void query(int id,int l,int r,int x,int y,bool f) //fÓÃÓÚÅжÏÊDzéѯ»¹ÊÇÐÞ¸Ä
{
    if(x<=l && y>=r){
       if(f){sum[id]=maxx[id]=maxl[id]=maxr[id]=a[l]=val; return;}
       ans=max(ans,max(maxx[id],pre+maxl[id]));
       pre=max(pre+sum[id],maxr[id]);
       return;
}
int mid=(l+r)>>1;
    if(x<=mid) query(id<<1,l,mid,x,y,f);
    if(y>mid) query(id<<1|1,mid+1,r,x,y,f);
    if(f) pushup(id);
}
ƽºâÊ÷:¹¦ÄÜÇ¿´óµÄÊý¾Ý½á¹¹£¬¸÷ƽºâÊ÷¹¦Äܺ¯ÊýÀàËÆ
ÕÒµÚk´ó£¨Ð¡£©£º
int findkth(int id,int k){
	if(w[ch[id][0]]+1==k) return id;
	if(k<=w[ch[id][0]]) return findkth(ch[id][0],k);
	else return findkth(ch[id][1],k-w[ch[id][0]]-1);
}

ÕÒÇ°Çý£¨´úÂëÖÐt1ÊÇÇ°Çý£©£º
void ask_before(int x,int k)
{
    if(!k) return;
    if(w[k]>x) ask_before(x,ch[k][0]);
    else if(w[k]==x){t1=k; tk1=w[k]; return;}
    else {t1=k; tk1=w[k]; ask_before(x,ch[k][1]);}
}
Æô·¢Ê½ºÏ²¢£¨´úÂëÊÇsplay£¬treapÒ²Ò»Ñù£©:
void merge(int x,int y)
{
	splay(x); splay(y);
	if(w[x]<w[y]) swap(x,y);
	int head=1,tail=1;
	q[tail++]=x; q[tail++]=y; head++;
	while(head<tail){
		int u=q[head++];
		if(ch[u][0]) q[tail++]=ch[u][0];
		if(ch[u][1]) q[tail++]=ch[u][1];
		insert(q[head-2],u,0);
	}
}
Treap£ºTreap±ØÐëдÁ½¸öÐýתº¯Êý¾ÍºÜ·³£¬×¢Òâɾ³ý²Ù×÷Óësplay²»Í¬£¬²»´æÔÚÀàËÆsplayº¯ÊýµÄ¸Ä±äƽºâÊ÷ÐÔÖʵĺ¯ÊýʹÆäÐÎ̬Îȶ¨³£Êý¸üС
struct data{
	int l,r,v,size,rnd,w;
}tr[100005];
int n,size,root,ans;
void update(int k) {tr[k].size=tr[tr[k].l].size+tr[tr[k].r].size+tr[k].w;}
void rturn(int &k){
	int t=tr[k].l;tr[k].l=tr[t].r;tr[t].r=k;
	tr[t].size=tr[k].size;update(k);k=t;
}
void lturn(int &k){
	int t=tr[k].r;tr[k].r=tr[t].l;tr[t].l=k;
	tr[t].size=tr[k].size;update(k);k=t;
}
void insert(int &k,int x)
{
	if(k==0){
		size++;k=size;
		tr[k].size=tr[k].w=1;tr[k].v=x;tr[k].rnd=rand();
		return;
	}tr[k].size++;
????  if(tr[k].v==x)tr[k].w++;
    //ÿ¸ö½áµã˳±ã¼Ç¼ÏÂÓë¸Ã½ÚµãÏàֵͬµÄÊýµÄ¸öÊý
	  else if(x>tr[k].v){
		insert(tr[k].r,x);
		if(tr[tr[k].r].rnd<tr[k].rnd)lturn(k);//ά»¤¶ÑÐÔÖÊ
	  }else {
		insert(tr[k].l,x);
		if(tr[tr[k].l].rnd<tr[k].rnd)rturn(k);
	} 
}
void del(int &k,int x)
{
???? if(k==0)return; 
   if(tr[k].v==x){
	    if(tr[k].w>1){
		   tr[k].w--;tr[k].size--;return;
          //Èô²»Ö¹ÏàֵͬµÄ¸öÊýÓжà¸ö£¬É¾È¥Ò»¸ö
		}
		if(tr[k].l*tr[k].r==0)k=tr[k].l+tr[k].r;//ÓÐÒ»¸ö¶ù×ÓΪ¿Õ
		else if(tr[tr[k].l].rnd<tr[tr[k].r].rnd)
			rturn(k),del(k,x);
		else lturn(k),del(k,x);
	}
	else if(x>tr[k].v)
		tr[k].size--,del(tr[k].r,x);
	else tr[k].size--,del(tr[k].l,x);
}

Splay£ºÇø¼äsplayд·¨
void rotate(int x,int &k)
{
    int y=fa[x],z=fa[y],l,r;
    if(ch[y][0]==x) l=0; else l=1; r=l^1;
    if(y==k) k=x;
else{
	if(ch[z][0]==y) ch[z][0]=x; 
	else ch[z][1]=x;
}
    fa[ch[x][r]]=y; fa[x]=z; fa[y]=x;
    ch[y][l]=ch[x][r]; ch[x][r]=y;
}
void splay(int x,int &k)
{
    int y,z;
    while(x!=k)
    {
        y=fa[x]; z=fa[y];
        if(y!=k){
            if((ch[y][0]==x)^(ch[z][0]==y))
			rotate(x,k);
        	else rotate(y,k);
        }
		 rotate(x,k);
    }
}
void ins(int &k ,int x,int last)
{
if(!k){
	k=++size; fa[k]=last; 
	w[k]=x; splay(k,rt); 
	return;
}
    if(w[k]>x) ins(ch[k][0],x,k);
    else ins(ch[k][1],x,k);
}
splayµÄɾ³ý²Ù×÷Óëtreap²»Í¬Òª½«¸ùµÄ×óº¢×ÓÒƵ½ºó¼ÌµÄ×óº¢×ÓÔٰѺó¼Ìתµ½¸ù
void del(int x){
    splay(x,rt);
    if(!(ch[x][0]*ch[x][1])){rt=ch[x][0]+ch[x][1];}
    else {
        int k=ch[x][1];
        while(ch[k][0]) k=ch[k][0];
        ch[k][0]=ch[x][0]; fa[ch[x][0]]=k;
        rt=ch[x][1];
    }fa[rt]=0;
}
Ö÷ϯÊ÷
void insert(int &id,int l,int r,int x){
    ls[++siz]=ls[id]; rs[siz]=rs[id];
w[siz]=w[id]+1; id=siz£»
if(l==r) return;
    int mid=(l+r)>>1;
    if(x<=mid) insert(ls[id],l,mid,x);
    else insert(rs[id],mid+1,r,x);
}
int query(int x,int y,int l,int r,int k){
    if(l==r) return l;
    int t=w[ls[y]]-w[ls[x]];
    int mid=l+r>>1;
    if(k<=t) return query(ls[x],ls[y],l,mid,k);
    else return query(rs[x],rs[y],mid+1,r,k-t);
}
´øÐÞ¸ÄÖ÷ϯÊ÷
void ins(int &id,int l,int r,int x,int v)
{
    ls[++siz]=ls[id]; rs[siz]=rs[id];
¡¡¡¡sum[siz]=sum[id]+v; id=siz;
    if(l==r) return;
    int mid=(l+r)>>1;
    if(x<=mid) ins(ls[id],l,mid,x,v);
    else ins(rs[id],mid+1,r,x,v);
}
int query(int l,int r,int x)
{
    if(l==r) return l;
    int summ=0,mid=(l+r)>>1;
    for(int i=1;i<=ri;++i) summ+=sum[ls[R[i]]];
    for(int i=1;i<=le;++i) summ-=sum[ls[L[i]]];
    if(x<=summ){
        for(int i=1;i<=le;++i) L[i]=ls[L[i]];
        for(int i=1;i<=ri;++i) R[i]=ls[R[i]];
        return query(l,mid,x);
    }else {
        for(int i=1;i<=le;++i) L[i]=rs[L[i]];
        for(int i=1;i<=ri;++i) R[i]=rs[R[i]];
        return query(mid+1,r,x-summ);
    }
}
²åÈëʱҪÕâÑù£º
int kth=lower_bound(p+1,p+maxc+1,a[ansx[i]])-p; 
// ÕÒÀëÉ¢»¯ºóµÄÖµ
for(int k=ansx[i];k<=n;k+=lowbit(k))
     ins(root[k],1,maxc,kth,-1);
²éѯʱҪÕâÑù£º
le=ri=0;
for(int k=ansx[i]-1;k;k-=lowbit(k))
    L[++le]=root[k];
for(int k=ansy[i];k;k-=lowbit(k))
    R[++ri]=root[k];
int ans=query(1,maxc,K[i]);
Ï߶ÎÊ÷ºÏ²¢
ʵÖÊÊǶ¯Ì¬¿ªµãÏ߶ÎÊ÷µÄºÏ²¢£¬ºÏ²¢Ê±¼ä¸´ÔÓ¶Èlog(n)£¬nÊÇÏ߶ÎÊ÷ά»¤ÖµÓò£¬ÓÉÓÚҪά»¤¶à¿ÅÏ߶ÎÊ÷£¬¿Õ¼ä¸´ÔӶȽϸß
void insert(int &id,int l,int r,int x){
	if(!id) id=++siz;
	if(l==r){ w[id]=1; return ; }
	int mid=l+r>>1;
	if(x<=mid) insert(ls[id],l,mid,x);
	else insert(rs[id],mid+1,r,x);
	w[id]=w[ls[id]]+w[rs[id]];
}
int query(int id,int l,int r,int k){
	if(l==r) return l;
	int mid=l+r>>1;
	if(k<=w[ls[id]]) return query(ls[id],l,mid,k);
	else return query(rs[id],mid+1,r,k-w[ls[id]]);
}
int merge(int x,int y){
	if(!x) return y;
	if(!y) return x;
	ls[x]=merge(ls[x],ls[y]);
	rs[x]=merge(rs[x],rs[y]);
	w[x]=w[ls[x]]+w[rs[x]];
	return x;
}

















link cut tree(ÖÕÓÚ°ÚÍÑÊ÷ÆÊÈß³¤µÄ´úÂëÀ²£¡)
¡¡¡¡ÏÂÃæÊÇÊ÷ÉÏ·¾¶ÐÞ¸Ä+ά»¤×ÓÊ÷´óС£¬sumÊÇʵ×ÓÊ÷£¬sizÊÇÐé×ÓÊ÷£¬numÊÇsplay×ÓÊ÷´óС£¬½¨Ê÷Ç°Ó¦ÔÚµãȨֵÊäÈëÍêºóÔÙÒÀ´Î½øÐÐlink²Ù×÷£¬ÕâÊÇÒ»¸ö¿Óµã£¬Ê÷ÉÏ·¾¶ÐÞ¸ÄÓ¦ÏÈmakeroot(x); Access(y); splay(x); ÔÙupdate(x,y,k);
#define pushup1(x)sum[x]=(LL)sum[ch[x][0]]+sum[ch[x][1]]+siz[x]+w[x]
//define³£ÊýС
#define pushup2(x) num[x]=num[ch[x][0]]+num[ch[x][1]]+1
#define pushup(x) pushup1(x),pushup2(x)
#define makeroot(x) Access(x),splay(x),rev[x]^=1;
#define Link(x,y) makeroot(x),makeroot(y),siz[y]+=sum[x],fa[x]=y;pushup(y);
#define update(x,y,k) makeroot(x),Access(y),splay(x),add[x]+=k,sum[x]+=(LL)k*num[x],w[x]+=k;
bool isroot(int x){return ch[fa[x]][0]!=x&&ch[fa[x]][1]!=x;}
void rotate(int x){
	int y=fa[x],z=fa[y],l=(ch[y][1]==x),r=l^1;
	if(!isroot(y)) ch[z][ch[z][1]==y]=x;
	fa[x]=z; fa[y]=x; fa[ch[x][r]]=y;
	ch[y][l]=ch[x][r]; ch[x][r]=y;
	pushup(y);
}
void pushdown(int x){
	if(rev[x]){
		rev[x]=0; swap(ch[x][0],ch[x][1]);
		rev[ch[x][0]]^=1; rev[ch[x][1]]^=1;
	}
	if(add[x]){
		if(ch[x][0]){
			add[ch[x][0]]+=add[x];
			sum[ch[x][0]]+=(LL)add[x]*num[ch[x][0]];
			w[ch[x][0]]+=add[x];
		}
		if(ch[x][1]){
			add[ch[x][1]]+=add[x];
			sum[ch[x][1]]+=(LL)add[x]*num[ch[x][1]];
			w[ch[x][1]]+=add[x];
		}	
		add[x]=0;
	}
}
void splay(int x){
	st[++top]=x;
	for(int i=x;!isroot(i);i=fa[i]) st[++top]=fa[i];
	while(top) pushdown(st[top--]);
	while(!isroot(x)){
		int y=fa[x],z=fa[y];
		if(!isroot(y)){
			if((ch[z][0]==y)^(ch[y][0]==x)) rotate(x);
			else rotate(y);
		}rotate(x);
	}pushup(x);
}
void Access(int x)
{
	for(int y=0;x;y=x,x=fa[x]){
		splay(x); siz[x]+=sum[ch[x][1]]-sum[y];
		ch[x][1]=y; pushup(x);
	}
}










Ï߶ÎÊ÷ºÏ²¢£º
¡¡¡¡ÊµÖÊÊǶ¯Ì¬¿ªµãÏ߶ÎÊ÷µÄºÏ²¢£¬ºÏ²¢Ê±¼ä¸´ÔÓ¶Èlog(n)£¬nÊÇÏ߶ÎÊ÷ά»¤ÖµÓò£¬ÓÉÓÚҪά»¤¶à¿ÅÏ߶ÎÊ÷£¬¿Õ¼ä¸´ÔӶȽϸß
void insert(int &id,int l,int r,int x){
	if(!id) id=++siz;
	if(l==r){ w[id]=1; return ; }
	int mid=l+r>>1;
	if(x<=mid) insert(ls[id],l,mid,x);
	else insert(rs[id],mid+1,r,x);
	w[id]=w[ls[id]]+w[rs[id]];
}
int query(int id,int l,int r,int k){
	if(l==r) return l;
	int mid=l+r>>1;
	if(k<=w[ls[id]]) return query(ls[id],l,mid,k);
	else return query(rs[id],mid+1,r,k-w[ls[id]]);
}
int merge(int x,int y){
	if(!x) return y;
	if(!y) return x;
	ls[x]=merge(ls[x],ls[y]);
	rs[x]=merge(rs[x],rs[y]);
	w[x]=w[ls[x]]+w[rs[x]];
	return x;
}
ºó׺Êý×飺
¡¡¡¡Î¬»¤×Ö·û´®ºó׺ÅÅÃûÒÔ¼°heightÊý×é±íʾÅÅÃûΪiµÄºó׺ÓëÅÅÃûΪi-1µÄºó׺µÄ¹«¹²Ç°×ºµÄ³¤¶È¡£
void tsort()
{
   memset(tax,0,(n+2)<<2);
   for(int i=1;i<=n;++i) tax[Rank[i]]++;
   for(int i=1;i<=m;++i) tax[i]+=tax[i-1];
   for(int i=n;i;--i) SA[tax[Rank[tp[i]]]--]=tp[i];
}
bool com(int *f,int i,int j,int w){ 
	 return f[i]==f[j]&&f[i+w]==f[j+w];
}
void suffix()
{
   for(int i=1;i<=n;++i) Rank[i]=t[i],tp[i]=i;
   m=127; tsort();
   for(int p=0,w=1;p<n;w<<=1,m=p){
       p=0;
       for(int i=n-w+1;i<=n;++i) tp[++p]=i;
       for(int i=1;i<=n;++i) if(SA[i]>w) tp[++p]=SA[i]-w;
       tsort();
     	 swap(Rank,tp); 
     	 //Èç¹û²»ÄÜÓÃswap½»»»Ö¸ÕëÔò¿ÉÒÔÓÃ
     	 memcpy(tp,rank,(n+2)<<2);
     	 Rank[SA[1]]=p=1;
       for(int i=2;i<=n;++i) 		
			Rank[SA[i]]=com(tp,SA[i],SA[i-1],w)?p:++p;
     }
     int k=0,j;
     //Ò»¸öºÜÉñÆæµÄµÝÍÆÇóheight
     for(int i=1;i<=n;height[Rank[i++]]=k) 
        for(k=k?k-1:k,j=SA[Rank[i]-1];t[i+k]==t[j+k];k++);
}


ÃàÖÐʵÑé NOIP2017 ֪ʶģ°å


 3 / 10

This snippet took 0.16 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).