我说我题解没看懂你信吗
题意
给你个链,每次操作一下然后合并,求得到的树上任意两点的 LCA。
Step 0
考虑如果是两个叶子,代表链 和 怎么做。
我们把区间 内的所有链都拉出来,显然找到所有修改中涉及到最高的点,其父亲就是LCA,因为区间内比这个高的点都是没动过的。
这个用 ST 表维护一下 RMQ,然后考虑到这段区间任意一个点 的部分结构都是一样的,把节点询问挂到 上,转化成求某个链深度为 的点的坐标。然后就可以按 排序离线做了。
Step 1
考虑不是两个叶子怎么做。
首先只要经过两个点,任意选取链,然后特判一下祖先关系求 LCA 都是没问题的。这里我求的是第一个经过的链。
先不考虑怎么求链,我们把链求出来后有两种情况:
- 两点是祖先关系,亦即 这里 指的是之前求出来区间最小值深度 -1,就是说无论区间内怎么改,高的那个链都是不动的,直接取深度小的进行询问。
- 两点是兄弟关系,原来那个式子仍然成立。
Step 2
问题变成了怎么求链。如果直接二分,就是用树状数组维护每层点的编号,改变序列作为后缀加,然后查询位置与预期位置关系。
但你要用树状数组加贡献是 的,复杂度爆炸。但是考虑到每次二分你加的贡献实质上是一样的,只有 check 的部分不同,于是可以整体二分,直接全局维护之前的贡献,遍历完一个区间就把所有贡献加到 BIT 上,查询/修改复杂度 ,总复杂度 ,过不去。
表面上这个东西复杂度卡满了,但是其实这个可以优化。
因为每层你只想知道这个修改在 内生不生效,所以没必要按照修改时间排序,直接按照修改位置从小到大排序,修改在询问之前,前面的修改一定影响后面的,所以每层 check 的时候,直接记录前面有几个修改生效和传到这层的之前的累积修改,把询问下传到右侧的时候同时把之前的贡献传下去,就不用 BIT 了。感觉 这篇文章 提到这个优化的时候讲的很清楚。然后按顺序分裂到两个子问题也是仍然有序的。
直接把这几个部分拼到一起,合成了本题最长已知代码。
最终思路
把所有查询点转换成链和深度,用整体二分求,然后用 RMQ 维护 LCA,注意处理祖先关系。最后把答案再离线一遍,求出来点的坐标。
# include <bits/stdc++.h>
# define rep(i, a, b) for(int i = (a);i <= (b);i ++)
# define lop(i, a, b) for(int i = (a);i < (b);i ++)
# define dwn(i, a, b) for(int i = (a);i >= (b);i --)
# define int64 long long
# define ___main signed main()
# define f64 double
# define f32 float
# define f128 long double
# define int128 __int128
# define i128 ((__int128)1)
# define int32 signed
# define un unsigned
# define int16 short
# define int8 char
# define ret return
# define inl inline
# define reg register
# define fn void
# define elif else if
# define pb push_back
# define pp pop_back
# define eb emplace_back
using std::cout;
using std::cin;
# define CASES() int T;cin >> T;while(T--)
# define FaILurEg(s) freopen(s".in","r",stdin);freopen(s".out","w",stdout)
#if defined(_MSC_VER)
# define FORCE_INLINE __forceinline
#else
# define FORCE_INLINE __attribute__((always_inline)) inline
#endif
namespace packin {
template<typename T>struct _Arr {T *arr;int size;
FORCE_INLINE T& operator [](int i) {ret arr[i];}};
template<typename T>FORCE_INLINE fn read(T &x) {cin >> x;}
template<typename T>FORCE_INLINE fn read(_Arr<T>a) {rep(i, 1, a.size) cin >> a.arr[i];}
template<typename T>FORCE_INLINE fn read(T *a, int siz) {rep(i, 1, siz) cin >> a[i];}
template<typename T, typename... Args>FORCE_INLINE void read(T &x, Args&... args) {read(x);read(args...);}
inline void print_impl(const char* s) {while (*s) {cout << *s++;}}
template<typename T, typename... Args>inline void print_impl(const char* s, const T& x, const Args&... args) {while (*s) {if (s[0] == '{' && s[1] == '}') {cout << x;print_impl(s + 2, args...);return;}cout << *s++;}}
template<typename... Args>inline void print(const char* s, const Args&... args) {print_impl(s, args...);}
inline void error_impl(const char*s){while(*s)std::cerr<<*s++;}template<class T,class...A>inline void error_impl(const char*s,const T&x,const A&...a){while(*s){if(s[0]=='{'&&s[1]=='}'){std::cerr<<x;error_impl(s+2,a...);return;}std::cerr<<*s++;}}template<class...A>inline void error(const char*s,const A&...a){error_impl(s,a...);}
}
template<typename T>FORCE_INLINE T min(T a, T b) {ret (a < b)?a:b;}
template<typename T>FORCE_INLINE T max(T a, T b) {ret (a > b)?a:b;}
int Q;
constexpr int N = 500005;
int n, a[N], q;
namespace DC {
struct qry{
int id, tp, pos;// 0 chg 1 qry
int need, temp;
FORCE_INLINE bool operator <(qry &b) {
if(pos == b.pos) ret tp < b.tp;
ret pos < b.pos;
}
};
int ans[N << 2];
void DC(int l, int r, std::vector<qry>&g) {// id from l to r
//packin::error("{} {}:\n", l, r);
if(l == r) {
for(auto &q : g) if(q.tp == 1) ans[q.id] = l;
ret ;
}
int mid = (l + r) >> 1;
std::vector<qry> ql, qr;// 内部已经按照 pos 排序
int tag = 0;
for(auto &q : g) {
if(q.tp == 0) {
if(q.id <= mid) tag --, ql.eb(q); else qr.eb(q);
//packin::error("{} {} {};\n", q.id, q.pos, tag);
}
else {
//packin::error("{} {} {} {} {};\n", q.id, q.pos, tag, q.need, q.temp);
if(tag + q.temp <= q.need) ql.eb(q);else q.temp += tag, qr.eb(q);
}
}
DC(l, mid, ql);
DC(mid + 1, r, qr);
}
}
int L[N], st[N][20];
struct hyw {
int r1, c1, r2, c2;
} h[N];
struct qry2 {int id, r;};
std::pair<int, int> Ans[N];
std::vector<qry2> tcs[N];
int ps[N];
void add(int x,int v){
while(x <= n) ps[x] += v, x += (x&-x);
}
int64 qry(int x) {
int64 ans = 0;
while(x) ans += ps[x], x -= (x&-x);
ret ans;
}
___main {
std::ios::sync_with_stdio(0);cin.tie(0);
using namespace packin;
read(n);
std::vector<DC::qry>Q;
st[0][0] = n + 1;
rep(i, 1, n) {
read(a[i]);st[i][0] = a[i];
Q.pb({i, 0, a[i], 0, 0});
}
rep(j, 1, 19)
rep(i, 0, n)
st[i][j] = min(st[i][j - 1], st[min(i + (1 << (j - 1)), max(0, n - (1 << (j - 1)) + 1))][j - 1]);
read(q);
rep(i, 1, q) {
int r1, c1, r2, c2;
read(r1);read(c1);read(r2);read(c2);
L[i] = Q.size();
Q.pb({(int)Q.size(),1,r1, c1, r1});
Q.pb({(int)Q.size(),1,r2, c2, r2});
h[i] = {r1, c1, r2, c2};
}
std::sort(Q.begin(), Q.end());
DC::DC(0, n, Q);
rep(i, 1, q) {
//error("test: ans1 {} | ans2 {}\n", DC::ans[L[i]], DC::ans[L[i] + 1]);
if(DC::ans[L[i]] == DC::ans[L[i] + 1])
tcs[DC::ans[L[i]]].pb({i, min(h[i].r1, h[i].r2)});
else {
int LL = DC::ans[L[i]], RR = DC::ans[L[i] + 1];
if(LL > RR) std::swap(LL, RR);
int d = min(st[LL+1][std::__lg(RR - LL)], st[RR - (1 << std::__lg(RR - LL)) + 1][std::__lg(RR - LL)]);
tcs[DC::ans[L[i]]].pb({i, min(min(h[i].r2, h[i].r1), d- 1)});
}
}
rep(i, 1, n) add(i, 1);
rep(i, 0, n) {
if(i) add(a[i], -1);
for(auto [id, r] : tcs[i])
Ans[id] = {r, qry(r)};
}
rep(i, 1, q) print("{} {}\n", Ans[i].first, Ans[i].second);
ret 0;}