#include<bits/stdc++.h>
#define TASKNAME "codeforce"
#define pb push_back
#define pli pair<int,int>
#define fi first
#define se second
#define fastio ios_base::sync_with_stdio(false); cin.tie(NULL);
using namespace std;
using ll=long long;
const ll maxN=5e5+10;
const ll inf=1e18;
const ll mod=1e9+7;
ll n,c[maxN],l[maxN],r[maxN];
ll mn[4*maxN],mx[maxN*4];
vector<ll>vec[maxN],pos[maxN];
void update(ll pos,ll val,ll id=1,ll l=1,ll r=n)
{
if(l==r)
{
mn[id]=mx[id]=val;
return;
}
ll mid=l+r>>1;
if(pos<=mid) update(pos,val,id*2,l,mid);
else update(pos,val,id*2+1,mid+1,r);
mx[id]=max(mx[id*2],mx[id*2+1]);
mn[id]=min(mn[id*2],mn[id*2+1]);
}
ll bs1(ll i,ll val,ll id=1,ll l=1,ll r=n)
{
if(r<i||mx[id]<val) return r+1;
if(l==r) return l;
ll mid=l+r>>1;
ll cc=bs1(i,val,id*2,l,mid);
if(cc==mid+1) return bs1(i,val,id*2+1,mid+1,r);
else return cc;
}
ll bs2(ll i,ll val,ll id=1,ll l=1,ll r=n)
{
if(l>i||mn[id]>val) return l-1;
if(l==r) return l;
ll mid=l+r>>1;
ll cc=bs2(i,val,id*2+1,mid+1,r);
if(cc==mid) return bs2(i,val,id*2,l,mid);
else return cc;
}
ll bit[maxN]={0};
void upd(ll i,ll x)
{
while(i<=n)
{
bit[i]+=x;
i+=i&(-i);
}
}
ll get(ll i,ll j)
{
swap(i,j);
j--;
ll ans=0;
while(i>0)
{
ans+=bit[i];
i-=i&(-i);
}
while(j>0)
{
ans-=bit[j];
j-=(j&(-j));
}
return ans;
}
ll q;
vector<ll>xoa[maxN];
ll ans[maxN];
vector<pli>nho[maxN],lon[maxN];
void solve()
{
cin >> n;
for(int i=1;i<n;i++)
{
cin >> c[i];
}
for(int i=1;i<=n;i++)
{
ll k;
cin >> k;
vec[i].resize(k);
for(int j=0;j<k;j++)
{
cin >> vec[i][j];
pos[vec[i][j]].pb(i);
}
}
cin >> q;
for(int i=1;i<=q;i++)
{
ll l,r;
cin >> l >> r;
if(l>r)
{
nho[l].pb({r,i});
}
else lon[l].pb({r,i});
}
for(int i=1;i<n;i++)
{
r[i]=upper_bound(pos[c[i]].begin(),pos[c[i]].end(),i)-pos[c[i]].begin();
if(r[i]==pos[c[i]].size()) r[i]=n+1;
else r[i]=pos[c[i]][r[i]];
}
r[n]=n+1;
for(int i=2;i<=n;i++)
{
l[i]=lower_bound(pos[c[i-1]].begin(),pos[c[i-1]].end(),i)-pos[c[i-1]].begin()-1;
if(l[i]<0) l[i]=0;
else l[i]=pos[c[i-1]][l[i]];
}
l[1]=0;
for(int i=1;i<=n;i++) update(i,r[i]);
for(int i=n;i>=1;i--)
{
if(l[i]==0)
{
xoa[0].pb(i);
update(i,-inf);
continue;
}
ll x=bs1(l[i],i);
xoa[x].pb(i);
update(i,-inf);
}
for(int i=1;i<=n;i++)
{
upd(i,1);
}
for(int i=n;i>=1;i--)
{
for(auto zz:xoa[i])
{
upd(zz,-1);
}
for(auto zz:lon[i])
{
ans[zz.se]=get(i+1,zz.fi);
//cout << zz.fi<<'\n';
}
}
for(auto zz:xoa[0])
{
upd(zz,-1);
}
for(auto zz:xoa[n+1]) upd(zz,-1);
xoa[0].clear();
for(int i=1;i<=n;i++) upd(i,1),xoa[i].clear(),update(i,l[i]);
for(int i=1;i<=n;i++)
{
if(r[i]==n+1)
{
update(i,inf);
continue;
}
ll x=bs2(r[i],i);
xoa[x].pb(i);
update(i,inf);
}
for(int i=1;i<=n;i++)
{
for(auto zz:xoa[i])
{
upd(zz,-1);
}
for(auto zz:nho[i])
{
ans[zz.se]=get(zz.fi,i-1);
}
}
for(int i=1;i<=q;i++)
{
if(ans[i]>0) cout <<"NO\n";
else cout <<"YES\n";
}
}
int main()
{
fastio
//freopen(TASKNAME".INP","r",stdin);
//freopen(TASKNAME".OUT","w",stdout);
solve();
}
Compilation message
long_mansion.cpp: In function 'void update(ll, ll, ll, ll, ll)':
long_mansion.cpp:23:13: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
23 | ll mid=l+r>>1;
| ~^~
long_mansion.cpp: In function 'll bs1(ll, ll, ll, ll, ll)':
long_mansion.cpp:33:13: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
33 | ll mid=l+r>>1;
| ~^~
long_mansion.cpp: In function 'll bs2(ll, ll, ll, ll, ll)':
long_mansion.cpp:42:13: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
42 | ll mid=l+r>>1;
| ~^~
long_mansion.cpp: In function 'void solve()':
long_mansion.cpp:109:16: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
109 | if(r[i]==pos[c[i]].size()) r[i]=n+1;
| ~~~~^~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
31 ms |
59592 KB |
Output is correct |
2 |
Correct |
32 ms |
59764 KB |
Output is correct |
3 |
Correct |
34 ms |
60040 KB |
Output is correct |
4 |
Correct |
33 ms |
59604 KB |
Output is correct |
5 |
Correct |
32 ms |
59560 KB |
Output is correct |
6 |
Correct |
32 ms |
59604 KB |
Output is correct |
7 |
Correct |
32 ms |
59684 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
31 ms |
59592 KB |
Output is correct |
2 |
Correct |
32 ms |
59764 KB |
Output is correct |
3 |
Correct |
34 ms |
60040 KB |
Output is correct |
4 |
Correct |
33 ms |
59604 KB |
Output is correct |
5 |
Correct |
32 ms |
59560 KB |
Output is correct |
6 |
Correct |
32 ms |
59604 KB |
Output is correct |
7 |
Correct |
32 ms |
59684 KB |
Output is correct |
8 |
Correct |
125 ms |
75472 KB |
Output is correct |
9 |
Correct |
125 ms |
75428 KB |
Output is correct |
10 |
Correct |
133 ms |
76712 KB |
Output is correct |
11 |
Correct |
138 ms |
77032 KB |
Output is correct |
12 |
Correct |
113 ms |
74872 KB |
Output is correct |
13 |
Correct |
123 ms |
76112 KB |
Output is correct |
14 |
Correct |
117 ms |
75932 KB |
Output is correct |
15 |
Correct |
121 ms |
76036 KB |
Output is correct |
16 |
Correct |
133 ms |
75524 KB |
Output is correct |
17 |
Correct |
122 ms |
76364 KB |
Output is correct |
18 |
Correct |
117 ms |
75832 KB |
Output is correct |
19 |
Correct |
131 ms |
76064 KB |
Output is correct |
20 |
Correct |
117 ms |
74612 KB |
Output is correct |
21 |
Correct |
117 ms |
75552 KB |
Output is correct |
22 |
Correct |
130 ms |
76204 KB |
Output is correct |
23 |
Correct |
120 ms |
75596 KB |
Output is correct |
24 |
Correct |
122 ms |
75612 KB |
Output is correct |
25 |
Correct |
133 ms |
75656 KB |
Output is correct |
26 |
Correct |
121 ms |
75488 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
369 ms |
97908 KB |
Output is correct |
2 |
Correct |
350 ms |
97372 KB |
Output is correct |
3 |
Correct |
355 ms |
96772 KB |
Output is correct |
4 |
Correct |
355 ms |
97560 KB |
Output is correct |
5 |
Correct |
359 ms |
97520 KB |
Output is correct |
6 |
Correct |
340 ms |
93780 KB |
Output is correct |
7 |
Correct |
334 ms |
94200 KB |
Output is correct |
8 |
Correct |
339 ms |
94172 KB |
Output is correct |
9 |
Correct |
333 ms |
94248 KB |
Output is correct |
10 |
Correct |
343 ms |
94396 KB |
Output is correct |
11 |
Correct |
324 ms |
94156 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
31 ms |
59592 KB |
Output is correct |
2 |
Correct |
32 ms |
59764 KB |
Output is correct |
3 |
Correct |
34 ms |
60040 KB |
Output is correct |
4 |
Correct |
33 ms |
59604 KB |
Output is correct |
5 |
Correct |
32 ms |
59560 KB |
Output is correct |
6 |
Correct |
32 ms |
59604 KB |
Output is correct |
7 |
Correct |
32 ms |
59684 KB |
Output is correct |
8 |
Correct |
125 ms |
75472 KB |
Output is correct |
9 |
Correct |
125 ms |
75428 KB |
Output is correct |
10 |
Correct |
133 ms |
76712 KB |
Output is correct |
11 |
Correct |
138 ms |
77032 KB |
Output is correct |
12 |
Correct |
113 ms |
74872 KB |
Output is correct |
13 |
Correct |
123 ms |
76112 KB |
Output is correct |
14 |
Correct |
117 ms |
75932 KB |
Output is correct |
15 |
Correct |
121 ms |
76036 KB |
Output is correct |
16 |
Correct |
133 ms |
75524 KB |
Output is correct |
17 |
Correct |
122 ms |
76364 KB |
Output is correct |
18 |
Correct |
117 ms |
75832 KB |
Output is correct |
19 |
Correct |
131 ms |
76064 KB |
Output is correct |
20 |
Correct |
117 ms |
74612 KB |
Output is correct |
21 |
Correct |
117 ms |
75552 KB |
Output is correct |
22 |
Correct |
130 ms |
76204 KB |
Output is correct |
23 |
Correct |
120 ms |
75596 KB |
Output is correct |
24 |
Correct |
122 ms |
75612 KB |
Output is correct |
25 |
Correct |
133 ms |
75656 KB |
Output is correct |
26 |
Correct |
121 ms |
75488 KB |
Output is correct |
27 |
Correct |
369 ms |
97908 KB |
Output is correct |
28 |
Correct |
350 ms |
97372 KB |
Output is correct |
29 |
Correct |
355 ms |
96772 KB |
Output is correct |
30 |
Correct |
355 ms |
97560 KB |
Output is correct |
31 |
Correct |
359 ms |
97520 KB |
Output is correct |
32 |
Correct |
340 ms |
93780 KB |
Output is correct |
33 |
Correct |
334 ms |
94200 KB |
Output is correct |
34 |
Correct |
339 ms |
94172 KB |
Output is correct |
35 |
Correct |
333 ms |
94248 KB |
Output is correct |
36 |
Correct |
343 ms |
94396 KB |
Output is correct |
37 |
Correct |
324 ms |
94156 KB |
Output is correct |
38 |
Correct |
913 ms |
146072 KB |
Output is correct |
39 |
Correct |
984 ms |
155300 KB |
Output is correct |
40 |
Correct |
737 ms |
135028 KB |
Output is correct |
41 |
Correct |
855 ms |
150984 KB |
Output is correct |
42 |
Correct |
312 ms |
94704 KB |
Output is correct |
43 |
Correct |
309 ms |
94676 KB |
Output is correct |
44 |
Correct |
463 ms |
113868 KB |
Output is correct |
45 |
Correct |
490 ms |
113960 KB |
Output is correct |
46 |
Correct |
508 ms |
114308 KB |
Output is correct |
47 |
Correct |
322 ms |
95232 KB |
Output is correct |
48 |
Correct |
304 ms |
95048 KB |
Output is correct |
49 |
Correct |
467 ms |
113784 KB |
Output is correct |
50 |
Correct |
476 ms |
113940 KB |
Output is correct |
51 |
Correct |
480 ms |
114496 KB |
Output is correct |
52 |
Correct |
444 ms |
113492 KB |
Output is correct |
53 |
Correct |
597 ms |
135720 KB |
Output is correct |
54 |
Correct |
732 ms |
148380 KB |
Output is correct |
55 |
Correct |
567 ms |
135484 KB |
Output is correct |