【BZOJ3489】A simple rmq problem

Description

因为是OJ上的题,就简单点好了。给出一个长度为n的序列,给出M个询问:在[l,r]之间找到一个在这个区间里只出现过一次的数,并且要求找的这个数尽可能大。如果找不到这样的数,则直接输出0。我会采取一些措施强制在线。

Input

第一行为两个整数N,M。M是询问数,N是序列的长度(N<=100000,M<=200000)

第二行为N个整数,描述这个序列{ai},其中所有1<=ai<=N

再下面M行,每行两个整数x,y,

询问区间[l,r]由下列规则产生(OIER都知道是怎样的吧>_<):

l=min((x+lastans)mod n+1,(y+lastans)mod n+1);

r=max((x+lastans)mod n+1,(y+lastans)mod n+1);

Lastans表示上一个询问的答案,一开始lastans为0

Output

一共M行,每行给出每个询问的答案。

Sample Input

10 10
6 4 9 10 9 10 9 4 10 4
3 8
10 1
3 4
9 4
8 1
7 8
2 9
1 1
7 3
9 9

Sample Output

4
10
10
0
0
10
0
4
0
4

HINT

注意出题人为了方便,input的第二行最后多了个空格。

2015.6.24新加数据一组,2016.7.9放至40S,600M,但未重测

题解:在[l,r]中只出现一次等价于:上一次出现的位置<l&&l<=这次出现的位置<=r&&下一次出现的位置>r。然后写个三维kd-tree就行了。

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#define rep for(int i=0;i<3;i++)
#define D1 ((D+1)%3)
#define D2 ((D+2)%3)
using namespace std;
const int maxn=100010;
int A,B,D,n,m,ans,root;
int head[maxn],pre[maxn],next[maxn],buc[maxn],v[maxn];
struct kd
{int v[3],sm[3],sn[3],ls,rs,ms,s;kd (){}kd (int a,int b,int c,int d){v[0]=sm[0]=sn[0]=a,v[1]=sm[1]=sn[1]=b,v[2]=sm[2]=sn[2]=c,s=ms=d,ls=rs=0;}int  & operator [] (int a)   {return v[a];}bool operator < (kd a) const{return (v[D]==a[D])?((v[D1]==a[D1])?(v[D2]==a[D2]):(v[D1]<a[D1])):(v[D]<a[D]);}
};
kd t[maxn];
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 pushup(int x,int y)
{rep t[x].sm[i]=max(t[x].sm[i],t[y].sm[i]),t[x].sn[i]=min(t[x].sn[i],t[y].sn[i]);t[x].ms=max(t[x].ms,t[y].ms);
}
int build(int l,int r,int d)
{if(l>r) return 0;D=d;int mid=l+r>>1;nth_element(t+l,t+mid,t+r+1);t[mid].ls=build(l,mid-1,(d+1)%3),t[mid].rs=build(mid+1,r,(d+1)%3);if(t[mid].ls)  pushup(mid,t[mid].ls);if(t[mid].rs) pushup(mid,t[mid].rs);return mid;
}
int check(int x)
{if(t[x].ms<=ans||t[x].sm[0]<A||t[x].sn[0]>B||t[x].sn[1]>=A||t[x].sm[2]<=B)   return 0;return 1;
}
void query(int x)
{if(!x||!check(x))  return ;if(t[x][0]>=A&&t[x][0]<=B&&t[x][1]<A&&t[x][2]>B&&t[x].s>ans)   ans=t[x].s;query(t[x].ls),query(t[x].rs);
}
int main()
{n=rd(),m=rd();int i;for(i=1;i<=n;i++){v[i]=rd(),pre[i]=head[v[i]];if(head[v[i]])    next[head[v[i]]]=i;head[v[i]]=i;}for(i=1;i<=n;i++) if(!next[i])    next[i]=n+1;for(i=1;i<=n;i++)  t[i]=kd(i,pre[i],next[i],v[i]);root=build(1,n,0);for(i=1;i<=m;i++){A=(ans+rd())%n+1,B=(ans+rd())%n+1;if(A>B)  swap(A,B);ans=0,query(root);printf("%d\n",ans);}return 0;
}

