# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
1021952 | guagua0407 | File Paths (BOI15_fil) | C++17 | 145 ms | 52656 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
//#pragma GCC optimize("O3")
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pii pair<int,int>
#define f first
#define s second
#define all(x) x.begin(),x.end()
#define _ ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
int dx[4]={0,0,1,-1};
int dy[4]={1,-1,0,0};
void setIO(string s) {
freopen((s + ".in").c_str(), "r", stdin);
freopen((s + ".out").c_str(), "w", stdout);
}
const int mxn=1e6+5;
vector<int> adj[mxn];
vector<ll> pre;
vector<int> p;
vector<int> l;
vector<int> ans;
int len;
int n,m,k;
vector<int> st;
vector<vector<int>> cand;
vector<int> cnt(mxn);
void dfs(int v){
st.push_back(v);
if(v<=n){
for(auto u:st){
cand[u].push_back(pre[v]+len-pre[u]);
}
}
for(auto u:adj[v]){
dfs(u);
}
st.pop_back();
}
void dfs2(int v){
if(v<=n){
for(auto x:cand[v]){
if(x<mxn) cnt[x]++;
}
}
else{
int target=k-pre[v];
if(target>0){
for(int i=1;i*i<=target;i++){
if(target%i==0){
if(cnt[i] or cnt[target/i]){
ans[v-n-1]=1;
break;
}
}
}
}
}
for(auto u:adj[v]){
dfs2(u);
}
if(v<=n){
for(auto x:cand[v]){
if(x<mxn) cnt[x]--;
}
}
}
int main() {_
cin>>n>>m>>k;
cin>>len;
len++;
pre.resize(n+m+1);
p.resize(n+m+1);
l.resize(n+m+1);
for(int i=1;i<=n+m;i++){
cin>>p[i]>>l[i];
pre[i]=pre[p[i]]+l[i]+1;
}
for(int i=1;i<=n+m;i++){
adj[p[i]].push_back(i);
}
ans.resize(m);
cand.resize(n+1);
dfs(0);
dfs2(0);
vector<ll> vec(n+1);
for(int i=0;i<=n;i++){
vec[i]=pre[i]+len;
}
vec.push_back(0);
sort(all(vec));
for(int i=0;i<m;i++){
int pp=p[n+1+i],L=l[n+1+i];
int cur=pre[pp]+L+1;
bool tf=false;
while(true){
ll target=k-(cur-pre[pp]);
int pos=lower_bound(all(vec),target)-vec.begin();
if(pos!=n+1 and vec[pos]==target){
tf=true;
}
if(tf or pp==0) break;
pp=p[pp];
}
if(tf){
ans[i]=1;
}
}
for(int i=0;i<m;i++){
cout<<(ans[i]?"YES":"NO")<<'\n';
}
return 0;
}
//maybe its multiset not set
//yeeorz
//laborz
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |