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 ...
随机推荐
- PE基金的运作模式有哪些?
一.信托制(1)信托型基金是由基金管理机构与信托公司合作设立,通过发起设立信托受益份额募集资金,然后进行投资运作的集合投资工具(2)信托公司和基金管理机构组成决策委员会实施,共同进行决策(3)在内部分 ...
- 解析了grid2008的代码
import java.io.IOException; import java.util.ArrayList; import java.util.HashMap; import java.util.L ...
- 新版ADT出现appcompat_v7的问题
问题 更新ADT至22.6.0版本号之后,创建新的安装项目,会出现appcompat_v7的内容.而且是创建一个新的内容就会出现. 事实上appcompat_v7是Google自己的一个兼容包.一个支 ...
- AxisFault另外一个问题
出现以下情况,能够是proxy.setEndpoint(endpoint);中endpoint不正确导致 因该是:endpoint = http://127.0.0.1/8080/项目名/servic ...
- PLSQL developer 连接不上64位Oracle 的解决方法
Windows 64位下装Oracle 11g 64位,PLSQL Developer使用出现以下问题: 1.Database下拉框为空:2.强制输入用户名.密码及Database,登录弹出: 引用 ...
- Python之基础(二)
1.内建函数enumerate friends = ['john', 'pat', 'gary', 'michael'] for i, name in enumerate(friends): prin ...
- Python enumerate函数
enumerate函数接受一个可遍历的对象,如列表.字符串,可同时遍历下标(index)及元素值(value) >>> a = ['aaa','bbb','ccc',1235] &g ...
- Lambda 笔记
lambda表达式,将会带来代码的灵活性,同时使我们的代码更具表现力. Dim doubleIt As Func(Of Integer, Integer) = _ Function(x As Inte ...
- 转载——CLR标量函数、表值函数和聚合函数(UDA)
本节主要介绍使用CLR创建标量函数,表值函数和聚合函数. 所谓标量函数指的就是此函数只返回一个值.表值函数返回值是一个表.聚合函数是在select语句中使用的,用来聚合一个结果集,类似于Sum()或是 ...
- Oracle Enterprise Manager快速重建
我们在使用Oracle时, 可以利用Oracle自带的EM(Enterprise Manager)来更方便的管理我们的数据库.但是有时候我们的em却有时候无法连接,造成这个问题的原因有好多,例如没有正 ...