题目链接:http://www.lydsy.com:808/JudgeOnline/problem.php?id=3489

题意:一个数列。每次询问一个区间内出现一次的最大的数字是多少。

思路:设last[i]表示i位置的数字上一次出现的位置,next[i]类似。那么询问区间[L,R]时,这个区间的哪些数字可以只出现一次呢?是那些last值小于L且next值大于R的数字。因此按照last排序后,按照last可持久化。具体看代码吧 不好说

const int N=100005;

struct node
{
int val,last,next,id;
}; node b[N];
int last[N],next[N],n,m; struct Node1
{
int Max;
int L,R;
}; struct Node2
{
int root;
int L,R;
}; Node1 A[40000005];
Node2 a[N*20];
int ANum,aNum; int cmp(node a,node b)
{
return a.last<b.last;
} int root[N]; void insert1(int &t,int L,int R,int pos,int val)
{
int cur=++ANum;
A[cur]=A[t];
t=cur;
A[t].Max=max(A[t].Max,val); if(L==R) return; int M=(L+R)>>1;
if(pos<=M) insert1(A[t].L,L,M,pos,val);
else insert1(A[t].R,M+1,R,pos,val);
} void insert(int &t,int L,int R,int posX,int posY,int val)
{
int cur=++aNum;
a[cur]=a[t];
t=cur;
insert1(a[t].root,1,n,posY,val);
if(L==R) return;
int M=(L+R)>>1;
if(posX<=M) insert(a[t].L,L,M,posX,posY,val);
else insert(a[t].R,M+1,R,posX,posY,val);
} int query1(int t,int L,int R,int ll,int rr)
{
if(!t) return 0;
if(L==ll&&R==rr) return A[t].Max;
int M=(L+R)>>1;
if(rr<=M) return query1(A[t].L,L,M,ll,rr);
if(ll>M) return query1(A[t].R,M+1,R,ll,rr);
int ans1=query1(A[t].L,L,M,ll,M);
int ans2=query1(A[t].R,M+1,R,M+1,rr);
return max(ans1,ans2);
} int query(int t,int L,int R,int ll,int rr,int ll1,int rr1)
{
if(L==ll&&R==rr) return query1(a[t].root,1,n,ll1,rr1);
int M=(L+R)>>1;
if(rr<=M) return query(a[t].L,L,M,ll,rr,ll1,rr1);
if(ll>M) return query(a[t].R,M+1,R,ll,rr,ll1,rr1);
int ans1=query(a[t].L,L,M,ll,M,ll1,rr1);
int ans2=query(a[t].R,M+1,R,M+1,rr,ll1,rr1);
return max(ans1,ans2);
} int main()
{
//FFF; n=getInt(); m=getInt();
int i;
for(i=1;i<=n;i++) b[i].val=getInt(),b[i].id=i,last[i]=0,next[i]=n+1;
for(i=1;i<=n;i++)
{
b[i].last=last[b[i].val];
last[b[i].val]=i;
}
for(i=n;i>=1;i--)
{
b[i].next=next[b[i].val];
next[b[i].val]=i;
}
sort(b+1,b+n+1,cmp);
int j=1;
for(i=0;i<n;i++)
{
if(i) root[i]=root[i-1];
while(j<=n&&b[j].last==i)
{
insert(root[i],0,n+1,b[j].next,b[j].id,b[j].val);
j++;
}
} int ans=0;
while(m--)
{
int L=getInt();
int R=getInt();
L=(L+ans)%n+1;
R=(R+ans)%n+1;
if(L>R) swap(L,R);
ans=query(root[L-1],0,n+1,R+1,n+1,L,R);
printf("%d\n",ans);
}
}

