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 ...
随机推荐
- HTML,CSS笔记
text-indent 属性规定文本块中首行文本的缩进.允许使用负值.如果使用负值,那么首行会被缩进到左边.p{ text-indent:50px; } HTML <label> 标签的 ...
- MYSQL 创建数据库SQL
CREATE DATABASE crm CHARACTER SET utf8 COLLATE utf8_general_ci; MySQL :: MySQL 5.7 Reference Manual ...
- ubuntu使用squid搭建代理
安装squid //检查是否安装squid which squid // apt update sudo apt install squid 配置squid的配置文件squid.conf //备份sq ...
- 简要了解 MySql 5.5/5.6/5.7/8 出现的新特性
MySQL的开发周期 在比较之前,首先提一下MySQL的开发周期. MySQL一个大版本的开发,大致经历如下几个阶段: Feature Development Feature Testing Perf ...
- java中的定时任务小示例
package package_1; import java.text.SimpleDateFormat; import java.util.Date; import java.util.Timer; ...
- JSON Support in PostgreSQL and Entity Framework
JSON 和JSONB的区别(What's difference between JSON and JSONB data type in PosgresSQL?) When should be use ...
- python中random模块
random与numpy.random对比: 1.random.random():生成[0,1)之间的随机浮点数: numpy.random.random():生成[0,1)之间的随机浮点数: num ...
- 结巴(jieba)分词
一.介绍: jieba: “结巴”中文分词:做最好的 Python 中文分词组件 “Jieba” (Chinese for “to stutter”) Chinese text segmentatio ...
- ArcGIS DeskTop 10.2 的安装与破解
ArcGIS DeskTop 10.2套件作为一组常用的ArcGIS软件为我们提供了对地图原始数据进行加工以及各种操作,通过这组软件我们能够很好地定制我们最终的地图样式,但是更多的时候我们需要对这组软 ...
- 莫烦theano学习自修第八天【分类问题】
1. 代码实现 from __future__ import print_function import numpy as np import theano import theano.tensor ...