2025杭电多校第二场 苹果树、子集、半 个人题解

#权值线段树 #红黑树

题目

![[Pasted image 20250726191838.png]]

思路

由于 n n n的排列中的每个元素都会在两个数组中出现,我们取每个元素在两个数组中出现的序号作为其在二维平面上的坐标,如:
a : 2 1 5 4 3 6 b : 1 4 6 5 2 3 \begin{align} &a:2\quad 1\quad 5 \quad 4\quad 3 \quad 6\\ \\ &b:1\quad 4\quad 6\quad 5\quad 2\quad 3 \end{align} a:215436b:146523
则有:
![[Pasted image 20250726192746.png]]

以数字4为例,我们想要计算数组 a a a ≤ 4 \leq4 4的位置有几个数字,并上数组 b b b ≤ 2 \leq 2 2的位置有几个数字,那么肉眼观察共有 1 , 2 , 5 1,2,5 1,2,5这三个数。那么取他们的补集,即图中蓝色区域共有 3 , 4 , 6 3,4,6 3,4,6三个数字,数字4的答案即为 n − 3 = 3 n-3=3 n3=3

因此我们可以求出所有点的右上方有多少个点 c n t cnt cnt来计算出答案 n − c n t n-cnt ncnt

如何求每个点的右上方有多少个点?
我们可以从图中 a a a轴右端开始遍历这些点,每遍历到一个就把他在的 b b b轴上的值塞进权值线段树,随后求其排名即可知道在他右上方有多少个点了~

红黑树代码量小,运算速度快,因此代码实现就直接用红黑树写啦

代码实现

#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define rep(i,a,b) for(ll i=(a);i<=(b);i++)
#define see(stl) for(auto&ele:stl)cout<<ele<<" "; cout<<'\n';

const int N=1e6+5;

struct node{
    int x,y,num;
};
bool cmp(node a,node b){
    return a.x>b.x;
}

#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
tree<pair<int,int>,null_type,greater<pair<int,int>>,rb_tree_tag,
tree_order_statistics_node_update>s;

map<int,node>mp;
int ans[N];
void eachT(){
    s.clear();mp.clear();
    int n;cin>>n;
    vector<node>a;
    a.resize(n+1);
    rep(i,1,n){
        int x;cin>>x;
        a[x].num=x,a[x].x=i;
    }
    rep(i,1,n){
        int x;cin>>x;
        a[x].y=i;
    }
    sort(a.begin()+1,a.end(),cmp);
    rep(i,1,n){
        int y=a[i].y,num=a[i].num;
        s.insert({y,num});
        int rk=s.order_of_key({y,num})+1;
        ans[num]=n-rk;
    }
    rep(i,1,n)cout<<ans[i]<<" ";
    cout<<'\n';
}

int main(){
    ios::sync_with_stdio(0);
    cin.tie(0),cout.tie(0);
    int t=1;
    cin>>t;
    while(t--)eachT();
    return 0;
}

子集

#数学 #线性基 #dp #线性dp #dfs

题目

![[Pasted image 20250726193731.png]]

思路

本题需要前置知识:贪心法求解线性基

对于n个数求异或和的最大值,可以通过求解其线性基来解决。
但是本题添加了特殊的限制条件:不可以选相邻的数,因此想到了dfs来枚举所有选取的可能。

dfs传入当前处理的位置 p o s pos pos,每次都贪心法插入当前位置的数字进入线性基数组中,随后dfs下两个状态: d f s ( p o s + 2 ) , d f s ( p o s + 3 ) dfs(pos+2),dfs(pos+3) dfs(pos+2),dfs(pos+3)(不可以选取相邻的)

p o s > n pos>n pos>n时通过异或线性基数组来求解最大异或和即可

代码实现

#include <iostream>
#include <vector>
#include <cmath>
#include <string.h>
#include<algorithm>
#include<unordered_set>
using namespace std;
const double pi = acos(-1);
const double eps = 1e-6;
#define ll long long
#define rep(i,a,b) for(ll i=(a);i<=(b);i++)
#define per(i,a,b) for(ll i=(a);i>=(b);i--)
#define see(stl) for(auto&ele:stl)cout<<ele<<" "; cout<<'\n';
const int N = 65;

int n;
ll a[N], ans,b[N][N];
void dfs(int pos) {
    if (pos > n) {  
        ll res=0;
        per(i,n,0){
            if(!(res&(1ll<<i)))res^=b[pos][i];
        }
        ans=max(ans,res);
        return;
    }
    if(pos>0){
        ll now=a[pos];   
        per(i,n,0){
            if(now&(1ll<<i)){
                if(b[pos][i])now^=b[pos][i];
                else{
                    b[pos][i]=now;
                    break;
                }
            }
        }           
        rep(i,0,n)b[pos+2][i]=b[pos][i];   
        rep(i,0,n)b[pos+3][i]=b[pos][i];
    }   
    dfs(pos + 2),dfs(pos + 3);
}
void solve() {
    memset(b,0,sizeof(b));
    ans = 0;
    cin >> n;
    rep(i, 1, n)cin >> a[i];
    dfs(-1);
    cout << ans << '\n';
}
signed main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    int t = 1;
    cin >> t;
    while (t--)solve();
    return 0;
}

苹果树

#树链剖分 #线段树

题目

![[Pasted image 20250726194344.png]]

![[Pasted image 20250726194353.png]]

思路

对于操作1,就是非常模版的树链剖分+线段树维护树上区间最值以及单点修改

对于操作2,暴力的做法便是修改其父亲以及其所有儿子节点,复杂度为 o ( n ) o(n) o(n),在菊花图的情形下必将无法通过。

