【BZOJ4358】permu

Description

给出一个长度为n的排列P(P1,P2,...Pn),以及m个询问。每次询问某个区间[l,r]中,最长的值域连续段长度。

Input

第一行两个整数n,m。
接下来一行n个整数,描述P。
接下来m行,每行两个整数l,r,描述一组询问。

Output

对于每组询问,输出一行一个整数,描述答案。

Sample Input

8 3
3 1 7 2 5 8 6 4
1 4
5 8
1 7

Sample Output

3
3
4

HINT

对于询问[1,4],P2,P4,P1组成最长的值域连续段[1,3];
对于询问[5,8],P8,P5,P7组成最长的值域连续段[4,6];
对于询问[1,7],P5,P7,P3,P6组成最长的值域连续段[5,8]。
1<=n,m<=50000

题解:一开始想莫队没想出来,然后就去膜拜了Claris的题解

然后下传标记的时候又有些不明白,于是又去膜拜了Claris的代码。。。

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
const int maxn=50010;
const int inf=1<<30;
int n,m,X,rt,D;
int p[maxn],v[maxn],ans[maxn];
struct kd
{
int v[2],sm[2],sn[2],ls,rs,ts,val,tt,org,ht,hs,hv;
kd () {}
kd (int a,int b){v[0]=sm[0]=sn[0]=a,v[1]=sm[1]=sn[1]=b,ls=rs=ts=val=hs=hv=0,tt=ht=-inf;}
}t[maxn];
bool cmp(const kd &a,const kd &b)
{
return (a.v[D]==b.v[D])?(a.v[D^1]<b.v[D^1]):(a.v[D]<b.v[D]);
}
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 ps(int x,int y)
{
t[x].val+=y;
if(t[x].val>t[x].hv) t[x].hv=t[x].val;
if(t[x].tt>=0)
{
t[x].tt+=y;
if(t[x].tt>t[x].ht) t[x].ht=t[x].tt;
}
else
{
t[x].ts+=y;
if(t[x].ts>t[x].hs) t[x].hs=t[x].ts;
}
}
void pt(int x,int y)
{
t[x].val=y;
if(t[x].val>t[x].hv) t[x].hv=t[x].val;
t[x].tt=y,t[x].ts=0;
if(t[x].tt>t[x].ht) t[x].ht=t[x].tt;
}
void phs(int x,int y)
{
t[x].hv=max(t[x].hv,t[x].val+y);
if(t[x].ht>=0) t[x].ht=max(t[x].ht,t[x].tt+y);
else t[x].hs=max(t[x].hs,t[x].ts+y);
}
void pht(int x,int y)
{
t[x].hv=max(t[x].hv,y);
t[x].ht=max(t[x].ht,y);
}
void pushup(int x,int y)
{
t[x].sm[0]=max(t[x].sm[0],t[y].sm[0]);
t[x].sm[1]=max(t[x].sm[1],t[y].sm[1]);
t[x].sn[0]=min(t[x].sn[0],t[y].sn[0]);
t[x].sn[1]=min(t[x].sn[1],t[y].sn[1]);
}
void pushdown(int x)
{
if(t[x].hs)
{
if(t[x].ls) phs(t[x].ls,t[x].hs);
if(t[x].rs) phs(t[x].rs,t[x].hs);
t[x].hs=0;
}
if(t[x].ht>=0)
{
if(t[x].ls) pht(t[x].ls,t[x].ht);
if(t[x].rs) pht(t[x].rs,t[x].ht);
t[x].ht=-inf;
}
if(t[x].ts)
{
if(t[x].ls) ps(t[x].ls,t[x].ts);
if(t[x].rs) ps(t[x].rs,t[x].ts);
t[x].ts=0;
}
if(t[x].tt>=0)
{
if(t[x].ls) pt(t[x].ls,t[x].tt);
if(t[x].rs) pt(t[x].rs,t[x].tt);
t[x].tt=-inf;
}
}
int build(int l,int r,int d)
{
if(l>r) return 0;
int mid=(l+r)>>1;
D=d,nth_element(t+l,t+mid,t+r+1,cmp);
t[mid].ls=build(l,mid-1,d^1),t[mid].rs=build(mid+1,r,d^1);
if(t[mid].ls) pushup(mid,t[mid].ls);
if(t[mid].rs) pushup(mid,t[mid].rs);
return mid;
}
void updata(int x)
{
if(!x) return;
if(t[x].sn[0]>X||t[x].sm[1]<X)
{
pt(x,0);
return ;
}
if(t[x].sm[0]<=X&&t[x].sn[1]>=X)
{
ps(x,1);
return ;
}
pushdown(x);
if(t[x].v[0]<=X&&t[x].v[1]>=X) t[x].val++,t[x].hv=max(t[x].hv,t[x].val);
else t[x].val=0;
updata(t[x].ls),updata(t[x].rs);
}
void dfs(int x)
{
if(!x) return ;
pushdown(x),ans[t[x].org]=t[x].hv;
dfs(t[x].ls),dfs(t[x].rs);
}
int main()
{
n=rd(),m=rd();
int i,a,b;
for(i=1;i<=n;i++) p[rd()]=i;
for(i=1;i<=m;i++) a=rd(),b=rd(),t[i]=kd(a,b),t[i].org=i;
rt=build(1,m,0);
for(i=1;i<=n;i++)
X=p[i],updata(rt);
dfs(rt);
for(i=1;i<=m;i++) printf("%d\n",ans[i]);
return 0;
}

