【BZOJ4358】permu kd-tree
【BZOJ4358】permu
Description
Input
Output
Sample Input
3 1 7 2 5 8 6 4
1 4
5 8
1 7
Sample Output
3
4
HINT
题解:一开始想莫队没想出来,然后就去膜拜了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的更多相关文章
- 【数据结构】B-Tree, B+Tree, B*树介绍 转
[数据结构]B-Tree, B+Tree, B*树介绍 [摘要] 最近在看Mysql的存储引擎中索引的优化,神马是索引,支持啥索引.全是浮云,目前Mysql的MyISAM和InnoDB都支持B-Tre ...
- P3690 【模板】Link Cut Tree (动态树)
P3690 [模板]Link Cut Tree (动态树) 认父不认子的lct 注意:不 要 把 $fa[x]$和$nrt(x)$ 混 在 一 起 ! #include<cstdio> v ...
- LG3690 【模板】Link Cut Tree (动态树)
题意 给定n个点以及每个点的权值,要你处理接下来的m个操作.操作有4种.操作从0到3编号.点从1到n编号. 0:后接两个整数(x,y),代表询问从x到y的路径上的点的权值的xor和.保证x到y是联通的 ...
- AC日记——【模板】Link Cut Tree 洛谷 P3690
[模板]Link Cut Tree 思路: LCT模板: 代码: #include <bits/stdc++.h> using namespace std; #define maxn 30 ...
- LG3690 【模板】Link Cut Tree 和 SDOI2008 洞穴勘测
UPD:更新了写法. [模板]Link Cut Tree 给定n个点以及每个点的权值,要你处理接下来的m个操作.操作有4种.操作从0到3编号.点从1到n编号. 后接两个整数(x,y),代表询问从x到y ...
- (RE) luogu P3690 【模板】Link Cut Tree
二次联通门 : luogu P3690 [模板]Link Cut Tree 莫名RE第8个点....如果有dalao帮忙查错的话万分感激 #include <cstdio> #includ ...
- LuoguP3690 【模板】Link Cut Tree (动态树) LCT模板
P3690 [模板]Link Cut Tree (动态树) 题目背景 动态树 题目描述 给定n个点以及每个点的权值,要你处理接下来的m个操作.操作有4种.操作从0到3编号.点从1到n编号. 0:后接两 ...
- 【LeetCode】199. Binary Tree Right Side View 解题报告(Python)
[LeetCode]199. Binary Tree Right Side View 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/probl ...
- 【数据结构】B-Tree, B+Tree, B*树介绍
[摘要] 最近在看Mysql的存储引擎中索引的优化,神马是索引,支持啥索引.全是浮云,目前Mysql的MyISAM和InnoDB都支持B-Tree索引,InnoDB还支持B+Tree索引,Memory ...
随机推荐
- C#中out与ref区别
一.ref(参考)与out区别 1.out(只出不进) 将方法中的参数传递出去,在方法中将该参数传递出去之前需要在该方法起始赋初值:在方法外传递的该参数可以不用赋值: 简单理解就是:将一个东西抛出去之 ...
- ES6中的async函数
一.概述 async 函数是 Generator 函数的语法糖 使用Generator 函数,依次读取两个文件代码如下 var fs = require('fs'); var readFile = f ...
- 全栈技术经理——团队管理:每周问问你的团队这这些问题 V1.0
全栈技术经理--团队管理:每周问问你的团队这这些问题 V1.0 1.本周取得了哪些进展? 通过回答这个问题可以让员工庆祝甚至夸耀一些自己的成果,包括那些跟最高优先级不相干而被忽视的小事情.借此你也 ...
- [ElasticSearch]Java API 之 词条查询(Term Level Query)
1. 词条查询(Term Query) 词条查询是ElasticSearch的一个简单查询.它仅匹配在给定字段中含有该词条的文档,而且是确切的.未经分析的词条.term 查询 会查找我们设定的准确值 ...
- Troubles in Building Android Source Code
Some Troubles or problems you may encounter while you setup the Android source code build environmen ...
- es6 - filter for-chrome
'use strict'; let numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9]; // 除去取余2的 - es6 let es5OddNumbers = numbers ...
- sql习题练习
表结构: create database MyCompany go use MyCompany go create table Departments ( Department_ID ,) prima ...
- Php如何使用curl
cURL 是一个利用URL语法规定来传输文件和数据的工具,支持很多协议,如HTTP.FTP.TELNET等.最爽的是,PHP也支持 cURL 库.本文将介绍 cURL 的一些高级特性,以及在PHP中如 ...
- 由需求而产生的一款db导出excel的工具
代码地址如下:http://www.demodashi.com/demo/12062.html 程序员最大的毛病可能就是懒,因为懒所以做出了许许多多提高自己工作效率的工具. 起因于我是商业开发,既然是 ...
- UINavigationController改变动画效果
@interface UINavigationController (CustomTransition) - (void) pushWithCustomAnimation:(UIViewControl ...