P8659 [蓝桥杯 2017 国 A] 数组操作 题解
题目链接:洛谷 或者 蓝桥杯 或者 C语言中文网
几个OJ的AC记录:
忘了哪个OJ的:
洛谷:
C语言中文网:
蓝桥杯:
emmmmmmm,好像每个OJ给的时限和空间还不一样,蓝桥杯官方还给了 $3s$ 和 $2G$, C语言中文网机子比较老可能,挺卡常的,开了个究极快读和指令集就过去了,也可以自己调下重构常数,偷懒了。
回到本题
说一下标算,一开始没注意没有区间覆盖操作,还想着 ODT 去写,其实正解应该是可持久化的文艺平衡树去想,就很简单了。首先这个操作二这玩意肯定不是线段树能搞得,总有 \([l1,r1]\) 和 \([l2,r2]\) 对应的线段树节点非对称的,不过线段树分裂或者合并倒是应该能写,但涉及到了分裂区间和合并区间我们显然选择了文艺平衡树去维护信息,具体的可以参考我前面提的一些相关题。文艺平衡树以下标为键值,同时维护子树大小与节点值,它的中序遍历则是原序列 \(a\),所以对于操作 \(1\) 和 操作 \(3\) 非常常规,我们文艺平衡树维护一个子树大小,在 \(split\) 的过程中,根据左子树大小来确定走哪边,和主席树那些是一样的。
再来说说操作二,操作二这玩意很显然,我们不可能把整段进行复制了然后再贴到另一段上去,但如果这两段共用一段呢?假如有个平衡树节点为 \([l2,r2]\) 范围的信息,这点两次 \(split\) 常规拿到,那么如何如果将 \([l1,r1]\) 的平衡树节点换成它是 ok 的。但有一个问题,假如我要修改 \([l1,r1]\) 的节点的信息,比如操作 \(1\),岂不是也会同时修改了 \([l2,r2]\) 的信息?很显然,这个时候我们需要可持久化,复制版本信息,这样虽然共用一个节点,但它们的版本不同,这样一来修改的情况就互不影响了。具体的,拿到两个节点以后,新的根节点应该为:\(merge([1,l1-1]、[l2,r2]、[r2+1,n])\) 这三个节点的 \(merge\),这点很常规,不熟悉的可以刷一下洛谷的可持久化文艺平衡树板题练习下。
细节
注意到空间限制 \(128mb\) 这玩意显然在提示我们需要重构,类似替罪羊树那种思想一样,重构树,只需要 \(dfs\) 一遍树以中序遍历的形式就能拿到原序列信息,因为我们的平衡树是文艺平衡树,以下标为值键的,即右子树都是处于当前数组位置右边的数,左子树则是左边的。而建树,有些人喜欢用直接常规的从中点往两边建树,但这样会破坏 FHQ 的小根堆或者大根堆的性质,虽然我们的这个 \(rnk\) 本质就是让它成链的可能性降低,即使破坏掉原有的性质,但为了保证 \(FHQ\) 的根堆的合理性,我还是使用了常规的笛卡尔树建树法,\(O(n)\) 建树,我们可以考虑,当如果节点数量大于某个阈值时,即树的节点数量太多时,我们可以重构树,因为有些节点已经没用了,来保证空间的大小,这里我选的是十倍树的大小。
参照代码
#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 = 1e5 + 10;
int cnt;
struct FHQ
{
int child[2], siz, rnk;
ull add, sum, val;
FHQ() = default;
explicit FHQ(const ll val)
: val(val)
{
sum = val;
add = child[0] = child[1] = 0;
siz = 1, rnk = Rand(INT_MIN,INT_MAX);
}
} node[N * 20];
#define left(x) node[x].child[0]
#define right(x) node[x].child[1]
#define siz(x) node[x].siz
#define add(x) node[x].add
#define sum(x) node[x].sum
#define val(x) node[x].val
#define rnk(x) node[x].rnk
inline void push_up(const int curr)
{
siz(curr) = siz(left(curr)) + siz(right(curr)) + 1;
sum(curr) = sum(left(curr)) + sum(right(curr)) + val(curr);
}
//复制节点信息,开辟新版本节点
inline int NewNode(const int idx)
{
node[++cnt] = node[idx];
return cnt;
}
//加上一个数对一个树的信息影响,其实和线段树差不多的
inline void AddLazy(const int curr, const ll val)
{
sum(curr) += siz(curr) * val;
val(curr) += val;
add(curr) += val;
}
//有下传标记开新节点信息维护
inline void push_down(const int curr)
{
if (!add(curr))return;
if (left(curr))
left(curr) = NewNode(left(curr));
if (right(curr))
right(curr) = NewNode(right(curr));
AddLazy(left(curr),add(curr));
AddLazy(right(curr),add(curr));
add(curr) = 0;
}
//分裂记得新版本信息维护
inline void split(const int curr, const int idx, int& x, int& y)
{
if (!curr)
{
x = y = 0;
return;
}
push_down(curr);
if (siz(left(curr)) < idx)
{
x = NewNode(curr);
split(right(curr), idx - siz(left(curr)) - 1,right(x), y);
push_up(x);
}
else
{
y = NewNode(curr);
split(left(curr), idx, x,left(y));
push_up(y);
}
}
//合并也记得新版本信息维护
inline int merge(const int x, const int y)
{
if (!x or !y)return x ^ y;
push_down(x), push_down(y);
int curr;
if (rnk(x) < rnk(y))
{
curr = NewNode(x);
right(curr) = merge(right(curr), y);
}
else
{
curr = NewNode(y);
left(curr) = merge(x,left(curr));
}
push_up(curr);
return curr;
}
inline int merge(const int l, const int mid, const int r)
{
return merge(merge(l, mid), r);
}
//两次分裂拿rson为[l,r]区间节点
inline void Add(int& curr, const int l, const int r, const ll val)
{
int lTree, rTree;
split(curr, r, lTree, rTree);
int lson, rson;
split(lTree, l - 1, lson, rson);
AddLazy(rson, val);
curr = merge(lson, rson, rTree);
}
inline void Copy(int& curr, const int l1, const int r1, const int l2, const int r2)
{
int L11, R11, L12, R12; //对应区间:[1,r1]、[r1+1,n]、[1,l1-1]、[l1,r1]
split(curr, r1, L11, R11);
split(L11, l1 - 1, L12, R12);
int L21, R21, L22, R22; //对应区间:[1,r2]、[r2+1,n]、[1,l2-1]、[l2,r2]
split(curr, r2, L21, R21);
split(L21, l2 - 1, L22, R22);
curr = merge(L12, R22, R11); //由[1,l1-1]+[l2,r2]+[r1+1,n]这三个节点合并成新的根
}
//常规操作,同Add
inline ll Query(int& curr, const int l, const int r)
{
int lTree, rTree;
split(curr, r, lTree, rTree);
int lson, rson;
split(lTree, l - 1, lson, rson);
ll ans = sum(rson);
curr = merge(lson, rson, rTree);
return ans;
}
int a[N];
int n, q;
int root;
int idx;
int version;
//中序遍历找新序列
inline void dfs(const int curr)
{
if (!curr)return;
push_down(curr);
dfs(left(curr));
a[++idx] = val(curr);
dfs(right(curr));
}
//类似笛卡尔树单调栈建树
inline void build()
{
cnt = 0;
stack<int> st;
forn(i, 1, n)
{
int last = 0;
node[++cnt] = FHQ(a[i]);
while (!st.empty() and rnk(st.top()) > rnk(cnt))push_up(last = st.top()), st.pop();
if (!st.empty())
right(st.top()) = cnt;
left(cnt) = last, st.push(cnt);
}
while (!st.empty())
{
if (st.size() == 1)root = st.top();
push_up(st.top()), st.pop();
}
}
//重构
inline void rebuild()
{
idx = 0;
dfs(root);
root = 0;
build();
}
inline void solve()
{
cin >> n >> q;
forn(i, 1, n)cin >> a[i];
build();
forn(i, 1, q)
{
int op;
cin >> op;
if (op == 1)
{
int l, r;
ll val;
cin >> l >> r >> val;
Add(root, l, r, val);
}
else if (op == 2)
{
int l1, r1, l2, r2;
cin >> l1 >> r1 >> l2 >> r2;
Copy(root, l1, r1, l2, r2);
}
else
{
int l, r;
cin >> l >> r;
cout << Query(root, l, r) << endl;
}
if (cnt > 10 * n)rebuild(); //树节点太多就重构保证空间大小
}
}
signed int main()
{
// MyFile
Spider
//------------------------------------------------------
// clock_t start = clock();
int test = 1;
// read(test);
cin >> test;
test = 1;
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;
}
如果你被卡常了,可以考虑调整重构程度去优化,以及一些代码细节比如能用 \(int\) 的地方别用 \(long\ long\),需要再临时变,减少常数。不过亲测不咋卡常,正常写法能过。如果在C语言中文网被卡常的话,可以考虑快读或者指令集之类的一些优化手段。
PS:如果存在区间覆盖操作且数据随机,可以使用 ODT 来做,这里给一份本题 ODT 解法,只需要加上一个区间覆盖和数据随机限制即可。
参照代码
#include <bits/stdc++.h>
// #pragma GCC optimize("Ofast,unroll-loops")
// #pragma GCC optimize(2)
#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 = 5e5 + 10;
int n, q;
int a[N];
struct ODT
{
int l, r;
mutable ll val;
bool operator<(const ODT& other) const
{
return l < other.l;
}
};
set<ODT> node;
typedef set<ODT>::iterator iter;
inline iter split(const int pos)
{
auto it = node.lower_bound(ODT(pos, 0, 0));
if (it != node.end() and it->l == pos)return it;
--it;
if (it->r < pos)return node.end();
auto [l,r,val] = *it;
node.erase(it), node.insert(ODT(l, pos - 1, val));
return node.insert(ODT(pos, r, val)).first;
}
inline void add(const int l, const int r, const ll val)
{
auto itr = split(r + 1), itl = split(l);
for (auto it = itl; it != itr; ++it)it->val += val;
}
tll tmp[N];
inline void copy(const int l1, const int r1, const int l2, const int r2)
{
int idx = 0;
const int diff = l2 - l1;
auto itr = split(r2 + 1), itl = split(l2);
for (auto it = itl; it != itr; ++it)tmp[++idx] = tll(it->l, it->r, it->val);
itr = split(r1 + 1), itl = split(l1);
node.erase(itl, itr);
forn(i, 1, idx)
{
auto [l,r,val] = tmp[i];
l -= diff, r -= diff;
node.insert(ODT(l, r, val));
}
}
inline ll query(const int l, const int r)
{
ll ans = 0;
auto itr = split(r + 1), itl = split(l);
for (auto it = itl; it != itr; ++it)ans += (it->r - it->l + 1) * it->val;
return ans;
}
inline void solve()
{
cin >> n >> q;
forn(i, 1, n)cin >> a[i], node.insert(ODT(i, i, a[i]));
forn(i, 1, q)
{
int op;
cin >> op;
if (op == 1)
{
int l, r;
ll val;
cin >> l >> r >> val;
add(l, r, val);
}
else if (op == 2)
{
int l1, r1, l2, r2;
cin >> l1 >> r1 >> l2 >> r2;
copy(l1, r1, l2, r2);
}
else
{
int l, r;
cin >> l >> r;
cout << query(l, r) << endl;
}
}
}
signed int main()
{
// MyFile
Spider
//------------------------------------------------------
// clock_t start = clock();
int test = 1;
// read(test);
cin >> test;
test = 1;
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;
}