bzoj3339 Rmq Problem

传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=3339

【题解】

业界偷懒。

突然发现好像可以主席树啊。。然后就强行上了一波发现确实可以。

第i棵主席树维护[1...i]这个前缀内,某权值区间的“最小出现位置”。

比如

2 3 0 1

那么在rt[4]这棵主席树中,[0,3]最小出现位置为1,是2在第1位;[0,1]最小出现位置为3,是0在第3位。

那么查询(x,y)我们查询rt[y]根据最小出现位置x来在主席树上二分走即可。

复杂度O(nlog(ai))

似乎可以线段树啊先mark

# include <stdio.h>
# include <string.h>
# include <algorithm>
// # include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
const int M = 5e5 + 10;
const int mod = 1e9+7;

# define RG register
# define ST static

int n, m;

int rt[M];
namespace CMT {
    const int M = 6e6 + 10;
    int s[M], ch[M][2], siz;
    inline void change(int &x, int y, int l, int r, int pos, int va) {
        x = ++siz; ch[x][0] = ch[y][0]; ch[x][1] = ch[y][1];
        if(l==r) {
            s[x] = va;
            return ;
        }
        int mid = l+r>>1;
        if(pos <= mid) change(ch[x][0], ch[y][0], l, mid, pos, va);
        else change(ch[x][1], ch[y][1], mid+1, r, pos, va);
        s[x] = min(s[ch[x][0]], s[ch[x][1]]);
    }
    inline int mex(int x, int l, int r, int pos) {
        if(l == r) return l;
        int mid = l+r>>1;
        if(s[ch[x][0]] >= pos) return mex(ch[x][1], mid+1, r, pos);
        else return mex(ch[x][0], l, mid, pos);
    }
}


int main() {
    scanf("%d%d", &n, &m);
    for (int i=1, t; i<=n; ++i) {
        scanf("%d", &t);
        CMT::change(rt[i], rt[i-1], 0, 1e9, t, i);
    }
    while(m--) {
        int x, y; scanf("%d%d", &x, &y);
        printf("%d\n", CMT::mex(rt[y], 0, 1e9, x));
    }
    return 0;
}
View Code

 

posted @ 2017-04-29 00:12  Galaxies  阅读(274)  评论(0编辑  收藏  举报