因此考虑通过懒标记 a d d add add来将操作2延时进行:
![[Pasted image 20250726200453.png]]

例如对节点 u u u进行操作2,那么给他本身的 a d d add add参数 + = z +=z +=z,暴力修改其父节点以及重儿子节点的值。

如图中的剖分方式,当对黄、绿、红链执行query操作时,访问其链顶(图中的三个 s o n son son)的父亲(图中的 u u u节点)的 a d d add add值即可

代码实现

#include<iostream>
#include<vector>
using namespace std;
#define ll long long
#define rep(i,a,b) for(ll i=(a);i<=(b);i++)
#define see(stl) for(auto&ele:stl)cout<<ele<<" "; cout<<'\n';
#define lc p<<1
#define rc p<<1|1

//const int N=1e5+5;
struct tree {
    ll l, r, ma;
};//
vector<tree>tr;
ll n;
void pushup(ll p) {
    tr[p].ma = max(tr[lc].ma, tr[rc].ma);
}
vector<ll>w, nw;
void build(ll p, ll l, ll r) {
    if (l > n || r > n)return;
    tr[p] = { l,r,nw[l] };
    if (l == r)return;
    ll m = l + (r - l) / 2;
    build(lc, l, m), build(rc, m + 1, r);
    pushup(p);
}

void update(ll p, ll x, ll y, ll k) {
    if (x == 0) {
        x++;
    }
    if (x > y) {
        return;
    }
    //if(x>n||y>n)return;
    if (x <= tr[p].l && tr[p].r <= y) {
        tr[p].ma += k; 
        return;
    }
    ll mid = tr[p].l + (tr[p].r - tr[p].l) / 2;
    if (x <= mid)update(lc, x, y, k);
    if (y > mid)update(rc, x, y, k);
    pushup(p);
}

ll query(ll p, ll x, ll y) {
    //if(x>n||y>n)return 0;
    ll res = 0;
    if (x <= tr[p].l && tr[p].r <= y) {
        return tr[p].ma;
    }
    ll mid = tr[p].l + (tr[p].r - tr[p].l) / 2;
    if (x <= mid)res = max(res, query(lc, x, y));
    if (y > mid)res = max(res, query(rc, x, y));
    return res;
}

struct node {
    vector<ll>e;
    ll fa, dep, son, siz, top, id, add;
    node() {
        e.clear();
        fa = dep = son = siz = top = id = add = 0;

    }
};
vector<node>a;
void dfs1(ll u, ll father) {
    a[u].fa = father, a[u].dep = a[father].dep + 1, a[u].siz = 1;
    for (auto& ele : a[u].e) {
        if (ele == father)continue;
        dfs1(ele, u);
        a[u].siz += a[ele].siz;
        if (a[a[u].son].siz < a[ele].siz)a[u].son = ele;
    }
}
ll cnt = 0;
void dfs2(ll u, ll t) {
    a[u].top = t; a[u].id = ++cnt; nw[cnt] = w[u];
    if (!a[u].son)return;
    dfs2(a[u].son, t);
    for (auto& ele : a[u].e) {
        if (ele == a[u].fa || ele == a[u].son)continue;
        dfs2(ele, ele);
    }
}

void update_path(ll u, ll v, ll k) {
    while (a[u].top != a[v].top) {
        if (a[a[u].top].dep < a[a[v].top].dep)swap(u, v);
        update(1, a[a[u].top].id, a[u].id, k);
        u = a[a[u].top].fa;
    }
    if (a[u].dep < a[v].dep)swap(u, v);
    update(1, a[v].id, a[u].id, k);
}

ll query_path(ll u, ll v) {
    ll res = 0;
    while (a[u].top != a[v].top) {
        if (a[a[u].top].dep < a[a[v].top].dep)swap(u, v);
        ll top = a[u].top, topf = a[top].fa;
        res = max(res, query(1, a[top].id, a[top].id) + a[topf].add);
        res = max(res, query(1, a[a[u].top].id, a[u].id));
        u = a[a[u].top].fa;
    }
    if (a[u].dep < a[v].dep)swap(u, v);
    ll top = a[u].top, topf = a[top].fa;
    if (v == top)res = max(res, query(1, a[top].id, a[top].id) + a[topf].add);
    res = max(res, query(1, a[v].id, a[u].id));
    return res;
}


void eachT() {
    int m; cin >> n >> m;
    a.clear(); a.resize(n + 1);
    tr.clear(); tr.resize(4 * n);
    w.clear(); nw.clear();
    w.resize(n + 1); nw.resize(n + 1);
    cnt = 0;
    rep(i, 1, n)cin >> w[i];
    rep(i, 1, n - 1) {
        ll x, y; cin >> x >> y;
        a[x].e.push_back(y), a[y].e.push_back(x);
    }

    dfs1(1, 0), dfs2(1, 1);
    build(1, 1, n);

    rep(i, 1, m) {
        int opt; cin >> opt;
        if (opt == 1) {
            ll x, y; cin >> x >> y;
            cout << query_path(x, y) << '\n';
        }
        else {
            ll x, z;
            cin >> x >> z;
            ll hson = a[a[x].son].id, father = a[a[x].fa].id;
            update(1, hson, hson, z);
            if (a[x].fa > 0) { update(1, father, father, z); }
            a[x].add += z;
        }
    }
}

int main() {
    ios::sync_with_stdio(0);
    cin.tie(0), cout.tie(0);
    int t = 1;
    cin >> t;
    while (t--)eachT();
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值