【BZOJ4358】permu kd-tree的更多相关文章

  1. 【数据结构】B-Tree, B+Tree, B*树介绍 转

    [数据结构]B-Tree, B+Tree, B*树介绍 [摘要] 最近在看Mysql的存储引擎中索引的优化,神马是索引,支持啥索引.全是浮云,目前Mysql的MyISAM和InnoDB都支持B-Tre ...

  2. P3690 【模板】Link Cut Tree (动态树)

    P3690 [模板]Link Cut Tree (动态树) 认父不认子的lct 注意:不 要 把 $fa[x]$和$nrt(x)$ 混 在 一 起 ! #include<cstdio> v ...

  3. LG3690 【模板】Link Cut Tree (动态树)

    题意 给定n个点以及每个点的权值,要你处理接下来的m个操作.操作有4种.操作从0到3编号.点从1到n编号. 0:后接两个整数(x,y),代表询问从x到y的路径上的点的权值的xor和.保证x到y是联通的 ...

  4. AC日记——【模板】Link Cut Tree 洛谷 P3690

    [模板]Link Cut Tree 思路: LCT模板: 代码: #include <bits/stdc++.h> using namespace std; #define maxn 30 ...

  5. LG3690 【模板】Link Cut Tree 和 SDOI2008 洞穴勘测

    UPD:更新了写法. [模板]Link Cut Tree 给定n个点以及每个点的权值,要你处理接下来的m个操作.操作有4种.操作从0到3编号.点从1到n编号. 后接两个整数(x,y),代表询问从x到y ...

  6. (RE) luogu P3690 【模板】Link Cut Tree

    二次联通门 : luogu P3690 [模板]Link Cut Tree 莫名RE第8个点....如果有dalao帮忙查错的话万分感激 #include <cstdio> #includ ...

  7. LuoguP3690 【模板】Link Cut Tree (动态树) LCT模板

    P3690 [模板]Link Cut Tree (动态树) 题目背景 动态树 题目描述 给定n个点以及每个点的权值,要你处理接下来的m个操作.操作有4种.操作从0到3编号.点从1到n编号. 0:后接两 ...

  8. 【LeetCode】199. Binary Tree Right Side View 解题报告(Python)

    [LeetCode]199. Binary Tree Right Side View 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/probl ...

  9. 【数据结构】B-Tree, B+Tree, B*树介绍

    [摘要] 最近在看Mysql的存储引擎中索引的优化,神马是索引,支持啥索引.全是浮云,目前Mysql的MyISAM和InnoDB都支持B-Tree索引,InnoDB还支持B+Tree索引,Memory ...

随机推荐

  1. GPU hang

    最近做新项目 初期一直遇到个gpu hang的问题 就是command 提交过去gpu 就一直在那里 直到time out 也没什么别的错误提示 gpu debugger还抓不了 解决方案是 缩小之后 ...

  2. 配置Linux系统实现dhcp功能

    配置Linux系统实现dhcp功能 1.背景及原理    DHCP(Dynamic Host Configuration Protocol,动态主机配置协议)通常被应用在大型的局域网络环境中,主要作用 ...

  3. 【重点突破】—— Nodejs+Express+MongoDB的使用基础

    前言:最近学习vue和react的高阶项目,都需要和Nodejs+Express+MongoDB结合实现全栈开发.这里结合实例Demo和所学项目集中总结一下这部分服务端的基础知识. 一.Express ...

  4. 【IOS】mac终端运行.sh文件总是提示permission denied

    如果我目录jni有一个list.sh文件 我直接 nxgametekiMacBook-Air:jni luonan$  ./list.sh ../../Classes 提示 permission de ...

  5. react-native 扫一扫功能(二维码扫描)功能开发

    1.安装插件 yarn add react-native-smart-barcode 2.关联 react-native link react-native-smart-barcode 3.修改 an ...

  6. 【Cocosd2d-x CCMenu菜单之二】

    菜单项CCMenuItem是一个基类. 子类CCMenuItemFont.CCMenuItemLabel.CCMenuItemSprite.CCMenuItemToggle可增加CCMenu中形成菜单 ...

  7. shell脚本检测网络是否畅通

    shell初始化安装脚本执行时,需从网络上安装一些rpm包,所有需要先检测网络的畅通性, 代码 #检测网络链接&&ftp上传数据 function networkAndFtp() { ...

  8. 【Lucene】Apache Lucene全文检索引擎架构之搜索功能3

    上一节主要总结了一下Lucene是如何构建索引的,这一节简单总结一下Lucene中的搜索功能.主要分为几个部分,对特定项的搜索:查询表达式QueryParser的使用:指定数字范围内搜索:指定字符串开 ...

  9. python按行读取apk中版本号、包名等信息

    主要是读apk中manifest.xml中的信息. 读单一apk信息:见“文件”中“apkInfo.xml”.实际运行时,需要将后缀由“.xml”改为“.py". 批量自动获取apk软件信息 ...

  10. java之UDP(datagramsocket,datagramPacket)实例

    import java.net.DatagramPacket; import java.net.DatagramSocket; import java.net.InetAddress; import ...