# Codeforces Round #556 (Div. 1)

## A. Prefix Sum Primes

#include<iostream>
#include<cstdio>
using namespace std;
{
int x=0;bool t=false;char ch=getchar();
while((ch<'0'||ch>'9')&&ch!='-')ch=getchar();
if(ch=='-')t=true,ch=getchar();
while(ch<='9'&&ch>='0')x=x*10+ch-48,ch=getchar();
return t?-x:x;
}
int n,a[3];
int main()
{
if(a[2])
{
printf("2 "),a[2]-=1;
if(a[1])printf("1 "),a[1]-=1;
}
while(a[2])printf("2 "),a[2]-=1;
while(a[1])printf("1 "),a[1]-=1;
puts("");
return 0;
}

## B. Three Religions

#include<iostream>
#include<cstdio>
using namespace std;
#define MAX 100100
int n,Q;
int nxt[MAX][26],lst[26];
char s[MAX],a[4][MAX];
int len[4],f[255][255][255];
void dp(int x,int y,int z)
{
if(!(x|y|z))return;f[x][y][z]=n+1;
if(x&&f[x-1][y][z]<=n)f[x][y][z]=min(f[x][y][z],nxt[f[x-1][y][z]][a[1][x]-97]);
if(y&&f[x][y-1][z]<=n)f[x][y][z]=min(f[x][y][z],nxt[f[x][y-1][z]][a[2][y]-97]);
if(z&&f[x][y][z-1]<=n)f[x][y][z]=min(f[x][y][z],nxt[f[x][y][z-1]][a[3][z]-97]);
}
int main()
{
scanf("%d%d%s",&n,&Q,s+1);
for(int i=0;i<26;++i)lst[i]=n+1;
for(int i=n;~i;--i)
{
for(int j=0;j<26;++j)nxt[i][j]=lst[j];
if(i)lst[s[i]-97]=i;
}
while(Q--)
{
char ch[2],ss[2];int x;
scanf("%s%d",ch,&x);
if(ch[0]=='+')
{
scanf("%s",ss);
a[x][++len[x]]=ss[0];
if(x==1)
for(int i=0;i<=len[2];++i)
for(int j=0;j<=len[3];++j)
dp(len[1],i,j);
if(x==2)
for(int i=0;i<=len[1];++i)
for(int j=0;j<=len[3];++j)
dp(i,len[2],j);
if(x==3)
for(int i=0;i<=len[1];++i)
for(int j=0;j<=len[2];++j)
dp(i,j,len[3]);
}
else --len[x];
if(f[len[1]][len[2]][len[3]]<=n)puts("YES");else puts("NO");
}
return 0;
}

## C. Tree Generator™

#include<iostream>
#include<cstdio>
using namespace std;
#define MAX 200200
#define lson (now<<1)
#define rson (now<<1|1)
{
int x=0;bool t=false;char ch=getchar();
while((ch<'0'||ch>'9')&&ch!='-')ch=getchar();
if(ch=='-')t=true,ch=getchar();
while(ch<='9'&&ch>='0')x=x*10+ch-48,ch=getchar();
return t?-x:x;
}
int n,Q;char s[MAX];
struct Node{int pre[2],suf[2],ans,sum,tot;}t[MAX<<2],L,R;
Node operator+(Node a,Node b)
{
Node c;
c.pre[0]=max(a.pre[0],a.sum+b.pre[0]);
c.pre[1]=max(a.pre[1],max(a.tot+b.pre[0],-a.sum+b.pre[1]));
c.suf[0]=max(b.suf[0],-b.sum+a.suf[0]);
c.suf[1]=max(b.suf[1],max(b.tot+a.suf[0],b.sum+a.suf[1]));
c.sum=a.sum+b.sum;
c.tot=max(a.tot+b.sum,-a.sum+b.tot);
c.ans=max(max(a.ans,b.ans),max(a.suf[0]+b.pre[1],a.suf[1]+b.pre[0]));
return c;
}
void Modify(int now,int l,int r,int p)
{
if(l==r){t[now]=(s[p]=='(')?L:R;return;}
int mid=(l+r)>>1;
if(p<=mid)Modify(lson,l,mid,p);
else Modify(rson,mid+1,r,p);
t[now]=t[lson]+t[rson];
}
int main()
{
L=(Node){1,1,0,1,1,1,1};R=(Node){0,1,1,1,1,-1,1};
for(int i=1;i<=n;++i)Modify(1,1,n,i);
printf("%d\n",t[1].ans);
while(Q--)
{
Modify(1,1,n,x);Modify(1,1,n,y);
printf("%d\n",t[1].ans);
}
return 0;
}

