cf1000F One Occurrence (线段树)
这题我是离线做的
设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 (线段树)的更多相关文章
- cf1000F. One Occurrence(线段树 set)
题意 题目链接 Sol (真后悔没打这场EDU qwq) 首先把询问离线,预处理每个数的\(pre, nxt\),同时线段树维护\(pre\)(下标是\(pre\),值是\(i\)),同时维护一下最大 ...
- HDU-6704 K-th occurrence (后缀自动机father树上倍增建权值线段树合并)
layout: post title: HDU-6704 K-th occurrence (后缀自动机father树上倍增建权值线段树合并) author: "luowentaoaa&quo ...
- Sum of Squares of the Occurrence Counts解题报告(后缀自动机+LinkCutTree+线段树思想)
题目描述 给定字符串\(S(|S|\le10^5)\),对其每个前缀求出如下的统计量: 对该字符串中的所有子串,统计其出现的次数,求其平方和. Sample Input: aaa Sample Out ...
- 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 ...
- HDU - 6704 K-th occurrence (后缀数组+主席树/后缀自动机+线段树合并+倍增)
题意:给你一个长度为n的字符串和m组询问,每组询问给出l,r,k,求s[l,r]的第k次出现的左端点. 解法一: 求出后缀数组,按照排名建主席树,对于每组询问二分或倍增找出主席树上所对应的的左右端点, ...
- K-th occurrence HDU - 6704 (后缀数组+二分线段树+主席树)
大意: 给定串s, q个询问(l,r,k), 求子串s[l,r]的第kk次出现位置. 这是一篇很好的题解: https://blog.csdn.net/sdauguanweihong/article/ ...
- hdu6704 2019CCPC网络选拔赛1003 K-th occurrence 后缀自动机+线段树合并
解题思路: fail树上用权值线段树合并求right/endpos集合,再用倍增找到待查询串对应节点,然后权值线段树求第k大. #include<bits/stdc++.h> using ...
- hdu 6704 K-th occurrence(后缀数组+可持久化线段树)
Problem Description You are given a string S consisting of only lowercase english letters and some q ...
- hdu 4117 -- GRE Words (AC自动机+线段树)
题目链接 problem Recently George is preparing for the Graduate Record Examinations (GRE for short). Obvi ...
随机推荐
- windows浏览器访问虚拟机开的rabbitmq服务,无法访问
根据这个博主的建议 https://blog.csdn.net/csdnliuxin123524/article/details/78207427 换了一个浏览器上火狐浏览器输入“localhost: ...
- python之路--类的约束, 异常处理, MD5, 日志处理
一 . 类的约束 1. 写一个父类,父类中的某个方法要抛出一个异常 NotImplementedError class Base: # 对子类进行了约束. 必须重写该方法 # 以后上班了. 拿到公司代 ...
- 用mescroll实现无限上拉增加数据,下拉刷新数据 (学习笔记)
最近自己做一个web app需要用到上拉查询下页数据,网上看了很多很多帖子,发现并不能快速的套用,总是会出现各种问题无法使用,于是无奈自己跑去看了官方api文档,终于做了出来,至此做个笔记,以后用到可 ...
- 记一次生产mysql数据误操作恢复过程
提示:建议每次对数据库进行修改时都做下备份 注意:以下Mysql开启的是row格式的binlog日志,确定到误操作具体时间可能有些麻烦,默认的格式就能很快找出来.这里开启row的原因是还有一种更快的方 ...
- 织梦后台如何生成站点地图sitemap.xml
第一步在网站根目录建立sitemap.php文件 内容如下: 写一个计划任务文件命名为generate_sitemap.php,放在/plus/task目录里,文件内容如下: <?php//定时 ...
- 使用urllib2+re爬取web网站
应用1,使用urllib2+re爬取淘宝网指定页面的所有图片
- JMeter 连接 sql server
1.安装驱动 http://www.microsoft.com/zh-CN/download/details.aspx?id=11774 下载后解压后复制sqljdbc.jar到 “jmeter的安装 ...
- java 中的Collection
/* *一. Collection?-------->容器! * * 1.来源于java.util包 非常实用的数据结构! * *二. 方法? * * void clear()删除集合中所有元素 ...
- Python 第三方库 cp27、cp35 等文件名的含义(转)
转自 https://blog.csdn.net/lanchunhui/article/details/62417519 转自 https://stackoverflow.com/questions/ ...
- composer 出现You are running Composer with SSL/TLS protection disabled.
开启php的ssl开启 composer config -g -- disable-tls false