考虑怎么求一个序列的 。把所有数插入 Trie,在 Trie 上 DP,若 的两个儿子有一个子树是满的那么 ,否则 。但是这个做法无法拓展。
换一种刻画方式,发现可以变成,选择 Trie 上一个叶子,把它到根链所有点删除后,满子树的大小之和。
考虑排列怎么做。对 扫描线,我们希望时刻维护出每个左端点的答案。
对 Trie 上每个叶子,我们维护出一个分段函数,表示若选择它,每个左端点的答案。
在 Trie 上插入 ,如果出现一个满子树,设其出现位置最小值为 ,就枚举它兄弟子树的所有叶子,将其分段函数的 的值加上满子树大小 。这样每个叶子的分段函数的段数是 的。
更新完之后,我们相当于有一些若 则 对一个 chkmax 的操作。总共有 次操作,上数据结构维护至少 的复杂度,无法接受。
优化是只考虑子树的影响,总共有 个不同的答案,对每个答案处理出它的最大左端点,再枚举一下这个点的祖先的兄弟对它产生的影响,相当于是还有 个修改和原本的 个修改归并一下,修改次数从 变成 ,总修改次数变成 。那么剩下的问题就是区间 chkmax,区间历史和。
考虑不是排列怎么做。这时候我们不能暴力更新了。
在 Trie 上插入 的过程,每棵子树的最后出现位置的最小值可能会从 变成 。我们只用处理左端点在 的更新。
于是对于每棵 有变化的子树,找到它的兄弟子树在 时刻的所有不同 ,然后再和祖先的 段贡献归并一下。这一步需要对 Trie 上每棵子树再开一个 Trie 用于求子树所有点的 ,然后维护一个指针扫子树内所有在 时刻出现的数。这里的总修改次数仍然是 。
最后的问题是区间 chkmax,区间历史和。其实可以不用历史和线段树,考虑所有左端点的答案单调不升,区间 chkmax 可以变成区间加,每次用 ODT 或类似的数据结构找到要区间加的区间。然后在 时刻 对 时刻的查询有 的贡献,所以用两棵支持区间加区间求和的树状数组实现即可。
总时间复杂度 。
代码
// Problem: P12478 [集训队互测 2024] Désive
// Contest: Luogu
// URL: https://www.luogu.com.cn/problem/P12478
// Memory Limit: 512 MB
// Time Limit: 6000 ms
//
// Powered by CP Editor (https://cpeditor.org)
#include <bits/stdc++.h>
#define pb emplace_back
#define fst first
#define scd second
#define mkp make_pair
#define mems(a, x) memset((a), (x), sizeof(a))
using namespace std;
using ll = long long;
using ull = unsigned long long;
using db = double;
using ldb = long double;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
namespace IO {
const int maxn = 1 << 20;
char ibuf[maxn], *iS, *iT, obuf[maxn], *oS = obuf;
inline char gc() {
return (iS == iT ? iT = (iS = ibuf) + fread(ibuf, 1, maxn, stdin), (iS == iT ? EOF : *iS++) : *iS++);
}
template<typename T = int>
inline T read() {
char c = gc();
T x = 0;
bool f = 0;
while (c < '0' || c > '9') {
f |= (c == '-');
c = gc();
}
while (c >= '0' && c <= '9') {
x = (x << 1) + (x << 3) + (c ^ 48);
c = gc();
}
return f ? ~(x - 1) : x;
}
inline int reads(char *s) {
char c = gc();
int len = 0;
while (isspace(c)) {
c = gc();
}
while (!isspace(c) && c != EOF) {
s[len++] = c;
c = gc();
}
s[len] = '\0';
return len;
}
inline string reads() {
char c = gc();
string s;
while (isspace(c)) {
c = gc();
}
while (!isspace(c) && c != EOF) {
s += c;
c = gc();
}
return s;
}
inline void flush() {
fwrite(obuf, 1, oS - obuf, stdout);
oS = obuf;
}
struct Flusher {
~Flusher() {
flush();
}
} AutoFlush;
inline void pc(char ch) {
if (oS == obuf + maxn) {
flush();
}
*oS++ = ch;
}
inline void write(char *s) {
for (int i = 0; s[i]; ++i) {
pc(s[i]);
}
}
inline void write(const char *s) {
for (int i = 0; s[i]; ++i) {
pc(s[i]);
}
}
template<typename T>
inline void write(T x) {
static char stk[64], *tp = stk;
if (x < 0) {
x = ~(x - 1);
pc('-');
}
do {
*tp++ = x % 10;
x /= 10;
} while (x);
while (tp != stk) {
pc((*--tp) | 48);
}
}
template<typename T>
inline void writesp(T x) {
write(x);
pc(' ');
}
template<typename T>
inline void writeln(T x) {
write(x);
pc('\n');
}
}
using IO::read;
using IO::reads;
using IO::write;
using IO::pc;
using IO::writesp;
using IO::writeln;
const int maxn = (1 << 20) + 50;
const int maxm = 10000100;
int n, m, type, a[maxn], ch[maxm][2], f[maxm], sz[maxm], stk[maxm], nt, top;
ll ans[maxn];
vector<pii> vq[maxn >> 2];
inline int newnode() {
return top ? stk[top--] : (++nt);
}
inline void delnode(int &u) {
ch[u][0] = ch[u][1] = f[u] = sz[u] = 0;
stk[++top] = u;
u = 0;
}
inline void pushup(int u, int d) {
if (max(f[ch[u][0]], f[ch[u][1]]) == (1 << d)) {
f[u] = f[ch[u][0]] + f[ch[u][1]];
} else {
f[u] = max(f[ch[u][0]], f[ch[u][1]]);
}
}
void insert(int d, int &u, int x) {
if (!u) {
u = newnode();
}
++sz[u];
if (d == -1) {
f[u] = 1;
return;
}
int t = (x >> d) & 1;
insert(d - 1, ch[u][t], x);
pushup(u, d);
}
void erase(int d, int &u, int x) {
if (d == -1) {
if (!(--sz[u])) {
delnode(u);
}
return;
}
int t = (x >> d) & 1;
erase(d - 1, ch[u][t], x);
if (!(--sz[u])) {
delnode(u);
} else {
pushup(u, d);
}
}
int g[maxn], rt[maxn], pt[maxn], b[maxn], c[maxn], tot;
vector<int> vc[maxn];
inline void upd(int x, int y) {
if (!y) {
return;
}
if (!b[x]) {
c[++tot] = x;
}
b[x] = max(b[x], y);
}
inline void work(int d, int v, int &l, int r) {
if (d < n - 1 && l < r) {
int u = v ^ 1, s = 1 << (d + 1);
vector<pii> md;
for (int i = d + 1; i <= n - 2; ++i) {
int t = g[(v >>= 1) ^ 1];
if (l < t) {
s += (1 << (i + 1));
if (t < r) {
md.pb(t, 1 << (i + 1));
}
}
}
sort(md.begin(), md.end());
int p = 0;
while (pt[u] < (int)vc[u].size() && p < (int)md.size() && vc[u][pt[u]] <= r) {
if (vc[u][pt[u]] < md[p].fst) {
upd(vc[u][pt[u]], s + f[rt[u]]);
erase(d, rt[u], a[vc[u][pt[u]]]);
++pt[u];
} else {
upd(md[p].fst, s + f[rt[u]]);
s -= md[p].scd;
++p;
}
}
while (pt[u] < (int)vc[u].size() && vc[u][pt[u]] <= r) {
upd(vc[u][pt[u]], s + f[rt[u]]);
erase(d, rt[u], a[vc[u][pt[u]]]);
++pt[u];
}
while (p < (int)md.size() && md[p].fst <= r) {
upd(md[p].fst, s + f[rt[u]]);
s -= md[p].scd;
++p;
}
upd(r, s + f[rt[u]]);
}
l = r;
}
void calc(int d, int u, int t) {
insert(d, rt[u], a[t]);
vc[u].pb(t);
if (d == -1) {
work(d, u, g[u], t);
return;
}
calc(d - 1, u << 1 | ((a[t] >> d) & 1), t);
work(d, u, g[u], min(g[u << 1], g[u << 1 | 1]));
}
struct BIT {
ll a[maxn], b[maxn];
inline void update(int x, ll d) {
for (int i = x; i <= (1 << n); i += (i & (-i))) {
a[i] += d;
b[i] += x * d;
}
}
inline void update(int l, int r, ll x) {
if (!x) {
return;
}
update(l, x);
update(r + 1, -x);
}
inline ll query(int x) {
ll res = 0;
for (int i = x; i; i -= (i & (-i))) {
res += a[i] * (x + 1) - b[i];
}
return res;
}
inline ll query(int l, int r) {
return query(r) - query(l - 1);
}
} T1, T2;
struct node {
int l, r, x;
node(int _l = 0, int _r = 0, int _x = 0) : l(_l), r(_r), x(_x) {}
};
inline bool operator < (const node &a, const node &b) {
return a.l < b.l || (a.l == b.l && a.r < b.r);
}
set<node> S;
inline auto split(int p) {
if (p == (1 << n) + 1) {
return S.end();
}
auto it = S.lower_bound(node(p));
if (it != S.end() && it->l == p) {
return it;
}
node u = *(--it);
S.erase(it);
S.emplace(u.l, p - 1, u.x);
return S.emplace(p, u.r, u.x).fst;
}
inline void update(int r, int x, int t) {
auto it = prev(split(r + 1));
int l = r + 1;
while (1) {
if (it->x > x) {
break;
}
l = it->l;
T1.update(it->l, it->r, x - it->x);
T2.update(it->l, it->r, 1LL * t * (x - it->x));
it = S.erase(it);
if (it == S.begin()) {
break;
} else {
--it;
}
}
if (l <= r) {
S.emplace(l, r, x);
}
}
void solve() {
n = read();
m = read();
type = read();
for (int i = 1; i <= (1 << n); ++i) {
a[i] = read();
}
for (int i = 1; i <= m; ++i) {
int l = read();
int r = read();
vq[r].pb(l, i);
}
S.emplace(1, 1 << n, 0);
for (int i = 1; i <= (1 << n); ++i) {
calc(n - 1, 1, i);
for (int j = 1; j <= tot; ++j) {
int p = c[j];
update(p, b[p], i);
b[p] = 0;
}
tot = 0;
for (pii p : vq[i]) {
ans[p.scd] = (type == 1 ? T1.query(p.fst, p.fst) : T1.query(p.fst, i) * (i + 1) - T2.query(p.fst, i));
}
}
for (int i = 1; i <= m; ++i) {
writeln(ans[i]);
}
}
int main() {
int T = 1;
// scanf("%d", &T);
while (T--) {
solve();
}
return 0;
}