题解 P3834 【【模板】可持久化线段树 1(主席树)】
大家好,我是个毒瘤,我非常喜欢暴力数据结构,于是我就用莫队+分块过了这个题,而且跑的贼快,甚至跑到了最优解的第二页
Solution
发现这个题静态查询资瓷离线,于是考虑莫队。
在这里简单介绍一下莫队:
将所有询问离线后,对原序列分块。按照左端点所在块单调不降排序。当左端点所在块相同时,按照右端点单调排序。
然后用头尾指针指向当前的区间,维护区间内的信息。每两个查询间暴力移动指针。移动指针时每移动一下就维护一次答案。
考虑这么做的复杂度:一共有
维护答案时,最显然的想法是用树状数组维护前缀和,这样单次修改复杂度
Code
#include <cmath>
#include <cstdio>
#include <algorithm>
#ifdef ONLINE_JUDGE
#define freopen(a, b, c)
#endif
#define rg register
#define ci const int
#define cl const long long
typedef long long int ll;
namespace IPT {
const int L = 10000000;
char buf[L], *front=buf, *end=buf;
char GetChar() {
if (front == end) {
end = buf + fread(front = buf, 1, L, stdin);
if (front == end) return -1;
}
return *(front++);
}
}
template <typename T>
inline void qr(T &x) {
rg char ch = IPT::GetChar(), lst = ' ';
while ((ch > '9') || (ch < '0')) lst = ch, ch=IPT::GetChar();
while ((ch >= '0') && (ch <= '9')) x = (x << 1) + (x << 3) + (ch ^ 48), ch = IPT::GetChar();
if (lst == '-') x = -x;
}
template <typename T>
inline void ReadDb(T &x) {
rg char ch = IPT::GetChar(), lst = ' ';
while ((ch > '9') || (ch < '0')) lst = ch, ch = IPT::GetChar();
while ((ch >= '0') && (ch <= '9')) x = x * 10 + (ch ^ 48), ch = IPT::GetChar();
if (ch == '.') {
ch = IPT::GetChar();
double base = 1;
while ((ch >= '0') && (ch <= '9')) x += (ch ^ 48) * ((base *= 0.1)), ch = IPT::GetChar();
}
if (lst == '-') x = -x;
}
namespace OPT {
char buf[120];
}
template <typename T>
inline void qw(T x, const char aft, const bool pt) {
if (x < 0) {x = -x, putchar('-');}
rg int top=0;
do {OPT::buf[++top] = x % 10 + '0';} while ( x /= 10);
while (top) putchar(OPT::buf[top--]);
if (pt) putchar(aft);
}
const int maxn = 200010;
int n, m;
int belong[maxn], MU[maxn], temp[maxn], bk[maxn], block[maxn], rmp[maxn], lc[maxn];
struct Ask {
int l, r, id, ans, k;
inline bool operator<(const Ask &_others) const {
if (belong[this->l] != belong[_others.l]) return this->l < _others.l;
if (belong[this->l] & 1) return this->r < _others.r;
return this->r > _others.r;
}
};
Ask ask[maxn];
void init_hash();
void add(ci&);
void dlt(ci&);
inline bool cmp(const Ask &_a,const Ask &_b) {
return _a.id < _b.id;
}
int main() {
freopen("1.in", "r", stdin) ;
qr(n); qr(m);
for (rg int i = 1, sn = sqrt(n); i <= n; ++i) if((belong[i] = i / sn) != belong[i-1]) lc[belong[i]] = i;
for (rg int i = 1; i <= n; ++i) qr(MU[i]);
init_hash();
for (rg int i = 1; i <= m; ++i) {
qr(ask[i].l); qr(ask[i].r); qr(ask[i].k); ask[i].id = i;
}
std::sort(ask + 1, ask + 1 + m);
int prel = ask[1].l, prer = prel - 1;
for (rg int i = 1; i <= m; ++i) {
int l = ask[i].l, r = ask[i].r;
while (prel < l) dlt(prel++);
while (prel > l) add(--prel);
while (prer > r) dlt(prer--);
while (prer < r) add(++prer);
int _cnt = 0, cur = 0;
while (_cnt + block[cur] < ask[i].k) _cnt+=block[cur++];
for (rg int j = lc[cur]; ; ++j) if((_cnt += bk[j]) >= ask[i].k) {
ask[i].ans = j; break;
}
}
std::sort(ask + 1, ask + 1 + m, cmp);
for (rg int i = 1; i <= m; ++i) qw(rmp[ask[i].ans], '\n', true);
return 0;
}
void init_hash() {
for (rg int i = 1; i <= n; ++i) temp[i] = MU[i];
std::sort(temp + 1, temp + 1 + n);
int *ed = std::unique(temp + 1, temp + 1 + n);
for (rg int i = 1; i <= n; ++i) {
int k = MU[i];
rmp[MU[i] = std::lower_bound(temp + 1, ed, MU[i]) - temp] = k;
}
}
inline void dlt(ci &k) {
--bk[MU[k]];
--block[belong[MU[k]]];
}
inline void add(ci &k) {
++bk[MU[k]];
++block[belong[MU[k]]];
}
对了顺便把主席树代码放上防止有人喷我不自觉
#include <cstdio>
#include <algorithm>
#ifdef ONLINE_JUDGE
#define freopen(a, b, c)
#endif
#define rg register
#define ci const int
#define cl const long long
typedef long long int ll;
namespace IPT {
const int L = 1000000;
char buf[L], *front=buf, *end=buf;
char GetChar() {
if (front == end) {
end = buf + fread(front = buf, 1, L, stdin);
if (front == end) return -1;
}
return *(front++);
}
}
template <typename T>
inline void qr(T &x) {
rg char ch = IPT::GetChar(), lst = ' ';
while ((ch > '9') || (ch < '0')) lst = ch, ch=IPT::GetChar();
while ((ch >= '0') && (ch <= '9')) x = (x << 1) + (x << 3) + (ch ^ 48), ch = IPT::GetChar();
if (lst == '-') x = -x;
}
template <typename T>
inline void ReadDb(T &x) {
rg char ch = IPT::GetChar(), lst = ' ';
while ((ch > '9') || (ch < '0')) lst = ch, ch = IPT::GetChar();
while ((ch >= '0') && (ch <= '9')) x = x * 10 + (ch ^ 48), ch = IPT::GetChar();
if (ch == '.') {
ch = IPT::GetChar();
double base = 1;
while ((ch >= '0') && (ch <= '9')) x += (ch ^ 48) * ((base *= 0.1)), ch = IPT::GetChar();
}
if (lst == '-') x = -x;
}
namespace OPT {
char buf[120];
}
template <typename T>
inline void qw(T x, const char aft, const bool pt) {
if (x < 0) {x = -x, putchar('-');}
rg int top=0;
do {OPT::buf[++top] = x % 10 + '0';} while ( x /= 10);
while (top) putchar(OPT::buf[top--]);
if (pt) putchar(aft);
}
const int maxn = 200010;
const int maxt = 4000010;
int n, m, sz;
int MU[maxn], temp[maxn], rmp[maxn];
struct Tree {
Tree *ls, *rs;
int l, r, v, k;
inline void update() {
this->v = 0;
if(this->ls) this->v = this->ls->v;
if(this->rs) this->v += this->rs->v;
}
};
Tree *pool[maxt], qwq[maxt], *rot[maxn];
int pltp;
void init_hash();
void buildpool();
void buildzero(Tree*, ci, ci);
void build(Tree*, Tree*, ci, ci, ci);
int ask(Tree*, Tree*, ci);
int main() {
freopen("1.in", "r", stdin);
qr(n); qr(m);
for (rg int i = 1; i <= n; ++i) qr(MU[i]);
init_hash();
buildpool();
rot[0] = pool[pltp--];
buildzero(rot[0], 1, sz);
for (rg int i = 1; i <= n; ++i) {
rot[i] = pool[pltp--];
build(rot[i-1], rot[i], 1, sz, MU[i]);
}
int a, b, c;
while(m--) {
a = b = c = 0;
qr(a); qr(b); qr(c);
qw(rmp[ask(rot[a-1], rot[b], c)], '\n', true);
}
return 0;
}
void init_hash() {
for (rg int i = 1; i <= n; ++i) temp[i] = MU[i];
std::sort(temp + 1, temp + 1 + n);
int *ed = std::unique(temp + 1, temp + 1 + n);
for (rg int i = 1; i <= n; ++i) {
int _tp = MU[i];
rmp[MU[i] = std::lower_bound(temp + 1, ed, MU[i]) - temp] = _tp;
}
sz = ed - temp - 1;
}
void buildpool() {
for (rg int i = 0; i < maxt; ++i) pool[i] = qwq + i;
pltp = maxt - 1;
}
void buildzero(Tree *u, ci l, ci r) {
u->l = l; u->r = r;
if (l == r) return;
int mid = (l + r) >> 1;
if (l <= mid) {
u->ls = pool[pltp--];
buildzero(u->ls, l, mid);
}
if (mid < r) {
u->rs = pool[pltp--];
buildzero(u->rs, mid+1, r);
}
}
void build(Tree *pre, Tree *u, ci l, ci r, ci v) {
u->l = l; u->r = r;
if (l == r) {u->v = pre->v + 1;return;}
int mid = (l + r) >> 1;
if (v <= mid) {
u->rs = pre->rs;
u->ls = pool[pltp--];
build(pre->ls, u->ls, l, mid, v);
} else {
u->ls = pre->ls;
u->rs = pool[pltp--];
build(pre->rs, u->rs, mid + 1, r, v);
}
u->update();
}
int ask(Tree *pre, Tree *u, ci k) {
if(u->l == u->r) return u->l;
int _v = u->ls ? u->ls->v - pre->ls->v : 0;
return _v < k ? ask(pre->rs, u->rs, k - _v) : ask(pre->ls, u->ls, k);
}