【BZOJ1125】[POI2008]Poc hash+map+SBT
【BZOJ1125】[POI2008]Poc
Description
n列火车,每条有l节车厢。每节车厢有一种颜色(用小写字母表示)。有m次车厢交换操作。求:对于每列火车,在交换车厢的某个时刻,与其颜色完全相同的火车最多有多少。
Input
n l m (2 ≤ n ≤ 1000, 1 ≤ l ≤ 100, 0 ≤ m ≤ 100000) n行字符串,长度为l m行,每行4个数a b c d,a车的第b个字符与c车第d个字符交换。
Output
n个数,在交换车厢的某个时刻,与该车颜色完全相同的火车最多数目。
Sample Input
ababbd
abbbbd
aaabad
caabbd
cabaad
2 3 5 4
5 3 5 5
3 5 2 2
1 2 4 3
2 2 5 1
1 1 3 3
4 1 5 6
Sample Output
3
3
2
3
题解:考虑将hash值相同的都放到同一棵平衡树中(可以用map维护平衡树的root)。这样删除和插入都比较容易,每次加入的时候都给平衡树的整体打一个最大值标记即可。
注意a=c的情况。
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
using namespace std;
typedef unsigned long long ll;
int n,len,m,ret;
char str[1010][110];
struct node
{
int siz,ch[2],tag,org;
}s[1010];
ll bas[110],hs[1010];
int ans[1010];
map<ll,int> rt;
void pushup(int x)
{
s[x].siz=s[s[x].ch[0]].siz+s[s[x].ch[1]].siz+1;
}
void updata(int x,int val) {ans[s[x].org]=max(ans[s[x].org],val),s[x].tag=max(s[x].tag,val);}
void pushdown(int x)
{
if(s[x].tag)
{
if(s[x].ch[0]) updata(s[x].ch[0],s[x].tag);
if(s[x].ch[1]) updata(s[x].ch[1],s[x].tag);
s[x].tag=0;
}
}
inline int rd()
{
int ret=0,f=1; char gc=getchar();
while(gc<'0'||gc>'9') {if(gc=='-')f=-f; gc=getchar();}
while(gc>='0'&&gc<='9') ret=ret*10+gc-'0',gc=getchar();
return ret*f;
}
void rotate(int &x,int d)
{
int y=s[x].ch[d];
pushdown(x),pushdown(y);
s[x].ch[d]=s[y].ch[d^1],s[y].ch[d^1]=x;
pushup(x),pushup(y);
x=y;
}
void maintain(int &x,int d)
{
if(s[s[s[x].ch[d]].ch[d]].siz>s[s[x].ch[d^1]].siz) rotate(x,d);
else if(s[s[s[x].ch[d]].ch[d^1]].siz>s[s[x].ch[d^1]].siz) rotate(s[x].ch[d],d^1),rotate(x,d);
else return ;
maintain(s[x].ch[d],d),maintain(s[x].ch[d^1],d^1);
maintain(x,d),maintain(x,d^1);
}
void insert(int &x,int y)
{
if(!x)
{
x=y,s[x].ch[0]=s[x].ch[1]=0,s[x].siz=1,s[x].tag=0;
return ;
}
pushdown(x);
int d=(s[y].org>s[x].org);
insert(s[x].ch[d],y),s[x].siz++;
maintain(x,d);
}
void del(int &x,int y)
{
s[x].siz--,pushdown(x);
if(y>s[x].org) del(s[x].ch[1],y);
else if(y<s[x].org) del(s[x].ch[0],y);
else
{
if(!s[x].ch[0]||!s[x].ch[1]) ret=x,x=s[x].ch[0]^s[x].ch[1];
else
{
int u=s[x].ch[1]; pushdown(u);
while(s[u].ch[0]) u=s[u].ch[0],pushdown(u);
s[x].org=s[u].org;
del(s[x].ch[1],s[u].org);
}
}
}
int main()
{
n=rd(),len=rd(),m=rd();
int i,j,a,b,c,d,ra,rb;
for(bas[0]=1,i=1;i<=len;i++) bas[i]=bas[i-1]*131;
for(i=1;i<=n;i++)
{
scanf("%s",str[i]+1);
for(j=1;j<=len;j++) hs[i]=hs[i]*131+str[i][j];
s[i].org=i,insert(rt[hs[i]],i),updata(rt[hs[i]],s[rt[hs[i]]].siz);
}
for(i=1;i<=m;i++)
{
a=rd(),b=rd(),c=rd(),d=rd();
if(a==c)
{
del(rt[hs[a]],a),s[ret].org=a;
hs[a]+=(str[a][d]-str[a][b])*bas[len-b];
hs[a]+=(str[a][b]-str[a][d])*bas[len-d];
insert(rt[hs[a]],ret),updata(rt[hs[a]],s[rt[hs[a]]].siz);
swap(str[a][b],str[a][d]);
continue;
}
del(rt[hs[a]],a),s[ret].org=a,ra=ret;
del(rt[hs[c]],c),s[ret].org=c,rb=ret;
hs[a]+=(str[c][d]-str[a][b])*bas[len-b];
hs[c]+=(str[a][b]-str[c][d])*bas[len-d];
insert(rt[hs[a]],ra),updata(rt[hs[a]],s[rt[hs[a]]].siz);
insert(rt[hs[c]],rb),updata(rt[hs[c]],s[rt[hs[c]]].siz);
swap(str[a][b],str[c][d]);
}
for(i=1;i<=n;i++) del(rt[hs[i]],i);
for(i=1;i<=n;i++) printf("%d\n",ans[i]);
return 0;
}//5 6 7 ababbd abbbbd aaabad caabbd cabaad 2 3 5 4 5 3 5 5 3 5 2 2 1 2 4 3 2 2 5 1 1 1 3 3 4 1 5 6
【BZOJ1125】[POI2008]Poc hash+map+SBT的更多相关文章
- 洛谷P3369 【模板】普通平衡树(Treap/SBT)
洛谷P3369 [模板]普通平衡树(Treap/SBT) 平衡树,一种其妙的数据结构 题目传送门 题目描述 您需要写一种数据结构(可参考题目标题),来维护一些数,其中需要提供以下操作: 插入x数 删除 ...
- [luogu P3369]【模板】普通平衡树(Treap/SBT)
[luogu P3369][模板]普通平衡树(Treap/SBT) 题目描述 您需要写一种数据结构(可参考题目标题),来维护一些数,其中需要提供以下操作: 插入x数 删除x数(若有多个相同的数,因只删 ...
- 【BZOJ1112】[POI2008]砖块Klo Treap
[BZOJ1112][POI2008]砖块Klo Description N柱砖,希望有连续K柱的高度是一样的. 你可以选择以下两个动作 1:从某柱砖的顶端拿一块砖出来,丢掉不要了. 2:从仓库中拿出 ...
- 【BZOJ1116】[POI2008]CLO 并查集
[BZOJ1116][POI2008]CLO Description Byteotia城市有n个 towns m条双向roads. 每条 road 连接 两个不同的 towns ,没有重复的road. ...
- 【BZOJ1132】[POI2008]Tro 几何
[BZOJ1132][POI2008]Tro Description 平面上有N个点. 求出所有以这N个点为顶点的三角形的面积和 N<=3000 Input 第一行给出数字N,N在[3,3000 ...
- AC日记——【模板】普通平衡树(Treap/SBT) 洛谷 P3369
[模板]普通平衡树(Treap/SBT) 思路: 劳资敲了一个多星期: 劳资终于a了: 劳资一直不a是因为一个小错误: 劳资最后看的模板: 劳资现在很愤怒: 劳资不想谈思路!!! 来,上代码: #in ...
- 【BZOJ1124】[POI2008]枪战Maf 贪心+思路题
[BZOJ1124][POI2008]枪战Maf Description 有n个人,每个人手里有一把手枪.一开始所有人都选定一个人瞄准(有可能瞄准自己).然后他们按某个顺序开枪,且任意时刻只有一个人开 ...
- 数组splay ------ luogu P3369 【模板】普通平衡树(Treap/SBT)
二次联通门 : luogu P3369 [模板]普通平衡树(Treap/SBT) #include <cstdio> #define Max 100005 #define Inline _ ...
- 替罪羊树 ------ luogu P3369 【模板】普通平衡树(Treap/SBT)
二次联通门 : luogu P3369 [模板]普通平衡树(Treap/SBT) 闲的没事,把各种平衡树都写写 比较比较... 下面是替罪羊树 #include <cstdio> #inc ...
随机推荐
- 工具分享:GitHub的克隆工具Cl0neMast3r,轻松搞定各种测试
GitHub,相信大家并不陌生,咱搞技术的应该都会用到它,GitHub主要是进行代码工具的存储.下载等工作.今天介绍一款让我们操作GitHub相关工作变的更简单的工具, GitHub的克隆工具. Cl ...
- 转:全面分析 Spring 的编程式事务管理及声明式事务管理
转:from: https://www.ibm.com/developerworks/cn/education/opensource/os-cn-spring-trans/
- ant 报 make sure you have it in your classpath
检查build.xml的配置 build.xml配置出错,导致的这个问题
- /profile文件修改后立即生效
修改profile etc/profile文件是只读的,直接用vi或gedit打开修改后是无法保存的.要修改profile,需要取得root权限,(使用gedit编辑) $sudo gedit /et ...
- XJTU Summer Holiday Test 1(Brackets in Implications-构造)
B - Brackets in Implications Time Limit:2000MS Memory Limit:262144KB 64bit IO Format:%I64d & ...
- MySQL5.7 基于二进制包的安装
1.MySQL5.7安装注意事项 1.在MySQL5.7中mysql_install_db已经不再推荐使用,建议改成mysqld-initialize 完成实力初始化.(mysql_install_d ...
- python——iterator迭代器|iterator详解——20140918|
-----------------------------------------------------------------------------前言--------------------- ...
- PHP使用微软认知服务Face API
下面主要介绍基于PHP语言,基于guzzle类库,调用微软最新推出的认知服务:人脸识别. 实验环境: IDE:Eclipse for PHP Developers Version: Neon.1 Re ...
- 教你如何把php项目打包成EXE文件发布
家经常会接到一些编程的活,例如设计企业网站,做做财务,统计系统什么的.或许是因为朋友的需求,或许图个零花.不管什么原因吧.等程序做好了,给对方展示.安装,就成了问题.企业网站好说,至少需要个虚拟主机什 ...
- 如何从一个1G的文件中找到你所需要的东西
如何从一个1G的文件中找到你所需要的东西,这个问题貌似面试的时候会经常问到.不过不论你用什么语言,肯定逃脱不了按指针读或者按块读. 这里介绍python的用法.本人亲自实验了,速度还可以. 如果你的文 ...