BZOJ 3489 A simple rmq problem ——KD-Tree
考前写写板子。
用$(i,pre[i],nxt[i])$来描述一个点,然后就变成了区间求最值的问题。
KD-Tree 由低维转向高维的方法,可以用来敲暴力。
剩下就是KD-Tree的基本操作了。
#include <map>
#include <cmath>
#include <queue>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
#define F(i,j,k) for (int i=j;i<=k;++i)
#define D(i,j,k) for (int i=j;i>=k;--i)
#define inf 0x3f3f3f3f
#define L t[o].ch[0]
#define R t[o].ch[1]
#define ll long long
#define mp make_pair
#define maxn 200005 int D,a[maxn],n,m,ans,pre[maxn],nxt[maxn],lst[maxn],rt;
int ql,qr;
struct Point{
int d[3],mn[3],mx[3],ch[2],v,vmax;
}t[maxn];//i pre nxt bool operator < (Point a,Point b){return a.d[D]<b.d[D];} void pushup(int o)
{
F(i,0,2) t[o].mx[i]=t[o].mn[i]=t[o].d[i];
F(i,0,2)
{
t[o].mx[i]=max(max(t[L].mx[i],t[R].mx[i]),t[o].mx[i]);
t[o].mn[i]=min(min(t[L].mn[i],t[R].mn[i]),t[o].mn[i]);
}
t[o].vmax=max(t[o].v,max(t[L].vmax,t[R].vmax));
} void init(){F(i,0,2) t[0].mn[i]=inf,t[0].mx[i]=-inf;} int build(int l,int r,int dir)
{
D=dir;int mid=l+r>>1;int o=mid;
nth_element(t+l,t+mid,t+r+1);
L=l<mid?build(l,mid-1,(dir+1)%3):0;
R=r>mid?build(mid+1,r,(dir+1)%3):0;
pushup(o); return o;
} bool in(int o)
{
if (t[o].mx[1]>=ql||t[o].mn[2]<=qr) return 0;
if (t[o].mx[0]>qr||t[o].mx[1]<ql) return 0;
return 1;
} bool pin(int o)
{
if (t[o].d[0]>=ql&&t[o].d[0]<=qr&&t[o].d[1]<ql&&t[o].d[2]>qr) return 1;
return 0;
} bool check(int o)
{
if (t[o].mx[0]<ql||t[o].mn[0]>qr) return 0;
if (t[o].mn[1]>=ql||t[o].mx[2]<=qr) return 0;
return 1;
} void query(int o)
{
if (!o) return;
if (in(o)) {ans=max(ans,t[o].vmax);return;}
if (pin(o)) ans=max(ans,t[o].v);
if (t[L].vmax>t[R].vmax)
{
if (t[L].vmax>ans&&check(L)) query(L);
if (t[R].vmax>ans&&check(R)) query(R);
}
else
{
if (t[R].vmax>ans&&check(R)) query(R);
if (t[L].vmax>ans&&check(L)) query(L);
}
} int main()
{
scanf("%d%d",&n,&m);init();
F(i,1,n) scanf("%d",&a[i]);
F(i,1,n) lst[i]=0;
F(i,1,n) pre[i]=lst[a[i]],lst[a[i]]=i;
F(i,1,n) lst[i]=n+1;
D(i,n,1) nxt[i]=lst[a[i]],lst[a[i]]=i;
F(i,1,n) t[i].d[0]=i,t[i].d[1]=pre[i],t[i].d[2]=nxt[i],t[i].v=a[i];
rt=build(1,n,0);
F(i,1,m)
{
int x,y;
scanf("%d%d",&x,&y);
ql=min((x+ans)%n+1,(y+ans)%n+1);
qr=max((x+ans)%n+1,(y+ans)%n+1);
ans=0;query(rt);printf("%d\n",ans);
}
return 0;
}
BZOJ 3489 A simple rmq problem ——KD-Tree的更多相关文章
- BZOJ 3489: A simple rmq problem(K-D Tree)
Time Limit: 40 Sec Memory Limit: 512 MBSubmit: 2579 Solved: 888[Submit][Status][Discuss] Descripti ...
- bzoj 3489: A simple rmq problem k-d树思想大暴力
3489: A simple rmq problem Time Limit: 10 Sec Memory Limit: 512 MBSubmit: 551 Solved: 170[Submit][ ...
- BZOJ 3489: A simple rmq problem
3489: A simple rmq problem Time Limit: 40 Sec Memory Limit: 600 MBSubmit: 1594 Solved: 520[Submit] ...
- [BZOJ 3489] A simple rmq problem 【可持久化树套树】
题目链接:BZOJ - 3489 题目分析 “因为是OJ上的题,就简单点好了.”——出题人 真的..好..简单... 首先,我们求出每个数的前一个与它相同的数的位置,即 prev[i] ,如果前面没有 ...
- BZOJ3489 A simple rmq problem K-D Tree
传送门 什么可持久化树套树才不会写呢,K-D Tree大法吼啊 对于第\(i\)个数,设其前面最后的与它值相同的位置为\(pre_i\),其后面最前的与它值相同的位置为\(aft_i\),那么对于一个 ...
- BZOJ.3489.A simple rmq problem(主席树 Heap)
题目链接 当时没用markdown写,可能看起来比较难受...可以复制到别的地方看比如DevC++. \(Description\) 给定一个长为n的序列,多次询问[l,r]中最大的只出现一次的数.强 ...
- bzoj 3489 A simple rmq problem - 线段树
Description 因为是OJ上的题,就简单点好了.给出一个长度为n的序列,给出M个询问:在[l,r]之间找到一个在这个区间里只出现过一次的数,并且要求找的这个数尽可能大.如果找不到这样的数,则直 ...
- BZOJ 3489 A simple rmq problem(可持久化线段树)
题目链接:http://www.lydsy.com:808/JudgeOnline/problem.php?id=3489 题意:一个数列.每次询问一个区间内出现一次的最大的数字是多少. 思路:设la ...
- BZOJ 3489 A simple rmq problem 可持久化KDtree/二维线段树
题目链接:https://www.lydsy.com/JudgeOnline/problem.php?id=3489 题意概述: 给出一个序列,每次询问一个序列区间中仅出现了一次的数字最大是多少,如果 ...
- bzoj 3489 A simple rmq problem——主席树套线段树
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=3489 题解:http://www.itdaan.com/blog/2017/11/24/9b ...
随机推荐
- python基础一 day14 生成器函数进阶(1)
- .vue公共组件裁减导航
场景: 有一个公共头部和底部,vue搭建的框架,在app.vue里写的公共方法,首页是个登录页面,不需要公共部分,在这基础上进行公共部分的显示隐藏. 即注册页.登录页.404页面都不要导航 代码: ( ...
- NOIP模拟赛 篮球比赛2
篮球比赛2(basketball2.*) 由于Czhou举行了众多noip模拟赛,也导致放学后篮球比赛次数急剧增加.神牛们身体素质突飞猛进,并且球技不断精进.这引起了体育老师彩哥的注意,为了给校篮球队 ...
- 洛谷 1571 眼红的Medusa
洛谷 1571 眼红的Medusa 虽说这道题标签里写明了二分,然而我还是首先想到了map......毕竟map真的是简单好写. map做法 #include<bits/stdc++.h> ...
- 03大端和小端(Big endian and Little endian)
1.大端和小端的问题 对于整型.长整型等数据类型,Big endian 认为第一个字节是最高位字节(按照从低地址到高地址的顺序存放数据的高位字节到低位字节),而 Little endian 则相反 ...
- Vim编辑器基础
Vim编辑器基础 vi:Visual Interface vim:VI iMproved Vim模式 1.编辑模式(命令模式) 只能下达命令,不能键入字符 2.输入模式 键入字符 3.末行模式 左下角 ...
- day2-python 登录
# username = 'niuhanyang' # 写一个判断登录的程序: # 输入: username # password # 最大错误次数是3次,输入3次都没有登录成功,提示错误次数达到上限 ...
- Python基础——概述
新建Python代码 Jupyter Notebook是在浏览器中运行的. 地址栏输入http://localhost:8888后直接进入工作文件夹,显示文件夹中的内容. 右上角选择New——Pyth ...
- LeetCode(268) Missing Number
题目 Given an array containing n distinct numbers taken from 0, 1, 2, -, n, find the one that is missi ...
- 水题:CF16C-Monitor
Monitor 题目描述 Reca company makes monitors, the most popular of their models is AB999 with the screen ...