题目链接

当时没用markdown写,可能看起来比较难受...可以复制到别的地方看比如DevC++。


\(Description\)

给定一个长为n的序列,多次询问[l,r]中最大的只出现一次的数。强制在线。

\(Solution\)

我也不知道该怎么说,反正就是预处理 建主席树,套堆/set,树i存l为i,r为[i,n]的答案(这样就是在某棵树上单点查maxv了)。

处理好最初的树,就可以利用主席树根据前缀建树的性质,每个点i的建树只需要处理i位置。

那么最初的树就是将所有的数在区间[第一次出现位置,nxt[]-1]中加入这个数。

当左端点i移动时,A[i-1]的贡献没了,要删掉;但是如果A[i-1]在后面出现,则还要在[nxt[A[i-1]],nxt[nxt[A[i-1]]]-1]上加入A[i-1]。(A[i]已经处理了,要么在建最初树时要么在先前建的树中)

也不是每棵树每个节点都套堆,堆只需要帮助建主席树时加加删删(还是利用前缀),要用的只是根据堆得到的每个节点的maxv[],所以堆还是4n大就可以。。

和CF那题不同的是这题固定l更方便,那题固定r好些。所以只需要nxt(和最靠前的一个las用来建最初的树)。

第一次主席树区间修改。。在信息覆盖当前区间时直接给节点赋值,return;查询单点时加上沿路节点的mx[](标记永久化)。

注意新建节点时copy x的mx[]。因为每次删除是完全删除上一次的加入,so在未递归到完全覆盖区间前copy mx是对的?真这样麻烦吗。。以后是不是该乖乖写Update()。。


询问的限制条件可以看成三个维度,即在某三维空间内找权值最大的点,可以用K-D Tree。出题人说这不应是正解,可以卡,只是数据问题跑的比主席树快。。可以看这儿

不过还是K-D Tree更好的应用吧,不想看了,先放着。。

K-D Tree:https://blog.csdn.net/FromATP/article/details/61198101


代码参考(chao)自:https://blog.csdn.net/u014609452/article/details/51396271。


非强制在线可以直接线段树+排序扫描线做,有个简化版的题见这儿


//147544kb	9488ms
#include <queue>
#include <cstdio>
#include <cctype>
#include <algorithm>
//#define gc() getchar()
#define MAXIN 300000
#define gc() (SS==TT&&(TT=(SS=IN)+fread(IN,1,MAXIN,stdin),SS==TT)?EOF:*SS++)
const int N=1e5+5; int n,Q,A[N],las[N],nxt[N],rcnt,root[N*3],pos[N];
char IN[MAXIN],*SS=IN,*TT=IN;
struct Tree//Persistent Segment Tree
{
#define S N*100//我也不知道这样搞区间修改的主席树会有多少节点(2个log?)
#define lson son[x][0]
#define rson son[x][1]
#define ToL l,m,rt<<1
#define ToR m+1,r,rt<<1|1
struct Heap
{
std::priority_queue<int> h,d;
inline void Insert(int x) {h.push(x);}
inline void Delete(int x) {d.push(x);}
inline void Maintain(){
while(!h.empty()&&!d.empty()&&h.top()==d.top()) h.pop(),d.pop();
}
inline int Top(){
Maintain(); return h.empty()?0:h.top();
}
}hp[N<<2];
int tot,son[S][2],mx[S]; // inline void Update(int x){//Update没啥用啊 又不用区间信息
// mx[x]=std::max(mx[lson],mx[rson]);
// }
void Insert(int &y,int x,int l,int r,int rt,int L,int R,int v)//rt:一般线段树的节点 存储堆
{
y=++tot;
if(L<=l && r<=R){
hp[rt].Insert(v), mx[y]=hp[rt].Top(), son[y][0]=lson, son[y][1]=rson;
return ;
}
int m=l+r>>1; mx[y]=mx[x];//!
if(L<=m)
if(m<R) Insert(son[y][0],lson,ToL,L,R,v), Insert(son[y][1],rson,ToR,L,R,v);
else son[y][1]=rson, Insert(son[y][0],lson,ToL,L,R,v);
else son[y][0]=lson, Insert(son[y][1],rson,ToR,L,R,v);
}
void Delete(int &y,int x,int l,int r,int rt,int L,int R,int v)
{
y=++tot;
if(L<=l && r<=R){
hp[rt].Delete(v), mx[y]=hp[rt].Top(), son[y][0]=lson, son[y][1]=rson;
return;
}
int m=l+r>>1; mx[y]=mx[x];//!
if(L<=m)
if(m<R) Delete(son[y][0],lson,ToL,L,R,v), Delete(son[y][1],rson,ToR,L,R,v);
else son[y][1]=rson, Delete(son[y][0],lson,ToL,L,R,v);
else son[y][0]=lson, Delete(son[y][1],rson,ToR,L,R,v);
}
int Query(int x,int l,int r,int pos)
{
if(!x) return 0;//这个没啥用啊...
if(l==r) return mx[x];
int m=l+r>>1;
if(pos<=m) return std::max(mx[x],Query(lson,l,m,pos));
else return std::max(mx[x],Query(rson,m+1,r,pos));
}
}T; inline int read()
{
int now=0;register char c=gc();
for(;!isdigit(c);c=gc());
for(;isdigit(c);now=now*10+c-'0',c=gc());
return now;
} int main()
{
n=read(), Q=read();
for(int i=1; i<=n; ++i) A[i]=read();
for(int i=1; i<=n; ++i) las[i]=n+1;
for(int i=n; i; --i) nxt[i]=las[A[i]], las[A[i]]=i;
for(int i=n; i; --i)//直接枚举值域建树就可以啊 话说倒着插入堆会更快吗
if(las[i]<=n) T.Insert(root[rcnt],root[rcnt++],1,n,1,las[i],nxt[las[i]]-1,i);//参数顺序
pos[1]=root[rcnt];//root[rcnt] not rcnt→_→
for(int i=1; i<n; ++i)
{
T.Delete(root[rcnt],root[rcnt++],1,n,1,i,nxt[i]-1,A[i]);
if(nxt[i]<=n)
T.Insert(root[rcnt],root[rcnt++],1,n,1,nxt[i],nxt[nxt[i]]-1,A[i]);
pos[i+1]=root[rcnt];
}
for(int ans=0,i=1,l,r; i<=Q; ++i)
{
l=(read()+ans)%n+1, r=(read()+ans)%n+1;
if(l>r) std::swap(l,r);//l>r&&(std::swap(l,r),1);
printf("%d\n",ans=T.Query(pos[l],1,n,r));
}
return 0;
}

