Submission #1119265

# Submission time Handle Problem Language Result Execution time Memory
1119265 2024-11-26T17:49:38 Z MrAndria Tourism (JOI23_tourism) C++17
28 / 100
5000 ms 23404 KB
#pragma GCC optimize("Ofast")
#pragma GCC optimize ("O3")
#pragma GCC optimization ("unroll-loops")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,fma")
#include <bits/stdc++.h>
using namespace std;
#define ff first
#define ss second
#define pb push_back
//#define int long long
int n,k,u,v,tin[200005],m,num[200005],val[200005],l,r,ans[200005],a[200005],sz,timer=0,dst[200005],lg,up[200005][25],tout[200005],q;
pair <pair <int,int> ,int> query[200005];
vector <int> adj[100005];
set <int> s;
void dfs(int x,int p,int ds){
    tin[x]=timer++;
    val[tin[x]]=x;
    dst[x]=ds;
    up[x][0]=p;
    for (int i = 1; i <= lg; ++i)
        up[x][i] = up[up[x][i-1]][i-1];
    for(int u=0;u<adj[x].size();u++){
        if(adj[x][u]!=p){
            dfs(adj[x][u],x,ds+1);

        }
    }
    tout[x] = timer++;
}
bool is_ancestor(int u, int v)
{
    return tin[u] <= tin[v] && tout[u] >= tout[v];
}

int lca(int u, int v)
{
    if (is_ancestor(u, v))
        return u;
    if (is_ancestor(v, u))
        return v;
    for (int i = lg; i >= 0; --i) {
        if (!is_ancestor(up[u][i], v))
            u = up[u][i];
    }
    return up[u][0];
}
int dist(int x,int y){
	return dst[x]+dst[y]-2*dst[lca(x,y)];
}
inline void add(int x){
    // cout<<x<<"-"<<tin[x]<<endl;
    num[x]++;
    if(num[x]>1){
        return;
    }
    if(s.size()==0){
        s.insert(tin[x]);
        sz=0;
 
        return;
    }
    s.insert(tin[x]);
    
	set <int> :: iterator it=s.lower_bound(tin[x]);
    set <int> :: iterator it1=it;
    set <int> :: iterator it2=it;
    if(it1==s.begin()){
        it1=s.end();
 
    }
    it1--;
    it2++;
    if(it2==s.end()){
        it2=s.begin();
 
    }
	// cout<<(*it1)<<" "<<(*it)<<" "<<(*it2)<<" "<<val[(*it1)]<<" "<<val[(*it)]<<" "<<val[(*it2)]<<" "<<s.size()<<" "<<dist(val[(*it1)],val[(*it)])<<" "<<dist(val[(*it)],val[(*it2)])<<" "<<dist(val[(*it1)],val[(*it2)])<<" add\n";
    sz+=(dist(val[(*it1)],val[(*it)])+dist(val[(*it)],val[(*it2)])-dist(val[(*it1)],val[(*it2)]));
//    cout<<" "<<sz<<endl;
}
inline void remove(int x){
    num[x]--;
    if(num[x]>0){
        return;
    }
    set <int> :: iterator it=s.lower_bound(tin[x]);
    if(s.size()==1){
        s.erase(it);
//        cout<<s.size()<<endl;
        sz=0;
 
        return;
    }
    set <int> :: iterator it1=it;
    set <int> :: iterator it2=it;
    if(it1==s.begin()){
        it1=s.end();
 
    }
    it1--;
    it2++;
    if(it2==s.end()){
        it2=s.begin();
 
    }
//    cout<<(*it1)<<" "<<(*it)<<" "<<(*it2)<<" "<<val[(*it1)]<<" "<<val[(*it)]<<" "<<val[(*it2)]<<" "<<s.size()<<" "<<dist(val[(*it1)],val[(*it)])<<" "<<dist(val[(*it)],val[(*it2)])<<" "<<dist(val[(*it1)],val[(*it2)])<<" remove";
    sz-=(dist(val[(*it1)],val[(*it)])+dist(val[(*it)],val[(*it2)])-dist(val[(*it1)],val[(*it2)]));
    
    s.erase(it);
    
//    cout<<" "<<sz<<endl;
}
bool comp(pair <pair <int,int>,int> a, pair <pair <int,int> ,int > b){
    if(a.ff.ff/k ==b.ff.ff/k){
        return a.ff.ss<b.ff.ss;
    }
    return (a.ff.ff)/k < (b.ff.ff)/k;
}
int main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);

    cin>>n>>m>>q;


    lg=ceil(log2(n));
    k=ceil(sqrt(m));
    
    for(int i=1;i<=n-1;i++){
        cin>>u>>v;

        adj[u].pb(v);
        adj[v].pb(u);

    }
    for(int i=1;i<=m;i++){
        cin>>a[i];

    }
    for(int i=1;i<=q;i++){
        cin>>query[i].ff.ff>>query[i].ff.ss;
        query[i].ss=i;
        
    }

    dfs(1,1,0);
    sort(query+1,query+q+1,comp);
    int tl=1;
    int tr=1;
    int l,r;
    add(a[tl]);
    for(int i=1;i<=q;i++){
    	l=query[i].ff.ff;
        r=query[i].ff.ss;
        while(tr<r){
            tr++;
            add(a[tr]);

        }
        while(l<tl){
            tl--;
            add(a[tl]);
        }
        while(r<tr){
            remove(a[tr]);
            tr--;
        }

        while(tl<l){
            remove(a[tl]);
            tl++;
        }
        
        ans[query[i].ss]=(sz/2)+1;

    }
    for(int i=1;i<=q;i++){
        printf("%d ",ans[i]);
    }
}

