QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#240802#7103. Red Black Treeucup-team2307#AC ✓994ms41140kbC++203.6kb2023-11-05 19:24:252023-11-05 19:24:25

Judging History

你现在查看的是最新测评结果

  • [2023-11-05 19:24:25]
  • 评测
  • 测评结果:AC
  • 用时:994ms
  • 内存:41140kb
  • [2023-11-05 19:24:25]
  • 提交

answer

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
#define int ll

const int N=1e5+100,L=20,INF=1e18;

bool r[N];
vector<pair<int,int>> g[N];

int where[N];
int par[N][L];
int dep[N],sum[N];
int timer,tin[N],tout[N];

void dfs(int v,int p,int wh,int d,int s)
{
    if(r[v])
        wh=v,
        s=0;
    where[v]=wh;
    dep[v]=d;
    sum[v]=s;

    par[v][0]=p;
    for(int i=1;i<L;i++)
        par[v][i]=par[par[v][i-1]][i-1];

    timer++;
    tin[v]=timer;

    for(auto[to,w]:g[v])
        if(to!=p)
            dfs(to,v,wh,d+1,s+w);

    tout[v]=timer;
}

int lca(int u,int v)
{
    if(dep[u]>dep[v])
        swap(u,v);
    for(int i=L-1;i>=0;i--)
        if(dep[v]-dep[u]>=(1<<i))
            v=par[v][i];
    if(u==v)
        return u;
    for(int i=L-1;i>=0;i--)
        if(par[v][i]!=par[u][i])
            v=par[v][i],
            u=par[u][i];
    return par[v][0];
}

struct SegTree {
    int n;
    vector<int> tree;
    SegTree(int N) : n(N), tree(2*n,-INF) {}
    void upd(int pos, int val) {
        for(tree[pos += n] = val; pos /= 2;)
            tree[pos] = max(tree[2 * pos], tree[2 * pos + 1]);
    }
    int query(int l, int r) {
        r++;
        int res = 0;
        for(l += n, r += n; l < r; l>>=1,r>>=1) {
            if(l & 1) res = max(res,tree[l++]);
            if(r & 1) res = max(res,tree[--r]);
        }
        return res;
    }
} seg(N);

int solve(vector<int>&vers)
{
//    return 123;
    sort(vers.begin(),vers.end(),[](int u,int v){return tin[u]<tin[v];});
    for(int v:vers) {
        seg.upd(tin[v], sum[v]);
    }
    int ans=seg.query(0,N-1);
    auto process=[&](int l)
    {
        int cur= max({
            seg.query(0,tin[l]-1),
            seg.query(tin[l],tout[l])-sum[l],
            seg.query(tout[l]+1,N-1),
        });
        ans=min(ans,cur);
    };
    for(int l:vers)
        process(l);
    for(int i=0;i+1<vers.size();i++)
    {
        int l=lca(vers[i],vers[i+1]);
        process(l);
    }
    for(int v:vers)
        seg.upd(tin[v],-INF);
    return ans;
}

signed main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);

    mt19937 rng;

    map<int,vector<int>> query;
    int T=10;
    cin>>T;
    while(T--)
    {
        int n=1e5,m=1,q=2e5;
        cin>>n>>m>>q;
        for(int i=1;i<=n;i++)
            g[i].clear(),
            r[i]=false;
        for(int i=1;i<=m;i++)
        {
            int x=1;
            cin>>x;
            r[x]=true;
        }
        for(int i=2;i<=n;i++)
        {
//            int u=rng()%(i-1)+1,v=i,w=rng()%1000000000+1;
            int u,v,w;
            cin>>u>>v>>w;
            g[u].emplace_back(v,w);
            g[v].emplace_back(u,w);
        }
        timer=0;
        dfs(1,0,0,0,0);
        while(q--)
        {
            int k=1;
            cin>>k;
            query.clear();
            while(k--)
            {
//                int x=rng()%n+1;
                int x;
                cin>>x;
                query[where[x]].push_back(x);
            }
            pair<int,int> first(0,0);
            pair<int,int> second(0,0);
            for(auto&[red,vec]:query)
            {
                int mx=0;
                for(int v:vec)
                    mx=max(mx,sum[v]);
                pair<int,int> cur(mx,red);
                second=max(second,cur);
                if(first<second)
                    swap(first,second);
            }
            cout<<max(solve(query[first.second]),second.first)<<"\n";
        }
    }
}

这程序好像有点Bug,我给组数据试试?

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 2ms
memory: 9852kb

input:

2
12 2 4
1 9
1 2 1
2 3 4
3 4 3
3 5 2
2 6 2
6 7 1
6 8 2
2 9 5
9 10 2
9 11 3
1 12 10
3 3 7 8
4 4 5 7 8
4 7 8 10 11
3 4 5 12
3 2 3
1 2
1 2 1
1 3 1
1 1
2 1 2
3 1 2 3

output:

4
5
3
8
0
0
0

result:

ok 7 lines

Test #2:

score: 0
Accepted
time: 994ms
memory: 41140kb

input:

522
26 1 3
1
1 4 276455
18 6 49344056
18 25 58172365
19 9 12014251
2 1 15079181
17 1 50011746
8 9 2413085
23 24 23767115
22 2 26151339
26 21 50183935
17 14 16892041
9 26 53389093
1 20 62299200
24 18 56114328
11 2 50160143
6 26 14430542
16 7 32574577
3 16 59227555
3 15 8795685
4 12 5801074
5 20 57457...

output:

148616264
148616264
0
319801028
319801028
255904892
317070839
1265145897
1265145897
1072765445
667742619
455103436
285643094
285643094
285643094
317919339
0
785245841
691421476
605409472
479058444
371688030
303203698
493383271
919185207
910180170
919185207
121535083
181713164
181713164
181713164
181...

result:

ok 577632 lines

Extra Test:

score: 0
Extra Test Passed