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的更多相关文章

  1. 【BZOJ3489】A simple rmq problem(KD-Tree)

    [BZOJ3489]A simple rmq problem(KD-Tree) 题面 BZOJ 题解 直接做肯定不好做,首先我们知道我们是一个二维平面数点,但是限制区间只能出现一次很不好办,那么我们给 ...

  2. 【bzoj3489】 A simple rmq problem

    http://www.lydsy.com/JudgeOnline/problem.php?id=3489 (题目链接) 题意 在线求区间不重复出现的最大的数. Solution KDtree竟然能够处 ...

  3. 【BZOJ3489】A simple rmq problem

    [BZOJ3489]A simple rmq problem 题面 bzoj 题解 这个题不强制在线的话随便做啊... 考虑强制在线时怎么搞 预处理出一个位置上一个出现的相同数的位置\(pre\)与下 ...

  4. BZOJ3489 A simple rmq problem 【可持久化树套树】*

    BZOJ3489 A simple rmq problem Description 因为是OJ上的题,就简单点好了.给出一个长度为n的序列,给出M个询问:在[l,r]之间找到一个在这个区间里只出现过一 ...

  5. 【kd-tree】bzoj3489 A simple rmq problem

    Orz zyf教给蒟蒻做法 蒟蒻并不会这题正解……(可持久化树套树?...Orz 对于每个点,我们可以求出pre[i],nex[i],那么询问的答案就是:求max (a[i]),其中 i 满足(pre ...

  6. 【BZOJ3489】A simple rmq problem kd-tree

    [BZOJ3489]A simple rmq problem Description 因为是OJ上的题,就简单点好了.给出一个长度为n的序列,给出M个询问:在[l,r]之间找到一个在这个区间里只出现过 ...

  7. BZOJ3489: A simple rmq problem

    设$i$的前驱为$p_i$,后继为$q_i$,把询问看成点$(L,R)$,有贡献的$i$满足$L\in(p_i,i]$且$R\in[i,q_i)$,询问的就是覆盖这个点的矩形的最大值.那么可以用可持久 ...

  8. bzoj3489 A simple rmq problem 可持久化树套树

    先预处理出两个个数组pre,next.pre[i]表示上一个与i位置数字相同的位置,若不存在则设为0:next[i]表示下一个与i位置数字相同的位置,若不存在则设为n+1.那么一个满足在区间[L,R] ...

  9. BZOJ3489 A simple rmq problem K-D Tree

    传送门 什么可持久化树套树才不会写呢,K-D Tree大法吼啊 对于第\(i\)个数,设其前面最后的与它值相同的位置为\(pre_i\),其后面最前的与它值相同的位置为\(aft_i\),那么对于一个 ...

  10. 【bzoj3489】 A simple rmq problem k-d树

    由于某些原因,我先打了一个错误的树套树,后来打起了$k-d$.接着因不明原因在思路上被卡了很久,在今天中午蹲坑时恍然大悟...... 对于一个数字$a_i$,我们可以用一组三维坐标$(i,pre,nx ...

随机推荐

  1. Odoo calendar 提醒器

    Odoo calendar 提供了一个提醒功能,它包含邮件通知以及web client弹窗功能     创建日历事件的时候,可以设置提醒器     Meeting [ calendar.event ] ...

  2. WinDbg抓取dmp文件

    应用程序发生异常时抓取dmp: adplus.vbs -crash -pn w3wp.exe -y srv*c:\symbols*http://msdl.microsoft.com/download/ ...

  3. CSDN - 进程结束后new出的内存会回收吗?

    http://blog.csdn.net/stanjiang2010/article/details/5386647     关键词:内存回收  

  4. SpringBoot学习之@Controller和@RestController

    今天我们来研究一下@Controller和@RestController的用法 @Controller 1.Controller可以用来跳转页面,并且必须配合模板来使用. @Controller // ...

  5. Linaro/Yocto/Openwrt

    http://en.wikipedia.org/wiki/Linaro Linaro From Wikipedia, the free encyclopedia     This article ap ...

  6. SQL Cursor生命周期

      阅读导航 1 Cursor Step 1.1 Create cursor 1.2 Parse statement 1.3 descript and define 1.4 Bind variable ...

  7. 【BZOJ2521】[Shoi2010]最小生成树 最小割

    [BZOJ2521][Shoi2010]最小生成树 Description Secsa最近对最小生成树问题特别感兴趣.他已经知道如果要去求出一个n个点.m条边的无向图的最小生成树有一个Krustal算 ...

  8. Maximum likelihood from incomplete data via the EM algorithm (1977)

    Maximum likelihood from incomplete data via the EM algorithm (1977)  

  9. Java其实不支持垃圾回收

    Java其实不支持垃圾回收.如果真的支持的话,大多数Java程序在运行的一开始就应该把程序本身删除,因为这些程序本身就是垃圾.   // TODO: This is a 分割线. Please no ...

  10. 常用git命令和工具

    0. ln -s src_dir  //一个参数即可在当前目录下生成一个软链接   1.git command --clone/push a branch      git clone <url ...