## D. Abandoning Roads

#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;
#define MAX 72
{
int x=0;bool t=false;char ch=getchar();
while((ch<'0'||ch>'9')&&ch!='-')ch=getchar();
if(ch=='-')t=true,ch=getchar();
while(ch<='9'&&ch>='0')x=x*10+ch-48,ch=getchar();
return t?-x:x;
}
struct Line{int v,next,w;}e[500];
int h[MAX],cnt=1;
inline void Add(int u,int v,int w){e[cnt]=(Line){v,h[u],w};h[u]=cnt++;}
int f[MAX],sz[MAX];
int getf(int x){return x==f[x]?x:f[x]=getf(f[x]);}
void Merge(int x,int y){x=getf(x);y=getf(y);f[y]=x;sz[x]+=sz[y];}
int dis[MAX][131072];bool vis[MAX][131072];
struct Node{int u,d,S;};
bool operator<(Node a,Node b){return a.d>b.d;}
priority_queue<Node> Q;
void upd(int u,int d,int S){if(dis[u][S]>d)dis[u][S]=d,Q.push((Node){u,d,S});}
int n,m,A,B,book[MAX],tim;
int main()
{
for(int i=1;i<=n;++i)f[i]=i,sz[i]=1,book[i]=-1;
for(int i=1;i<=m;++i)
{
if(w==A)Merge(u,v);
}
for(int i=1;i<=n;++i)
if(book[i]==-1&&sz[getf(i)]>3)
{
for(int j=i;j<=n;++j)
if(getf(i)==getf(j))
book[j]=tim;
++tim;
}
memset(dis,63,sizeof(dis));
upd(1,0,(~book[1])?1<<book[1]:0);
while(!Q.empty())
{
int u=Q.top().u,S=Q.top().S;Q.pop();
if(vis[u][S])continue;vis[u][S]=true;
for(int i=h[u];i;i=e[i].next)
{
int v=e[i].v,w=e[i].w;
if(w==A)upd(v,dis[u][S]+w,S);
else
{
if(getf(u)==getf(v))continue;
if(~book[v]&&(S&(1<<book[v])))continue;
upd(v,dis[u][S]+w,S|((~book[v])?1<<book[v]:0));
}
}
}
for(int i=1;i<=n;++i)
{
int ans=1<<30;
for(int j=0;j<1<<tim;++j)ans=min(ans,dis[i][j]);
printf("%d ",ans);
}
return 0;
}

## E. Election Promises

#include<iostream>
#include<cstdio>
#include<vector>
using namespace std;
#define MAX 200200
{
int x=0;bool t=false;char ch=getchar();
while((ch<'0'||ch>'9')&&ch!='-')ch=getchar();
if(ch=='-')t=true,ch=getchar();
while(ch<='9'&&ch>='0')x=x*10+ch-48,ch=getchar();
return t?-x:x;
}
vector<int> E[MAX];
int n,m,h[MAX],sg[MAX],sum[MAX];
int dg[MAX],Q[MAX],vis[MAX];
void Topsort()
{
int h=1,t=0;
for(int i=1;i<=n;++i)if(!dg[i])Q[++t]=i;
while(h<=t){int u=Q[h++];for(int v:E[u])if(!--dg[v])Q[++t]=v;}
}
int main()
{
for(int i=1;i<=m;++i)
{
E[u].push_back(v);
++dg[v];
}
Topsort();
for(int i=n;i;--i)
{
int u=Q[i];
for(int v:E[u])vis[sg[v]]=i;
while(vis[sg[u]]==i)++sg[u];
sum[sg[u]]^=h[u];
}
for(int i=n;~i;--i)
if(sum[i])
{
int pos;
for(int j=1;j<=n;++j)
if(sg[j]==i&&h[j]>(sum[i]^h[j]))pos=j;
h[pos]^=sum[i];
for(int v:E[pos])h[v]^=sum[sg[v]],sum[sg[v]]=0;
puts("WIN");
for(int j=1;j<=n;++j)printf("%d ",h[j]);
puts("");return 0;
}
puts("LOSE");
return 0;
}
posted @ 2019-05-02 15:48 小蒟蒻yyb 阅读(...) 评论(...) 编辑 收藏