BZOJ.3489.A simple rmq problem(主席树 Heap)的更多相关文章

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

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

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

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

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

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

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

  5. BZOJ 3489: A simple rmq problem

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

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

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

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

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

  8. BZOJ 3489 A simple rmq problem(可持久化线段树)

    题目链接:http://www.lydsy.com:808/JudgeOnline/problem.php?id=3489 题意:一个数列.每次询问一个区间内出现一次的最大的数字是多少. 思路:设la ...

  9. bzoj 3585: mex && 3339: Rmq Problem -- 主席树

    3585: mex Time Limit: 20 Sec  Memory Limit: 128 MB Description 有一个长度为n的数组{a1,a2,...,an}.m次询问,每次询问一个区 ...

随机推荐

  1. javascript 中检测数据类型的方法

    typeof 检测数据类型 javascript 中检测数据类型有好几种,其中最简单的一种是 typeof 方式.typeof 方法返回的结果是一个字符串.typeof 的用法如下: typeof v ...

  2. 利用反射型XSS二次注入绕过CSP form-action限制

    利用反射型XSS二次注入绕过CSP form-action限制 翻译:SecurityToolkit 0x01 简单介绍 CSP(Content-Security-Policy)是为了缓解XSS而存在 ...

  3. 洛谷 P4592: bzoj 5338: [TJOI2018]异或

    题目传送门:洛谷P4592. 题意简述: 题面说的很清楚了. 题解: 发现没有修改很快乐.再看异或最大值操作,很容易想到可持久化 01trie. 这里要把 01trie 搬到树上,有点难受. 树剖太捞 ...

  4. Java Dom对XML的解析和修改操作

    与Dom4J和JDom对XML的操作类似,JDK提供的JavaDom解析器用起来一样方便,在解析XML方面Java DOM甚至更甚前两者一筹!其不足之处在于对XML的增删改比较繁琐,特开篇介绍... ...

  5. 源码安装postgresql数据库

    一般情况下,postgresql由非root用户启动. 1.创建postgres用户 groupadd postgres useradd -g postgres postgres 下面的操作都在pos ...

  6. aarch64_m2

    mingw32-leptonica-1.74.4-1.fc26.noarch.rpm 2017-06-12 17:20 1.0M fedora Mirroring Project mingw32-le ...

  7. Docker Compose practice

    Docker Compose 什么是 Docker-Compose? Compose 可以让用户在集群中部署分布式应用.简单的说,Docker Compose 属于一个"应用层"的 ...

  8. linux文件管理 -> 系统文件属性

    -rw-------. 1 root root 4434 May 30 13:58 ks.cfg -rw-------. ①:文件类型与权限 ②:硬链接次数 root ③:所属用户 root ④:所属 ...

  9. poj1102

    模拟 #include <iostream> #include <string> using namespace std; ][][] = { { ' ', '-', ' ', ...

  10. matlab随笔

    主要是记录一些函数.(博客园的一些操作实在是太不方便了) cat函数:http://blog.sina.com.cn/s/blog_6b7dfd9d0100mnz7.html 联结两个数组 magic ...