Intention:
There are a total of n positions, each with an attribute k[i], indicating that the i-th position will be instantaneously transferred to i + k[i] (and this process continues). The task is to determine how many times a point will go out of bounds. Additionally, the system should support modifying the value of k[i].
Answer:
The idea is to move from position i to i + k[i]. If the new position is out of bounds, it connects to the outer root. The problem essentially asks for the depth of a node in the tree structure formed by these transitions.
When a node is splayed to the root (assuming the root remains the original tree's root), its left subtree must be shallower than itself. This is because the access operation ensures that the path from the node to the original root forms a chain. As a result, the size of the left subtree corresponds to the depth of the node, where the root has a depth of 0.
I spent about half a day working on implementing the Link-Cut Tree (LCT) for this problem. Without proper stack space settings on BZOJ, it would have been difficult to get accepted on the first try.
*************************************************
Problem: 2002
User: Lazer2001
Language: C++
Result: Accepted
Time: 1988 ms
Memory: 22324 kb
**************************************************/
#include <bits/stdc++.h>
inline int read() {
register int x, c;
while (isspace(c = getchar()));
for (x = -48 + c; isdigit(c = getchar()); (x *= 10) += c - 48);
return x;
}
template <class T> inline T min(const T& a, const T& b) { return a < b ? a : b; }
template <class T> inline void swap(T& a, T& b) { T c(a); a = b, b = c; }
#define N 200010
class LinkCutTree {
private:
struct node {
int siz;
bool rev_flag;
node *ch[2], *fa;
inline void update() {
siz = ch[0]->siz + ch[1]->siz + 1;
}
} pool[N+1], *root[N], *null;
int n;
inline void push_down(node*& p) {
if (p->rev_flag) {
swap(p->ch[0], p->ch[1]);
if (p->ch[0] != null) p->ch[0]->rev_flag ^= 1;
if (p->ch[1] != null) p->ch[1]->rev_flag ^= 1;
p->rev_flag = 0;
}
}
inline node* newnode(node*& fa) {
static node* tp(pool + 1);
tp->siz = 1;
tp->rev_flag = 0;
return tp->fa = fa, tp->ch[0] = tp->ch[1] = null, tp++;
}
#define isroot(p) (p->fa == null || (p->fa->ch[0] != p && p->fa->ch[1] != p))
#define isrs(p) (p == p->fa->ch[1])
inline void rotate(node* p) {
if (p == null || isroot(p)) return;
bool d(isrs(p));
node* par = p->fa;
par->ch[d] = p->ch[!d];
if (p->ch[!d] != null) p->ch[!d]->fa = par;
if (!isroot(par)) par->fa->ch[isrs(par)] = p;
p->fa = par->fa;
par->fa = p;
p->ch[!d] = par;
par->update();
p->update();
}
node* st[N+1];
inline void splay(node* p) {
if (p == null) return;
int tp;
st[tp = 1] = p;
for (node* t = p; !isroot(t); t = t->fa) st[++tp] = t->fa;
while (tp) push_down(st[tp--]);
while (!isroot(p)) {
if (isrs(p) == isrs(p->fa) && !isroot(p->fa)) rotate(p->fa);
rotate(p);
}
}
Key Features & Advantages
Why Choose a Stereo Amplifier?
stereo sound amplifier,sound amplifier,home audio amplifier,hi fi amp
Guangzhou Aiwo Audio Technology Co., LTD , https://www.aiwoaudio.com