题意

题目链接

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. 萝卜保卫战3内购破解+Toast窗口增加(Love版)

    涉及到一些不同的破解的方法,以及不同的破解思路,还有一些重要权限的删除等. 作者:HAI_ 这次目标是经常玩的萝卜保卫战,不知不觉,已经更新到3了.详细分析请参考https://bbs.ichunqi ...

  2. IntelliJ IDEA优秀插件(编程通用)

    一.IntelliJ IDEA开发 最近大部分开发IDE工具都切换到了,所以也花了点心思去找了相关的插件.这里整理的适合各种语言开发的通用插件,也排除掉IntelliJ IDEA自带的常用插件了(有些 ...

  3. javascript 实用工具函数

    整理日常开发中我们常常会使用到的一些工具函数. var utils = (function(){ var fay = {}; // 返回当前时间的毫秒数 fay.getTime = Date.now( ...

  4. Fast Newman-FN算法以及模块度定义介绍

    一.社区的定义 Newman第一次提出模块度定义就是在2004年发表的这篇文章“fast algorithm for community structure in networks”,第一次用量化的公 ...

  5. 2018春招-美团后台开发方向编程题 (python实现)

    第一题:字符串距离 题目: 给出两个相同长度的由字符 a 和 b 构成的字符串,定义它们的距离为对应位置不同的字符的数量.如串”aab”与串”aba”的距离为 2:串”ba”与串”aa”的距离为 1: ...

  6. Mapreduce部署与第三方依赖包管理

    Mapreduce部署是总会涉及到第三方包依赖问题,这些第三方包配置的方式不同,会对mapreduce的部署便捷性有一些影响,有时候还会导致脚本出错.本文介绍几种常用的配置方式: 1. HADOOP_ ...

  7. python基础笔记之注释三种方法

    ---恢复内容开始--- 1,,单行注释  用# 2,多行注释 用 “”” dddd""" 3,较长行虽然分行写但是只是注释,最终显示为一行:用 \ ---恢复内容结束- ...

  8. IE中的userData

    之前做项目时用到了localstorage,但是考虑到浏览器存在IE8以下不兼容问题,所以来介绍以下IE中的userData. 本地存储解决方案很多,比如Flash SharedObject.Goog ...

  9. AndroidStudio生成自己的Compile依赖

    在AndroidStudio中经常使用compile的方式来导入第三方代码,如图所示: 想要拥有自己的依赖,可参考本篇的方法 在github上托管一个带有Android Libray的项目,我们要co ...

  10. nodejs + express 热更新

    以前node中的express框架,每次修改代码之后,都需要重新npm start 才能看到改动的效果,非常麻烦,所以这里引入nodemon模块,实现了不用重启也能自动更新这样的好处 1.全局安装no ...