转载于:https://www.cnblogs.com/CQzhangyu/p/7114730.html

【BZOJ3489】A simple rmq problem kd-tree相关推荐

  1. 【BZOJ3489】A simple rmq problem(树套树)

    链接w:http://www.lydsy.com/JudgeOnline/problem.php?id=3489 题解:http://blog.csdn.net/PoPoQQQ/article/det ...

  2. BZOJ 3489: A simple rmq problem(K-D Tree)

    Time Limit: 40 Sec  Memory Limit: 512 MB Submit: 2579  Solved: 888 [Submit][Status][Discuss] Descrip ...

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

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

  4. 论文阅读 【CVPR-2022】 A Simple Multi-Modality Transfer Learning Baseline for Sign Language Translation

    论文阅读 [CVPR-2022] A Simple Multi-Modality Transfer Learning Baseline for Sign Language Translation st ...

  5. BZOJ 3489: A simple rmq problem

    3489: A simple rmq problem Time Limit: 40 Sec  Memory Limit: 600 MB Submit: 1594  Solved: 520 [Submi ...

  6. P4897 【模板】最小割树(Gomory-Hu Tree)(网络流/最小割/树形结构)

    P4897 [模板]最小割树(Gomory-Hu Tree) 这个算法可以用来求解一个无向图上任意两点的最小割,具体过程就是每次选择两个点求最小割,然后在一个新图中这两个点连边,然后对于这两个点的连通 ...

  7. 【MOT】GHOST: Simple Cues Lead to a Strong Multi-Object Tracker

    GHOST: Simple Cues Lead to a Strong Multi-Object Tracker 论文地址:https://arxiv.org/abs/2206.04656 代码:暂未 ...

  8. 【CodeForces】914 E. Palindromes in a Tree 点分治

    [题目]E. Palindromes in a Tree [题意]给定一棵树,每个点都有一个a~t的字符,一条路径回文定义为路径上的字符存在一个排列构成回文串,求经过每个点的回文路径数.n<=2 ...

  9. 【CodeForces】741 D. Arpa’s letter-marked tree and Mehrdad’s Dokhtar-kosh paths(dsu on tree)

    [题意]给定n个点的树,每条边有一个小写字母a~v,求每棵子树内的最长回文路径,回文路径定义为路径上所有字母存在一种排列为回文串.n<=5*10^5. [算法]dsu on tree [题解]这 ...

最新文章

  1. mxGraph破解说明
  2. 多项式快速插值学习小记
  3. 生成验证码的流程分析.
  4. github 和git_学习编码时如何学习Git和GitHub
  5. go语言slice使用的时候遇到的奇怪现象以及分析
  6. 转:KVC与KVO机制
  7. WINDOWS下获取目录环境变量的C代码
  8. Atitit glb 1tie 2tie city lst 一二线城市列表数据约50个一线城市Alpha ++ 阿尔法++,,London 伦敦,,New York 纽约,,Alpha +
  9. 常用设计模式Python实现
  10. 以接口请求的方式,解决移动端页面中字体文件过大的问题
  11. 积分墙渠道运营那些事儿
  12. 光学中的几个物理量的意义
  13. 文本域中只允许输入特定长度的字符
  14. UVa 12307 Smallest Enclosing Rectangle(旋转卡壳+最小覆盖矩形)
  15. java实现1 12 123 1234 12345 123456 输出
  16. [论文阅读] Beyond part models: Person retrieval with refined part pooling
  17. CentOS6.5的安装及本地源的配置
  18. 计算机的cup颗数、核数、线程数
  19. swagger初体验
  20. 转】oracle 小数格式化为百分数

热门文章

  1. ajax 异步插入图片到数据库(多图上传)
  2. 【LeetCode】48. Rotate Image (2 solutions)
  3. mysql做千万条压测数据
  4. 关于一类docker容器闪退问题定位
  5. 基于字典SR各种方法【稀疏编码多种方法】
  6. iOS事件机制,以及不同手势使用touchesBegan等表现形式
  7. 给页面中的所有链接新窗口中打开
  8. linux添加开机自启动脚本
  9. Block的引用循环问题 (ARC non-ARC)
  10. [转] jQuery 选择器