Compilation message

tourism.cpp:3: warning: ignoring '#pragma GCC optimization' [-Wunknown-pragmas]
    3 | #pragma GCC optimization ("unroll-loops")
      | 
tourism.cpp: In function 'void dfs(int, int, int)':
tourism.cpp:22:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   22 |     for(int u=0;u<adj[x].size();u++){
      |                 ~^~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 4 ms 2640 KB Output is correct
2 Correct 4 ms 2896 KB Output is correct
3 Correct 3 ms 2640 KB Output is correct
4 Correct 3 ms 7048 KB Output is correct
5 Correct 2 ms 2896 KB Output is correct
6 Correct 4 ms 2896 KB Output is correct
7 Correct 4 ms 2824 KB Output is correct
8 Correct 3 ms 2896 KB Output is correct
9 Correct 4 ms 2896 KB Output is correct
10 Correct 5 ms 2896 KB Output is correct
11 Correct 4 ms 3064 KB Output is correct
12 Correct 5 ms 2896 KB Output is correct
13 Correct 5 ms 3060 KB Output is correct
14 Correct 4 ms 2896 KB Output is correct
15 Correct 3 ms 2896 KB Output is correct
16 Correct 4 ms 2896 KB Output is correct
17 Correct 5 ms 2896 KB Output is correct
18 Correct 3 ms 2896 KB Output is correct
19 Correct 3 ms 2896 KB Output is correct
20 Correct 3 ms 2896 KB Output is correct
21 Correct 4 ms 2896 KB Output is correct
22 Correct 5 ms 2896 KB Output is correct
23 Correct 4 ms 2820 KB Output is correct
24 Correct 6 ms 2940 KB Output is correct
25 Correct 4 ms 2896 KB Output is correct
26 Correct 4 ms 2896 KB Output is correct
27 Correct 3 ms 2640 KB Output is correct
28 Correct 4 ms 2668 KB Output is correct
29 Correct 2 ms 2896 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 2640 KB Output is correct
2 Correct 4 ms 2896 KB Output is correct
3 Correct 3 ms 2640 KB Output is correct
4 Correct 3 ms 7048 KB Output is correct
5 Correct 2 ms 2896 KB Output is correct
6 Correct 4 ms 2896 KB Output is correct
7 Correct 4 ms 2824 KB Output is correct
8 Correct 3 ms 2896 KB Output is correct
9 Correct 4 ms 2896 KB Output is correct
10 Correct 5 ms 2896 KB Output is correct
11 Correct 4 ms 3064 KB Output is correct
12 Correct 5 ms 2896 KB Output is correct
13 Correct 5 ms 3060 KB Output is correct
14 Correct 4 ms 2896 KB Output is correct
15 Correct 3 ms 2896 KB Output is correct
16 Correct 4 ms 2896 KB Output is correct
17 Correct 5 ms 2896 KB Output is correct
18 Correct 3 ms 2896 KB Output is correct
19 Correct 3 ms 2896 KB Output is correct
20 Correct 3 ms 2896 KB Output is correct
21 Correct 4 ms 2896 KB Output is correct
22 Correct 5 ms 2896 KB Output is correct
23 Correct 4 ms 2820 KB Output is correct
24 Correct 6 ms 2940 KB Output is correct
25 Correct 4 ms 2896 KB Output is correct
26 Correct 4 ms 2896 KB Output is correct
27 Correct 3 ms 2640 KB Output is correct
28 Correct 4 ms 2668 KB Output is correct
29 Correct 2 ms 2896 KB Output is correct
30 Correct 16 ms 3152 KB Output is correct
31 Correct 17 ms 3152 KB Output is correct
32 Correct 28 ms 3152 KB Output is correct
33 Correct 29 ms 3312 KB Output is correct
34 Correct 24 ms 3316 KB Output is correct
35 Correct 5 ms 3084 KB Output is correct
36 Correct 5 ms 3152 KB Output is correct
37 Correct 8 ms 3152 KB Output is correct
38 Correct 14 ms 3384 KB Output is correct
39 Correct 17 ms 3392 KB Output is correct
40 Correct 16 ms 3408 KB Output is correct
41 Correct 5 ms 3408 KB Output is correct
42 Correct 7 ms 3196 KB Output is correct
43 Correct 7 ms 3152 KB Output is correct
44 Correct 19 ms 3356 KB Output is correct
45 Correct 24 ms 3152 KB Output is correct
46 Correct 22 ms 3344 KB Output is correct
47 Correct 4 ms 3192 KB Output is correct
48 Correct 7 ms 3464 KB Output is correct
49 Correct 7 ms 3580 KB Output is correct
50 Correct 31 ms 3156 KB Output is correct
51 Correct 24 ms 3312 KB Output is correct
52 Correct 30 ms 5256 KB Output is correct
53 Correct 29 ms 3152 KB Output is correct
54 Correct 38 ms 3312 KB Output is correct
55 Correct 37 ms 3312 KB Output is correct
56 Correct 3 ms 2896 KB Output is correct
57 Correct 5 ms 3152 KB Output is correct
58 Correct 5 ms 3152 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 2640 KB Output is correct
2 Correct 2 ms 2640 KB Output is correct
3 Correct 4 ms 4688 KB Output is correct
4 Execution timed out 5076 ms 20040 KB Time limit exceeded
5 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 3 ms 2640 KB Output is correct
2 Correct 92 ms 12460 KB Output is correct
3 Correct 120 ms 13552 KB Output is correct
4 Correct 121 ms 14512 KB Output is correct
5 Correct 130 ms 23068 KB Output is correct
6 Correct 169 ms 22528 KB Output is correct
7 Correct 239 ms 21188 KB Output is correct
8 Correct 234 ms 20556 KB Output is correct
9 Correct 222 ms 20432 KB Output is correct
10 Correct 227 ms 20376 KB Output is correct
11 Correct 225 ms 20372 KB Output is correct
12 Correct 192 ms 20412 KB Output is correct
13 Correct 176 ms 20804 KB Output is correct
14 Correct 207 ms 21048 KB Output is correct
15 Correct 197 ms 23404 KB Output is correct
16 Correct 223 ms 22344 KB Output is correct
17 Correct 245 ms 22552 KB Output is correct
18 Correct 231 ms 22520 KB Output is correct
19 Correct 117 ms 23164 KB Output is correct
20 Correct 137 ms 23268 KB Output is correct
21 Correct 135 ms 21864 KB Output is correct
22 Correct 152 ms 21072 KB Output is correct
23 Correct 139 ms 20552 KB Output is correct
24 Correct 168 ms 20348 KB Output is correct
25 Correct 192 ms 20408 KB Output is correct
26 Correct 239 ms 20416 KB Output is correct
27 Correct 212 ms 20376 KB Output is correct
28 Correct 192 ms 20296 KB Output is correct
29 Correct 194 ms 20184 KB Output is correct
30 Correct 227 ms 20296 KB Output is correct
31 Correct 185 ms 20612 KB Output is correct
32 Correct 201 ms 20912 KB Output is correct
33 Correct 205 ms 21720 KB Output is correct
34 Correct 222 ms 23084 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 2640 KB Output is correct
2 Correct 4 ms 2640 KB Output is correct
3 Correct 4 ms 2896 KB Output is correct
4 Execution timed out 5082 ms 17208 KB Time limit exceeded
5 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 4 ms 2640 KB Output is correct
2 Correct 4 ms 2896 KB Output is correct
3 Correct 3 ms 2640 KB Output is correct
4 Correct 3 ms 7048 KB Output is correct
5 Correct 2 ms 2896 KB Output is correct
6 Correct 4 ms 2896 KB Output is correct
7 Correct 4 ms 2824 KB Output is correct
8 Correct 3 ms 2896 KB Output is correct
9 Correct 4 ms 2896 KB Output is correct
10 Correct 5 ms 2896 KB Output is correct
11 Correct 4 ms 3064 KB Output is correct
12 Correct 5 ms 2896 KB Output is correct
13 Correct 5 ms 3060 KB Output is correct
14 Correct 4 ms 2896 KB Output is correct
15 Correct 3 ms 2896 KB Output is correct
16 Correct 4 ms 2896 KB Output is correct
17 Correct 5 ms 2896 KB Output is correct
18 Correct 3 ms 2896 KB Output is correct
19 Correct 3 ms 2896 KB Output is correct
20 Correct 3 ms 2896 KB Output is correct
21 Correct 4 ms 2896 KB Output is correct
22 Correct 5 ms 2896 KB Output is correct
23 Correct 4 ms 2820 KB Output is correct
24 Correct 6 ms 2940 KB Output is correct
25 Correct 4 ms 2896 KB Output is correct
26 Correct 4 ms 2896 KB Output is correct
27 Correct 3 ms 2640 KB Output is correct
28 Correct 4 ms 2668 KB Output is correct
29 Correct 2 ms 2896 KB Output is correct
30 Correct 16 ms 3152 KB Output is correct
31 Correct 17 ms 3152 KB Output is correct
32 Correct 28 ms 3152 KB Output is correct
33 Correct 29 ms 3312 KB Output is correct
34 Correct 24 ms 3316 KB Output is correct
35 Correct 5 ms 3084 KB Output is correct
36 Correct 5 ms 3152 KB Output is correct
37 Correct 8 ms 3152 KB Output is correct
38 Correct 14 ms 3384 KB Output is correct
39 Correct 17 ms 3392 KB Output is correct
40 Correct 16 ms 3408 KB Output is correct
41 Correct 5 ms 3408 KB Output is correct
42 Correct 7 ms 3196 KB Output is correct
43 Correct 7 ms 3152 KB Output is correct
44 Correct 19 ms 3356 KB Output is correct
45 Correct 24 ms 3152 KB Output is correct
46 Correct 22 ms 3344 KB Output is correct
47 Correct 4 ms 3192 KB Output is correct
48 Correct 7 ms 3464 KB Output is correct
49 Correct 7 ms 3580 KB Output is correct
50 Correct 31 ms 3156 KB Output is correct
51 Correct 24 ms 3312 KB Output is correct
52 Correct 30 ms 5256 KB Output is correct
53 Correct 29 ms 3152 KB Output is correct
54 Correct 38 ms 3312 KB Output is correct
55 Correct 37 ms 3312 KB Output is correct
56 Correct 3 ms 2896 KB Output is correct
57 Correct 5 ms 3152 KB Output is correct
58 Correct 5 ms 3152 KB Output is correct
59 Correct 3 ms 2640 KB Output is correct
60 Correct 2 ms 2640 KB Output is correct
61 Correct 4 ms 4688 KB Output is correct
62 Execution timed out 5076 ms 20040 KB Time limit exceeded
63 Halted 0 ms 0 KB -