搜索树与可持久化数组
一道搜索题引发了我对离线算法的兴趣,后来发现我想到的这种方法在所有离线算法中算是比较优美的,其它的一个比一个恶心
以此博客为界,对离线算法的学习先告一段落
#include <bits/stdc++.h>
using namespace std;
int k,cnt,ans;
map<string,int> mp;
int t[3][25];
bool is[100];
void dfs(int i=0,int res=0){
if(i==k){
if(res>ans)ans = res;
return ;
}
dfs(i+1,res);
if(!is[t[0][i]] && !is[t[1][i]] && !is[t[2][i]]){
is[t[0][i]] = 1; is[t[1][i]] = 1; is[t[2][i]] = 1;
dfs(i+1,res+1);
is[t[0][i]] = 0; is[t[1][i]] = 0; is[t[2][i]] = 0;
}
}
int main(){
// freopen("teams.in","r",stdin);
// freopen("teams.out","w",stdout);
scanf("%d",&k);
for(int i = 0; i < k ;i++){
for(int j = 0 ; j < 3 ; j++){
string str;
cin >> str;
if(!mp.count(str))mp[str] = ++cnt;
t[j][i] = mp[str];
}
}
dfs();
printf("%d",ans);
return 0;
}
这是一道非常简单的搜索题,题目大意是:有
问最多有几支队伍能同时参赛
我们观察这个函数,发现它像是在遍历一棵二叉树
事实上,这个函数一直在维护一个
那么我们只要把
高级打字机
这道题要求我们维护一个
这样一来,实际上每
无论在空间上还是时间上都造成了巨大的浪费
运用上面的思想,我们发现字符串的版本之间的依赖关系也是一棵树
因此,我们只要将树建出来,遍历一次即可
这样一来,无论是时间复杂度还是空间复杂度都是
因为在时间复杂度和空间复杂度上的巨大优势,这份代码只需要加一个快读便可以轻松登顶排行榜
#include<bits/stdc++.h>
using namespace std;
int n;
char c[100005],op[100005];
int f[100005],ne[100005],to[100005],cnt,ed=1,loc[100005];
char ans[100005];
inline int read(){
register int x=0,w=1;
register char ch=getchar();
for (;ch<'0'||ch>'9';ch=getchar())if(ch == '-')w=-1;
for (;ch>='0'&&ch<='9';ch=getchar())x=x*10+ch-'0';
return x * w;
}
inline void link(int x,int y){
to[++cnt]=y;
ne[cnt]=f[x];
f[x]=cnt;
}
void dfs(int x){
if(op[x]=='Q')ans[x]=c[loc[x]];
for(int i=f[x];i;i=ne[i]){
int u=to[i];
if(op[u]!='Q'&&op[u]!='U')c[ed++]=op[u];
dfs(u);
if(op[u]!='Q'&&op[u]!='U')ed--;
}
}
int stk[100005],top;
int main(){
n=read();
for(int i=1;i<=n;i++){
scanf("%s",&op[i]);
if(op[i]=='T'){
scanf("%s",&op[i]);
link(stk[top],i);stk[++top]=i;
}
else if(op[i]=='Q'){
loc[i]=read();
link(stk[top],i);
}
else{
int x;
x=read();
link(stk[top-x],i);stk[++top]=i;
}
}
dfs(0);
for(int i=1;i<=n;i++)if(op[i]=='Q')printf("%c\n",ans[i]);
return 0;
}
可持久化并查集
这题也是一样,只需一个快读,就是榜首
#include<bits/stdc++.h>
using namespace std;
int n,m;
inline int read(){
register int x=0,w=1;
register char ch=getchar();
for (;ch<'0'||ch>'9';ch=getchar())if(ch == '-')w=-1;
for (;ch>='0'&&ch<='9';ch=getchar())x=x*10+ch-'0';
return x * w;
}
int f[200005],ne[200005],to[200005],cnt;
inline void link(int x,int y){
to[++cnt]=y;
ne[cnt]=f[x];
f[x]=cnt;
}
int fa[100005],size[100005];
inline int find(int x){
while(fa[x]^x)x=fa[x];
return x;
}
int a[200005],b[200005],op[200005];
bool ans[200005];
void dfs(int x){
if(op[x]==3)ans[x]=find(a[x])==find(b[x]);
// printf("%d %d\n",x,op[x]);
// for(int i=1;i<=n;i++)printf("%d: %d\n",i,fa[i]);
for(int i=f[x];i;i=ne[i]){
int u=to[i];
if(op[u]==1){
a[u]=find(a[u]);b[u]=find(b[u]);
if(size[a[u]]>size[b[u]]){
fa[b[u]]=a[u];size[a[u]]+=size[b[u]];
dfs(u);
fa[b[u]]=b[u];size[a[u]]-=size[b[u]];
}
else {
fa[a[u]]=b[u];size[b[u]]+=size[a[u]];
dfs(u);
fa[a[u]]=a[u];size[b[u]]-=size[a[u]];
}
}
else dfs(u);
}
}
int main(){
n=read();m=read();
for(int i=1;i<=m;i++){
op[i]=read();
if(op[i]==1){
a[i]=read();b[i]=read();
link(i-1,i);
}
else if(op[i]==2){
int k=read();
link(k,i);
}
else {
a[i]=read();b[i]=read();
link(i-1,i);
}
}
for(int i=1;i<=n;i++)fa[i]=i,size[i]=1;
dfs(0);
for(int i=1;i<=m;i++)if(op[i]==3)puts(ans[i]?"1":"0");
return 0;
}
可持久化线段树 1(可持久化数组)
既然是讨论可持久化数组,自然不能少了可持久化数组的板子
#include<bits/stdc++.h>
using namespace std;
int n,m;
int a[1000005],op[1000005],loc[1000005];
int f[1000005],ne[1000005],to[1000005],cnt;
int ans[1000005];
inline int read(){
register int x=0,w=1;
register char ch=getchar();
for (;ch<'0'||ch>'9';ch=getchar())if(ch == '-')w=-1;
for (;ch>='0'&&ch<='9';ch=getchar())x=x*10+ch-'0';
return x * w;
}
inline void link(int x,int y){
to[++cnt]=y;
ne[cnt]=f[x];
f[x]=cnt;
}
void dfs(int x){
ans[x]=a[loc[x]];
for(int i=f[x];i;i=ne[i]){
int u=to[i];
if(op[u])swap(a[loc[u]],op[u]);
dfs(u);
if(op[u])swap(a[loc[u]],op[u]);
}
}
int main(){
n=read();m=read();
for(int i=1;i<=n;i++)a[i]=read();
for(int i=1;i<=m;i++){
int v=read();op[i]=read();loc[i]=read();
link(v,i);
if(op[i]==2)op[i]=0;
else scanf("%d",&op[i]);
}
dfs(0);
for(int i=1;i<=m;i++)if(!op[i])printf("%d\n",ans[i]);
return 0;
}