ABC339_g Smaller Sum 题解
题目链接:Atcoder 或者 洛谷
比价朴素的题,首先有暴力的想法就是树套树或者分块。这两种就不再赘述,这里来正式提提 主席树 的本质而不再停留在板题找第 \(k\) 大上。
对于可差性问题和传统问题不同,我们对于可差性问题往往都有更好的优化方案。例如对于树类问题来说,可差性问题我们的 主席树 其实是可以算作能够实现树套树的常见功能的一种更优的结构,只是它只能解决的普遍问题需要基于可差性,当然它的实现方式是基于持久化的,所以它也是一类可持久化的线段树。对于莫队来说,可差性问题,往往我们都可以使用莫队二次离线进行优化。
什么是可差性问题,传统一点解释就是一个问题的答案可以转化为两个其他问题的答案做差。比如区间和问题,我们求 \([l,r]\) 区间和可以由 \([0,r]\) 的和减去 \([0,l-1]\) 的和做差。当然了,再列举一些其他可差性问题,例如 \([l,r]\) 中 \(\ge x\) 的数量,显然也可以转化为两个区间差。\([l,r]\) 上 \(\le x\) 的和,等等,这些则是莫二的常见模型了。
主席树 普遍应用上可以当做一个可以解决可差性问题的树套树。当然常说的是静态 主席树,不能带修改,如果带修改,我们可以外层套个 \(BIT\) 去维护 \(logn\) 个权值树,实现方式还是基于可持久化,所以也可以将这类可差性带修树套树当做动态 主席树。
简单阐述下原理:
如图所示,每次开辟一个新的该节点,会获得对应原来节点的信息,同时还能够继承原有除了这条链的其他点。可以看见蓝色的新数是基于原来的橙色链得到的,它们共用了一部分信息,这部分信息并不属于这条链上的。除此之外,这条蓝色的链基于橙色的链来说,会多出一个当前问题的答案,比如数量增加,和增加等等。我们为每个点在增加一条新链基于上一个版本,那么很轻易的可以拿到 \(1 \sim curr\) 当前的线段树情况即为 \(root[curr]\) 为根的线段树。而一个 \([l,r]\) 区间上的问问题,我们通过两棵线段树,\(1\sim l-1\) 与 \(1 \sim r\) 的两棵权值线段树不断往下走作差即可拿到子区间答案。例如已经知道它们的 \([L,mid]\) 的数量和或者值总和,作差显然就是我们所需区间的 \([L,mid]\) 上的情况答案。大白话解释下:对于单点叶子结点而言,每个点都是 \(1\sim r\) 或者 \(1\sim l-1\) 上这个点的数量总和, 单点问题其实就是一个简单区间和问题转化为前缀和作差问题。单点问题映射到区间问题上是同理的,一系列单点问题同时拿到答案。这点建议反复体会,后期我会专门出一系列教学篇关于各种 \(ds\) 的教学,本处只是提提大致思想原理。
回到本题
常见的有人会 \(build\) 一棵空树,然后上面所有点都是 \(0\) 模拟 \(pre[0]\) 为 \(0\) 的情况。其实在如果你的第一棵空树如果值全是 \(0\) 的情况下并不需要 \(build\) 的。本题值域比较大,离散化之类的其实是减少权值线段树的常数,众所周知每次无论修改还是访问线段树,复杂度都是 \(O(\log{root_{len}})\),\(root_{len}\) 为根的区间长度。对于权值线段树而言,我们这里当做 \(0 \sim 1e9\),那显然这个 \(log\) 即为 \(\log{1e9}\) 级别,离散化则会减小到 \(\log{2e5}\) 级别。其实是常数上的差异。所以不离散化的情况下,数组开大点,防止动态开点数量不够,大概本题开个 \(2\) 倍常数就行了吧,差不多 \(60n\) 的感觉,剩余的 主席树 上每个权值树不再维护前缀数量了,而是维护前缀和。这样一来查询其实相当于 主席树 的权值树上找 \(\le x\) 的在 \([l,r]\) 上的总和。是不是这个问题我们也可以用线段树套权值树直接解决,但复杂度则为两支 \(log\),分别为找到特定的子区间线段树节点后再在这个线段树节点对应的权值树上查询。而这类可差性问题则可以被 主席树 优化到单支 \(log\),其实有线段树上二分那个味道了。(常常的由于有个 \(l-1\),所以我们主席树起点是 \(0\) 处的空树,如果空树表示的所有信息都是 \(0\),那么就并不需要 build 出来,直接完全当做 \(0\)就行)
第一种解法
参照代码
#include <bits/stdc++.h>
// #pragma GCC optimize(2)
// #pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math")
// #pragma GCC target("sse,sse2,sse3,ssse3,sse4.1,sse4.2,avx,avx2,popcnt,tune=native")
#define isPbdsFile
#ifdef isPbdsFile
#include <bits/extc++.h>
#else
#include <ext/pb_ds/priority_queue.hpp>
#include <ext/pb_ds/hash_policy.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/trie_policy.hpp>
#include <ext/pb_ds/tag_and_trait.hpp>
#include <ext/pb_ds/hash_policy.hpp>
#include <ext/pb_ds/list_update_policy.hpp>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/exception.hpp>
#include <ext/rope>
#endif
using namespace std;
using namespace __gnu_cxx;
using namespace __gnu_pbds;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef tuple<int, int, int> tii;
typedef tuple<ll, ll, ll> tll;
typedef unsigned int ui;
typedef unsigned long long ull;
typedef __int128 i128;
#define hash1 unordered_map
#define hash2 gp_hash_table
#define hash3 cc_hash_table
#define stdHeap std::priority_queue
#define pbdsHeap __gnu_pbds::priority_queue
#define sortArr(a, n) sort(a+1,a+n+1)
#define all(v) v.begin(),v.end()
#define yes cout<<"YES"
#define no cout<<"NO"
#define Spider ios_base::sync_with_stdio(false);cin.tie(nullptr);cout.tie(nullptr);
#define MyFile freopen("..\\input.txt", "r", stdin),freopen("..\\output.txt", "w", stdout);
#define forn(i, a, b) for(int i = a; i <= b; i++)
#define forv(i, a, b) for(int i=a;i>=b;i--)
#define ls(x) (x<<1)
#define rs(x) (x<<1|1)
#define endl '\n'
//用于Miller-Rabin
[[maybe_unused]] static int Prime_Number[13] = {0, 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37};
template <typename T>
int disc(T* a, int n)
{
return unique(a + 1, a + n + 1) - (a + 1);
}
template <typename T>
T lowBit(T x)
{
return x & -x;
}
template <typename T>
T Rand(T l, T r)
{
static mt19937 Rand(time(nullptr));
uniform_int_distribution<T> dis(l, r);
return dis(Rand);
}
template <typename T1, typename T2>
T1 modt(T1 a, T2 b)
{
return (a % b + b) % b;
}
template <typename T1, typename T2, typename T3>
T1 qPow(T1 a, T2 b, T3 c)
{
a %= c;
T1 ans = 1;
for (; b; b >>= 1, (a *= a) %= c)if (b & 1)(ans *= a) %= c;
return modt(ans, c);
}
template <typename T>
void read(T& x)
{
x = 0;
T sign = 1;
char ch = getchar();
while (!isdigit(ch))
{
if (ch == '-')sign = -1;
ch = getchar();
}
while (isdigit(ch))
{
x = (x << 3) + (x << 1) + (ch ^ 48);
ch = getchar();
}
x *= sign;
}
template <typename T, typename... U>
void read(T& x, U&... y)
{
read(x);
read(y...);
}
template <typename T>
void write(T x)
{
if (typeid(x) == typeid(char))return;
if (x < 0)x = -x, putchar('-');
if (x > 9)write(x / 10);
putchar(x % 10 ^ 48);
}
template <typename C, typename T, typename... U>
void write(C c, T x, U... y)
{
write(x), putchar(c);
write(c, y...);
}
template <typename T11, typename T22, typename T33>
struct T3
{
T11 one;
T22 tow;
T33 three;
bool operator<(const T3 other) const
{
if (one == other.one)
{
if (tow == other.tow)return three < other.three;
return tow < other.tow;
}
return one < other.one;
}
T3() { one = tow = three = 0; }
T3(T11 one, T22 tow, T33 three) : one(one), tow(tow), three(three)
{
}
};
template <typename T1, typename T2>
void uMax(T1& x, T2 y)
{
if (x < y)x = y;
}
template <typename T1, typename T2>
void uMin(T1& x, T2 y)
{
if (x > y)x = y;
}
constexpr int N = 2e5 + 10;
struct Node
{
ll sum;
int left, right;
} node[N * 100];
#define left(x) node[x].left
#define right(x) node[x].right
#define sum(x) node[x].sum
int cnt;
constexpr int MX = 1e9;
inline void add(const int pre, int& curr, const int pos, const int l = 0, const int r = MX)
{
node[curr = ++cnt] = node[pre];
sum(curr) += pos;
const int mid = l + r >> 1;
if (l == r)return;
if (pos <= mid)add(left(pre),left(curr), pos, l, mid);
else add(right(pre),right(curr), pos, mid + 1, r);
}
inline ll query(const int lTree, const int rTree, const int pos, const int l = 0, const int r = MX)
{
if (l == r)return sum(rTree) - sum(lTree);
ll leftSum = sum(left(rTree)) - sum(left(lTree));
const int mid = l + r >> 1;
if (pos <= mid)return query(left(lTree),left(rTree), pos, l, mid);
return leftSum + query(right(lTree),right(rTree), pos, mid + 1, r);
}
int n, q;
ll a[N];
int root[N];
ll last;
inline void solve()
{
cin >> n;
forn(i, 1, n)cin >> a[i], add(root[i - 1], root[i], a[i]);
cin >> q;
while (q--)
{
ll l, r, x;
cin >> l >> r >> x;
l ^= last, r ^= last, x ^= last;
cout << (last = query(root[l - 1], root[r], x)) << endl;
}
}
signed int main()
{
// MyFile
Spider
//------------------------------------------------------
// clock_t start = clock();
int test = 1;
// read(test);
// cin >> test;
forn(i, 1, test)solve();
// while (cin >> n, n)solve();
// while (cin >> test)solve();
// clock_t end = clock();
// cerr << "time = " << double(end - start) / CLOCKS_PER_SEC << "s" << endl;
}
补一个分块咋写
这题不带修,所以不需要考虑值域分块。为啥很多人都认为这个应该和教主的魔法,模版题差不多为块内有序二分,但是最优块长却取的 \(\sqrt{n}\),这样不还需要带个常数 \(\log{\sqrt{n}}\) 吗?这玩意在分块里面常数可不小,后面说说咋做。这里先讲讲其他分块的做法怎么做。平常维护的都是序列前缀和,我们可以维护值域上的前缀和,就是 \(<= x\) 的和。具体的预处理出每个序列块的值域前缀和就结束了,然后离散化下,常数优化上看个人一些了,这里给出一份朴素代码。
第二种解法
参照代码
#include <bits/stdc++.h>
// #pragma GCC optimize(2)
// #pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math")
// #pragma GCC target("sse,sse2,sse3,ssse3,sse4.1,sse4.2,avx,avx2,popcnt,tune=native")
#define isPbdsFile
#ifdef isPbdsFile
#include <bits/extc++.h>
#else
#include <ext/pb_ds/priority_queue.hpp>
#include <ext/pb_ds/hash_policy.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/trie_policy.hpp>
#include <ext/pb_ds/tag_and_trait.hpp>
#include <ext/pb_ds/hash_policy.hpp>
#include <ext/pb_ds/list_update_policy.hpp>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/exception.hpp>
#include <ext/rope>
#endif
using namespace std;
using namespace __gnu_cxx;
using namespace __gnu_pbds;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef tuple<int, int, int> tii;
typedef tuple<ll, ll, ll> tll;
typedef unsigned int ui;
typedef unsigned long long ull;
typedef __int128 i128;
#define hash1 unordered_map
#define hash2 gp_hash_table
#define hash3 cc_hash_table
#define stdHeap std::priority_queue
#define pbdsHeap __gnu_pbds::priority_queue
#define sortArr(a, n) sort(a+1,a+n+1)
#define all(v) v.begin(),v.end()
#define yes cout<<"YES"
#define no cout<<"NO"
#define Spider ios_base::sync_with_stdio(false);cin.tie(nullptr);cout.tie(nullptr);
#define MyFile freopen("..\\input.txt", "r", stdin),freopen("..\\output.txt", "w", stdout);
#define forn(i, a, b) for(int i = a; i <= b; i++)
#define forv(i, a, b) for(int i=a;i>=b;i--)
#define ls(x) (x<<1)
#define rs(x) (x<<1|1)
#define endl '\n'
//用于Miller-Rabin
[[maybe_unused]] static int Prime_Number[13] = {0, 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37};
template <typename T>
int disc(T* a, int n)
{
return unique(a + 1, a + n + 1) - (a + 1);
}
template <typename T>
T lowBit(T x)
{
return x & -x;
}
template <typename T>
T Rand(T l, T r)
{
static mt19937 Rand(time(nullptr));
uniform_int_distribution<T> dis(l, r);
return dis(Rand);
}
template <typename T1, typename T2>
T1 modt(T1 a, T2 b)
{
return (a % b + b) % b;
}
template <typename T1, typename T2, typename T3>
T1 qPow(T1 a, T2 b, T3 c)
{
a %= c;
T1 ans = 1;
for (; b; b >>= 1, (a *= a) %= c)if (b & 1)(ans *= a) %= c;
return modt(ans, c);
}
template <typename T>
void read(T& x)
{
x = 0;
T sign = 1;
char ch = getchar();
while (!isdigit(ch))
{
if (ch == '-')sign = -1;
ch = getchar();
}
while (isdigit(ch))
{
x = (x << 3) + (x << 1) + (ch ^ 48);
ch = getchar();
}
x *= sign;
}
template <typename T, typename... U>
void read(T& x, U&... y)
{
read(x);
read(y...);
}
template <typename T>
void write(T x)
{
if (typeid(x) == typeid(char))return;
if (x < 0)x = -x, putchar('-');
if (x > 9)write(x / 10);
putchar(x % 10 ^ 48);
}
template <typename C, typename T, typename... U>
void write(C c, T x, U... y)
{
write(x), putchar(c);
write(c, y...);
}
template <typename T11, typename T22, typename T33>
struct T3
{
T11 one;
T22 tow;
T33 three;
bool operator<(const T3 other) const
{
if (one == other.one)
{
if (tow == other.tow)return three < other.three;
return tow < other.tow;
}
return one < other.one;
}
T3() { one = tow = three = 0; }
T3(T11 one, T22 tow, T33 three) : one(one), tow(tow), three(three)
{
}
};
template <typename T1, typename T2>
void uMax(T1& x, T2 y)
{
if (x < y)x = y;
}
template <typename T1, typename T2>
void uMin(T1& x, T2 y)
{
if (x > y)x = y;
}
constexpr int N = 2e5 + 10;
ll a[N], mp[N], mx;
hash2<int, int> idx;
int siz, cnt;
int pos[N], s[N], e[N];
int n, q;
constexpr int SIZE = sqrt(N);
constexpr int CNT = (N + SIZE - 1) / SIZE;
ll pre[CNT + 1][N];
inline ll query(const int l, const int r, const int val)
{
const int L = pos[l], R = pos[r];
if (L == R)
{
ll ans = 0;
forn(i, l, r)ans += a[i] <= val ? mp[a[i]] : 0;
return ans;
}
ll ans = 0;
forn(i, l, e[L])ans += a[i] <= val ? mp[a[i]] : 0;
forn(i, s[R], r)ans += a[i] <= val ? mp[a[i]] : 0;
forn(i, L+1, R-1)ans += pre[i][val];
return ans;
}
ll last;
inline void solve()
{
cin >> n;
siz = sqrt(n), cnt = (n + siz - 1) / siz;
forn(i, 1, n)cin >> a[i], mp[i] = a[i], pos[i] = (i - 1) / siz + 1;
sortArr(mp, n), mx = disc(mp, n);
forn(i, 1, mx)idx[mp[i]] = i;
forn(i, 1, n)a[i] = idx[a[i]];
forn(i, 1, cnt)s[i] = (i - 1) * siz + 1, e[i] = i * siz;
e[cnt] = n;
forn(i, 1, cnt)
{
forn(j, s[i], e[i])pre[i][a[j]] += mp[a[j]];
forn(j, 1, mx)pre[i][j] += pre[i][j - 1];
}
cin >> q;
while (q--)
{
ll l, r, val;
cin >> l >> r >> val;
l ^= last, r ^= last, val ^= last;
val = upper_bound(mp + 1, mp + mx + 1, val) - mp - 1;
cout << (last = query(l, r, val)) << endl;
}
}
signed int main()
{
// MyFile
Spider
//------------------------------------------------------
// clock_t start = clock();
int test = 1;
// read(test);
// cin >> test;
forn(i, 1, test)solve();
// while (cin >> n, n)solve();
// while (cin >> test)solve();
// clock_t end = clock();
// cerr << "time = " << double(end - start) / CLOCKS_PER_SEC << "s" << endl;
}
值域分块优化常数
当然上面这种常规分块是常数比较大的,我们可以用用 \(ynoi\) 当中喜闻乐见的值域分块优化。值域分块维护一波 \(pre[curr][val]\) 表示前 \(curr\) 序列块的前 \(val\) 个值域块前缀和。做两遍前缀和预处理就能得到,分块是某个序列块的值域块前缀和以及序列块的总前缀和。查询就简单了,转化为 \(0\sim r\) 上的 \(\le x\) 和与 \(0 \sim l-1\) 上 \(\le x\) 的和作差。对于一个查询而言,先拿到前一个块对应 \(x\) 的前一个值域块得到总前缀和,然后分别枚举值域单点以及序列单点符合题意的就行了。我们可以不去重,然后让值域块和序列快共用一组分块查询即可。
第三种解法
参照代码
#include <bits/stdc++.h>
// #pragma GCC optimize(2)
// #pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math")
// #pragma GCC target("sse,sse2,sse3,ssse3,sse4.1,sse4.2,avx,avx2,popcnt,tune=native")
#define isPbdsFile
#ifdef isPbdsFile
#include <bits/extc++.h>
#else
#include <ext/pb_ds/priority_queue.hpp>
#include <ext/pb_ds/hash_policy.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/trie_policy.hpp>
#include <ext/pb_ds/tag_and_trait.hpp>
#include <ext/pb_ds/hash_policy.hpp>
#include <ext/pb_ds/list_update_policy.hpp>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/exception.hpp>
#include <ext/rope>
#endif
using namespace std;
using namespace __gnu_cxx;
using namespace __gnu_pbds;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef tuple<int, int, int> tii;
typedef tuple<ll, ll, ll> tll;
typedef unsigned int ui;
typedef unsigned long long ull;
typedef __int128 i128;
#define hash1 unordered_map
#define hash2 gp_hash_table
#define hash3 cc_hash_table
#define stdHeap std::priority_queue
#define pbdsHeap __gnu_pbds::priority_queue
#define sortArr(a, n) sort(a+1,a+n+1)
#define all(v) v.begin(),v.end()
#define yes cout<<"YES"
#define no cout<<"NO"
#define Spider ios_base::sync_with_stdio(false);cin.tie(nullptr);cout.tie(nullptr);
#define MyFile freopen("..\\input.txt", "r", stdin),freopen("..\\output.txt", "w", stdout);
#define forn(i, a, b) for(int i = a; i <= b; i++)
#define forv(i, a, b) for(int i=a;i>=b;i--)
#define ls(x) (x<<1)
#define rs(x) (x<<1|1)
#define endl '\n'
//用于Miller-Rabin
[[maybe_unused]] static int Prime_Number[13] = {0, 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37};
template <typename T>
int disc(T* a, int n)
{
return unique(a + 1, a + n + 1) - (a + 1);
}
template <typename T>
T lowBit(T x)
{
return x & -x;
}
template <typename T>
T Rand(T l, T r)
{
static mt19937 Rand(time(nullptr));
uniform_int_distribution<T> dis(l, r);
return dis(Rand);
}
template <typename T1, typename T2>
T1 modt(T1 a, T2 b)
{
return (a % b + b) % b;
}
template <typename T1, typename T2, typename T3>
T1 qPow(T1 a, T2 b, T3 c)
{
a %= c;
T1 ans = 1;
for (; b; b >>= 1, (a *= a) %= c)if (b & 1)(ans *= a) %= c;
return modt(ans, c);
}
template <typename T>
void read(T& x)
{
x = 0;
T sign = 1;
char ch = getchar();
while (!isdigit(ch))
{
if (ch == '-')sign = -1;
ch = getchar();
}
while (isdigit(ch))
{
x = (x << 3) + (x << 1) + (ch ^ 48);
ch = getchar();
}
x *= sign;
}
template <typename T, typename... U>
void read(T& x, U&... y)
{
read(x);
read(y...);
}
template <typename T>
void write(T x)
{
if (typeid(x) == typeid(char))return;
if (x < 0)x = -x, putchar('-');
if (x > 9)write(x / 10);
putchar(x % 10 ^ 48);
}
template <typename C, typename T, typename... U>
void write(C c, T x, U... y)
{
write(x), putchar(c);
write(c, y...);
}
template <typename T11, typename T22, typename T33>
struct T3
{
T11 one;
T22 tow;
T33 three;
bool operator<(const T3 other) const
{
if (one == other.one)
{
if (tow == other.tow)return three < other.three;
return tow < other.tow;
}
return one < other.one;
}
T3() { one = tow = three = 0; }
T3(T11 one, T22 tow, T33 three) : one(one), tow(tow), three(three)
{
}
};
template <typename T1, typename T2>
void uMax(T1& x, T2 y)
{
if (x < y)x = y;
}
template <typename T1, typename T2>
void uMin(T1& x, T2 y)
{
if (x > y)x = y;
}
constexpr int N = 2e5 + 10;
constexpr int SIZE = sqrt(N);
constexpr int CNT = (N + SIZE - 1) / SIZE;
int a[N];
ll pre[CNT + 1][CNT + 1];
int n, q;
int siz, cnt;
pll mp[N];
int s[N], e[N], pos[N];
int order[N];
ll binary[N];
inline ll query(const int curr, const int x)
{
if (curr == 0 or x == 0)return 0;
const int idx1 = pos[curr];
const int idx2 = pos[x];
ll ans = pre[idx1 - 1][idx2 - 1];
forn(i, s[idx1], curr)ans += order[i] <= x ? a[i] : 0;
forn(i, s[idx2], x)ans += mp[i].second <= e[idx1 - 1] ? binary[i] : 0;
return ans;
}
ll last;
inline void solve()
{
cin >> n;
siz = sqrt(n), cnt = (n + siz - 1) / siz;
forn(i, 1, n)cin >> a[i], mp[i].first = a[i], mp[i].second = i, pos[i] = (i - 1) / siz + 1;
sortArr(mp, n);
forn(i, 1, n)order[mp[i].second] = i, binary[i] = mp[i].first;
forn(i, 1, cnt)s[i] = (i - 1) * siz + 1, e[i] = i * siz;
e[cnt] = n;
forn(i, 1, cnt)
{
forn(j, s[i], e[i])pre[i][pos[order[j]]] += a[j];
forn(j, 1, cnt)pre[i][j] += pre[i][j - 1];
forn(j, 1, cnt)pre[i][j] += pre[i - 1][j];
}
cin >> q;
while (q--)
{
ll l, r, val;
cin >> l >> r >> val;
l ^= last, r ^= last, val ^= last;
val = upper_bound(binary + 1, binary + n + 1, val) - binary - 1;
cout << (last = query(r, val) - query(l - 1, val)) << endl;
}
}
signed int main()
{
// MyFile
Spider
//------------------------------------------------------
// clock_t start = clock();
int test = 1;
// read(test);
// cin >> test;
forn(i, 1, test)solve();
// while (cin >> n, n)solve();
// while (cin >> test)solve();
// clock_t end = clock();
// cerr << "time = " << double(end - start) / CLOCKS_PER_SEC << "s" << endl;
}
实测常数很小,能跟很多人的大常数 主席树 比比,因为相比上面那种枚举两个散块和一堆整块加两个散块而言,显然前者常数要优很多,完全跑不满。
PS 补充其他解法
讲讲另一种分块方式如何去掉 \(\log{\sqrt{n}}\),具体的维护每个块是有序的,然后每个块内前缀和,这样一来整块就能通过二分定位到块内位置,这样一来假设块长为 \(B\) 总复杂度为 \(O(n \times B+n\times \dfrac{n}{B} \times \log{B})\),那么我们只需要让 \(\dfrac{B^2}{\log{B}}=n\) 即可,具体是提出 \(n\) 由均值不等式取等得到,这里的 \(B\) 已经可以二分得到了。当然如果注意到 \(\log{B}\) 和 \(\log{n}\) 差不多,那么最优块长为 \(B=\sqrt{n\log{n}}\),这样一来就可以保证复杂度在 \(O(n\sqrt{n\log{n}})\)。为啥这里不像一般的题还带个 \(nB\log{B}\) 的复杂度,那是因为这题不带修改,所以只需要初始化 \(\dfrac{n}{B}\) 的排序即可,这样复杂度为:\(O(\dfrac{n}{B} \times B\log{B})=O(n\log{B})\) 并不会影响最坏复杂度,可以忽略讨论。
第四种解法
参照代码
#include <bits/stdc++.h>
// #pragma GCC optimize(2)
// #pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math")
// #pragma GCC target("sse,sse2,sse3,ssse3,sse4.1,sse4.2,avx,avx2,popcnt,tune=native")
#define isPbdsFile
#ifdef isPbdsFile
#include <bits/extc++.h>
#else
#include <ext/pb_ds/priority_queue.hpp>
#include <ext/pb_ds/hash_policy.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/trie_policy.hpp>
#include <ext/pb_ds/tag_and_trait.hpp>
#include <ext/pb_ds/hash_policy.hpp>
#include <ext/pb_ds/list_update_policy.hpp>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/exception.hpp>
#include <ext/rope>
#endif
using namespace std;
using namespace __gnu_cxx;
using namespace __gnu_pbds;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef tuple<int, int, int> tii;
typedef tuple<ll, ll, ll> tll;
typedef unsigned int ui;
typedef unsigned long long ull;
typedef __int128 i128;
#define hash1 unordered_map
#define hash2 gp_hash_table
#define hash3 cc_hash_table
#define stdHeap std::priority_queue
#define pbdsHeap __gnu_pbds::priority_queue
#define sortArr(a, n) sort(a+1,a+n+1)
#define all(v) v.begin(),v.end()
#define yes cout<<"YES"
#define no cout<<"NO"
#define Spider ios_base::sync_with_stdio(false);cin.tie(nullptr);cout.tie(nullptr);
#define MyFile freopen("..\\input.txt", "r", stdin),freopen("..\\output.txt", "w", stdout);
#define forn(i, a, b) for(int i = a; i <= b; i++)
#define forv(i, a, b) for(int i=a;i>=b;i--)
#define ls(x) (x<<1)
#define rs(x) (x<<1|1)
#define endl '\n'
//用于Miller-Rabin
[[maybe_unused]] static int Prime_Number[13] = {0, 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37};
template <typename T>
int disc(T* a, int n)
{
return unique(a + 1, a + n + 1) - (a + 1);
}
template <typename T>
T lowBit(T x)
{
return x & -x;
}
template <typename T>
T Rand(T l, T r)
{
static mt19937 Rand(time(nullptr));
uniform_int_distribution<T> dis(l, r);
return dis(Rand);
}
template <typename T1, typename T2>
T1 modt(T1 a, T2 b)
{
return (a % b + b) % b;
}
template <typename T1, typename T2, typename T3>
T1 qPow(T1 a, T2 b, T3 c)
{
a %= c;
T1 ans = 1;
for (; b; b >>= 1, (a *= a) %= c)if (b & 1)(ans *= a) %= c;
return modt(ans, c);
}
template <typename T>
void read(T& x)
{
x = 0;
T sign = 1;
char ch = getchar();
while (!isdigit(ch))
{
if (ch == '-')sign = -1;
ch = getchar();
}
while (isdigit(ch))
{
x = (x << 3) + (x << 1) + (ch ^ 48);
ch = getchar();
}
x *= sign;
}
template <typename T, typename... U>
void read(T& x, U&... y)
{
read(x);
read(y...);
}
template <typename T>
void write(T x)
{
if (typeid(x) == typeid(char))return;
if (x < 0)x = -x, putchar('-');
if (x > 9)write(x / 10);
putchar(x % 10 ^ 48);
}
template <typename C, typename T, typename... U>
void write(C c, T x, U... y)
{
write(x), putchar(c);
write(c, y...);
}
template <typename T11, typename T22, typename T33>
struct T3
{
T11 one;
T22 tow;
T33 three;
bool operator<(const T3 other) const
{
if (one == other.one)
{
if (tow == other.tow)return three < other.three;
return tow < other.tow;
}
return one < other.one;
}
T3() { one = tow = three = 0; }
T3(T11 one, T22 tow, T33 three) : one(one), tow(tow), three(three)
{
}
};
template <typename T1, typename T2>
void uMax(T1& x, T2 y)
{
if (x < y)x = y;
}
template <typename T1, typename T2>
void uMin(T1& x, T2 y)
{
if (x > y)x = y;
}
constexpr int N = 2e5 + 10;
constexpr int SIZE = sqrt(N);
constexpr int CNT = (N + SIZE - 1) / SIZE;
int s[N], e[N], pos[N], idx[N];
ll pre[CNT][N];
ll a[N];
ll mp[CNT][N];
int siz, cnt;
int n, q;
#define len(x) (e[x]-s[x]+1)
inline ll query(const int l, const int r, const ll val)
{
const int L = pos[l], R = pos[r];
if (L == R)
{
ll ans = 0;
forn(i, l, r)ans += a[i] <= val ? a[i] : 0;
return ans;
}
ll ans = 0;
forn(i, l, e[L])ans += a[i] <= val ? a[i] : 0;
forn(i, s[R], r)ans += a[i] <= val ? a[i] : 0;
forn(i, L+1, R-1)ans += pre[i][upper_bound(mp[i] + 1, mp[i] + len(i) + 1, val) - mp[i] - 1];
return ans;
}
ll last;
inline void solve()
{
cin >> n;
siz = sqrt(n * log2(n));
cnt = (n + siz - 1) / siz;
forn(i, 1, n)cin >> a[i], pos[i] = (i - 1) / siz + 1, idx[i] = (i - 1) % siz + 1;
forn(i, 1, cnt)s[i] = (i - 1) * siz + 1, e[i] = i * siz;
e[cnt] = n;
forn(i, 1, cnt)
{
forn(j, s[i], e[i])mp[i][idx[j]] = a[j];
sortArr(mp[i], len(i));
forn(j, s[i], e[i])pre[i][idx[j]] += pre[i][idx[j] - 1] + mp[i][idx[j]];
}
cin >> q;
while (q--)
{
ll l, r, val;
cin >> l >> r >> val;
l ^= last, r ^= last, val ^= last;
cout << (last = query(l, r, val)) << endl;
}
}
signed int main()
{
// MyFile
Spider
//------------------------------------------------------
// clock_t start = clock();
int test = 1;
// read(test);
// cin >> test;
forn(i, 1, test)solve();
// while (cin >> n, n)solve();
// while (cin >> test)solve();
// clock_t end = clock();
// cerr << "time = " << double(end - start) / CLOCKS_PER_SEC << "s" << endl;
}
考虑树套树方面的解法
一个最无脑的解法,直接区间线段树套权值线段树,注意需要离散化,否则空间常数巨大。这里使用标记永久化写法来降低各类常数。
第五种解法
参照代码
#include <bits/stdc++.h>
// #pragma GCC optimize(2)
// #pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math")
// #pragma GCC target("sse,sse2,sse3,ssse3,sse4.1,sse4.2,avx,avx2,popcnt,tune=native")
#define isPbdsFile
#ifdef isPbdsFile
#include <bits/extc++.h>
#else
#include <ext/pb_ds/priority_queue.hpp>
#include <ext/pb_ds/hash_policy.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/trie_policy.hpp>
#include <ext/pb_ds/tag_and_trait.hpp>
#include <ext/pb_ds/hash_policy.hpp>
#include <ext/pb_ds/list_update_policy.hpp>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/exception.hpp>
#include <ext/rope>
#endif
using namespace std;
using namespace __gnu_cxx;
using namespace __gnu_pbds;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef tuple<int, int, int> tii;
typedef tuple<ll, ll, ll> tll;
typedef unsigned int ui;
typedef unsigned long long ull;
typedef __int128 i128;
#define hash1 unordered_map
#define hash2 gp_hash_table
#define hash3 cc_hash_table
#define stdHeap std::priority_queue
#define pbdsHeap __gnu_pbds::priority_queue
#define sortArr(a, n) sort(a+1,a+n+1)
#define all(v) v.begin(),v.end()
#define yes cout<<"YES"
#define no cout<<"NO"
#define Spider ios_base::sync_with_stdio(false);cin.tie(nullptr);cout.tie(nullptr);
#define MyFile freopen("..\\input.txt", "r", stdin),freopen("..\\output.txt", "w", stdout);
#define forn(i, a, b) for(int i = a; i <= b; i++)
#define forv(i, a, b) for(int i=a;i>=b;i--)
#define ls(x) (x<<1)
#define rs(x) (x<<1|1)
#define endl '\n'
//用于Miller-Rabin
[[maybe_unused]] static int Prime_Number[13] = {0, 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37};
template <typename T>
int disc(T* a, int n)
{
return unique(a + 1, a + n + 1) - (a + 1);
}
template <typename T>
T lowBit(T x)
{
return x & -x;
}
template <typename T>
T Rand(T l, T r)
{
static mt19937 Rand(time(nullptr));
uniform_int_distribution<T> dis(l, r);
return dis(Rand);
}
template <typename T1, typename T2>
T1 modt(T1 a, T2 b)
{
return (a % b + b) % b;
}
template <typename T1, typename T2, typename T3>
T1 qPow(T1 a, T2 b, T3 c)
{
a %= c;
T1 ans = 1;
for (; b; b >>= 1, (a *= a) %= c)if (b & 1)(ans *= a) %= c;
return modt(ans, c);
}
template <typename T>
void read(T& x)
{
x = 0;
T sign = 1;
char ch = getchar();
while (!isdigit(ch))
{
if (ch == '-')sign = -1;
ch = getchar();
}
while (isdigit(ch))
{
x = (x << 3) + (x << 1) + (ch ^ 48);
ch = getchar();
}
x *= sign;
}
template <typename T, typename... U>
void read(T& x, U&... y)
{
read(x);
read(y...);
}
template <typename T>
void write(T x)
{
if (typeid(x) == typeid(char))return;
if (x < 0)x = -x, putchar('-');
if (x > 9)write(x / 10);
putchar(x % 10 ^ 48);
}
template <typename C, typename T, typename... U>
void write(C c, T x, U... y)
{
write(x), putchar(c);
write(c, y...);
}
template <typename T11, typename T22, typename T33>
struct T3
{
T11 one;
T22 tow;
T33 three;
bool operator<(const T3 other) const
{
if (one == other.one)
{
if (tow == other.tow)return three < other.three;
return tow < other.tow;
}
return one < other.one;
}
T3() { one = tow = three = 0; }
T3(T11 one, T22 tow, T33 three) : one(one), tow(tow), three(three)
{
}
};
template <typename T1, typename T2>
void uMax(T1& x, T2 y)
{
if (x < y)x = y;
}
template <typename T1, typename T2>
void uMin(T1& x, T2 y)
{
if (x > y)x = y;
}
constexpr int N = 2e5 + 10;
int n, q;
int a[N], mp[N];
int MX;
struct
{
int cnt;
struct InNode
{
int left, right;
ll sum;
} node[N * 200];
#define left(x) node[x].left
#define right(x) node[x].right
#define sum(x) node[x].sum
void add(int& curr, const int pos, const int l = 0, const int r = MX)
{
if (!curr)curr = ++cnt;
sum(curr) += mp[pos];
const int mid = l + r >> 1;
if (l == r)return;
if (pos <= mid)add(left(curr), pos, l, mid);
else add(right(curr), pos, mid + 1, r);
}
ll query(const int curr, const int pos, const int l = 0, const int r = MX)
{
if (!curr)return 0;
if (l == r)return sum(curr);
const int mid = l + r >> 1;
if (pos <= mid)return query(left(curr), pos, l, mid);
return query(right(curr), pos, mid + 1, r) + sum(left(curr));
}
int root[N << 2];
void build(const int curr = 1, const int l = 1, const int r = n)
{
forn(i, l, r)add(root[curr], a[i]);
const int mid = l + r >> 1;
if (l == r)return;
build(ls(curr), l, mid);
build(rs(curr), mid + 1, r);
}
ll query_ans(const int curr, const int l, const int r, const int x, const int s = 1, const int e = n)
{
if (l <= s and e <= r)return query(root[curr], x);
const int mid = s + e >> 1;
ll ans = 0;
if (l <= mid)ans += query_ans(ls(curr), l, r, x, s, mid);
if (r > mid)ans += query_ans(rs(curr), l, r, x, mid + 1, e);
return ans;
}
} seg;
ll last;
inline void solve()
{
cin >> n;
forn(i, 1, n)cin >> a[i], mp[i] = a[i];
sortArr(mp, n), MX = disc(mp, n);
forn(i, 1, n)a[i] = lower_bound(mp + 1, mp + MX + 1, a[i]) - mp;
seg.build();
cin >> q;
while (q--)
{
ll l, r, val;
cin >> l >> r >> val;
l ^= last, r ^= last, val ^= last;
val = upper_bound(mp + 1, mp + MX + 1, val) - mp - 1;
cout << (last = seg.query_ans(1, l, r, val)) << endl;
}
}
signed int main()
{
// MyFile
Spider
//------------------------------------------------------
// clock_t start = clock();
int test = 1;
// read(test);
// cin >> test;
forn(i, 1, test)solve();
// while (cin >> n, n)solve();
// while (cin >> test)solve();
// clock_t end = clock();
// cerr << "time = " << double(end - start) / CLOCKS_PER_SEC << "s" << endl;
}
当然少不了最经典的 动态主席树 了。这是可差性问题,所以我们可以用 BIT 套权值树,每次修改和查询只会涉及到到 \(\log{n}\) 级别的权值树,我们利用前缀和作差,因为它的空间比传统树套树小很多,所以是否离散化影响都不大。这种 ds 同时支持单点修改了。
第六种解法
离散化参照代码
#include <bits/stdc++.h>
// #pragma GCC optimize(2)
// #pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math")
// #pragma GCC target("sse,sse2,sse3,ssse3,sse4.1,sse4.2,avx,avx2,popcnt,tune=native")
#define isPbdsFile
#ifdef isPbdsFile
#include <bits/extc++.h>
#else
#include <ext/pb_ds/priority_queue.hpp>
#include <ext/pb_ds/hash_policy.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/trie_policy.hpp>
#include <ext/pb_ds/tag_and_trait.hpp>
#include <ext/pb_ds/hash_policy.hpp>
#include <ext/pb_ds/list_update_policy.hpp>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/exception.hpp>
#include <ext/rope>
#endif
using namespace std;
using namespace __gnu_cxx;
using namespace __gnu_pbds;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef tuple<int, int, int> tii;
typedef tuple<ll, ll, ll> tll;
typedef unsigned int ui;
typedef unsigned long long ull;
typedef __int128 i128;
#define hash1 unordered_map
#define hash2 gp_hash_table
#define hash3 cc_hash_table
#define stdHeap std::priority_queue
#define pbdsHeap __gnu_pbds::priority_queue
#define sortArr(a, n) sort(a+1,a+n+1)
#define all(v) v.begin(),v.end()
#define yes cout<<"YES"
#define no cout<<"NO"
#define Spider ios_base::sync_with_stdio(false);cin.tie(nullptr);cout.tie(nullptr);
#define MyFile freopen("..\\input.txt", "r", stdin),freopen("..\\output.txt", "w", stdout);
#define forn(i, a, b) for(int i = a; i <= b; i++)
#define forv(i, a, b) for(int i=a;i>=b;i--)
#define ls(x) (x<<1)
#define rs(x) (x<<1|1)
#define endl '\n'
//用于Miller-Rabin
[[maybe_unused]] static int Prime_Number[13] = {0, 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37};
template <typename T>
int disc(T* a, int n)
{
return unique(a + 1, a + n + 1) - (a + 1);
}
template <typename T>
T lowBit(T x)
{
return x & -x;
}
template <typename T>
T Rand(T l, T r)
{
static mt19937 Rand(time(nullptr));
uniform_int_distribution<T> dis(l, r);
return dis(Rand);
}
template <typename T1, typename T2>
T1 modt(T1 a, T2 b)
{
return (a % b + b) % b;
}
template <typename T1, typename T2, typename T3>
T1 qPow(T1 a, T2 b, T3 c)
{
a %= c;
T1 ans = 1;
for (; b; b >>= 1, (a *= a) %= c)if (b & 1)(ans *= a) %= c;
return modt(ans, c);
}
template <typename T>
void read(T& x)
{
x = 0;
T sign = 1;
char ch = getchar();
while (!isdigit(ch))
{
if (ch == '-')sign = -1;
ch = getchar();
}
while (isdigit(ch))
{
x = (x << 3) + (x << 1) + (ch ^ 48);
ch = getchar();
}
x *= sign;
}
template <typename T, typename... U>
void read(T& x, U&... y)
{
read(x);
read(y...);
}
template <typename T>
void write(T x)
{
if (typeid(x) == typeid(char))return;
if (x < 0)x = -x, putchar('-');
if (x > 9)write(x / 10);
putchar(x % 10 ^ 48);
}
template <typename C, typename T, typename... U>
void write(C c, T x, U... y)
{
write(x), putchar(c);
write(c, y...);
}
template <typename T11, typename T22, typename T33>
struct T3
{
T11 one;
T22 tow;
T33 three;
bool operator<(const T3 other) const
{
if (one == other.one)
{
if (tow == other.tow)return three < other.three;
return tow < other.tow;
}
return one < other.one;
}
T3() { one = tow = three = 0; }
T3(T11 one, T22 tow, T33 three) : one(one), tow(tow), three(three)
{
}
};
template <typename T1, typename T2>
void uMax(T1& x, T2 y)
{
if (x < y)x = y;
}
template <typename T1, typename T2>
void uMin(T1& x, T2 y)
{
if (x > y)x = y;
}
constexpr int N = 2e5 + 10;
int n, q;
int MX;
int a[N], mp[N];
struct
{
int cnt;
struct InNode
{
int left, right;
ll sum;
} node[N * 200];
#define left(x) node[x].left
#define right(x) node[x].right
#define sum(x) node[x].sum
void add(int& curr, const int pos, const int l = 0, const int r = MX)
{
if (!curr)curr = ++cnt;
sum(curr) += mp[pos];
const int mid = l + r >> 1;
if (l == r)return;
if (pos <= mid)add(left(curr), pos, l, mid);
else add(right(curr), pos, mid + 1, r);
}
int root[N];
void Add(int x, const int val)
{
while (x <= n)add(root[x], val), x += lowBit(x);
}
ll query(vector<int>& L, vector<int>& R, const int pos, const int l = 0, const int r = MX)
{
ll ans = 0;
if (l == r)
{
for (int x : R)ans += sum(x);
for (int x : L)ans -= sum(x);
return ans;
}
const int mid = l + r >> 1;
if (pos <= mid)
{
for (int& x : L)x = left(x);
for (int& x : R)x = left(x);
return query(L, R, pos, l, mid);
}
for (const auto x : R)ans += sum(left(x));
for (const auto x : L)ans -= sum(left(x));
for (int& x : L)x = right(x);
for (int& x : R)x = right(x);
return ans + query(L, R, pos, mid + 1, r);
}
vector<int> get(int x) const
{
vector<int> curr;
while (x)curr.push_back(root[x]), x -= lowBit(x);
return curr;
}
ll Query(const int l, const int r, const int val)
{
vector<int> L = get(l - 1);
vector<int> R = get(r);
return query(L, R, val);
}
} seg;
ll last;
inline void solve()
{
cin >> n;
forn(i, 1, n)cin >> a[i], mp[i] = a[i];
sortArr(mp, n), MX = disc(mp, n);
forn(i, 1, n)a[i] = lower_bound(mp + 1, mp + MX + 1, a[i]) - mp, seg.Add(i, a[i]);
cin >> q;
while (q--)
{
ll l, r, val;
cin >> l >> r >> val;
l ^= last, r ^= last, val ^= last;
val = upper_bound(mp + 1, mp + MX + 1, val) - mp - 1;
cout << (last = seg.Query(l, r, val)) << endl;
}
}
signed int main()
{
// MyFile
Spider
//------------------------------------------------------
// clock_t start = clock();
int test = 1;
// read(test);
// cin >> test;
forn(i, 1, test)solve();
// while (cin >> n, n)solve();
// while (cin >> test)solve();
// clock_t end = clock();
// cerr << "time = " << double(end - start) / CLOCKS_PER_SEC << "s" << endl;
}
不离散化参照代码
#include <bits/stdc++.h>
// #pragma GCC optimize(2)
// #pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math")
// #pragma GCC target("sse,sse2,sse3,ssse3,sse4.1,sse4.2,avx,avx2,popcnt,tune=native")
#define isPbdsFile
#ifdef isPbdsFile
#include <bits/extc++.h>
#else
#include <ext/pb_ds/priority_queue.hpp>
#include <ext/pb_ds/hash_policy.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/trie_policy.hpp>
#include <ext/pb_ds/tag_and_trait.hpp>
#include <ext/pb_ds/hash_policy.hpp>
#include <ext/pb_ds/list_update_policy.hpp>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/exception.hpp>
#include <ext/rope>
#endif
using namespace std;
using namespace __gnu_cxx;
using namespace __gnu_pbds;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef tuple<int, int, int> tii;
typedef tuple<ll, ll, ll> tll;
typedef unsigned int ui;
typedef unsigned long long ull;
typedef __int128 i128;
#define hash1 unordered_map
#define hash2 gp_hash_table
#define hash3 cc_hash_table
#define stdHeap std::priority_queue
#define pbdsHeap __gnu_pbds::priority_queue
#define sortArr(a, n) sort(a+1,a+n+1)
#define all(v) v.begin(),v.end()
#define yes cout<<"YES"
#define no cout<<"NO"
#define Spider ios_base::sync_with_stdio(false);cin.tie(nullptr);cout.tie(nullptr);
#define MyFile freopen("..\\input.txt", "r", stdin),freopen("..\\output.txt", "w", stdout);
#define forn(i, a, b) for(int i = a; i <= b; i++)
#define forv(i, a, b) for(int i=a;i>=b;i--)
#define ls(x) (x<<1)
#define rs(x) (x<<1|1)
#define endl '\n'
//用于Miller-Rabin
[[maybe_unused]] static int Prime_Number[13] = {0, 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37};
template <typename T>
int disc(T* a, int n)
{
return unique(a + 1, a + n + 1) - (a + 1);
}
template <typename T>
T lowBit(T x)
{
return x & -x;
}
template <typename T>
T Rand(T l, T r)
{
static mt19937 Rand(time(nullptr));
uniform_int_distribution<T> dis(l, r);
return dis(Rand);
}
template <typename T1, typename T2>
T1 modt(T1 a, T2 b)
{
return (a % b + b) % b;
}
template <typename T1, typename T2, typename T3>
T1 qPow(T1 a, T2 b, T3 c)
{
a %= c;
T1 ans = 1;
for (; b; b >>= 1, (a *= a) %= c)if (b & 1)(ans *= a) %= c;
return modt(ans, c);
}
template <typename T>
void read(T& x)
{
x = 0;
T sign = 1;
char ch = getchar();
while (!isdigit(ch))
{
if (ch == '-')sign = -1;
ch = getchar();
}
while (isdigit(ch))
{
x = (x << 3) + (x << 1) + (ch ^ 48);
ch = getchar();
}
x *= sign;
}
template <typename T, typename... U>
void read(T& x, U&... y)
{
read(x);
read(y...);
}
template <typename T>
void write(T x)
{
if (typeid(x) == typeid(char))return;
if (x < 0)x = -x, putchar('-');
if (x > 9)write(x / 10);
putchar(x % 10 ^ 48);
}
template <typename C, typename T, typename... U>
void write(C c, T x, U... y)
{
write(x), putchar(c);
write(c, y...);
}
template <typename T11, typename T22, typename T33>
struct T3
{
T11 one;
T22 tow;
T33 three;
bool operator<(const T3 other) const
{
if (one == other.one)
{
if (tow == other.tow)return three < other.three;
return tow < other.tow;
}
return one < other.one;
}
T3() { one = tow = three = 0; }
T3(T11 one, T22 tow, T33 three) : one(one), tow(tow), three(three)
{
}
};
template <typename T1, typename T2>
void uMax(T1& x, T2 y)
{
if (x < y)x = y;
}
template <typename T1, typename T2>
void uMin(T1& x, T2 y)
{
if (x > y)x = y;
}
constexpr int N = 2e5 + 10;
int n, q;
int a[N];
constexpr int MX = 1e9;
struct
{
int cnt;
struct InNode
{
int left, right;
ll sum;
} node[N * 300];
#define left(x) node[x].left
#define right(x) node[x].right
#define sum(x) node[x].sum
void add(int& curr, const int pos, const int l = 0, const int r = MX)
{
if (!curr)curr = ++cnt;
sum(curr) += pos;
const int mid = l + r >> 1;
if (l == r)return;
if (pos <= mid)add(left(curr), pos, l, mid);
else add(right(curr), pos, mid + 1, r);
}
int root[N];
void Add(int x, const int val)
{
while (x <= n)add(root[x], val), x += lowBit(x);
}
ll query(vector<int>& L, vector<int>& R, const int pos, const int l = 0, const int r = MX)
{
ll ans = 0;
if (l == r)
{
for (int x : R)ans += sum(x);
for (int x : L)ans -= sum(x);
return ans;
}
const int mid = l + r >> 1;
if (pos <= mid)
{
for (int& x : L)x = left(x);
for (int& x : R)x = left(x);
return query(L, R, pos, l, mid);
}
for (const auto x : R)ans += sum(left(x));
for (const auto x : L)ans -= sum(left(x));
for (int& x : L)x = right(x);
for (int& x : R)x = right(x);
return ans + query(L, R, pos, mid + 1, r);
}
vector<int> get(int x) const
{
vector<int> curr;
while (x)curr.push_back(root[x]), x -= lowBit(x);
return curr;
}
ll Query(const int l, const int r, const int val)
{
vector<int> L = get(l - 1);
vector<int> R = get(r);
return query(L, R, val);
}
} seg;
ll last;
inline void solve()
{
cin >> n;
forn(i, 1, n)cin >> a[i];
forn(i, 1, n)seg.Add(i, a[i]);
cin >> q;
while (q--)
{
ll l, r, val;
cin >> l >> r >> val;
l ^= last, r ^= last, val ^= last;
cout << (last = seg.Query(l, r, val)) << endl;
}
}
signed int main()
{
// MyFile
Spider
//------------------------------------------------------
// clock_t start = clock();
int test = 1;
// read(test);
// cin >> test;
forn(i, 1, test)solve();
// while (cin >> n, n)solve();
// while (cin >> test)solve();
// clock_t end = clock();
// cerr << "time = " << double(end - start) / CLOCKS_PER_SEC << "s" << endl;
}
其实树套树的两个 \(log\) 一个是 \(\log{n}\) 一个才是 \(\log{V_{max}}\),不过一般离散化后二者数量级相当,也可以作为一并讨论。
看了下官解给的归并树
其实归并树这玩意和划分树相比,个人感觉归并树要更好用一些。划分树每层无序,但归并树每层有序。我个人理解归并树就当一个简约的树套树使用,每一个线段树节点维护一个有序数组,这个可以利用线段树的 \(pushUp\) 改为合并左右子树两个有序数组轻松实现。本题要求查询和,基于上述几种解法思想,我们可以维护有序数列的前缀和,基于二分定位所需。其实这跟分块内维护有序序列二分查前缀和完全一致的。
第七种解法
参照代码
#include <bits/stdc++.h>
// #pragma GCC optimize(2)
// #pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math")
// #pragma GCC target("sse,sse2,sse3,ssse3,sse4.1,sse4.2,avx,avx2,popcnt,tune=native")
#define isPbdsFile
#ifdef isPbdsFile
#include <bits/extc++.h>
#else
#include <ext/pb_ds/priority_queue.hpp>
#include <ext/pb_ds/hash_policy.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/trie_policy.hpp>
#include <ext/pb_ds/tag_and_trait.hpp>
#include <ext/pb_ds/hash_policy.hpp>
#include <ext/pb_ds/list_update_policy.hpp>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/exception.hpp>
#include <ext/rope>
#endif
using namespace std;
using namespace __gnu_cxx;
using namespace __gnu_pbds;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef tuple<int, int, int> tii;
typedef tuple<ll, ll, ll> tll;
typedef unsigned int ui;
typedef unsigned long long ull;
typedef __int128 i128;
#define hash1 unordered_map
#define hash2 gp_hash_table
#define hash3 cc_hash_table
#define stdHeap std::priority_queue
#define pbdsHeap __gnu_pbds::priority_queue
#define sortArr(a, n) sort(a+1,a+n+1)
#define all(v) v.begin(),v.end()
#define yes cout<<"YES"
#define no cout<<"NO"
#define Spider ios_base::sync_with_stdio(false);cin.tie(nullptr);cout.tie(nullptr);
#define MyFile freopen("..\\input.txt", "r", stdin),freopen("..\\output.txt", "w", stdout);
#define forn(i, a, b) for(int i = a; i <= b; i++)
#define forv(i, a, b) for(int i=a;i>=b;i--)
#define ls(x) (x<<1)
#define rs(x) (x<<1|1)
#define endl '\n'
//用于Miller-Rabin
[[maybe_unused]] static int Prime_Number[13] = {0, 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37};
template <typename T>
int disc(T* a, int n)
{
return unique(a + 1, a + n + 1) - (a + 1);
}
template <typename T>
T lowBit(T x)
{
return x & -x;
}
template <typename T>
T Rand(T l, T r)
{
static mt19937 Rand(time(nullptr));
uniform_int_distribution<T> dis(l, r);
return dis(Rand);
}
template <typename T1, typename T2>
T1 modt(T1 a, T2 b)
{
return (a % b + b) % b;
}
template <typename T1, typename T2, typename T3>
T1 qPow(T1 a, T2 b, T3 c)
{
a %= c;
T1 ans = 1;
for (; b; b >>= 1, (a *= a) %= c)if (b & 1)(ans *= a) %= c;
return modt(ans, c);
}
template <typename T>
void read(T& x)
{
x = 0;
T sign = 1;
char ch = getchar();
while (!isdigit(ch))
{
if (ch == '-')sign = -1;
ch = getchar();
}
while (isdigit(ch))
{
x = (x << 3) + (x << 1) + (ch ^ 48);
ch = getchar();
}
x *= sign;
}
template <typename T, typename... U>
void read(T& x, U&... y)
{
read(x);
read(y...);
}
template <typename T>
void write(T x)
{
if (typeid(x) == typeid(char))return;
if (x < 0)x = -x, putchar('-');
if (x > 9)write(x / 10);
putchar(x % 10 ^ 48);
}
template <typename C, typename T, typename... U>
void write(C c, T x, U... y)
{
write(x), putchar(c);
write(c, y...);
}
template <typename T11, typename T22, typename T33>
struct T3
{
T11 one;
T22 tow;
T33 three;
bool operator<(const T3 other) const
{
if (one == other.one)
{
if (tow == other.tow)return three < other.three;
return tow < other.tow;
}
return one < other.one;
}
T3() { one = tow = three = 0; }
T3(T11 one, T22 tow, T33 three) : one(one), tow(tow), three(three)
{
}
};
template <typename T1, typename T2>
void uMax(T1& x, T2 y)
{
if (x < y)x = y;
}
template <typename T1, typename T2>
void uMin(T1& x, T2 y)
{
if (x > y)x = y;
}
constexpr int N = 2e5 + 10;
int n, q;
int a[N];
struct Node
{
int l, r;
vector<int> val;
vector<ll> pre;
} node[N << 2];
#define val(x) node[x].val
#define pre(x) node[x].pre
inline void merge(vector<ll>& pre, vector<int>& curr, const vector<int>& left, const vector<int>& right)
{
int i = 0, j = 0;
while (i < left.size() and j < right.size())
{
if (left[i] <= right[j])curr.push_back(left[i++]);
else curr.push_back(right[j++]);
}
while (i < left.size())curr.push_back(left[i++]);
while (j < right.size())curr.push_back(right[j++]);
ll sum = 0;
for (const auto v : curr)pre.push_back(sum += v);
}
inline void build(const int curr = 1, const int l = 1, const int r = n)
{
const int mid = l + r >> 1;
if (l == r)
{
val(curr).push_back(a[l]), pre(curr).push_back(a[l]);
return;
}
build(ls(curr), l, mid);
build(rs(curr), mid + 1, r);
merge(pre(curr),val(curr),val(ls(curr)),val(rs(curr)));
}
inline ll getSum(const vector<ll>& pre, const vector<int>& curr, const int val)
{
if (val < curr[0])return 0;
return pre[upper_bound(all(curr), val) - curr.begin() - 1];
}
inline ll query(const int curr, const int l, const int r, const int val, const int s = 1, const int e = n)
{
if (l <= s and e <= r)return getSum(pre(curr),val(curr), val);
const int mid = s + e >> 1;
ll ans = 0;
if (l <= mid)ans += query(ls(curr), l, r, val, s, mid);
if (r > mid)ans += query(rs(curr), l, r, val, mid + 1, e);
return ans;
}
ll last;
inline void solve()
{
cin >> n;
forn(i, 1, n)cin >> a[i];
build();
cin >> q;
while (q--)
{
ll l, r, val;
cin >> l >> r >> val;
l ^= last, r ^= last, val ^= last;
cout << (last = query(1, l, r, val)) << endl;
}
}
signed int main()
{
// MyFile
Spider
//------------------------------------------------------
// clock_t start = clock();
int test = 1;
// read(test);
// cin >> test;
forn(i, 1, test)solve();
// while (cin >> n, n)solve();
// while (cin >> test)solve();
// clock_t end = clock();
// cerr << "time = " << double(end - start) / CLOCKS_PER_SEC << "s" << endl;
}
小总结
可能还有其他没有想到的解法。在本题的一些条件修改下,去掉强制在线,我们可以应该还可以使用整体二分求了。emmmm,还有莫队加值域分块可以乱做了。带个修的话,里面就得用动态主席树、树套树、序列分块套值域分块、整体二分了。这三个复杂度都是比较优的。加个强制在线,整体二分就不能做了。做法挺多的,是一类经典的 \(ds\) 问题。
第八种解法
也是回忆起之前某题里面用到的,具体的权值树套有序序列,本题不带修,用动态数组即可。考虑再维护前缀和,这样一来,在满足权值域的情况下,二分出 \([l,r]\) 的贡献起点和终点即可。如果带修,可以考虑换成文艺平衡树维护信息查询。
参照代码
#include <bits/stdc++.h>
// #pragma GCC optimize(2)
// #pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math")
// #pragma GCC target("sse,sse2,sse3,ssse3,sse4.1,sse4.2,avx,avx2,popcnt,tune=native")
#define isPbdsFile
#ifdef isPbdsFile
#include <bits/extc++.h>
#else
#include <ext/pb_ds/priority_queue.hpp>
#include <ext/pb_ds/hash_policy.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/trie_policy.hpp>
#include <ext/pb_ds/tag_and_trait.hpp>
#include <ext/pb_ds/hash_policy.hpp>
#include <ext/pb_ds/list_update_policy.hpp>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/exception.hpp>
#include <ext/rope>
#endif
using namespace std;
using namespace __gnu_cxx;
using namespace __gnu_pbds;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef tuple<int, int, int> tii;
typedef tuple<ll, ll, ll> tll;
typedef unsigned int ui;
typedef unsigned long long ull;
typedef __int128 i128;
#define hash1 unordered_map
#define hash2 gp_hash_table
#define hash3 cc_hash_table
#define stdHeap std::priority_queue
#define pbdsHeap __gnu_pbds::priority_queue
#define sortArr(a, n) sort(a+1,a+n+1)
#define all(v) v.begin(),v.end()
#define yes cout<<"YES"
#define no cout<<"NO"
#define Spider ios_base::sync_with_stdio(false);cin.tie(nullptr);cout.tie(nullptr);
#define MyFile freopen("..\\input.txt", "r", stdin),freopen("..\\output.txt", "w", stdout);
#define forn(i, a, b) for(int i = a; i <= b; i++)
#define forv(i, a, b) for(int i=a;i>=b;i--)
#define ls(x) (x<<1)
#define rs(x) (x<<1|1)
#define endl '\n'
//用于Miller-Rabin
[[maybe_unused]] static int Prime_Number[13] = {0, 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37};
template <typename T>
int disc(T* a, int n)
{
return unique(a + 1, a + n + 1) - (a + 1);
}
template <typename T>
T lowBit(T x)
{
return x & -x;
}
template <typename T>
T Rand(T l, T r)
{
static mt19937 Rand(time(nullptr));
uniform_int_distribution<T> dis(l, r);
return dis(Rand);
}
template <typename T1, typename T2>
T1 modt(T1 a, T2 b)
{
return (a % b + b) % b;
}
template <typename T1, typename T2, typename T3>
T1 qPow(T1 a, T2 b, T3 c)
{
a %= c;
T1 ans = 1;
for (; b; b >>= 1, (a *= a) %= c)if (b & 1)(ans *= a) %= c;
return modt(ans, c);
}
template <typename T>
void read(T& x)
{
x = 0;
T sign = 1;
char ch = getchar();
while (!isdigit(ch))
{
if (ch == '-')sign = -1;
ch = getchar();
}
while (isdigit(ch))
{
x = (x << 3) + (x << 1) + (ch ^ 48);
ch = getchar();
}
x *= sign;
}
template <typename T, typename... U>
void read(T& x, U&... y)
{
read(x);
read(y...);
}
template <typename T>
void write(T x)
{
if (typeid(x) == typeid(char))return;
if (x < 0)x = -x, putchar('-');
if (x > 9)write(x / 10);
putchar(x % 10 ^ 48);
}
template <typename C, typename T, typename... U>
void write(C c, T x, U... y)
{
write(x), putchar(c);
write(c, y...);
}
template <typename T11, typename T22, typename T33>
struct T3
{
T11 one;
T22 tow;
T33 three;
bool operator<(const T3 other) const
{
if (one == other.one)
{
if (tow == other.tow)return three < other.three;
return tow < other.tow;
}
return one < other.one;
}
T3() { one = tow = three = 0; }
T3(T11 one, T22 tow, T33 three) : one(one), tow(tow), three(three)
{
}
};
template <typename T1, typename T2>
void uMax(T1& x, T2 y)
{
if (x < y)x = y;
}
template <typename T1, typename T2>
void uMin(T1& x, T2 y)
{
if (x > y)x = y;
}
constexpr int N = 2e5 + 10;
int a[N], mp[N], n, q, mx;
vector<int> ord[N << 2], old[N];
vector<ll> pre[N << 2];
inline void merge(vector<int>& curr, const vector<int>& left, const vector<int>& right)
{
int i = 0, j = 0;
while (i < left.size() and j < right.size())curr.push_back(left[i] < right[j] ? left[i++] : right[j++]);
while (i < left.size())curr.push_back(left[i++]);
while (j < right.size())curr.push_back(right[j++]);
}
inline void updatePre(const int curr)
{
ll sum = 0;
pre[curr].push_back(sum);
for (const auto v : ord[curr])pre[curr].push_back(sum += mp[a[v]]);
}
inline void build(const int curr = 1, const int l = 1, const int r = mx)
{
if (l == r)
{
ord[curr].swap(old[l]);
updatePre(curr);
return;
}
const int mid = l + r >> 1;
build(ls(curr), l, mid);
build(rs(curr), mid + 1, r);
merge(ord[curr], ord[ls(curr)], ord[rs(curr)]);
updatePre(curr);
}
inline ll query(const int curr, const int val, const int L, const int R, const int l = 1, const int r = mx)
{
if (l > val)return 0;
if (r <= val)
{
if (ord[curr].empty() or ord[curr].back() < L or ord[curr].front() > R)return 0;
const int e = ranges::upper_bound(ord[curr], R) - ord[curr].begin();
const int s = ranges::lower_bound(ord[curr], L) - ord[curr].begin();
return pre[curr][e] - pre[curr][s];
}
const int mid = l + r >> 1;
return query(ls(curr), val, L, R, l, mid) + query(rs(curr), val, L, R, mid + 1, r);
}
ll last, l, r, val;
inline void solve()
{
cin >> n;
forn(i, 1, n)cin >> a[i], mp[i] = a[i];
sortArr(mp, n), mx = disc(mp, n);
forn(i, 1, n) old[a[i] = ranges::lower_bound(mp + 1, mp + mx + 1, a[i]) - mp].push_back(i);
build();
cin >> q;
forn(i, 1, q)
{
cin >> l >> r >> val;
l ^= last, r ^= last, val ^= last;
if (val < mp[1])cout << (last = 0) << endl;
else
{
val = ranges::upper_bound(mp + 1, mp + mx + 1, val) - mp - 1;
cout << (last = query(1, val, l, r)) << endl;
}
}
}
signed int main()
{
// MyFile
Spider
//------------------------------------------------------
// clock_t start = clock();
int test = 1;
// read(test);
// cin >> test;
forn(i, 1, test)solve();
// while (cin >> n, n)solve();
// while (cin >> test)solve();
// clock_t end = clock();
// cerr << "time = " << double(end - start) / CLOCKS_PER_SEC << "s" << endl;
}