【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. ElasticSearch的Rest的访问方式查询总量

    由于安装ElasticSearch插件会影响ES的性能,所以会尽量减少ES的插件安装 可以通过ElasticSearch-Sql插件 然后将生成的执行参数拷贝 { "query": ...

  2. Python 自用代码(scrapy多级页面(三级页面)爬虫)

    2017-03-28 入职接到的第一个小任务,scrapy多级页面爬虫,从来没写过爬虫,也没学过scrapy,甚至连xpath都没用过,最后用了将近一周才搞定.肯定有很多low爆的地方,希望大家可以给 ...

  3. 编译安装Apache httpd和php搭建KodExplorer网盘

    编译安装Apache httpd和php搭建KodExplorer网盘 环境说明: 系统版本    CentOS 6.9 x86_64 软件版本    httpd-2.2.31        php- ...

  4. Python - 连续替换(replace)的正則表達式(re)

    字符串连续替换, 能够连续使用replace, 也能够使用正則表達式. 正則表達式, 通过字典的样式, key为待替换, value为替换成, 进行一次替换就可以. 代码 # -*- coding: ...

  5. Node.js 极简入门Helloworld版服务器例子

    粗浅得很,纯属备忘. // 内置http模块,提供了http服务器和客户端功能(path模块也是内置模块,而mime是附加模块) var http=require("http"); ...

  6. ReadWriteLock 读写锁(读书笔记)

     读写分离锁可以有效的帮助减少锁的竞争,提升系统的效率, 读-读不互斥 读读之间不阻塞 读-写互斥 读阻塞写,写也会阻塞读 写-写互斥 写写阻塞 在系统中,读操作次数远远大于写操作,则读写锁就可以发挥 ...

  7. Linux 命令 indent 用法

    此命令用于调整C源码的格式. 在LKD中的例子: indent -kr -i8 -ts8 -sob -l80 -ss -bs -psl filename   参数解释如下: -i :设置缩进的格数 - ...

  8. 字体和颜色 Font Color 基础笔记

    private void SelectFontAndColor_Load(object sender, EventArgs e) {//窗体加载的时候,初始化字体 //返回所有的字体 FontFami ...

  9. 最美应用-从Android研发project师的角度之[最美时光]

    最美应用-从Android研发project师的角度之最美时光 @author ASCE1885的 Github 简书 微博 CSDN 近期发现最美应用这样一个站点.它会定期推介一些非常有意思的app ...

  10. _.pick lodash

    http://lodash.think2011.net/pick _.pick(object, [props]) 创建一个从 object 中选中的属性的对象. 参数 object (Object) ...