BZOJ 3489 A simple rmq problem(可持久化线段树)
题目链接: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(可持久化线段树)的更多相关文章
- BZOJ 3489 A simple rmq problem 可持久化KDtree/二维线段树
题目链接:https://www.lydsy.com/JudgeOnline/problem.php?id=3489 题意概述: 给出一个序列,每次询问一个序列区间中仅出现了一次的数字最大是多少,如果 ...
- 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] ,如果前面没有 ...
- bzoj 3489 A simple rmq problem - 线段树
Description 因为是OJ上的题,就简单点好了.给出一个长度为n的序列,给出M个询问:在[l,r]之间找到一个在这个区间里只出现过一次的数,并且要求找的这个数尽可能大.如果找不到这样的数,则直 ...
- bzoj 3489 A simple rmq problem——主席树套线段树
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=3489 题解:http://www.itdaan.com/blog/2017/11/24/9b ...
- bzoj 3489 A simple rmq problem —— 主席树套线段树
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=3489 题解:http://www.itdaan.com/blog/2017/11/24/9b ...
- BZOJ 3489: A simple rmq problem (KD-tree做法)
KD树水过这道可持久化树套树-其实就是个三维偏序 题解戳这里 CODE #include <bits/stdc++.h> using namespace std; #define ls ( ...
- BZOJ.3489.A simple rmq problem(主席树 Heap)
题目链接 当时没用markdown写,可能看起来比较难受...可以复制到别的地方看比如DevC++. \(Description\) 给定一个长为n的序列,多次询问[l,r]中最大的只出现一次的数.强 ...
随机推荐
- 夺命雷公狗---Thinkphp----5之数据库的链接
我们打开WEB目录下发现了Common和Home以及Runtime这三个文件夹 那么我们第一个目标是完成网站后台的首页吧,那么我们就直接将Home的文件夹复制一份出来,并且改名为Admin这样就可以分 ...
- [ubuntu] ubuntu13.04安装rabbitcvs管理svn
加入源 sudo add-apt-repository ppa:rabbitvcs/ppa 更新 sudo apt-get update 安装软件 sudo apt-get install rabbi ...
- 杭电1003 MAX SUN
Problem Description Given a sequence a[1],a[2],a[3]......a[n], your job is to calculate the max sum ...
- sql多表查询(out join,inner join, left join, right join)
left join以左表为基准显示所有左表的信息,在on中有符合条件的其他表也显示出来 right join则相反 inner join的只显示on中符合条件的 1 使用多个表格 在「world」资料 ...
- 【python】__future__模块
转自:http://www.jb51.net/article/65030.htm Python的每个新版本都会增加一些新的功能,或者对原来的功能作一些改动.有些改动是不兼容旧版本的,也就是在当前版本运 ...
- scala偏函数
package com.ming.test /** * 在Scala中,偏函数是具有类型PartialFunction[-T,+V]的一种函数.T是其接受的函数类型,V是其返回的结果类型. * 偏函数 ...
- BZOJ 1036:树的统计Count(树链剖分)
http://www.lydsy.com/JudgeOnline/problem.php?id=1036 题意:中文题意. 思路:也是普通的树链剖分.唯一注意的点是在change函数中 while(t ...
- C#:DataTable内容转换为String(XML)
//DataTable转String方法 public static String DataTable2String(DataTable dt) { string strXML = "< ...
- Browser对象
Window对象即浏览器中打开的窗口,当文档里面有框架(frame或者iframe标签)时,浏览器会为HTML文档创建一个window对象,并为每个框架创建一个额外的window对象. 属性close ...
- python7 静态方法、类方法、属性方法 ;反射;异常处理
#-*- coding:utf8 -*- # 静态方法@staticmethod # 静态方法(当eat变成静态方法后,再通过实例调用时就不会自动把实例本身当作一个参数传给self了.) clas ...