/*input
2 4 22
2
0 1
1 5
2 13
2 10
1 4
0 7
*/
#pragma GCC optimize ("O3")
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
vector<int>dal[1000001];
bool turi(const vector<int>&a, const vector<int>&b)
{
int i = 0;
int j = 0;
while (i < (int)a.size() && j < (int)b.size())
{
if (a[i] == b[j])
return true;
if (a[i] < b[j])
i++;
else
j++;
}
return false;
}
int main()
{
ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0), cerr.tie(0);
for (int i = 1; i <= 1000; i++)
for (int j = i * i; j <= 1000000; j += i)
dal[j].push_back(i);
int n, m, k, s;
cin >> n >> m >> k >> s;
s++;
int p[n + 1], d[n + 1], l[n + 1];
p[0] = -1;
d[0] = 1;
vector<int>D = {1};
for (int i = 1; i <= n; i++)
{
cin >> p[i] >> l[i];
d[i] = d[p[i]] + 1 + l[i];
D.push_back(d[i]);
}
sort(D.begin(), D.end());
vector<int>cikl[n + 1];
for (int i = 0; i <= n; i++)
{
for (int j = i; j != -1; j = p[j])
{
cikl[j].push_back(d[i] - d[j] + s);
}
}
for (int i = 0; i <= n; i++)
sort(cikl[i].begin(), cikl[i].end());
function<bool(int, int)>ok = [&](int v, int ilg)
{
if (d[v] == ilg)
return true;
if (ilg <= 0)
return false;
if (v != 0 && ok(p[v], ilg - 1 - l[v]))
return true;
if (binary_search(D.begin(), D.end(), ilg - s))
return true;
if (ilg <= d[v] + s)
return false;
ilg -= d[v];
if (cikl[v].size() >= 30)
{
for (int c : dal[ilg])
{
if (binary_search(cikl[v].begin(), cikl[v].end(), c))
return true;
if (binary_search(cikl[v].begin(), cikl[v].end(), ilg / c))
return true;
}
return false;
}
else
{
if (turi(dal[ilg], cikl[v]))
return true;
vector<int>x;
for (int c : dal[ilg])
x.push_back(ilg / c);
reverse(x.begin(), x.end());
return turi(x, cikl[v]);
}
};
while (m--)
{
int p, l;
cin >> p >> l;
if (ok(p, k - l))
cout << "YES\n";
else
cout << "NO\n";
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
662 ms |
84732 KB |
Output is correct |
2 |
Correct |
551 ms |
84796 KB |
Output is correct |
3 |
Correct |
525 ms |
84728 KB |
Output is correct |
4 |
Correct |
551 ms |
85240 KB |
Output is correct |
5 |
Correct |
568 ms |
84728 KB |
Output is correct |
6 |
Correct |
536 ms |
84728 KB |
Output is correct |
7 |
Correct |
543 ms |
85496 KB |
Output is correct |
8 |
Correct |
550 ms |
84680 KB |
Output is correct |
9 |
Correct |
538 ms |
84716 KB |
Output is correct |
10 |
Correct |
577 ms |
84728 KB |
Output is correct |
11 |
Correct |
618 ms |
84672 KB |
Output is correct |
12 |
Correct |
518 ms |
85112 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
660 ms |
84856 KB |
Output is correct |
2 |
Correct |
545 ms |
84832 KB |
Output is correct |
3 |
Correct |
557 ms |
84984 KB |
Output is correct |
4 |
Correct |
558 ms |
84728 KB |
Output is correct |
5 |
Correct |
871 ms |
104988 KB |
Output is correct |
6 |
Correct |
794 ms |
104824 KB |
Output is correct |
7 |
Correct |
768 ms |
95864 KB |
Output is correct |
8 |
Correct |
755 ms |
95984 KB |
Output is correct |
9 |
Correct |
542 ms |
84800 KB |
Output is correct |
10 |
Correct |
601 ms |
84852 KB |
Output is correct |
11 |
Correct |
540 ms |
85368 KB |
Output is correct |
12 |
Correct |
872 ms |
104268 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
662 ms |
84732 KB |
Output is correct |
2 |
Correct |
551 ms |
84796 KB |
Output is correct |
3 |
Correct |
525 ms |
84728 KB |
Output is correct |
4 |
Correct |
551 ms |
85240 KB |
Output is correct |
5 |
Correct |
568 ms |
84728 KB |
Output is correct |
6 |
Correct |
536 ms |
84728 KB |
Output is correct |
7 |
Correct |
543 ms |
85496 KB |
Output is correct |
8 |
Correct |
550 ms |
84680 KB |
Output is correct |
9 |
Correct |
538 ms |
84716 KB |
Output is correct |
10 |
Correct |
577 ms |
84728 KB |
Output is correct |
11 |
Correct |
618 ms |
84672 KB |
Output is correct |
12 |
Correct |
518 ms |
85112 KB |
Output is correct |
13 |
Correct |
660 ms |
84856 KB |
Output is correct |
14 |
Correct |
545 ms |
84832 KB |
Output is correct |
15 |
Correct |
557 ms |
84984 KB |
Output is correct |
16 |
Correct |
558 ms |
84728 KB |
Output is correct |
17 |
Correct |
871 ms |
104988 KB |
Output is correct |
18 |
Correct |
794 ms |
104824 KB |
Output is correct |
19 |
Correct |
768 ms |
95864 KB |
Output is correct |
20 |
Correct |
755 ms |
95984 KB |
Output is correct |
21 |
Correct |
542 ms |
84800 KB |
Output is correct |
22 |
Correct |
601 ms |
84852 KB |
Output is correct |
23 |
Correct |
540 ms |
85368 KB |
Output is correct |
24 |
Correct |
872 ms |
104268 KB |
Output is correct |
25 |
Correct |
577 ms |
84856 KB |
Output is correct |
26 |
Correct |
549 ms |
84804 KB |
Output is correct |
27 |
Correct |
620 ms |
84984 KB |
Output is correct |
28 |
Correct |
553 ms |
84728 KB |
Output is correct |
29 |
Correct |
807 ms |
104792 KB |
Output is correct |
30 |
Correct |
810 ms |
104852 KB |
Output is correct |
31 |
Correct |
762 ms |
96008 KB |
Output is correct |
32 |
Correct |
773 ms |
95736 KB |
Output is correct |
33 |
Correct |
577 ms |
84944 KB |
Output is correct |
34 |
Correct |
663 ms |
84960 KB |
Output is correct |
35 |
Correct |
572 ms |
85508 KB |
Output is correct |
36 |
Execution timed out |
1068 ms |
109432 KB |
Time limit exceeded |