Submission #1119238

# Submission time Handle Problem Language Result Execution time Memory
1119238 2024-11-26T17:27:22 Z MrAndria Tourism (JOI23_tourism) C++17
28 / 100
5000 ms 23524 KB
#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(){
    scanf("%d%d%d",&n,&m,&q);

    lg=ceil(log2(n));
    k=ceil(sqrt(m));
    
    for(int i=1;i<=n-1;i++){
        scanf("%d%d",&u,&v);
        adj[u].pb(v);
        adj[v].pb(u);

    }
    for(int i=1;i<=m;i++){
        scanf("%d",&a[i]);
    }
    for(int i=1;i<=q;i++){
        scanf("%d%d",&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\n",ans[i]);
    }
}

Compilation message

tourism.cpp: In function 'void dfs(int, int, int)':
tourism.cpp:18:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   18 |     for(int u=0;u<adj[x].size();u++){
      |                 ~^~~~~~~~~~~~~~
tourism.cpp: In function 'int main()':
tourism.cpp:116:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  116 |     scanf("%d%d%d",&n,&m,&q);
      |     ~~~~~^~~~~~~~~~~~~~~~~~~
tourism.cpp:122:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  122 |         scanf("%d%d",&u,&v);
      |         ~~~~~^~~~~~~~~~~~~~
tourism.cpp:128:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  128 |         scanf("%d",&a[i]);
      |         ~~~~~^~~~~~~~~~~~
tourism.cpp:131:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  131 |         scanf("%d%d",&query[i].ff.ff,&query[i].ff.ss);
      |         ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2780 KB Output is correct
2 Correct 3 ms 2644 KB Output is correct
3 Correct 3 ms 2900 KB Output is correct
4 Correct 3 ms 2644 KB Output is correct
5 Correct 3 ms 2660 KB Output is correct
6 Correct 3 ms 2644 KB Output is correct
7 Correct 3 ms 2644 KB Output is correct
8 Correct 4 ms 2900 KB Output is correct
9 Correct 4 ms 2900 KB Output is correct
10 Correct 5 ms 2900 KB Output is correct
11 Correct 4 ms 2900 KB Output is correct
12 Correct 4 ms 2804 KB Output is correct
13 Correct 3 ms 2900 KB Output is correct
14 Correct 3 ms 2900 KB Output is correct
15 Correct 5 ms 3004 KB Output is correct
16 Correct 4 ms 2900 KB Output is correct
17 Correct 5 ms 2900 KB Output is correct
18 Correct 4 ms 2900 KB Output is correct
19 Correct 4 ms 2900 KB Output is correct
20 Correct 4 ms 2900 KB Output is correct
21 Correct 4 ms 2900 KB Output is correct
22 Correct 4 ms 2804 KB Output is correct
23 Correct 6 ms 2804 KB Output is correct
24 Correct 4 ms 2900 KB Output is correct
25 Correct 4 ms 2900 KB Output is correct
26 Correct 5 ms 3048 KB Output is correct
27 Correct 2 ms 2644 KB Output is correct
28 Correct 3 ms 2644 KB Output is correct
29 Correct 3 ms 2900 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2780 KB Output is correct
2 Correct 3 ms 2644 KB Output is correct
3 Correct 3 ms 2900 KB Output is correct
4 Correct 3 ms 2644 KB Output is correct
5 Correct 3 ms 2660 KB Output is correct
6 Correct 3 ms 2644 KB Output is correct
7 Correct 3 ms 2644 KB Output is correct
8 Correct 4 ms 2900 KB Output is correct
9 Correct 4 ms 2900 KB Output is correct
10 Correct 5 ms 2900 KB Output is correct
11 Correct 4 ms 2900 KB Output is correct
12 Correct 4 ms 2804 KB Output is correct
13 Correct 3 ms 2900 KB Output is correct
14 Correct 3 ms 2900 KB Output is correct
15 Correct 5 ms 3004 KB Output is correct
16 Correct 4 ms 2900 KB Output is correct
17 Correct 5 ms 2900 KB Output is correct
18 Correct 4 ms 2900 KB Output is correct
19 Correct 4 ms 2900 KB Output is correct
20 Correct 4 ms 2900 KB Output is correct
21 Correct 4 ms 2900 KB Output is correct
22 Correct 4 ms 2804 KB Output is correct
23 Correct 6 ms 2804 KB Output is correct
24 Correct 4 ms 2900 KB Output is correct
25 Correct 4 ms 2900 KB Output is correct
26 Correct 5 ms 3048 KB Output is correct
27 Correct 2 ms 2644 KB Output is correct
28 Correct 3 ms 2644 KB Output is correct
29 Correct 3 ms 2900 KB Output is correct
30 Correct 15 ms 3412 KB Output is correct
31 Correct 24 ms 3168 KB Output is correct
32 Correct 24 ms 3292 KB Output is correct
33 Correct 35 ms 3280 KB Output is correct
34 Correct 24 ms 3156 KB Output is correct
35 Correct 6 ms 3156 KB Output is correct
36 Correct 6 ms 3208 KB Output is correct
37 Correct 7 ms 3300 KB Output is correct
38 Correct 15 ms 3156 KB Output is correct
39 Correct 19 ms 3484 KB Output is correct
40 Correct 18 ms 3172 KB Output is correct
41 Correct 5 ms 3156 KB Output is correct
42 Correct 5 ms 3156 KB Output is correct
43 Correct 6 ms 3324 KB Output is correct
44 Correct 16 ms 3156 KB Output is correct
45 Correct 21 ms 3156 KB Output is correct
46 Correct 22 ms 3412 KB Output is correct
47 Correct 5 ms 3156 KB Output is correct
48 Correct 7 ms 3068 KB Output is correct
49 Correct 5 ms 3108 KB Output is correct
50 Correct 25 ms 3304 KB Output is correct
51 Correct 24 ms 3200 KB Output is correct
52 Correct 34 ms 3312 KB Output is correct
53 Correct 26 ms 3168 KB Output is correct
54 Correct 26 ms 3304 KB Output is correct
55 Correct 39 ms 3676 KB Output is correct
56 Correct 5 ms 2852 KB Output is correct
57 Correct 4 ms 3168 KB Output is correct
58 Correct 4 ms 3168 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 2792 KB Output is correct
2 Correct 3 ms 2656 KB Output is correct
3 Correct 6 ms 2912 KB Output is correct
4 Execution timed out 5047 ms 19956 KB Time limit exceeded
5 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 3 ms 2780 KB Output is correct
2 Correct 91 ms 12380 KB Output is correct
3 Correct 124 ms 13388 KB Output is correct
4 Correct 110 ms 14668 KB Output is correct
5 Correct 134 ms 23172 KB Output is correct
6 Correct 167 ms 22544 KB Output is correct
7 Correct 178 ms 21064 KB Output is correct
8 Correct 184 ms 20484 KB Output is correct
9 Correct 185 ms 20412 KB Output is correct
10 Correct 222 ms 20256 KB Output is correct
11 Correct 205 ms 20300 KB Output is correct
12 Correct 211 ms 20300 KB Output is correct
13 Correct 192 ms 20564 KB Output is correct
14 Correct 203 ms 21376 KB Output is correct
15 Correct 179 ms 23052 KB Output is correct
16 Correct 217 ms 22268 KB Output is correct
17 Correct 200 ms 22232 KB Output is correct
18 Correct 217 ms 22092 KB Output is correct
19 Correct 119 ms 23172 KB Output is correct
20 Correct 115 ms 23524 KB Output is correct
21 Correct 132 ms 21836 KB Output is correct
22 Correct 133 ms 21036 KB Output is correct
23 Correct 149 ms 20728 KB Output is correct
24 Correct 155 ms 20496 KB Output is correct
25 Correct 191 ms 20204 KB Output is correct
26 Correct 213 ms 20300 KB Output is correct
27 Correct 212 ms 20296 KB Output is correct
28 Correct 195 ms 20224 KB Output is correct
29 Correct 235 ms 20276 KB Output is correct
30 Correct 230 ms 20472 KB Output is correct
31 Correct 217 ms 20396 KB Output is correct
32 Correct 208 ms 20812 KB Output is correct
33 Correct 207 ms 21692 KB Output is correct
34 Correct 193 ms 23180 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2776 KB Output is correct
2 Correct 3 ms 2644 KB Output is correct
3 Correct 5 ms 2900 KB Output is correct
4 Execution timed out 5067 ms 17492 KB Time limit exceeded
5 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2780 KB Output is correct
2 Correct 3 ms 2644 KB Output is correct
3 Correct 3 ms 2900 KB Output is correct
4 Correct 3 ms 2644 KB Output is correct
5 Correct 3 ms 2660 KB Output is correct
6 Correct 3 ms 2644 KB Output is correct
7 Correct 3 ms 2644 KB Output is correct
8 Correct 4 ms 2900 KB Output is correct
9 Correct 4 ms 2900 KB Output is correct
10 Correct 5 ms 2900 KB Output is correct
11 Correct 4 ms 2900 KB Output is correct
12 Correct 4 ms 2804 KB Output is correct
13 Correct 3 ms 2900 KB Output is correct
14 Correct 3 ms 2900 KB Output is correct
15 Correct 5 ms 3004 KB Output is correct
16 Correct 4 ms 2900 KB Output is correct
17 Correct 5 ms 2900 KB Output is correct
18 Correct 4 ms 2900 KB Output is correct
19 Correct 4 ms 2900 KB Output is correct
20 Correct 4 ms 2900 KB Output is correct
21 Correct 4 ms 2900 KB Output is correct
22 Correct 4 ms 2804 KB Output is correct
23 Correct 6 ms 2804 KB Output is correct
24 Correct 4 ms 2900 KB Output is correct
25 Correct 4 ms 2900 KB Output is correct
26 Correct 5 ms 3048 KB Output is correct
27 Correct 2 ms 2644 KB Output is correct
28 Correct 3 ms 2644 KB Output is correct
29 Correct 3 ms 2900 KB Output is correct
30 Correct 15 ms 3412 KB Output is correct
31 Correct 24 ms 3168 KB Output is correct
32 Correct 24 ms 3292 KB Output is correct
33 Correct 35 ms 3280 KB Output is correct
34 Correct 24 ms 3156 KB Output is correct
35 Correct 6 ms 3156 KB Output is correct
36 Correct 6 ms 3208 KB Output is correct
37 Correct 7 ms 3300 KB Output is correct
38 Correct 15 ms 3156 KB Output is correct
39 Correct 19 ms 3484 KB Output is correct
40 Correct 18 ms 3172 KB Output is correct
41 Correct 5 ms 3156 KB Output is correct
42 Correct 5 ms 3156 KB Output is correct
43 Correct 6 ms 3324 KB Output is correct
44 Correct 16 ms 3156 KB Output is correct
45 Correct 21 ms 3156 KB Output is correct
46 Correct 22 ms 3412 KB Output is correct
47 Correct 5 ms 3156 KB Output is correct
48 Correct 7 ms 3068 KB Output is correct
49 Correct 5 ms 3108 KB Output is correct
50 Correct 25 ms 3304 KB Output is correct
51 Correct 24 ms 3200 KB Output is correct
52 Correct 34 ms 3312 KB Output is correct
53 Correct 26 ms 3168 KB Output is correct
54 Correct 26 ms 3304 KB Output is correct
55 Correct 39 ms 3676 KB Output is correct
56 Correct 5 ms 2852 KB Output is correct
57 Correct 4 ms 3168 KB Output is correct
58 Correct 4 ms 3168 KB Output is correct
59 Correct 4 ms 2792 KB Output is correct
60 Correct 3 ms 2656 KB Output is correct
61 Correct 6 ms 2912 KB Output is correct
62 Execution timed out 5047 ms 19956 KB Time limit exceeded
63 Halted 0 ms 0 KB -