这题我是离线做的

设i位置的数上次出现的位置是pre[i](如果第一次出现那就是0)

可以想到,用线段树维护一个区间的pre的最小值,如果它小于区间左端点,那这个数就是一个合法的答案

但直接这样做是错的

考虑1,2,3,4,[1,1],5,虽然前一个1的pre在区间外面,但他后面还有一个1啊

所以可以按照询问的右端点排序,推着来维护这个最小值

具体来说,对于i,先把i位置的值改成pre[i],然后如果有pre[i],那把pre[i]位置的值改成inf(一开始都要初始化成inf)

然后再查的话,我查到的就都是这个区间里的最后一次出现的那个数了,就不会有锅

 #include<bits/stdc++.h>
#define pa pair<int,int>
#define CLR(a,x) memset(a,x,sizeof(a))
using namespace std;
typedef long long ll;
const int maxn=5e5+; inline ll rd(){
ll x=;char c=getchar();int neg=;
while(c<''||c>''){if(c=='-') neg=-;c=getchar();}
while(c>=''&&c<='') x=x*+c-'',c=getchar();
return x*neg;
} int N,Q,pre[maxn],A[maxn],tmp[maxn],ans[maxn];
pa mn[maxn<<];
struct Node{
int l,r,i;
}que[maxn]; inline bool cmp(Node a,Node b){return a.r<b.r;}; inline void update(int p){
mn[p]=min(mn[p<<],mn[p<<|]);
} void change(int p,int l,int r,int x,int y){
if(l==r) mn[p]=make_pair(y,x);
else{
int m=l+r>>;
if(x<=m) change(p<<,l,m,x,y);
else change(p<<|,m+,r,x,y);
update(p);
}
} pa query(int p,int l,int r,int x,int y){
if(x<=l&&r<=y) return mn[p];
int m=l+r>>;pa re=make_pair(N+,);
if(x<=m) re=query(p<<,l,m,x,y);
if(y>=m+) re=min(re,query(p<<|,m+,r,x,y));
return re;
} int main(){
int i,j,k;
N=rd();
for(i=;i<=N;i++){
A[i]=rd();
pre[i]=tmp[A[i]],tmp[A[i]]=i;
}Q=rd();
for(i=;i<=Q;i++){
que[i].l=rd(),que[i].r=rd(),que[i].i=i;
}sort(que+,que+Q+,cmp);
CLR(mn,);
for(i=,j=;i<=Q;i++){
for(;j<=que[i].r&&j<=N;j++){
if(pre[j]) change(,,N,pre[j],N+);
change(,,N,j,pre[j]);
}
pa re=query(,,N,que[i].l,que[i].r);
if(re.first<que[i].l) ans[que[i].i]=A[re.second];
}
for(i=;i<=Q;i++) printf("%d\n",ans[i]);
return ;
}

cf1000F One Occurrence (线段树)的更多相关文章

  1. cf1000F. One Occurrence(线段树 set)

    题意 题目链接 Sol (真后悔没打这场EDU qwq) 首先把询问离线,预处理每个数的\(pre, nxt\),同时线段树维护\(pre\)(下标是\(pre\),值是\(i\)),同时维护一下最大 ...

  2. HDU-6704 K-th occurrence (后缀自动机father树上倍增建权值线段树合并)

    layout: post title: HDU-6704 K-th occurrence (后缀自动机father树上倍增建权值线段树合并) author: "luowentaoaa&quo ...

  3. Sum of Squares of the Occurrence Counts解题报告(后缀自动机+LinkCutTree+线段树思想)

    题目描述 给定字符串\(S(|S|\le10^5)\),对其每个前缀求出如下的统计量: 对该字符串中的所有子串,统计其出现的次数,求其平方和. Sample Input: aaa Sample Out ...

  4. F - One Occurrence CodeForces - 1000F (线段树+离线处理)

    You are given an array aa consisting of nn integers, and qq queries to it. ii-th query is denoted by ...

  5. HDU - 6704 K-th occurrence (后缀数组+主席树/后缀自动机+线段树合并+倍增)

    题意:给你一个长度为n的字符串和m组询问,每组询问给出l,r,k,求s[l,r]的第k次出现的左端点. 解法一: 求出后缀数组,按照排名建主席树,对于每组询问二分或倍增找出主席树上所对应的的左右端点, ...

  6. K-th occurrence HDU - 6704 (后缀数组+二分线段树+主席树)

    大意: 给定串s, q个询问(l,r,k), 求子串s[l,r]的第kk次出现位置. 这是一篇很好的题解: https://blog.csdn.net/sdauguanweihong/article/ ...

  7. hdu6704 2019CCPC网络选拔赛1003 K-th occurrence 后缀自动机+线段树合并

    解题思路: fail树上用权值线段树合并求right/endpos集合,再用倍增找到待查询串对应节点,然后权值线段树求第k大. #include<bits/stdc++.h> using ...

  8. hdu 6704 K-th occurrence(后缀数组+可持久化线段树)

    Problem Description You are given a string S consisting of only lowercase english letters and some q ...

  9. hdu 4117 -- GRE Words (AC自动机+线段树)

    题目链接 problem Recently George is preparing for the Graduate Record Examinations (GRE for short). Obvi ...

随机推荐

  1. MySQL 遇到的问题以及解决方案

    Problem: ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/lib/mysql/mysq ...

  2. 阿里云ECS服务器云监控(cloudmonitor)Go语言版本插件安装卸载与维护

    云监控Go语言版本插件安装_主机监控_用户指南_云监控-阿里云https://help.aliyun.com/document_detail/97929.html 云监控cloudmonitor 1. ...

  3. .net WCF WF4.5

    花了两天时间学习使用WF,把一些遇到的问题记录下来,使用的环境是VS2017,网上的资料普遍太老了 需要注意,如果使用多项目同时启动的方式需要把WCF调整到WF启动顺序之上 1.怎么使用代码活动 新建 ...

  4. WebSocket推送

    本篇博客只是记录websocket在自己的项目中的应用,只是记录,不做说明(后来替换为GoEasy了). /** * 握手的设置,这其实是为了获取session */ public class Get ...

  5. JSON Support in PostgreSQL and Entity Framework

    JSON 和JSONB的区别(What's difference between JSON and JSONB data type in PosgresSQL?) When should be use ...

  6. Hbase存储思想

  7. java.lang.Comparable 接口 详解

    参考https://blog.csdn.net/itm_hadf/article/details/7432782 http://www.blogjava.net/jjshcc/archive/2011 ...

  8. swagger bug

    https://blog.csdn.net/u011943534/article/details/81778125 处理swagger报错Could not resolve pointer: /def ...

  9. pip 安装 MySQL-python 报错

    报错一:EnvironmentError: mysql_config not found 解决:yum install mysql-devel 报错二:Python.h No such file or ...

  10. php1

    正则表达式 $p = '/name:(\w+\s?\w+)/'; $str = "name:steven jobs"; preg_match($p, $str, $match); ...