# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
1035039 |
2024-07-26T03:25:07 Z |
pcc |
Prize (CEOI22_prize) |
C++17 |
|
0 ms |
0 KB |
#include <bits/stdc++.h>
using namespace std;
#define pii pair<int,int>
#define fs first
#define sc second
#define ll long long
#define pll pair<ll,ll>
#define al4 array<ll,4>
#define ai4 array<int,4>
const int mxn = 1e6+10;
const int B = 20;
struct Tree{
vector<vector<int>> par;
vector<int> dfn;
vector<pii> eul;
vector<int> dep;
vector<vector<int>> edges;
int pt = 0;
Tree(){}
void init(int n){
pt = 0;
dfn.clear();
eul = vector<pii>(n);
dep = vector<int>(n);
edges = vector<vector<int>>(n);
par = vector<vector<int>>(n,vector<int>(B,0));
}
void add_edge(int a,int b){
edges[a].push_back(b);
}
void dfs(int now = 1,int fa = 1){
eul[now].fs = ++pt;
dfn.push_back(now);
par[now][0] = fa;
for(int i = 1;i<B;i++)par[now][i] = par[par[now][i-1]][i-1];
for(auto nxt:edges[now]){
if(nxt == par[now][0])continue;
par[nxt][0] = now;
dep[nxt] = dep[now]+1;
dfs(nxt,now);
}
eul[now].sc = pt;
}
int LCA(int a,int b){
if(dep[a]<dep[b])swap(a,b);
int d = dep[a]-dep[b];
for(int i = B-1;i>=0;i--){
if(d&(1<<i))a = par[a][i];
}
for(int i = B-1;i>=0;i--){
if(par[a][i] != par[b][i])a = par[a][i],b = par[b][i];
}
return a==b?a:par[a][0];
}
};
Tree t1,t2,vt;
int rt1,rt2;
int N,K,Q,T;
int val1[mxn],val2[mxn];
vector<int> tar;
namespace PEKO{
Tree re;
Tree GO(vector<int> v,Tree& tr){
re.init(tr.edges.size());
sort(v.begin(),v.end(),[&](int a,int b){return tr.eul[a].fs<tr.eul[b].fs;});
int s = v.size();
for(int i = 1;i<s;i++){
v.push_back(tr.LCA(v[i-1],v[i]));
}
sort(v.begin(),v.end(),[&](int a,int b){return tr.eul[a].fs<tr.eul[b].fs;});
v.resize(unique(v.begin(),v.end())-v.begin());
for(int i = 1;i<v.size();i++){
int a = v[i],b = tr.LCA(v[i],v[i-1]);
re.add_edge(a,b);
re.add_edge(b,a);
}
re.dfs(rt1,rt1);
return re;
}
}
vector<pii> req;
vector<ai4> res;
vector<iil> ans;
int C = 0;
void ask(int a,int b){
C++;
assert(C<=K*2-2);
cout<<"? "<<a<<' '<<b<<'\n';
req.push_back(pii(a,b));
res.push_back(ai4({0,0,0,0}));
return;
}
int main(){
ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
cin.exceptions(cin.failbit);
cin>>N>>K>>Q>>T;
t1.init(N+1);t2.init(N+1);
for(int i = 1;i<=N;i++){
int p;
cin>>p;
if(p == -1)rt1 = i;
else t1.add_edge(p,i);
}
for(int i = 1;i<=N;i++){
int p;
cin>>p;
if(p == -1)rt2 = i;
else t2.add_edge(p,i);
}
t1.dfs(rt1,rt1);t2.dfs(rt2,rt2);
for(int i = 0;i<K;i++)tar.push_back(t1.dfn[i]);
for(auto &i:tar)cout<<i<<' ';cout<<endl;
vt = PEKO::GO(tar,t2);
for(int i = 1;i<=N;i++){
if(!vt.edges[i].empty()&&i != rt1){
ask(rt1,i);
}
}
cout<<"!"<<endl;
for(int i = 0;i<req.size();i++){
auto [a,b] = req[i];
for(auto &j:res[i])cin>>j;
val1[b] = res[i][1];
val2[b] = res[i][2]+res[i][3];
}
while(T--){
int a,b;
cin>>a>>b;
pii tans;
tans.fs = 1ll*val1[a]+val1[b]-val1[t1.LCA(a,b)]*2;
tans.sc = 1ll*val2[a]+val2[b]-val2[vt.LCA(a,b)]*2;
ans.push_back(tans);
}
for(auto &i:ans)cout<<i.fs<<' '<<i.sc<<'\n';
cout<<endl;
return 0;
}
Compilation message
Main.cpp: In function 'Tree PEKO::GO(std::vector<int>, Tree&)':
Main.cpp:77:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
77 | for(int i = 1;i<v.size();i++){
| ~^~~~~~~~~
Main.cpp: At global scope:
Main.cpp:89:8: error: 'iil' was not declared in this scope
89 | vector<iil> ans;
| ^~~
Main.cpp:89:11: error: template argument 1 is invalid
89 | vector<iil> ans;
| ^
Main.cpp:89:11: error: template argument 2 is invalid
Main.cpp: In function 'int main()':
Main.cpp:119:2: warning: this 'for' clause does not guard... [-Wmisleading-indentation]
119 | for(auto &i:tar)cout<<i<<' ';cout<<endl;
| ^~~
Main.cpp:119:31: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'for'
119 | for(auto &i:tar)cout<<i<<' ';cout<<endl;
| ^~~~
Main.cpp:127:17: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
127 | for(int i = 0;i<req.size();i++){
| ~^~~~~~~~~~~
Main.cpp:139:7: error: request for member 'push_back' in 'ans', which is of non-class type 'int'
139 | ans.push_back(tans);
| ^~~~~~~~~
Main.cpp:141:14: error: 'begin' was not declared in this scope
141 | for(auto &i:ans)cout<<i.fs<<' '<<i.sc<<'\n';
| ^~~
Main.cpp:141:14: note: suggested alternatives:
In file included from /usr/include/x86_64-linux-gnu/c++/10/bits/stdc++.h:95,
from Main.cpp:1:
/usr/include/c++/10/valarray:1224:5: note: 'std::begin'
1224 | begin(const valarray<_Tp>& __va)
| ^~~~~
In file included from /usr/include/c++/10/filesystem:46,
from /usr/include/x86_64-linux-gnu/c++/10/bits/stdc++.h:129,
from Main.cpp:1:
/usr/include/c++/10/bits/fs_dir.h:549:3: note: 'std::filesystem::__cxx11::begin'
549 | begin(recursive_directory_iterator __iter) noexcept
| ^~~~~
Main.cpp:141:14: error: 'end' was not declared in this scope
141 | for(auto &i:ans)cout<<i.fs<<' '<<i.sc<<'\n';
| ^~~
Main.cpp:141:14: note: suggested alternatives:
In file included from /usr/include/x86_64-linux-gnu/c++/10/bits/stdc++.h:95,
from Main.cpp:1:
/usr/include/c++/10/valarray:1244:5: note: 'std::end'
1244 | end(const valarray<_Tp>& __va)
| ^~~
In file included from /usr/include/c++/10/filesystem:46,
from /usr/include/x86_64-linux-gnu/c++/10/bits/stdc++.h:129,
from Main.cpp:1:
/usr/include/c++/10/bits/fs_dir.h:554:3: note: 'std::filesystem::__cxx11::end'
554 | end(recursive_directory_iterator) noexcept
| ^~~