bzoj 4358 permu
比较容易想到莫队算法+线段树,但是这样时间复杂度是O(nsqrtnlogn)无法通过,考虑如果不进行删除操作,只有添加操作的话那么并查集就可以实现了,于是可以设定sqrtn块,每个块范围为(i-1)*sqrtn到i*sqrtn,那么对于一个左端点在该块之中的询问,若右端点超过块的范围,由于右端点递增,直接添加,而询问在块中的部分则暴力添加,添加完注意还原,复杂度O(nlogn),此题想法与CF620F有一些类似。
代码
#include<cstdio>
#include<algorithm>
#define nc() getchar()
#define N 500100
#define Q 300
using namespace std;
int n,m,i,j,k,pos[N],ans,Ans[N],tmp,top,stack[N];
inline int read(){
int x=;char ch=nc();for(;ch<''||ch>'';ch=nc());
for(;ch<=''&&ch>='';x=x*+ch-,ch=nc());return x;
}
struct g{
int l,r,id;
}a[N];
int f[N],s[N],e[N];
bool cmp(g a,g b)
{
if (pos[a.id]==pos[b.id])
return a.r<b.r;
return pos[a.id]<pos[b.id];
}
int gf(int x)
{
int p=x,t;
while (p!=f[p]) p=f[p];
while (x!=p) t=f[x],f[x]=p,x=t;
return p;
}
int GF(int x){
int p=x;
while (p!=f[p]) p=f[p];return p;
}
void add(int x)
{
s[x]=;
int a,b;
a=gf(x+);b=gf(x-);
if (s[a]) s[a]+=s[gf(x)],f[gf(x)]=a;
if (s[b]) s[b]+=s[gf(x)],f[gf(x)]=b;
ans=max(ans,s[gf(x)]);
}
void add2(int x)
{
s[x]=;
int a,b;
a=GF(x+);b=GF(x-);
if (s[a])
{
s[GF(x)]+=s[a];f[a]=GF(x);
top++;stack[top]=a;
}
if (s[b])
{
s[GF(x)]+=s[b];f[b]=GF(x);
top++;stack[top]=b;
}
ans=max(ans,s[x]);
}
int main()
{
n=read();
m=read();
for (i=;i<=n;i++)
e[i]=read();
for (i=;i<=m;i++)
{
a[i].l=read();
a[i].r=read();
a[i].id=i;
pos[i]=a[i].l/Q;
} sort(a+,a++m,cmp);
for (i=;i<=m;i++)
{
int cur=(a[i].l/Q+)*Q;
int now=min(n+,cur);
for (k=i;k<=m;k++) if (a[k].l/Q!=a[i].l/Q) break;
int o=k;
for (j=;j<=n+;j++) f[j]=j,s[j]=;ans=; for (j=i;j<o;j++)
{
while (cur<=a[j].r) add(e[cur++]);
tmp=ans;top=; for (k=a[j].l;k<=min(a[j].r,now-);k++)
add2(e[k]); Ans[a[j].id]=ans; for (k=top;k>=;k--) f[stack[k]]=stack[k];
for (k=a[j].l;k<=min(a[j].r,now-);k++)
s[e[k]]=,f[e[k]]=e[k];
ans=tmp; } i=o-;
} for (i=;i<=m;i++)
printf("%d\n",Ans[i]);
}
bzoj 4358 permu的更多相关文章
- bzoj 4358 Permu - 莫队算法 - 链表
题目传送门 需要高级权限的传送门 题目大意 给定一个全排列,询问一个区间内的值域连续的一段的长度的最大值. 考虑使用莫队算法. 每次插入一个数$x$,对值域的影响可以分成4种情况: $x - 1$, ...
- bzoj 4358: permu 莫队
第一步先莫队分块. 对于每一块l~r,初始右端点设为r+1,然后每个询问先将右端点往右移,然后处理询问在l~r之间的部分,最后用一个栈再把l~r的复原. 具体来说是维护两个数组now1和now2,一个 ...
- 4358: permu
4358: permu 链接 分析: 不删除的莫队+可撤销的并查集. 每次询问先固定左端点到一个块内,然后将这些右端点从小到大排序,然后询问的过程中,右端点不断往右走,左端点可能会撤销,但是移动区间不 ...
- 【BZOJ】4358: permu 莫队算法
[题意]给定长度为n的排列,m次询问区间[L,R]的最长连续值域.n<=50000. [算法]莫队算法 [题解]考虑莫队维护增加一个数的信息:设up[x]表示数值x往上延伸的最大长度,down[ ...
- BZOJ 4358 坑 莫队+线段树 死T
这是一个坑 竟然卡nsqrt(n)logn T死 等更 //By SiriusRen #include <cmath> #include <cstdio> #include & ...
- bzoj AC倒序
Search GO 说明:输入题号直接进入相应题目,如需搜索含数字的题目,请在关键词前加单引号 Problem ID Title Source AC Submit Y 1000 A+B Problem ...
- BZOJ 3143 Luogu P3232 [HNOI2013]游走 (DP、高斯消元)
题目链接: (bzoj) https://www.lydsy.com/JudgeOnline/problem.php?id=3143 (luogu) https://www.luogu.org/pro ...
- BZOJ 1859 Luogu P2589 [ZJOI2006]碗的叠放 (计算几何)
woc, 13年前的ZJOI就这么毒瘤的嘛... 题目链接: (bzoj)https://www.lydsy.com/JudgeOnline/problem.php?id=1859 (luogu)ht ...
- BZOJ 2127: happiness [最小割]
2127: happiness Time Limit: 51 Sec Memory Limit: 259 MBSubmit: 1815 Solved: 878[Submit][Status][Di ...
随机推荐
- EF的表连接方法Include()
在EF中表连接常用的有Join()和Include(),两者都可以实现两张表的连接,但又有所不同. 例如有个唱片表Album(AlbumId,Name,CreateDate,GenreId),表中含外 ...
- php YAF
Yaf 的特点: 用C语言开发的PHP框架, 相比原生的PHP, 几乎不会带来额外的性能开销. 所有的框架类, 不需要编译, 在PHP启动的时候加载, 并常驻内存. 更短的内存周转周期, 提高内存利用 ...
- Mixing Delphi and C++(相互调用)
Mixing Delphi and C++ You have a TStringList and <algorithm>. What can you do? Quite a lot, ac ...
- php--yii2框架错误提示
if($code!=200){ $user=new UserAuth(); $user->mobile=$register['mobile']; $user->password=md5($ ...
- 使用Dom的Range对象处理chrome和IE文本光标位置
有这样一段js: var sel = obj.createTextRange(); sel.move('character', num); sel.collapse(); sel.select(); ...
- IOS本地通知:UILocalNotification使用记录
第一次接触IOS的本地通知的使用,看到别人写的一个比较详细的记录,自己整理过来,方便以后再次使用和拓展: 1.创建一个本地通知,添加到系统: // 初始化本地通知对象 UILocalNotificat ...
- HTTP 协议的历史演变和设计思路
HTTP 协议是互联网的基础协议,也是网页开发的必备知识,最新版本 HTTP/2 更是让它成为技术热点. 本文介绍 HTTP 协议的历史演变和设计思路. 一.HTTP/0.9 HTTP 是基于 TCP ...
- POI对Excel
完美兼容excel2003 和excel2007的读取,处理了所有excel所有的类型,依赖包如下: poi-3.10-FNAL.jar poi-ooxml-3.10-FNAL.jar poi-oox ...
- 苹果公司给出的检测 advertisingIdentifier 的方法
To locate the reference to the advertisingIdentifier selector, please perform these steps to create ...
- LeetCode Find Minimum in Rotated Sorted Array II
原题链接在这里:https://leetcode.com/problems/find-minimum-in-rotated-sorted-array-ii/ 题目: Follow up for &qu ...