设为首页 加入收藏

TOP

cf1000F. One Occurrence(线段树 set)
2019-01-06 18:08:35 】 浏览:46
Tags:cf1000F. One Occurrence 线段 set

题意

题目链接

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 */

编程开发网
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇C++重写new和delete,比想像中困难 下一篇BZOJ4337: BJOI2015 树的同构(has..

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容:

array(4) { ["type"]=> int(8) ["message"]=> string(24) "Undefined variable: jobs" ["file"]=> string(32) "/mnt/wp/cppentry/do/bencandy.php" ["line"]=> int(217) }