bzoj3489
kdtree
3维kdtree,就是三个维度轮换着切,我们把每个元素看成一个点,坐标是上次出现的位置,下次出现的位置,自己的位置,第一个<l,第二个>r,第三个[l,r],然后kdtree上爆搜剪枝就行了。
kdtree看起来能解决所有偏序问题。
#include<bits/stdc++.h>
using namespace std;
const int N = 1e5 + , inf = 1e9;
int rd()
{
int x = , f = ; char c = getchar();
while(c < '' || c > '') { if(c == '-') f = -; c = getchar(); }
while(c >= '' && c <= '') { x = x * + c - ''; c = getchar(); }
return x * f;
}
int n, m, root, d, ans;
int last[N];
struct data {
int p[], mn[], mx[], lc, rc, ans, val;
bool friend operator < (const data &a, const data &b) {
if(a.p[d] != b.p[d]) return a.p[d] < b.p[d];
if(a.p[(d + ) % ] != b.p[(d + ) % ]) return a.p[(d + ) % ] < b.p[(d + ) % ];
if(a.p[(d + ) % ] != b.p[(d + ) % ]) return a.p[(d + ) % ] < b.p[(d + ) % ];
}
} a[N];
void update(int x)
{
int lc = a[x].lc, rc = a[x].rc;
for(int i = ; i < ; ++i)
{
a[x].mn[i] = min(a[x].p[i], min(a[lc].mn[i], a[rc].mn[i]));
a[x].mx[i] = max(a[x].p[i], max(a[lc].mx[i], a[rc].mx[i]));
}
a[x].ans = max(a[x].val, max(a[lc].ans, a[rc].ans));
}
int build(int l, int r, int D)
{
if(l > r) return ;
d = D;
int mid = (l + r) >> ;
nth_element(a + l, a + mid, a + r + );
a[mid].lc = build(l, mid - , (D + ) % );
a[mid].rc = build(mid + , r, (D + ) % );
update(mid);
return mid;
}
bool out(int k, int l, int r)
{
return ans < a[k].ans && a[k].mn[] <= r && a[k].mx[] >= l && a[k].mn[] < l && a[k].mx[] > r;
}
void query(int k, int l, int r)
{
if(!k || !out(k, l, r)) return;
if(a[k].mx[] <= r && a[k].mn[] >= l && a[k].mx[] < l && a[k].mn[] > r)
{
ans = max(ans, a[k].ans);
return;
}
if(a[k].p[] <= r && a[k].p[] >= l && a[k].p[] < l && a[k].p[] > r) ans = max(ans, a[k].val);
query(a[k].lc, l, r);
query(a[k].rc, l, r);
}
int main()
{
for(int i = ; i < ; ++i) a[].mn[i] = inf, a[].mx[i] = a[].ans = -inf;
n = rd();
m = rd();
for(int i = ; i <= n; ++i)
{
a[i].val = rd();
a[i].p[] = i;
a[i].p[] = last[a[i].val];
a[last[a[i].val]].p[] = i;
last[a[i].val] = i;
}
for(int i = ; i <= n; ++i)
if(!a[i].p[])
a[i].p[] = n + ;
root = build(, n, );
while(m--)
{
int l = (rd() + ans) % n + , r = (rd() + ans) % n + ;
if(l > r) swap(l, r);
ans = ;
query(root, l, r);
printf("%d\n", ans);
}
return ;
}
bzoj3489的更多相关文章
- 【BZOJ3489】A simple rmq problem(KD-Tree)
[BZOJ3489]A simple rmq problem(KD-Tree) 题面 BZOJ 题解 直接做肯定不好做,首先我们知道我们是一个二维平面数点,但是限制区间只能出现一次很不好办,那么我们给 ...
- 【bzoj3489】 A simple rmq problem
http://www.lydsy.com/JudgeOnline/problem.php?id=3489 (题目链接) 题意 在线求区间不重复出现的最大的数. Solution KDtree竟然能够处 ...
- 【BZOJ3489】A simple rmq problem
[BZOJ3489]A simple rmq problem 题面 bzoj 题解 这个题不强制在线的话随便做啊... 考虑强制在线时怎么搞 预处理出一个位置上一个出现的相同数的位置\(pre\)与下 ...
- BZOJ3489 A simple rmq problem 【可持久化树套树】*
BZOJ3489 A simple rmq problem Description 因为是OJ上的题,就简单点好了.给出一个长度为n的序列,给出M个询问:在[l,r]之间找到一个在这个区间里只出现过一 ...
- 【kd-tree】bzoj3489 A simple rmq problem
Orz zyf教给蒟蒻做法 蒟蒻并不会这题正解……(可持久化树套树?...Orz 对于每个点,我们可以求出pre[i],nex[i],那么询问的答案就是:求max (a[i]),其中 i 满足(pre ...
- 【BZOJ3489】A simple rmq problem kd-tree
[BZOJ3489]A simple rmq problem Description 因为是OJ上的题,就简单点好了.给出一个长度为n的序列,给出M个询问:在[l,r]之间找到一个在这个区间里只出现过 ...
- BZOJ3489: A simple rmq problem
设$i$的前驱为$p_i$,后继为$q_i$,把询问看成点$(L,R)$,有贡献的$i$满足$L\in(p_i,i]$且$R\in[i,q_i)$,询问的就是覆盖这个点的矩形的最大值.那么可以用可持久 ...
- bzoj3489 A simple rmq problem 可持久化树套树
先预处理出两个个数组pre,next.pre[i]表示上一个与i位置数字相同的位置,若不存在则设为0:next[i]表示下一个与i位置数字相同的位置,若不存在则设为n+1.那么一个满足在区间[L,R] ...
- BZOJ3489 A simple rmq problem K-D Tree
传送门 什么可持久化树套树才不会写呢,K-D Tree大法吼啊 对于第\(i\)个数,设其前面最后的与它值相同的位置为\(pre_i\),其后面最前的与它值相同的位置为\(aft_i\),那么对于一个 ...
- 【bzoj3489】 A simple rmq problem k-d树
由于某些原因,我先打了一个错误的树套树,后来打起了$k-d$.接着因不明原因在思路上被卡了很久,在今天中午蹲坑时恍然大悟...... 对于一个数字$a_i$,我们可以用一组三维坐标$(i,pre,nx ...
随机推荐
- 学会读懂 MySql 的慢查询日志
在前边的博客<何时.怎样开启 MySql 日志?>中,我们了解到了怎样启用 MySql 的慢查询日志. 今天我们来看一下怎样去读懂这些慢查询日志.在跟踪慢查询日志之前.首先你得保证最少发生 ...
- JS 常用字符串操作
Js字符串操作函数大全 /******************************************* 字符串函数扩充 ...
- 40个国人iOS技术博客
40个国人iOS技术博客 博客地址 RSS地址 OneV's Den http://onevcat.com/atom.xml 破船之家 http://beyondvincent.com/atom.xm ...
- Tomcat Context 组件介绍(转载)
来源:http://diecui1202.iteye.com/blog/1037370 Context代表一个Web应用,它运行在某个指定的虚拟主机(Host)上:每个Web应用都是一个WAR文件,或 ...
- RYU改动监听port Mininet在custom自建拓扑和连接到指定控制器命令解释
1.RYU控制器改动监听port 在ryu/ryu/ofproto以下的ofproto_common.py watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvc ...
- 2018.11.22-day24 面向对象-继承
1.归一化设计 2.抽象类 3.钻石继承 4.C3算法 5.新式类中的super
- python基础教程_学习笔记18:标准库:一些最爱——shelve
版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/signjing/article/details/36029981 标准库:一些最爱 shelve S ...
- 前后端通吃的单元测试---mocha
git clone https://github.com/shenggen1987/mocha-demo.git npm install front 前台测试代码 backend 后台测试代码 先安装 ...
- Raspberry Pi3 ~ 安装 nfs Server
l 安装必要服务: sudo apt-get install portmap sudo apt-get install nfs-kernel-server sudo apt ...
- 收集Oracle数据库中的SQL基线信息(一)基础信息收集
Oracle数据库中的SQL基线信息,当数据库出现性能问题时,在业务无法提供相应业务信息时,通过对比SQL基线信息来查找SQL的变化. 查找数据库一天内运行次数大于5000次的sqlid select ...