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 ...
随机推荐
- Redis学习笔记(9)-管道/分布式
package cn.com; import java.util.Arrays; import java.util.List; import redis.clients.jedis.Jedis; im ...
- xftp的使用
1.xftp 一个基于 MS windows 平台的功能强大的SFTP.FTP 文件传输软件 2.下载安装 *3.在linux上安装服务 sudo yum install vsftp
- 日志案例分析(PV,UV),以及动态分区
1.实现的流程 需求分析 时间:日,时段 分区表:两级 PV UV 数据清洗 2015-08-28 18:19:10 字段:id,url,guid,tracktime 数据分析 导出 2.新建源数据库 ...
- Let's Encrypt这个免费的证书签发服务
使用的是Let's Encrypt这个免费的证书签发服务,按照这里的教程一步步照着来,很快就完成了. 迁移过程总体来说比较顺利,只是遇到了两个不大不小的坑.一个是域名的跳转问题,迁移完成以后对于所有h ...
- [LeetCode]题解(python):112 Path Sum
题目来源 https://leetcode.com/problems/path-sum/ Given a binary tree and a sum, determine if the tree ha ...
- php empty isset is_null
总是忘记这些变量的区别,参考下http://www.jb51.net/article/38020.htm,记录下 设置几个变量 <?php $a; $b = false; $c = ''; $d ...
- http://blog.csdn.net/pi9nc/article/details/23169357
http://blog.csdn.net/pi9nc/article/details/23169357
- rank 和 星星评级
我们常常看到打分,如下图 这样的效果有几种方法 1.用:lt(index) /* $('dl').each(function(){ var ths = $(this); $(this).find('d ...
- Sublime插件库新成员基于APICloud快速开发跨平台App
互联网时代强调用户体验,那什么是HTML5跨平台App开发者的编程体验?“不剥夺.不替换开发者喜欢的开发工具,就是人性化的用户体验”,APICloud给出了这样的答案! 重磅发布“多开发工具支持策略” ...
- iptables常用操作
1.iptables服务重启 service iptables restart 2.保存iptables规则 iptables-save > ~/iptables.save 3.恢复iptabl ...