跳到正文

题解

题解:P6157 有趣的游戏

fush约 7 分钟
原文
文章信息与版本

文章信息

发布
2024年11月14日
收录
2025年12月12日
更新
2026年11月18日
篇幅
约 7 分钟
获赞
3
题目
P6157

版本

  1. v1当前

明显,wxw_x 一定是严格次大值的,wyw_y 一定是最大值。

证明: 我们可以假设 x<yx < y,那么 x mod y=xx \bmod y = x,而 y mod x<wxy \bmod x < w_x,所以若 x<yx < y,有 x mod y>y mod xx \bmod y > y \bmod x。 当 x=yx = y 显然两种均为 00。

因此,要使 x mod yx\bmod y 最大,wxw_x 一定是严格次大值的,wyw_y 一定是最大值。

于是问题就变得十分简单了:

在链上记录严格次大值与最大值,然后找到去掉这两个值后的严格次大值。

第一个询问用树链剖分和线段树维护,第二个询问可以用 multiset 维护。

整体代码要 3KB,一定要注意细节,耐心调试。

C++
#include<bits/stdc++.h>
using namespace std;
#define endl '\n'
constexpr int N = 1e5 + 10;
int id[N], top[N], siz[N], dep[N], son[N], rnk[N], a[N], to[N << 1], nex[N << 1], tot, head[N], fa[N], n, m;
multiset<int>s;
#define PII pair<int, int>
#define mp make_pair
#define ls (u << 1)
#define rs (u << 1 | 1)
#define mid (l + r >> 1)
PII w[N << 2];
PII merge(PII x, PII y){ //第二项是严格次小值
    int f, s = max(x.second, y.second);
    if(x.first > y.first)f = x.first, s = max(y.first, s);
    else if(x.first < y.first)f = y.first, s = max(x.first, s);
    else f = y.first;
    return mp(f, s);
}
void build(int u, int l, int r){
    if(l == r)return void(w[u] = mp(a[l], -1));
    build(ls, l, mid), build(rs, mid + 1, r);
    w[u] = merge(w[ls], w[rs]);
}
void modify(int u, int l, int r, int pos, int v){
    if(l == r)return void(w[u] = mp(v, -1));
    (pos <= mid) ? (modify(ls, l, mid, pos, v)) : modify(rs, mid + 1, r, pos, v);
    w[u] = merge(w[ls], w[rs]);
}
PII query(int u, int l, int r, int L, int R){
    if(L <= l && r <= R)return w[u];
    if(R <= mid)return query(ls, l, mid, L, R);
    if(mid < L)return query(rs, mid + 1, r, L, R);
    return merge(query(ls, l, mid, L, R), query(rs, mid + 1, r, L, R));
}
#undef u
#undef mid
void add(int u, int v){to[++tot] = v, nex[tot] = head[u], head[u] = tot;}
void dfs1(int x, int f){
    dep[x] = dep[fa[x] = f] + (siz[x] = 1);
    for(int i = head[x], v; i; i = nex[i])
        if((v = to[i]) != f){
            dfs1(v, x), siz[x] += siz[v];
            if(siz[v] > siz[son[x]])son[x] = v;
        }
}
void dfs2(int x, int f){
    top[x] = f, a[id[x] = ++tot] = rnk[x];
    if(son[x]) dfs2(son[x], f);
    for(int i = head[x], v; i; i = nex[i]) 
        if(!id[v = to[i]])dfs2(v, v);
}
PII query(int l, int r){
    PII ans = mp(-1, -1);
    while(top[l] != top[r]){
        if(dep[top[l]] < dep[top[r]])swap(l, r);
        ans = merge(ans, query(1, 1, n, id[top[l]], id[l])), l = fa[top[l]];
    }
    if(id[l] > id[r])swap(l, r);
    return merge(ans, query(1, 1, n, id[l], id[r]));
}
int32_t main(){
    ios::sync_with_stdio(0);
    cin.tie(0), cout.tie(0);
    cin >> n;
    for(int i = 1, u, v; i < n; i++)
        cin >> u >> v, add(u, v), add(v, u);
    for(int i = 1; i <= n; i++)
        cin >> rnk[i], s.insert(rnk[i]);
    tot = 0, dfs1(1, 1), dfs2(1, 1), build(1, 1, n);
    cin >> m;
    while(m--){
        int op, l, r;
        cin >> op >> l >> r;
        if(op){
            PII ans = query(l, r);
            if(ans.second < 0){
                cout << -1 << endl;
                continue;
            }
            s.erase(s.find(ans.first)), s.erase(s.find(ans.second));
            cout << ans.second << " " << *(--s.lower_bound(*--s.end())) << endl;
            s.insert(ans.first), s.insert(ans.second);
        }
        else{
            s.erase(s.find(rnk[l])), s.insert((rnk[l] += r));
            modify(1, 1, n, id[l], rnk[l]);
        }
    }
    return 0;
}
原文