답안 #968559

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
968559 2024-04-23T15:40:39 Z anton Prize (CEOI22_prize) C++17
0 / 100
1808 ms 547192 KB
#include<bits/stdc++.h>

using namespace std;
#define int long long
#define pii pair<int, int>


const int P = 22;
struct Tree{
    int len;
    vector<pii> anc;
    vector<int> dpth;
    vector<vector<pii>> ch;
    vector<vector<pii>> bl;

    pii& get_ch(int u, int v){
        int cur=  0;
        for(int step = (1<<22); step>=1; step/=2){
            if(cur+step<ch[u].size()){
                if(ch[u][cur+step].first<=v){
                    cur+=step;
                }
            }
        }
        return ch[u][cur];
    }

    void dfs(int u, int d){
        dpth[u]= d;
        for(auto v: ch[u]){
            dfs(v.first, d+1);
        }
    }
    int root =0;
    Tree(int sz){
        len = sz;
        anc.resize(sz);
        ch.resize(sz);
        dpth.resize(sz);
        for(int i = 0; i<sz; i++){
            cin>>anc[i].first;
            
            if(anc[i].first ==-1){
                root = i;
            }
            else{
                anc[i].first--;
                ch[anc[i].first].push_back({i, 0});

            }
        }
        for(int i = 0; i<sz; i++){
            sort(ch[i].begin(), ch[i].end());
        }
        dfs(root, 0);
    }

    void select(int u, int need, vector<int>& s, vector<pii>& queries){
        if(need ==s.size()){
            return;
        }
        if(u!=root){
            queries.push_back({anc[u].first, u});
        }
        s.push_back(u);
        for(auto v: ch[u]){
            select(v.first, need, s, queries);
        }
    }

    void build_bl(){
        bl.resize(22, vector<pii>(len));
        for(int i = 0; i<len; i++){
            if(i!=root){
                bl[0][i]  =anc[i];
            }
            else{
                bl[0][i] = {root, 0};
            }
        }
        for(int p = 1; p<P; p++){
            for(int i = 0; i<len; i++){
                bl[p][i] = {bl[p-1][bl[p-1][i].first].first, bl[p-1][i].second + bl[p-1][bl[p-1][i].first].second};
            }
        }
    }

    int sum_on_path(int u, int v){
        if(dpth[u]<dpth[v]){
            swap(u, v);
        }
    
        //cout<<"p "<<u<<" "<<v<<endl;
        int res= 0;
        for(int step =P-1; step>=0; step--){
            if(dpth[bl[step][u].first] >= dpth[v]){
                res += bl[step][u].second;
                u = bl[step][u].first;
            }
        }
        for(int step =P-1; step>=0; step--){
            if(bl[step][u].first != bl[step][v].first){
                res += bl[step][u].second + bl[step][v].second;
                u = bl[step][u].first;
                v = bl[step][v].first;
            }
        }
        return res;
    }
};

signed main(){
    
    int n, k, q, t;
    cin>>n>>k>>q>>t;
    pair<Tree, Tree> tr = {Tree(n), Tree(n)};

    vector<int> selected;
    vector<pii> queries;
    tr.first.select(tr.first.root, k, selected, queries);

    for(auto e: selected){
        cout<<e+1<<" ";
    }
    cout<<endl;
    for(auto e: queries){
        cout<<"? "<<e.first+1<<" "<<e.second+1<<'\n';
    }

    cout<<"!"<<endl;

    for(auto e: queries){
        int a, b, c, d;
        cin>>a>>b>>c>>d;
        tr.first.get_ch(e.first, e.second).second = b;
        tr.first.anc[e.second].second=  b;
        //cout<<"anc "<<e.second<<endl;
    }

    tr.first.build_bl();

    vector<pii> quest;
    for(int i = 0; i<t; i++){
        pii q;
        cin>>q.first>>q.second;
        quest.push_back(q);
    }
    for(int i = 0; i<t; i++){
        int a = quest[i].first, b= quest[i].second;
        a--, b--;
        int r= tr.first.sum_on_path(a, b);
        cout<<r<<" "<<r<<'\n';
    }
    cout<<endl;
}

Compilation message

Main.cpp: In member function 'std::pair<long long int, long long int>& Tree::get_ch(long long int, long long int)':
Main.cpp:19:24: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   19 |             if(cur+step<ch[u].size()){
      |                ~~~~~~~~^~~~~~~~~~~~~
Main.cpp: In member function 'void Tree::select(long long int, long long int, std::vector<long long int>&, std::vector<std::pair<long long int, long long int> >&)':
Main.cpp:59:17: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   59 |         if(need ==s.size()){
      |            ~~~~~^~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1344 ms 274840 KB Output is correct
2 Correct 1238 ms 275080 KB Output is correct
3 Runtime error 716 ms 254804 KB Execution killed with signal 13
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 891 ms 274908 KB Execution killed with signal 13
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 839 ms 272708 KB Execution killed with signal 13
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 1758 ms 544804 KB Execution killed with signal 13
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 1808 ms 547192 KB Execution killed with signal 13
2 Halted 0 ms 0 KB -