//Be Name Khoda
#include<bits/stdc++.h>
#pragma GCC target ("avx2,tune=native")
using namespace std;
#define ll long long
#define ld long double
#define all(x) x.begin(), x.end()
#define pii pair<int, int>
#define pll pair<ll, ll>
#define plll pair<pll, ll>
const int mxn = 5e5 + 16;
int n, t, k, q;
int c[mxn], cnt[mxn];
pii ans[mxn];
vector<int> keys[mxn];
void input() {
cin >> n;
for(int i = 1; i < n; i++) {
cin >> c[i];
}
for(int i = 1; i <= n; i++) {
cin >> k;
for(int j = 0; j < k; j++) {
cin >> q;
keys[i].push_back(q);
}
} cin >> t;
}
inline void merge(int l, int r, int m) {
bool mark[r - l + 5];
int tmp[r - l + 5];
int pr = m, pl = m - 1;
memset(mark, 0, sizeof(mark));
for(int i = m - 1; i > l - 1; i--) {
for(auto u : keys[i]) cnt[u]++;
while(pr < r) {
if(cnt[c[pr - 1]] > 0) {
for(auto u : keys[pr]) cnt[u]++;
pr++;
}
else break;
}
tmp[i - l] = pr;
if(i > l) {
if(cnt[c[i - 1]] > 0) mark[i - l] = 1;
}
}
for(int i = l; i < m; i++) {
if(mark[i - l]) continue;
if(ans[i].second >= m) {
ans[i].second = tmp[i - l];
}
for(int j = i + 1; j < m; j++) {
if(!mark[j - l]) break;
if(ans[j].second < m) continue;
ans[j].first = i, ans[j].second = tmp[i - l];
}
}
for(int i = l; i < m; i++) {
for(auto u : keys[i]) cnt[u]--;
}
for(int i = m; i < pr; i++) {
for(auto u : keys[i]) cnt[u]--;
}
for(int i = m; i < r; i++) {
for(auto u : keys[i]) cnt[u]++;
while(pl > l - 1) {
if(cnt[c[pl]] > 0) {
for(auto u : keys[pl]) cnt[u]++;
pl--;
}
else break;
}
tmp[i - l] = pl + 1;
if(i < r - 1) {
if(cnt[c[i]] > 0) mark[i - l] = 1;
}
}
for(int i = r - 1; i > m - 1; i--) {
if(mark[i - l]) continue;
if(ans[i].first <= m) {
ans[i].first = tmp[i - l];
}
for(int j = i - 1; j > m - 1; j--) {
if(!mark[j - l]) continue;
if(ans[j].first > m) continue;
ans[j].first = tmp[i - l], ans[j].second = i + 1;
}
}
for(int i = m; i < r; i++) {
for(auto u : keys[i]) cnt[u]--;
}
for(int i = m - 1; i > pl; i--) {
for(auto u : keys[i]) cnt[u]--;
}
return;
}
void solve(int l, int r) {
if(r - l == 1) {
ans[l] = make_pair(l, r);
return;
}
int m = (l + r) >> 1;
solve(l, m); solve(m, r);
merge(l, r, m);
return;
}
int main() {
ios::sync_with_stdio(false); cin.tie(NULL);
input(), solve(1, n + 1);
for(int i = 0; i < t; i++) {
int x, y;
cin >> x >> y;
if(y < ans[x].first || ans[x].second <= y) {
cout << "NO\n";
continue;
}
cout << "YES\n";
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
12184 KB |
Output is correct |
2 |
Correct |
11 ms |
12300 KB |
Output is correct |
3 |
Correct |
10 ms |
12320 KB |
Output is correct |
4 |
Correct |
10 ms |
12200 KB |
Output is correct |
5 |
Correct |
11 ms |
12236 KB |
Output is correct |
6 |
Correct |
8 ms |
12236 KB |
Output is correct |
7 |
Correct |
9 ms |
12364 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
12184 KB |
Output is correct |
2 |
Correct |
11 ms |
12300 KB |
Output is correct |
3 |
Correct |
10 ms |
12320 KB |
Output is correct |
4 |
Correct |
10 ms |
12200 KB |
Output is correct |
5 |
Correct |
11 ms |
12236 KB |
Output is correct |
6 |
Correct |
8 ms |
12236 KB |
Output is correct |
7 |
Correct |
9 ms |
12364 KB |
Output is correct |
8 |
Correct |
93 ms |
14972 KB |
Output is correct |
9 |
Correct |
93 ms |
14872 KB |
Output is correct |
10 |
Correct |
95 ms |
15112 KB |
Output is correct |
11 |
Correct |
97 ms |
15356 KB |
Output is correct |
12 |
Correct |
77 ms |
15112 KB |
Output is correct |
13 |
Correct |
80 ms |
15160 KB |
Output is correct |
14 |
Correct |
89 ms |
15096 KB |
Output is correct |
15 |
Correct |
83 ms |
15136 KB |
Output is correct |
16 |
Correct |
82 ms |
15388 KB |
Output is correct |
17 |
Correct |
90 ms |
15228 KB |
Output is correct |
18 |
Correct |
82 ms |
15192 KB |
Output is correct |
19 |
Correct |
87 ms |
15272 KB |
Output is correct |
20 |
Correct |
104 ms |
15336 KB |
Output is correct |
21 |
Correct |
88 ms |
15328 KB |
Output is correct |
22 |
Correct |
90 ms |
15188 KB |
Output is correct |
23 |
Correct |
89 ms |
15012 KB |
Output is correct |
24 |
Correct |
84 ms |
15044 KB |
Output is correct |
25 |
Correct |
88 ms |
14984 KB |
Output is correct |
26 |
Correct |
93 ms |
15008 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
305 ms |
20376 KB |
Output is correct |
2 |
Correct |
266 ms |
20400 KB |
Output is correct |
3 |
Correct |
245 ms |
20316 KB |
Output is correct |
4 |
Correct |
261 ms |
20336 KB |
Output is correct |
5 |
Correct |
266 ms |
20352 KB |
Output is correct |
6 |
Correct |
213 ms |
19920 KB |
Output is correct |
7 |
Correct |
194 ms |
20088 KB |
Output is correct |
8 |
Correct |
187 ms |
20192 KB |
Output is correct |
9 |
Correct |
178 ms |
20000 KB |
Output is correct |
10 |
Correct |
176 ms |
20264 KB |
Output is correct |
11 |
Correct |
187 ms |
20104 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
12184 KB |
Output is correct |
2 |
Correct |
11 ms |
12300 KB |
Output is correct |
3 |
Correct |
10 ms |
12320 KB |
Output is correct |
4 |
Correct |
10 ms |
12200 KB |
Output is correct |
5 |
Correct |
11 ms |
12236 KB |
Output is correct |
6 |
Correct |
8 ms |
12236 KB |
Output is correct |
7 |
Correct |
9 ms |
12364 KB |
Output is correct |
8 |
Correct |
93 ms |
14972 KB |
Output is correct |
9 |
Correct |
93 ms |
14872 KB |
Output is correct |
10 |
Correct |
95 ms |
15112 KB |
Output is correct |
11 |
Correct |
97 ms |
15356 KB |
Output is correct |
12 |
Correct |
77 ms |
15112 KB |
Output is correct |
13 |
Correct |
80 ms |
15160 KB |
Output is correct |
14 |
Correct |
89 ms |
15096 KB |
Output is correct |
15 |
Correct |
83 ms |
15136 KB |
Output is correct |
16 |
Correct |
82 ms |
15388 KB |
Output is correct |
17 |
Correct |
90 ms |
15228 KB |
Output is correct |
18 |
Correct |
82 ms |
15192 KB |
Output is correct |
19 |
Correct |
87 ms |
15272 KB |
Output is correct |
20 |
Correct |
104 ms |
15336 KB |
Output is correct |
21 |
Correct |
88 ms |
15328 KB |
Output is correct |
22 |
Correct |
90 ms |
15188 KB |
Output is correct |
23 |
Correct |
89 ms |
15012 KB |
Output is correct |
24 |
Correct |
84 ms |
15044 KB |
Output is correct |
25 |
Correct |
88 ms |
14984 KB |
Output is correct |
26 |
Correct |
93 ms |
15008 KB |
Output is correct |
27 |
Correct |
305 ms |
20376 KB |
Output is correct |
28 |
Correct |
266 ms |
20400 KB |
Output is correct |
29 |
Correct |
245 ms |
20316 KB |
Output is correct |
30 |
Correct |
261 ms |
20336 KB |
Output is correct |
31 |
Correct |
266 ms |
20352 KB |
Output is correct |
32 |
Correct |
213 ms |
19920 KB |
Output is correct |
33 |
Correct |
194 ms |
20088 KB |
Output is correct |
34 |
Correct |
187 ms |
20192 KB |
Output is correct |
35 |
Correct |
178 ms |
20000 KB |
Output is correct |
36 |
Correct |
176 ms |
20264 KB |
Output is correct |
37 |
Correct |
187 ms |
20104 KB |
Output is correct |
38 |
Execution timed out |
3081 ms |
30628 KB |
Time limit exceeded |
39 |
Halted |
0 ms |
0 KB |
- |