#include <bits/stdc++.h>
#pragma GCC optimize ("Ofast")
#pragma GCC optimize ("fast-math")
#pragma GCC optimize ("unroll-loops")
using namespace std;
mt19937 rng(chrono::steady_clock().now().time_since_epoch().count());
typedef pair<int,int> pii;
int bucksize=320;
int n,m,q,c[100005],niv[100005],in[100005],out[100005],lin[500005],rmq[21][500005],loga[500005],timp,par[100005];
int sol[100005],nivmin,cnt;
set<int> s;
vector<int> muchii[100005];
void dfs(int nod)
{
timp++;
lin[timp]=nod;
in[nod]=timp;
bool have=0;
for(int i:muchii[nod])
if(i!=par[nod])
{
par[i]=nod;
niv[i]=niv[nod]+1;
dfs(i);
timp++;
lin[timp]=nod;
have=1;
}
if(!have)
{
timp++;
lin[timp]=nod;
}
out[nod]=timp;
}
struct date
{
int l,r,ind;
};
int getbuck(int x)
{
return x/bucksize+(x%bucksize!=0);
}
bool byr(date a, date b)
{
return a.r<b.r;
}
vector<date> qr[100005];
bool isancestor(int a,int b)
{
return in[a]<=in[b]&&out[a]>=out[b];
}
int getmin(int l,int r)
{
int lg=loga[r-l+1];
int a=rmq[lg][r-(1<<lg)+1];
int b=rmq[lg][l];
if(niv[a]<niv[b])
return a;
return b;
}
int LCA(int a,int b)
{
if(in[a]>in[b])
swap(a,b);
if(isancestor(a,b))
return a;
int l=out[a];
int r=in[b];
return getmin(l,r);
}
void addnode(int nod)
{
if(s.find(in[nod])!=s.end())
return;
s.insert(in[nod]);
if(s.size()==1)
{
nivmin=niv[nod];
cnt=nivmin+1;
return;
}
int nmin=1e9;
int nmax=0;
auto it=s.find(in[nod]);
bool have=0;
if(it!=s.begin())
{
int st=lin[(*prev(it))];
int x=LCA(st,nod);
nmin=min(nmin,niv[x]);
nmax=max(nmax,niv[x]);
}
it++;
if(it!=s.end())
{
int dr=lin[(*it)];
if(isancestor(nod,dr))
have=1;
int x=LCA(nod,dr);
nmin=min(nmin,niv[x]);
nmax=max(nmax,niv[x]);
}
if(nivmin>nmin)
{
if(!have)
cnt+=niv[nod]-nmin;
nivmin=min(nivmin,nmin);
}
else
{
if(!have)
cnt+=niv[nod]-nmax;
}
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
cin>>n>>m>>q;
for(int i=1;i<n;i++)
{
int a,b;
cin>>a>>b;
/*a=i+1;
b=rng()%i+1;*/
muchii[a].push_back(b);
muchii[b].push_back(a);
}
for(int i=1;i<=m;i++)
{
cin>>c[i];
//c[i]=rng()%n+1;
}
dfs(1);
for(int i=1;i<=timp;i++)
{
for(int bit=0;bit<=20;bit++)
if((1<<bit)<=i)
loga[i]=bit;
}
for(int i=1;i<=timp;i++)
rmq[0][i]=lin[i];
for(int j=1;j<=20;j++)
for(int i=1;i<=timp;i++)
{
rmq[j][i]=rmq[j-1][i];
int poz=i+(1<<(j-1));
if(poz<=timp&&niv[rmq[j-1][poz]]<niv[rmq[j][i]])
rmq[j][i]=rmq[j-1][poz];
}
int bmax=0;
for(int i=1;i<=q;i++)
{
int l,r;
cin>>l>>r;
/*l=rng()%m+1;
r=rng()%m+1;
if(l>r)
swap(l,r);*/
int b=getbuck(l);
bmax=max(bmax,b);
qr[b].push_back({l,r,i});
}
for(int b=1;b<=bmax;b++)
if(!qr[b].empty())
{
sort(qr[b].begin(),qr[b].end(),byr);
reverse(qr[b].begin(),qr[b].end());
while(!qr[b].empty()&&getbuck(qr[b].back().r)==b)
{
int l=qr[b].back().l;
int r=qr[b].back().r;
int ind=qr[b].back().ind;
qr[b].pop_back();
s.clear();
nivmin=niv[c[l]];
cnt=nivmin+1;
s.insert(in[c[l]]);
for(int i=l+1;i<=r;i++)
{
int nod=c[i];
addnode(nod);
}
sol[ind]=cnt-nivmin;
}
s.clear();
int poz=b*bucksize;
cnt=0;
nivmin=0;
while(!qr[b].empty())
{
int l=qr[b].back().l;
int r=qr[b].back().r;
int ind=qr[b].back().ind;
qr[b].pop_back();
while(poz<r)
{
poz++;
int nod=c[poz];
addnode(nod);
}
int last=b*bucksize;
int auxniv=nivmin;
int auxcnt=cnt;
vector<int> toerase;
assert(last-l+1<=bucksize);
for(int i=l;i<=last;i++)
{
int nod=c[i];
if(s.find(in[nod])!=s.end())
continue;
toerase.push_back(in[nod]);
addnode(nod);
}
sol[ind]=cnt-nivmin;
for(int p:toerase)
s.erase(p);
nivmin=auxniv;
cnt=auxcnt;
}
}
for(int i=1;i<=q;i++)
cout<<sol[i]<<'\n';
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
51804 KB |
Output is correct |
2 |
Correct |
7 ms |
51804 KB |
Output is correct |
3 |
Correct |
6 ms |
51864 KB |
Output is correct |
4 |
Correct |
9 ms |
51800 KB |
Output is correct |
5 |
Correct |
8 ms |
51804 KB |
Output is correct |
6 |
Correct |
8 ms |
51960 KB |
Output is correct |
7 |
Correct |
10 ms |
51804 KB |
Output is correct |
8 |
Correct |
8 ms |
51920 KB |
Output is correct |
9 |
Correct |
11 ms |
51804 KB |
Output is correct |
10 |
Correct |
11 ms |
51804 KB |
Output is correct |
11 |
Correct |
24 ms |
49908 KB |
Output is correct |
12 |
Correct |
16 ms |
51804 KB |
Output is correct |
13 |
Correct |
17 ms |
51804 KB |
Output is correct |
14 |
Correct |
16 ms |
51804 KB |
Output is correct |
15 |
Correct |
10 ms |
51960 KB |
Output is correct |
16 |
Correct |
12 ms |
51804 KB |
Output is correct |
17 |
Correct |
11 ms |
51804 KB |
Output is correct |
18 |
Correct |
11 ms |
51804 KB |
Output is correct |
19 |
Correct |
10 ms |
51940 KB |
Output is correct |
20 |
Correct |
11 ms |
52056 KB |
Output is correct |
21 |
Correct |
12 ms |
51804 KB |
Output is correct |
22 |
Correct |
11 ms |
51932 KB |
Output is correct |
23 |
Correct |
11 ms |
51804 KB |
Output is correct |
24 |
Correct |
12 ms |
49756 KB |
Output is correct |
25 |
Correct |
11 ms |
51800 KB |
Output is correct |
26 |
Correct |
11 ms |
51972 KB |
Output is correct |
27 |
Correct |
8 ms |
51804 KB |
Output is correct |
28 |
Correct |
7 ms |
51804 KB |
Output is correct |
29 |
Correct |
8 ms |
51804 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
51804 KB |
Output is correct |
2 |
Correct |
7 ms |
51804 KB |
Output is correct |
3 |
Correct |
6 ms |
51864 KB |
Output is correct |
4 |
Correct |
9 ms |
51800 KB |
Output is correct |
5 |
Correct |
8 ms |
51804 KB |
Output is correct |
6 |
Correct |
8 ms |
51960 KB |
Output is correct |
7 |
Correct |
10 ms |
51804 KB |
Output is correct |
8 |
Correct |
8 ms |
51920 KB |
Output is correct |
9 |
Correct |
11 ms |
51804 KB |
Output is correct |
10 |
Correct |
11 ms |
51804 KB |
Output is correct |
11 |
Correct |
24 ms |
49908 KB |
Output is correct |
12 |
Correct |
16 ms |
51804 KB |
Output is correct |
13 |
Correct |
17 ms |
51804 KB |
Output is correct |
14 |
Correct |
16 ms |
51804 KB |
Output is correct |
15 |
Correct |
10 ms |
51960 KB |
Output is correct |
16 |
Correct |
12 ms |
51804 KB |
Output is correct |
17 |
Correct |
11 ms |
51804 KB |
Output is correct |
18 |
Correct |
11 ms |
51804 KB |
Output is correct |
19 |
Correct |
10 ms |
51940 KB |
Output is correct |
20 |
Correct |
11 ms |
52056 KB |
Output is correct |
21 |
Correct |
12 ms |
51804 KB |
Output is correct |
22 |
Correct |
11 ms |
51932 KB |
Output is correct |
23 |
Correct |
11 ms |
51804 KB |
Output is correct |
24 |
Correct |
12 ms |
49756 KB |
Output is correct |
25 |
Correct |
11 ms |
51800 KB |
Output is correct |
26 |
Correct |
11 ms |
51972 KB |
Output is correct |
27 |
Correct |
8 ms |
51804 KB |
Output is correct |
28 |
Correct |
7 ms |
51804 KB |
Output is correct |
29 |
Correct |
8 ms |
51804 KB |
Output is correct |
30 |
Correct |
47 ms |
52060 KB |
Output is correct |
31 |
Correct |
49 ms |
50012 KB |
Output is correct |
32 |
Correct |
62 ms |
52148 KB |
Output is correct |
33 |
Correct |
59 ms |
52092 KB |
Output is correct |
34 |
Correct |
62 ms |
52076 KB |
Output is correct |
35 |
Correct |
53 ms |
52096 KB |
Output is correct |
36 |
Correct |
48 ms |
52016 KB |
Output is correct |
37 |
Correct |
57 ms |
52060 KB |
Output is correct |
38 |
Correct |
57 ms |
52056 KB |
Output is correct |
39 |
Correct |
58 ms |
52200 KB |
Output is correct |
40 |
Correct |
56 ms |
52212 KB |
Output is correct |
41 |
Correct |
51 ms |
52060 KB |
Output is correct |
42 |
Correct |
49 ms |
52060 KB |
Output is correct |
43 |
Correct |
47 ms |
52060 KB |
Output is correct |
44 |
Correct |
60 ms |
52076 KB |
Output is correct |
45 |
Correct |
59 ms |
52128 KB |
Output is correct |
46 |
Correct |
57 ms |
42000 KB |
Output is correct |
47 |
Correct |
55 ms |
39972 KB |
Output is correct |
48 |
Correct |
52 ms |
41976 KB |
Output is correct |
49 |
Correct |
48 ms |
39956 KB |
Output is correct |
50 |
Correct |
82 ms |
42004 KB |
Output is correct |
51 |
Correct |
67 ms |
39768 KB |
Output is correct |
52 |
Correct |
57 ms |
52060 KB |
Output is correct |
53 |
Correct |
70 ms |
52096 KB |
Output is correct |
54 |
Correct |
60 ms |
52116 KB |
Output is correct |
55 |
Correct |
59 ms |
49900 KB |
Output is correct |
56 |
Correct |
7 ms |
47708 KB |
Output is correct |
57 |
Correct |
8 ms |
50012 KB |
Output is correct |
58 |
Correct |
7 ms |
47964 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
49752 KB |
Output is correct |
2 |
Correct |
6 ms |
49756 KB |
Output is correct |
3 |
Correct |
10 ms |
47916 KB |
Output is correct |
4 |
Execution timed out |
5056 ms |
61576 KB |
Time limit exceeded |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
51800 KB |
Output is correct |
2 |
Correct |
38 ms |
54228 KB |
Output is correct |
3 |
Correct |
49 ms |
54236 KB |
Output is correct |
4 |
Correct |
46 ms |
54364 KB |
Output is correct |
5 |
Correct |
92 ms |
58968 KB |
Output is correct |
6 |
Correct |
98 ms |
56916 KB |
Output is correct |
7 |
Correct |
84 ms |
56148 KB |
Output is correct |
8 |
Correct |
82 ms |
55904 KB |
Output is correct |
9 |
Correct |
77 ms |
55560 KB |
Output is correct |
10 |
Correct |
78 ms |
55752 KB |
Output is correct |
11 |
Correct |
72 ms |
55636 KB |
Output is correct |
12 |
Correct |
70 ms |
55656 KB |
Output is correct |
13 |
Correct |
70 ms |
55856 KB |
Output is correct |
14 |
Correct |
63 ms |
56144 KB |
Output is correct |
15 |
Correct |
69 ms |
57644 KB |
Output is correct |
16 |
Correct |
78 ms |
57464 KB |
Output is correct |
17 |
Correct |
79 ms |
57432 KB |
Output is correct |
18 |
Correct |
83 ms |
57408 KB |
Output is correct |
19 |
Correct |
81 ms |
58576 KB |
Output is correct |
20 |
Correct |
74 ms |
57612 KB |
Output is correct |
21 |
Correct |
77 ms |
56616 KB |
Output is correct |
22 |
Correct |
72 ms |
56144 KB |
Output is correct |
23 |
Correct |
71 ms |
56016 KB |
Output is correct |
24 |
Correct |
69 ms |
55776 KB |
Output is correct |
25 |
Correct |
68 ms |
55804 KB |
Output is correct |
26 |
Correct |
76 ms |
56060 KB |
Output is correct |
27 |
Correct |
66 ms |
55676 KB |
Output is correct |
28 |
Correct |
62 ms |
55644 KB |
Output is correct |
29 |
Correct |
59 ms |
55572 KB |
Output is correct |
30 |
Correct |
59 ms |
55888 KB |
Output is correct |
31 |
Correct |
64 ms |
55848 KB |
Output is correct |
32 |
Correct |
55 ms |
56144 KB |
Output is correct |
33 |
Correct |
61 ms |
56668 KB |
Output is correct |
34 |
Correct |
65 ms |
57744 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
51804 KB |
Output is correct |
2 |
Correct |
7 ms |
51804 KB |
Output is correct |
3 |
Correct |
7 ms |
51804 KB |
Output is correct |
4 |
Execution timed out |
5043 ms |
57700 KB |
Time limit exceeded |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
51804 KB |
Output is correct |
2 |
Correct |
7 ms |
51804 KB |
Output is correct |
3 |
Correct |
6 ms |
51864 KB |
Output is correct |
4 |
Correct |
9 ms |
51800 KB |
Output is correct |
5 |
Correct |
8 ms |
51804 KB |
Output is correct |
6 |
Correct |
8 ms |
51960 KB |
Output is correct |
7 |
Correct |
10 ms |
51804 KB |
Output is correct |
8 |
Correct |
8 ms |
51920 KB |
Output is correct |
9 |
Correct |
11 ms |
51804 KB |
Output is correct |
10 |
Correct |
11 ms |
51804 KB |
Output is correct |
11 |
Correct |
24 ms |
49908 KB |
Output is correct |
12 |
Correct |
16 ms |
51804 KB |
Output is correct |
13 |
Correct |
17 ms |
51804 KB |
Output is correct |
14 |
Correct |
16 ms |
51804 KB |
Output is correct |
15 |
Correct |
10 ms |
51960 KB |
Output is correct |
16 |
Correct |
12 ms |
51804 KB |
Output is correct |
17 |
Correct |
11 ms |
51804 KB |
Output is correct |
18 |
Correct |
11 ms |
51804 KB |
Output is correct |
19 |
Correct |
10 ms |
51940 KB |
Output is correct |
20 |
Correct |
11 ms |
52056 KB |
Output is correct |
21 |
Correct |
12 ms |
51804 KB |
Output is correct |
22 |
Correct |
11 ms |
51932 KB |
Output is correct |
23 |
Correct |
11 ms |
51804 KB |
Output is correct |
24 |
Correct |
12 ms |
49756 KB |
Output is correct |
25 |
Correct |
11 ms |
51800 KB |
Output is correct |
26 |
Correct |
11 ms |
51972 KB |
Output is correct |
27 |
Correct |
8 ms |
51804 KB |
Output is correct |
28 |
Correct |
7 ms |
51804 KB |
Output is correct |
29 |
Correct |
8 ms |
51804 KB |
Output is correct |
30 |
Correct |
47 ms |
52060 KB |
Output is correct |
31 |
Correct |
49 ms |
50012 KB |
Output is correct |
32 |
Correct |
62 ms |
52148 KB |
Output is correct |
33 |
Correct |
59 ms |
52092 KB |
Output is correct |
34 |
Correct |
62 ms |
52076 KB |
Output is correct |
35 |
Correct |
53 ms |
52096 KB |
Output is correct |
36 |
Correct |
48 ms |
52016 KB |
Output is correct |
37 |
Correct |
57 ms |
52060 KB |
Output is correct |
38 |
Correct |
57 ms |
52056 KB |
Output is correct |
39 |
Correct |
58 ms |
52200 KB |
Output is correct |
40 |
Correct |
56 ms |
52212 KB |
Output is correct |
41 |
Correct |
51 ms |
52060 KB |
Output is correct |
42 |
Correct |
49 ms |
52060 KB |
Output is correct |
43 |
Correct |
47 ms |
52060 KB |
Output is correct |
44 |
Correct |
60 ms |
52076 KB |
Output is correct |
45 |
Correct |
59 ms |
52128 KB |
Output is correct |
46 |
Correct |
57 ms |
42000 KB |
Output is correct |
47 |
Correct |
55 ms |
39972 KB |
Output is correct |
48 |
Correct |
52 ms |
41976 KB |
Output is correct |
49 |
Correct |
48 ms |
39956 KB |
Output is correct |
50 |
Correct |
82 ms |
42004 KB |
Output is correct |
51 |
Correct |
67 ms |
39768 KB |
Output is correct |
52 |
Correct |
57 ms |
52060 KB |
Output is correct |
53 |
Correct |
70 ms |
52096 KB |
Output is correct |
54 |
Correct |
60 ms |
52116 KB |
Output is correct |
55 |
Correct |
59 ms |
49900 KB |
Output is correct |
56 |
Correct |
7 ms |
47708 KB |
Output is correct |
57 |
Correct |
8 ms |
50012 KB |
Output is correct |
58 |
Correct |
7 ms |
47964 KB |
Output is correct |
59 |
Correct |
7 ms |
49752 KB |
Output is correct |
60 |
Correct |
6 ms |
49756 KB |
Output is correct |
61 |
Correct |
10 ms |
47916 KB |
Output is correct |
62 |
Execution timed out |
5056 ms |
61576 KB |
Time limit exceeded |
63 |
Halted |
0 ms |
0 KB |
- |