GDKOI2015
problems
http://gdoi.sysu.edu.cn/wp-content/uploads/2015/03/GDKOI-2015-day1.pdf
http://gdoi.sysu.edu.cn/wp-content/uploads/2015/03/GDKOI-2015-day21.pdf
necklace
回文串问题。
把字符串复制一遍,使得环状变成线状。
然后就是问最长回文串,直接套用manacher算法。
wordcount
网络流问题。
把每个格点(i,j)在网络流中拆分成两个点,这两个点之间连一条流量为cnt[i][j]的边,其中左边的称为入点,右边的称为出点。
#include<cstdio>
#include<cstdlib>
#include<iostream>
#include<fstream>
#include<algorithm>
#include<cstring>
#include<string>
#include<cmath>
#include<queue>
#include<stack>
#include<map>
#include<utility>
#include<set>
#include<bitset>
#include<vector>
#include<functional> using namespace std; typedef long long LL;
typedef double DB;
typedef pair<int,int> PII; #define mmst(a,v) memset(a,v,sizeof(a))
#define mmcy(a,b) memcpy(a,b,sizeof(a))
#define re(i,a,b) for(i=a;i<=b;i++)
#define red(i,a,b) for(i=a;i>=b;i--)
#define fi first
#define se second template<class T>inline T sqr(T x){return x*x;}
template<class T>inline void upmin(T &t,T tmp){if(t>tmp)t=tmp;}
template<class T>inline void upmax(T &t,T tmp){if(t<tmp)t=tmp;} const DB EPS=1e-;
inline int dblcmp(DB x){if(abs(x)<EPS)return ;return(x>)?:-;} inline void SetOpen(string s)
{
freopen((s+".in").c_str(),"r",stdin);
freopen((s+".out").c_str(),"w",stdout);
} inline int Getin_Int()
{
int res=,flag=;char z;
for(z=getchar();z!=EOF && z!='-' && !isdigit(z);z=getchar());
if(z==EOF)return ;
if(z=='-'){flag=-flag;z=getchar();}
for(;z!=EOF && isdigit(z);res=res*+z-'',z=getchar());
return res*flag;
}
inline LL Getin_LL()
{
LL res=,flag=;char z;
for(z=getchar();z!=EOF && z!='-' && !isdigit(z);z=getchar());
if(z==EOF)return ;
if(z=='-'){flag=-flag;z=getchar();}
for(;z!=EOF && isdigit(z);res=res*+z-'',z=getchar());
return res*flag;
} const int maxN=; int N;
vector<int> edge[maxN/+],ans; inline int DFS(int x)
{
int now=x;
x%=N/;
while(!edge[x].empty())
{
int y=edge[x].back();
edge[x].pop_back();
DFS(y);
}
ans.push_back(now);
} int main()
{
SetOpen("circle");
int i;
N=Getin_Int();
if(N&){puts("-1\n");return ;}
re(i,,N/-){edge[i].push_back(*i);edge[i].push_back(*i+);}
DFS();
red(i,ans.size()-,)printf("%d ",ans[i]);printf("\n");
return ;
}
Ask u v c:先求出lca,然后在第c棵中查询:u到根节点的路径中颜色为c的点的权值和+u到根节点的路径中颜色为c的点的权值和-lca到根节点的路径中颜色为c的权值和-lca的父亲到根节点的路径中颜色为c的权值和
#include<cstdio>
#include<cstdlib>
#include<iostream>
#include<fstream>
#include<algorithm>
#include<cstring>
#include<string>
#include<cmath>
#include<queue>
#include<stack>
#include<map>
#include<utility>
#include<set>
#include<bitset>
#include<vector>
#include<functional> using namespace std; typedef long long LL;
typedef double DB;
typedef pair<int,int> PII; #define mmst(a,v) memset(a,v,sizeof(a))
#define mmcy(a,b) memcpy(a,b,sizeof(a))
#define re(i,a,b) for(i=a;i<=b;i++)
#define red(i,a,b) for(i=a;i>=b;i--)
#define fi first
#define se second template<class T>inline T sqr(T x){return x*x;}
template<class T>inline void upmin(T &t,T tmp){if(t>tmp)t=tmp;}
template<class T>inline void upmax(T &t,T tmp){if(t<tmp)t=tmp;} const DB EPS=1e-;
inline int dblcmp(DB x){if(abs(x)<EPS)return ;return(x>)?:-;} inline void SetOpen(string s)
{
freopen((s+".in").c_str(),"r",stdin);
freopen((s+".out").c_str(),"w",stdout);
} inline int Getin_Int()
{
int res=,flag=;char z;
for(z=getchar();z!=EOF && z!='-' && !isdigit(z);z=getchar());
if(z==EOF)return ;
if(z=='-'){flag=-flag;z=getchar();}
for(;z!=EOF && isdigit(z);res=res*+z-'',z=getchar());
return res*flag;
}
inline LL Getin_LL()
{
LL res=,flag=;char z;
for(z=getchar();z!=EOF && z!='-' && !isdigit(z);z=getchar());
if(z==EOF)return ;
if(z=='-'){flag=-flag;z=getchar();}
for(;z!=EOF && isdigit(z);res=res*+z-'',z=getchar());
return res*flag;
} const int maxN=;
const int maxM=;
const int maxC=; int N,M,C;
int c[maxN+],v[maxN+];
int first[maxN+],now;
struct Tedge{int v,next;}edge[*maxN+];
int fa[maxN+],dep[maxN+];
int jump[maxN+][];
int sum[maxN+][maxC+];
int id[maxN+],l[maxN+],r[maxN+],cnt; inline void addedge(int u,int v)
{
now++;
edge[now].v=v;
edge[now].next=first[u];
first[u]=now;
} int que[maxN+],head,tail;
inline void BFS(int S)
{
mmst(fa,-);
que[head=tail=]=S;
fa[S]=;
dep[S]=;
while(head<=tail)
{
int u=que[head++],i,v;
for(i=first[u],v=edge[i].v;i!=-;i=edge[i].next,v=edge[i].v)if(v!=fa[u])
{
que[++tail]=v;
fa[v]=u;
dep[v]=dep[u]+;
}
}
} int sta[maxN+],top;
int last[maxN+];
inline void DFS(int S)
{
int j;
re(j,,N)last[j]=first[j];
sta[top=]=S;
id[S]=cnt=;
while(top>=)
{
int u=sta[top],&i=last[u],v;
for(v=edge[i].v;i!=-;i=edge[i].next,v=edge[i].v)if(!id[v])
{
id[v]=++cnt;
sta[++top]=v;
break;
}
if(i==-)top--;
}
} inline void swim(int &x,int H)
{
int i;
for(i=;H!=;H/=,i++)if(H&)x=jump[x][i];
}
inline int Ask_LCA(int x,int y)
{
if(dep[x]<dep[y])swap(x,y);
swim(x,dep[x]-dep[y]);
if(x==y)return x;
int i;
red(i,-,)if(jump[x][i]!=jump[y][i]){x=jump[x][i];y=jump[y][i];}
return jump[x][];
} struct Ttree
{
Ttree *l,*r;
int add,v;
}*tree[maxC+]; inline Ttree *NewTtree()
{
Ttree *res=new Ttree;
res->l=res->r=;
res->add=res->v=;
return res;
} inline void down(Ttree *&root,int l,int r,int mid)
{
if(root->add==)return;
int add=root->add;root->add=;
if(l==r)return;
if(!root->l)root->l=NewTtree();
if(l==mid) root->l->v+=add;
root->l->add+=add;
if(!root->r)root->r=NewTtree();
if(mid+==r) root->r->v+=add;
root->r->add+=add;
} inline void update(Ttree *&root,int l,int r,int x,int y,int v)
{
if(!root) root=NewTtree();
if(l>r || y<l || r<x)return;
int mid=(l+r)/;
down(root,l,r,mid);
if(x<=l && r<=y)
{
if(l==r)root->v+=v;
root->add+=v;
return;
}
update(root->l,l,mid,x,y,v);
update(root->r,mid+,r,x,y,v);
} inline void change(Ttree *&R1,Ttree *&R2,int l,int r,int x,int y,int temp1,int temp2)
{
if(!R1) R1=NewTtree();
if(!R2) R2=NewTtree();
if(l>r || y<l || r<x) return;
int mid=(l+r)/;
down(R1,l,r,mid);
down(R2,l,r,mid);
if(x<=l && r<=y)
{
if(l==r)R1->v+=-temp1+temp2;R1->add+=-temp1+temp2;
if(l==r)R2->v+=-temp2+temp1;R2->add+=-temp2+temp1;
swap(R1,R2);
return;
}
change(R1->l,R2->l,l,mid,x,y,temp1,temp2);
change(R1->r,R2->r,mid+,r,x,y,temp1,temp2);
} inline int ask(Ttree *&root,int l,int r,int x)
{
if(!root) root=NewTtree();
if(l>r || x<l || r<x)return ;
int mid=(l+r)/;
down(root,l,r,mid);
if(x<=l && r<=x)return root->v;
if(x<=mid) return ask(root->l,l,mid,x); else return ask(root->r,mid+,r,x);
} int main()
{
SetOpen("tree");
int i,j;
N=Getin_Int();C=;
re(i,,N){c[i]=Getin_Int()+;upmax(C,c[i]);}
re(i,,N)v[i]=Getin_Int();
mmst(first,-);now=-;
re(i,,N-)
{
int x=Getin_Int()+,y=Getin_Int()+;
addedge(x,y);
addedge(y,x);
}
BFS();
re(i,,tail)
{
int u=que[i];
jump[u][]=fa[u];
re(j,,-)jump[u][j]=jump[jump[u][j-]][j-];
}
re(i,,tail)
{
int u=que[i];
re(j,,C)sum[u][j]=sum[fa[u]][j];
sum[u][c[u]]+=v[u];
}
DFS();
red(j,tail,)
{
int u=que[j],v;
l[u]=r[u]=id[u];
for(i=first[u],v=edge[i].v;i!=-;i=edge[i].next,v=edge[i].v)if(v!=fa[u])
{
upmin(l[u],l[v]);
upmax(r[u],r[v]);
}
}
re(i,,N)re(j,,C)update(tree[j],,N,id[i],id[i],sum[i][j]);
M=Getin_Int();
while(M--)
{
char S[];
int a,b,x,y,c,val,lca,temp1,temp2,temp3;
scanf("%s",S);
switch(S[])
{
case 'C':
a=Getin_Int()+;x=Getin_Int()+;y=Getin_Int()+;
temp1=ask(tree[x],,N,id[fa[a]]);
temp2=ask(tree[y],,N,id[fa[a]]);
change(tree[x],tree[y],,N,l[a],r[a],temp1,temp2);
break;
case 'A':
a=Getin_Int()+;b=Getin_Int()+;c=Getin_Int()+;
lca=Ask_LCA(a,b);
printf("%d\n",ask(tree[c],,N,id[a])+ask(tree[c],,N,id[b])-ask(tree[c],,N,id[lca])-ask(tree[c],,N,id[fa[lca]]));
break;
case 'S':
a=Getin_Int()+;c=Getin_Int()+;val=Getin_Int();
re(i,,C)
{
temp1=ask(tree[i],,N,id[a])-ask(tree[i],,N,id[fa[a]]);
if(temp1==) continue;
update(tree[i],,N,l[a],r[a],-temp1);
break;
}
update(tree[c],,N,l[a],r[a],val);
break;
}
}
return ;
}
#include<cstdio>
#include<cstdlib>
#include<iostream>
#include<fstream>
#include<algorithm>
#include<cstring>
#include<string>
#include<cmath>
#include<queue>
#include<stack>
#include<map>
#include<utility>
#include<set>
#include<bitset>
#include<vector>
#include<functional> using namespace std; typedef long long LL;
typedef double DB;
typedef pair<int,int> PII; #define mmst(a,v) memset(a,v,sizeof(a))
#define mmcy(a,b) memcpy(a,b,sizeof(a))
#define re(i,a,b) for(i=a;i<=b;i++)
#define red(i,a,b) for(i=a;i>=b;i--)
#define fi first
#define se second template<class T>inline T sqr(T x){return x*x;}
template<class T>inline void upmin(T &t,T tmp){if(t>tmp)t=tmp;}
template<class T>inline void upmax(T &t,T tmp){if(t<tmp)t=tmp;} const DB EPS=1e-;
inline int dblcmp(DB x){if(abs(x)<EPS)return ;return(x>)?:-;} inline void SetOpen(string s)
{
freopen((s+".in").c_str(),"r",stdin);
freopen((s+".out").c_str(),"w",stdout);
} inline int Getin_Int()
{
int res=,flag=;char z;
for(z=getchar();z!=EOF && z!='-' && !isdigit(z);z=getchar());
if(z==EOF)return ;
if(z=='-'){flag=-flag;z=getchar();}
for(;z!=EOF && isdigit(z);res=res*+z-'',z=getchar());
return res*flag;
}
inline LL Getin_LL()
{
LL res=,flag=;char z;
for(z=getchar();z!=EOF && z!='-' && !isdigit(z);z=getchar());
if(z==EOF)return ;
if(z=='-'){flag=-flag;z=getchar();}
for(;z!=EOF && isdigit(z);res=res*+z-'',z=getchar());
return res*flag;
} const int maxN=;
const int maxK=maxN/; int N,K;
struct Tdata
{
int x,y,z;
inline void input(){x=Getin_Int();y=Getin_Int();z=Getin_Int();}
}data[maxN+];
int F[maxN+][maxK+];
int ans; inline bool cmpy(Tdata a,Tdata b){return a.y>b.y;} int main()
{
SetOpen("planetcup");
int i,j,k;
N=Getin_Int();K=Getin_Int();
re(i,,N)data[i].input();
sort(data+,data+N+,cmpy);
ans=;
re(k,,N)
{
int p=data[k].x,cnt=;
mmst(F,-);
F[][]=;
re(i,,N-)
{
if(data[i+].z== && data[i+].x<p)
{
re(j,,K)F[i+][j]=F[i][j];
continue;
}
re(j,,K)
{
if(F[i][j]==-)continue;
if(data[i+].z== && data[i+].x<p)
{
if(cnt-j+<=K) upmax(F[i+][j],F[i][j]+data[i+].y); else upmax(F[i+][j],F[i][j]);
continue;
}
if(data[i+].x>=p && j+<=K)
upmax(F[i+][j+],F[i][j]+data[i+].x*data[i+].z);
if(cnt-j+<=K) upmax(F[i+][j],F[i][j]+data[i+].y*data[i+].z); else upmax(F[i+][j],F[i][j]);
}
cnt++;
}
upmax(ans,F[N][K]);
}
cout<<ans<<endl;
return ;
}
#include<cstdio>
#include<cstdlib>
#include<iostream>
#include<fstream>
#include<algorithm>
#include<cstring>
#include<string>
#include<cmath>
#include<queue>
#include<stack>
#include<map>
#include<utility>
#include<set>
#include<bitset>
#include<vector>
#include<functional> using namespace std; typedef long long LL;
typedef double DB;
typedef pair<int,int> PII; #define mmst(a,v) memset(a,v,sizeof(a))
#define mmcy(a,b) memcpy(a,b,sizeof(a))
#define re(i,a,b) for(i=a;i<=b;i++)
#define red(i,a,b) for(i=a;i>=b;i--)
#define fi first
#define se second template<class T>inline T sqr(T x){return x*x;}
template<class T>inline void upmin(T &t,T tmp){if(t>tmp)t=tmp;}
template<class T>inline void upmax(T &t,T tmp){if(t<tmp)t=tmp;} const DB EPS=1e-;
inline int dblcmp(DB x){if(abs(x)<EPS)return ;return(x>)?:-;} inline void SetOpen(string s)
{
freopen((s+".in").c_str(),"r",stdin);
freopen((s+".out").c_str(),"w",stdout);
} inline int Getin_Int()
{
int res=,flag=;char z;
for(z=getchar();z!=EOF && z!='-' && !isdigit(z);z=getchar());
if(z==EOF)return ;
if(z=='-'){flag=-flag;z=getchar();}
for(;z!=EOF && isdigit(z);res=res*+z-'',z=getchar());
return res*flag;
}
inline LL Getin_LL()
{
LL res=,flag=;char z;
for(z=getchar();z!=EOF && z!='-' && !isdigit(z);z=getchar());
if(z==EOF)return ;
if(z=='-'){flag=-flag;z=getchar();}
for(;z!=EOF && isdigit(z);res=res*+z-'',z=getchar());
return res*flag;
} const int maxN=; int N,Q;
int first[maxN+],now;
struct Tedge{int v,next;}edge[*maxN+];
int fa[maxN+],dep[maxN+];
int jump[maxN+][];
int p[maxN+][];//0上升 1下降 2先上后下 3先下后上
int t[maxN+][];
int ans; inline void addedge(int u,int v)
{
now++;
edge[now].v=v;
edge[now].next=first[u];
first[u]=now;
} int que[maxN+],head,tail;
inline void BFS(int S)
{
dep[que[head=tail=]=S]=;
while(head<=tail)
{
int u=que[head++],i,v;
for(i=first[u],v=edge[i].v;i!=-;i=edge[i].next,v=edge[i].v)dep[que[++tail]=v]=dep[u]+;
}
} inline int swim(int x,int H)
{
for(int i=;H!=;H/=,i++)if(H&)x=jump[x][i];
return x;
}
inline int Ask_LCA(int x,int y)
{
if(dep[x]<dep[y]) swap(x,y);
x=swim(x,dep[x]-dep[y]);
if(x==y)return x;
int i;
red(i,,)if(jump[x][i]!=jump[y][i]){x=jump[x][i];y=jump[y][i];}
return jump[x][];
} inline int up(int x,int y,int f)
{
return min(p[x][f],dep[x]-dep[y]+);
}
inline int down(int x,int y,int f)
{
int l=,r=dep[x]-dep[y],mid;
while(l<=r)
{
mid=(l+r)/;
int temp=swim(x,mid);
if(p[temp][f]>=dep[temp]-dep[y]+) r=mid-; else l=mid+;
}
return dep[swim(x,l)]-dep[y]+;
} inline int solve(int a,int b)
{
int i,res=,H=dep[a]-dep[b]+,x=a,y;
for(i=;H!=;H/=,i++)if(H&)
{
y=swim(x,(<<i)-);
upmax(res,t[x][i]);
if(a!=x)
{
upmax(res,down(a,x,)+up(x,y,)-);
upmax(res,down(a,x,)+up(x,y,)-);
upmax(res,down(a,x,)+up(x,y,)-);
upmax(res,down(a,x,)+up(x,y,)-);
}
x=fa[y];
}
return res;
} int main()
{
SetOpen("v");
int i,j;
N=Getin_Int();
mmst(first,-);now=-;
re(i,,N){fa[i]=Getin_Int();addedge(fa[i],i);}
BFS();
re(i,,)jump[][i]=;
re(j,,tail)
{
int u=que[j];
jump[u][]=fa[u];
re(i,,)jump[u][i]=jump[jump[u][i-]][i-];
}
re(j,,tail)
{
int u=que[j];
if(u<fa[u])p[u][]=p[fa[u]][]+; else p[u][]=;
if(u>fa[u])p[u][]=p[fa[u]][]+; else p[u][]=;
p[u][]=p[u][];if(u<fa[u])p[u][]=max(p[fa[u]][],p[fa[u]][])+;
p[u][]=p[u][];if(u>fa[u])p[u][]=max(p[fa[u]][],p[fa[u]][])+;
}
re(j,,tail)
{
int a=que[j],c,d;
t[a][]=;
re(i,,)
{
c=jump[a][i-];
d=swim(c,dep[a]-dep[c]-);
t[a][i]=max(t[a][i-],t[c][i-]);
upmax(t[a][i],down(a,c,)+up(c,d,)-);
upmax(t[a][i],down(a,c,)+up(c,d,)-);
upmax(t[a][i],down(a,c,)+up(c,d,)-);
upmax(t[a][i],down(a,c,)+up(c,d,)-);
}
}
ans=;
Q=Getin_Int();
while(Q--)
{
int u=Getin_Int()^ans,v=Getin_Int()^ans,lca;
ans=;
lca=Ask_LCA(u,v);
upmax(ans,solve(u,lca));
upmax(ans,solve(v,lca));
upmax(ans,down(u,lca,)+down(v,lca,)-);
upmax(ans,down(u,lca,)+down(v,lca,)-);
upmax(ans,down(u,lca,)+down(v,lca,)-);
upmax(ans,down(u,lca,)+down(v,lca,)-);
printf("%d\n",ans);
}
return ;
}
GDKOI2015的更多相关文章
- GDKOI2015 Day2
P1 题目描述: 给出一个二分图,选择互不相交的边,使得边覆盖的点权和最大. solution: 简单DP,用树状数组维护最大值. 时间复杂度:$O(n \log n) $ P2 题目描述: 给出N个 ...
- GDKOI2015 Day1
P1 题目描述: 判断一个环形字符串(或者减去一个字符之后)是否是回文串 solution: 1.hash 将字符串的前缀进行hash,然后将字符串翻转,再做一次hash,然后枚举对称轴,判断两边的h ...
- GDKOI2015滚粗记
又是愉悦的滚粗了hahaha(特别不甘心啊啊啊) 其实去比赛每次都一样啦,就是每次吃饭睡觉补番考试评讲互黑跪烂什么的,这次就不用说了啦,先把老师要求写的东西贴出来再写点别的啦 这次暴露了很多问题,首先 ...
- GDKOI 2015 Day1 T2 单词统计Pascal
我虽然没有参加GDKOI2015,但是我找了2015年的题练了一下. 题意如下: 思路:最大流,因为有多组数据,每次读入一组数据都要清零. a. 将每个点拆分成两个点,例如样例G→G`,再将字母一一编 ...
- GDOI2016酱油记(补发)
这篇酱油记是前年发在MCHacker一个叫code-hub的博客上的(已崩),现在来补发一下... GDOI2016扯淡(爆零记) 大家好,我是巨弱DCDCBigBig,在五一期间和一群神牛去考GDO ...
随机推荐
- mybatis dao无实现类的配置
spring的配置文件 添加: <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> ...
- Spring MVC 前后台数据交互
本文是转载文章,感觉比较好,如有侵权,请联系本人,我将及时删除. 原文网址地址:<Spring MVC 前后台数据交互> 1.服务端数据到客户端 (1)返回页面,Controller中方法 ...
- css浮动+应用(瀑布流效果的实现)
首先是index.html文件: <!DOCTYPE html> <html> <head> <meta charset="UTF-8"& ...
- ie11 selenium 报错org.openqa.selenium.NoSuchWindowException: Unable to get browser 处理方法
selenium + ie11运行报错 org.openqa.selenium.NoSuchWindowException: Unable to get browser (WARNING: The s ...
- servletContext百科
servletContext 编辑 servletContext接口是Servlet中最大的一个接口,呈现了web应用的Servlet视图.ServletContext实例是通过 getServl ...
- java\C#\php主流语言实现FMS流媒体传输协议RTMP的开源组件
java:bladeDS http://sourceforge.net/adobe/blazeds/wiki/Home/ .net:FlourinceFX http://www.fluorinefx. ...
- NS2仿真:两个移动节点网络连接及协议性能分析
NS2仿真实验报告2 实验名称:两个移动节点网络连接及协议性能分析 实验日期:2015年3月9日~2015年3月14日 实验报告日期:2015年3月15日 一.实验环境(网络平台,操作系统,网络拓扑图 ...
- css选择器基本属性
选择器一,相邻选择器: 1,相邻选择器 1),定义:相邻选择器匹配指定元素的相邻兄弟元素 2),用法:如果需要选择紧接在另一个元素后的元素,而且二者有相同的父元素,可以使用相邻兄弟选择器 3),表示符 ...
- 图片设置3D效果
/** * 图片绘制3d效果 * @param srcImage * @param radius * @param border * @param padding * @return * @throw ...
- C库专题(Day1)
<assert.h> C库宏-assert() 定义:#define assert(ignore) ((void)0) void assert(int experession); ex ...