[POI2008] Poc (原名 Trians) Treap+Hash
这个题和千山鸟飞绝体现出了一种用平衡树解决动态集合问题,主要套路就是蜜汁标记。
这个题我一开始用替罪羊树搞了一下对了28个点,后来我换成了Treap一搞对了14个点,再后来发现被卡了Hash我竟然在自然溢出中用了256....
上代码
#include<cstdio>
#include<cstring>
#include<map>
#include<cstdlib>
#define N 1010
#define L 110
#define M 100010
using namespace std;
typedef unsigned long long ULL;
const ULL base=;
ULL tool[L];
ULL hash[N];
char s[N][L];
int size[N],n,l,m;
map<ULL,int>Hash;
inline int Max(int x,int y){ return x>y?x:y; }
inline void swap(char &a,char &b){char c=a;a=b;b=c;}
struct Treap
{
Treap *ch[];
int size,Max,i,r;
Treap(){ch[]=ch[]=NULL;size=Max=i=r=;}
}node[(N+M)<<],*null,*root[(N+M)<<];
int sz,tp;
inline void Init()
{
null=node;
null->size=null->Max=null->i=null->r=;
null->ch[]=null->ch[]=null;
for(int i=;i<((N+M)<<);i++)root[i]=null;
}
inline Treap *New(int i)
{
Treap *p=&node[++sz];
p->ch[]=p->ch[]=null;
p->Max=;
p->i=i;
p->size=;
p->r=rand()+;
return p;
}
inline void pushup(Treap *p)
{
if(p==null)return;
p->size=p->ch[]->size+p->ch[]->size+;
}
inline void down(Treap *p,int x)
{
size[p->i]=Max(size[p->i],x);
p->Max=Max(p->Max,x);
}
inline void pushdown(Treap *p)
{
if(p->Max==)return;
if(p->ch[]!=null) down(p->ch[],p->Max);
if(p->ch[]!=null) down(p->ch[],p->Max);
p->Max=;
}
inline void rotate(Treap *&p)
{
int wh=p->ch[]->r>p->ch[]->r;
Treap *ch=p->ch[wh];
pushdown(p);
pushdown(ch);
p->ch[wh]=ch->ch[wh^];
ch->ch[wh^]=p;
pushup(p);
pushup(ch);
p=ch;
}
void insert(Treap *&p,int i)
{
if(p==null)
{
p=New(i);
return;
}
pushdown(p);
insert(p->ch[p->i<i],i);
if(p->r<p->ch[p->i<i]->r)rotate(p);
pushup(p);
}
void del(Treap *&p,int i)
{
pushdown(p);
if(p->i==i)
{
if(p->ch[]==null||p->ch[]==null)
{
p=p->ch[]==null?p->ch[]:p->ch[];
pushup(p);
return;
}
rotate(p);
del(p->ch[p->ch[]->i==i],i);
pushup(p);
return;
}
del(p->ch[p->i<i],i);
pushup(p);
return;
}
inline void Insert(int i)
{
insert(root[Hash[hash[i]]],i);
down(root[Hash[hash[i]]],root[Hash[hash[i]]]->size);
}
inline void Del(int i)
{
del(root[Hash[hash[i]]],i);
}
int main()
{
Init();
scanf("%d%d%d",&n,&l,&m);
tool[]=;
for(int i=;i<=l;i++) tool[i]=tool[i-]*base;
for(int i=;i<=n;i++)
{
scanf("%s",s[i]+);
for(int j=;j<=l;j++)hash[i]=hash[i]*base+s[i][j];
if(Hash[hash[i]]==)Hash[hash[i]]=++tp;
Insert(i);
}
for(int i=;i<=m;i++)
{
int a,b,c,d;
scanf("%d%d%d%d",&a,&b,&c,&d);
Del(a);
if(a!=c)Del(c);
hash[a]-=s[a][b]*tool[l-b];
hash[c]-=s[c][d]*tool[l-d];
swap(s[a][b],s[c][d]);
hash[a]+=s[a][b]*tool[l-b];
hash[c]+=s[c][d]*tool[l-d];
if(Hash[hash[a]]==)Hash[hash[a]]=++tp;
Insert(a);
if(a!=c){if(Hash[hash[c]]==)Hash[hash[c]]=++tp;Insert(c);}
}
for(int i=;i<=n;i++)Del(i);
for(int i=;i<=n;i++)printf("%d\n",size[i]);
return ;
}
[POI2008] Poc (原名 Trians) Treap+Hash的更多相关文章
- 【BZOJ1125】[POI2008]Poc hash+map+SBT
[BZOJ1125][POI2008]Poc Description n列火车,每条有l节车厢.每节车厢有一种颜色(用小写字母表示).有m次车厢交换操作.求:对于每列火车,在交换车厢的某个时刻,与其颜 ...
- [Bzoj1014][JSOI2008]火星人prefix(无旋Treap&hash)
题目链接:https://www.lydsy.com/JudgeOnline/problem.php?id=1014 因为涉及到增加和修改,所以后缀数组就被pass掉了,想到的就是平衡树维护hash值 ...
- BZOJ 1862: [Zjoi2006]GameZ游戏排名系统 [treap hash]
1862: [Zjoi2006]GameZ游戏排名系统 Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 1318 Solved: 498[Submit][ ...
- 2018.06.28 BZOJ1014 [JSOI2008]火星人prefix(非旋treap+hash)
[JSOI2008]火星人prefix Time Limit: 10 Sec Memory Limit: 162 MB Submit: 8951 Solved: 2860 Description 火星 ...
- bzoj1125:[POI2008]Poc
传送门 这个题好难卡啊. 看到这种题自然会想到字符串hash是不是,但是对于每次操作造成的影响需要\(O(n)\)的时间去更新,自然是不优的 可以发现这个更新可以用数据结构来维护,对于每个hash值开 ...
- 关于 SSV-ID: 4474 POC的分析和思考
SSV-ID: 4474 SSV-AppDir: Discuz!漏洞 发布时间: 2008-11-21 (GMT+0800) URL:http://sebug.net/vuldb/ssvid-4474 ...
- bzoj AC倒序
Search GO 说明:输入题号直接进入相应题目,如需搜索含数字的题目,请在关键词前加单引号 Problem ID Title Source AC Submit Y 1000 A+B Problem ...
- gulp-rev:项目部署缓存解决方案----gulp系列(六)
引言: 前端工程化部署比较重要考虑的一个问题是缓存 ,可以参考 <变态的静态资源缓存与更新>. 使用gulp-rev解决的就是<变态的静态资源缓存与更新>提出的问题. rev会 ...
- 详解常用的gulp命令
gulp.js是一款自动化构建工具,我们经常使用它在开发过程自动执行常见的任务.gulp.js 是基于 Node.js 构建的,利用 Node.js,可以快速构建项目. 由于gulp使用基于node, ...
随机推荐
- 使用SQLite删除Mac OS X 中launchpad里的快捷方式
一般情况下,从App Store安装的应用程序,如果应用删除,那么launchpad里对应的图标会一起删除了. 而对于不是通过App Store安装的应用程序,删除应用程序,Launchpad中很可能 ...
- ruby File类
类方法 路径相关: File.basename(filename <, suffix>) -> string返回给定文件名 filename 的最后一部分.如果有 suffix 参数 ...
- .Net 面试题 汇总(一)
1.@page指令只能在_aspx___文件(填写扩展名)中使用,而@Control指令只能用在_ascx___文件(填写扩展名)中使用. 2.说明控件DataGrid,DataTable,DataV ...
- MVC5使用单选按钮与下拉框【转】
某人认为下拉列表的呈现形式不如单选按钮漂亮,我只好去测试一下单选按钮与下拉框了.测试代码如下: 1.model类Blog.cs(类型使用枚举类型,自动生成的视图会以下拉列表形式显示): using S ...
- malloc分配失败的两个现象
在实际代码中,malloc的反复分配释放,可能会导致某一次malloc分配失败,虽然上一次调用malloc分配成功(然后释放),下一次在相同地方调用malloc分配可能会失败,疑问在于,既然上一次分配 ...
- leetcode笔记--6 Add Digits
question: Given a non-negative integer num, repeatedly add all its digits until the result has only ...
- 问题:docker pull 用户登陆tricky,Error response from daemon: unauthorized: incorrect username or password
问题描述: PS C:\WINDOWS\system32> docker pull rabbitmqUsing default tag: latest Please login prior to ...
- Android之线程安全的单例模式,Adapter注意事项之引用传值
线程安全的单例模式单位模式一般写法如下: public static FestivalLab mInstance; private FestivalLab() { } public static Fe ...
- Linux上jdk的安装(CentOS6.5)
centos openjdk 安装 http://www.cnblogs.com/ilahsa/archive/2012/12/11/2813059.html 知CentOS6.5桌面版默认安装的是J ...
- spring mvc:实现给Controller函数传入list<pojo>参数
[1]前端js调用示例: ...insertStatisData?statisDatas=[{'cid':'2','devId':'9003','deviceName':'测试名','endTime' ...