BZOJ 3489 A simple rmq problem(可持久化线段树)的更多相关文章

  1. BZOJ 3489 A simple rmq problem 可持久化KDtree/二维线段树

    题目链接:https://www.lydsy.com/JudgeOnline/problem.php?id=3489 题意概述: 给出一个序列,每次询问一个序列区间中仅出现了一次的数字最大是多少,如果 ...

  2. 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][ ...

  3. BZOJ 3489: A simple rmq problem

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

  4. [BZOJ 3489] A simple rmq problem 【可持久化树套树】

    题目链接:BZOJ - 3489 题目分析 “因为是OJ上的题,就简单点好了.”——出题人 真的..好..简单... 首先,我们求出每个数的前一个与它相同的数的位置,即 prev[i] ,如果前面没有 ...

  5. bzoj 3489 A simple rmq problem - 线段树

    Description 因为是OJ上的题,就简单点好了.给出一个长度为n的序列,给出M个询问:在[l,r]之间找到一个在这个区间里只出现过一次的数,并且要求找的这个数尽可能大.如果找不到这样的数,则直 ...

  6. bzoj 3489 A simple rmq problem——主席树套线段树

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=3489 题解:http://www.itdaan.com/blog/2017/11/24/9b ...

  7. bzoj 3489 A simple rmq problem —— 主席树套线段树

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=3489 题解:http://www.itdaan.com/blog/2017/11/24/9b ...

  8. BZOJ 3489: A simple rmq problem (KD-tree做法)

    KD树水过这道可持久化树套树-其实就是个三维偏序 题解戳这里 CODE #include <bits/stdc++.h> using namespace std; #define ls ( ...

  9. BZOJ.3489.A simple rmq problem(主席树 Heap)

    题目链接 当时没用markdown写,可能看起来比较难受...可以复制到别的地方看比如DevC++. \(Description\) 给定一个长为n的序列,多次询问[l,r]中最大的只出现一次的数.强 ...

随机推荐

  1. Oracle游标总结三

    -- 声明游标:CURSOR cursor_name IS select_statement --For 循环游标--(1)定义游标--(2)定义游标变量--(3)使用for循环来使用这个游标,for ...

  2. 让未激活的win8.1不再跳出提示激活的窗口

    以管理员运行命令行: 输入以下命令: slmgr.vbs -upk

  3. php使用过滤器filter_var轻松验证邮箱url和ip地址等

    以前使用php的时候还不知道有过滤器filter这玩意,那时候判断邮箱.url和ip地址格式是否符合都是用正则表达式.后来随着使用的逐渐深入,才知道在php中也可以使用内置的函数库过滤器filter来 ...

  4. SQL关于apply的两种形式cross apply和outer apply(转载)

    SQL 关于apply的两种形式cross apply 和 outer apply   apply有两种形式: cross apply 和 outer apply   先看看语法:   <lef ...

  5. PayPal网站付款标准版(for PHP)

    简单整理一下PHP项目整合PayPal支付功能. 一.表单的构建: <form method="post" name="form" action=&quo ...

  6. c++ 程序在内存中的分布

    从低地址到高地址: 1.代码区[包含常量的]:存放函数体的二进制代码 2.全局变量区[已初始化 + 未初始化]: 全局变量和静态变量的存储是放一块的,初始化的全局变量和静态变量在一块区域, 未初始化的 ...

  7. Spring中@Cacheable的用法

    在Spring中通过获取MemCachedClient来实现与memcached服务器进行数据读取的方式.不过,在实际开发中,我们往往是通过Spring的@Cacheable来实现数据的缓存的,所以, ...

  8. yii2 批量插入or更新

    $sql1 = 'insert into business_ip (gid, name, area, belongName, belongArea, destIPv4, created, update ...

  9. 20145227 《Java程序设计》第4周学习总结

    20145227 <Java程序设计>第4周学习总结 教材学习内容总结 第六章 继承与多态 6.1 何谓继承 1.继承共同行为 多个类中存在相同属性和行为时,将这些内容抽取到单独一个类中, ...

  10. [ios][swift]swift混编

    http://blog.csdn.net/iflychenyang/article/details/8876542(如何在Objective-C的头文件引用C++的头文件) 1.将.m文件扩展名改为. ...