题意

题目链接

Sol

(真后悔没打这场EDU qwq)

首先把询问离线,预处理每个数的\(pre, nxt\),同时线段树维护\(pre\)(下标是\(pre\),值是\(i\)),同时维护一下最大值

那么每次在\((1, l - 1)\)内查询最大值,如果最大值\(>= l\),那么说明合法

但是\(pre\)可能会有相同的情况(0),直接开个set维护一下

然后用vector对\(nxt\)维护一个类似差分的东西,在\(nxt_i\)的位置删除掉\(i\)的影响

// luogu-judger-enable-o2
/*
*/
#include<bits/stdc++.h>
#define LL long long
#define Pair pair<int, int>
#define MP(x, y) make_pair(x, y)
#define fi first
#define se second
using namespace std;
const int MAXN = 2e6 + 10;
template <typename A, typename B> inline bool chmin(A &a, B b){if(a > b) {a = b; return 1;} return 0;}
template <typename A, typename B> inline bool chmax(A &a, B b){if(a < b) {a = b; return 1;} return 0;}
inline int read() {
char c = getchar(); int x = 0, f = 1;
while(c < '0' || c > '9') {if(c == '-') f = -1; c = getchar();}
while(c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar();
return x * f;
} int N, M, a[MAXN], pre[MAXN], nxt[MAXN], ans[MAXN], date[MAXN], num = 0;
vector<Pair> q[MAXN];
void Des() {
for(int i = 1; i <= N; i++) date[i] = a[i];
sort(date + 1, date + N + 1);
num = unique(date + 1, date + N + 1) - date - 1;
for(int i = 1; i <= N; i++) a[i] = lower_bound(date + 1, date + num + 1, a[i]) - date;
}
void Get() {
static int las[MAXN];
for(int i = 1; i <= N; i++) pre[i] = las[a[i]], las[a[i]] = i;
for(int i = 1; i <= N; i++) las[i] = N + 1;
for(int i = N; i >= 1; i--) nxt[i] = las[a[i]], las[a[i]] = i;
}
#define Getmid ((T[k].l + T[k].r) >> 1)
#define ls k << 1
#define rs k << 1 | 1
struct Node {
int l, r, mx;
}T[MAXN];
void update(int k) {
T[k].mx = max(T[ls].mx, T[rs].mx);
}
void Build(int k, int ll, int rr) {
T[k].l = ll; T[k].r = rr; T[k].mx = 0;
if(ll == rr) return ;
int mid = Getmid;
Build(ls, ll, mid); Build(rs, mid + 1, rr);
}
void Modify(int k, int pos, int v) {
if(T[k].l == T[k].r) {T[k].mx = v; return ;}
int mid = Getmid;
if(pos <= mid) Modify(ls, pos, v);
if(pos > mid) Modify(rs, pos, v);
update(k);
}
int Query(int k, int ll, int rr) {
if(ll <= T[k].l && T[k].r <= rr) return T[k].mx;
int mid = Getmid, ans = 0;
if(ll <= mid) chmax(ans, Query(ls, ll, rr));
if(rr > mid) chmax(ans, Query(rs, ll, rr));
return ans;
}
#undef ls
#undef rs
#undef Getmid
void Solve() {
set<int> s;
static vector<int> v[MAXN];
for(int i = 1; i <= N; i++) {
for(int j = 0; j < v[i].size(); j++) {
if(!pre[v[i][j]])
s.erase(v[i][j]);
else Modify(1, pre[v[i][j]], 0);
}
if(!pre[i])
s.insert(i);
else Modify(1, pre[i], i); v[nxt[i]].push_back(i);
for(int j = 0; j < q[i].size(); j++) {
int t = Query(1, 1, q[i][j].fi - 1);
if(t >= q[i][j].fi) ans[q[i][j].se] = date[a[t]];
if(!s.empty()) {
set<int>::iterator it = s.end(); it--;
if(*it >= q[i][j].fi) ans[q[i][j].se] = date[a[*it]];
} } }
}
signed main() {
N = read();
for(int i = 1; i <= N; i++) a[i] = read();
Des();
Get();
Build(1, 1, N + 1);
M = read();
for(int i = 1; i <= M; i++) {
int l = read(), r = read();
q[r].push_back(MP(l, i));
}
Solve();
for(int i = 1; i <= M; i++) printf("%d\n", ans[i]);
return 0;
}
/*
5
1 2 2 1 1
2
1 5
2 3 10
5 9 6 4 8 7 4 9 7 6
1
4 8
*/

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

  1. cf1000F One Occurrence (线段树)

    这题我是离线做的 设i位置的数上次出现的位置是pre[i](如果第一次出现那就是0) 可以想到,用线段树维护一个区间的pre的最小值,如果它小于区间左端点,那这个数就是一个合法的答案 但直接这样做是错 ...

  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. JS 对象 合并

    来自:https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Object/assign#Sy ...

  2. Chrome谷歌浏览器已停用不支持的扩展程序解决方法

    在不能上外网的情况下解决该问题: http://www.liu16.com/post/Chrome_2447.html

  3. JMeter基础:请求参数Parameters 、Body Data的区别

    使用Jmeter测试时,很多人不知道请求参数Parameters .Body Data的区别和用途,这里简单介绍下 先了解一个接口的基本概念 在客户机和服务器之间进行请求-响应时,HTTP协议中包括G ...

  4. 二:MyBatis学习总结(二)——使用MyBatis对表执行CRUD操作

    上一篇博文MyBatis学习总结(一)——MyBatis快速入门中我们讲了如何使用Mybatis查询users表中的数据,算是对MyBatis有一个初步的入门了,今天讲解一下如何使用MyBatis对u ...

  5. POJ 1082

    #include <iostream> using namespace std; int main() { //freopen("acm.acm","r&qu ...

  6. Python3之hashlib

    简介: 用于加密相关的操作,代替了md5模块和sha模块,主要提供SHA1,SHA224,SHA256,SHA384,SHA512,MD5算法. 在python3中已经废弃了md5和sha模块,简单说 ...

  7. Django的sitemap

    1.安装 安装Sitemap APP的步骤如下: 在INSTALLED_APPS设置中添加'django.contrib.sitemaps'; 确认你的TEMPLATES设置中包含DjangoTemp ...

  8. strcpy,memcpy,memset函数实现

    strcpy 实现,只能拷贝字符串 char* strcpy(char* des,const char* source) { char* r=des; assert((des != NULL) &am ...

  9. Explorer内存占用偶尔变高导致卡顿

    症状: 打开 "这台电脑",加载缓慢.此时查看任务管理器,explorer内存可能飙升到几G.cpu也很高 创建和删除文件缓慢,删除单个文件也会出现进度条.此时查看任务管理器,会出 ...

  10. 《垃圾回收的算法与实现》——GC复制算法

    基本概念 GC复制算法将堆分成From和To两个内存块,当From被占满时GC将From中的存活对象复制到To中,同时将From和To交换. 通过递归遍历GC root(即采用深度优先)复制存活对象, ...