Submission #1119257

# Submission time Handle Problem Language Result Execution time Memory
1119257 2024-11-26T17:42:59 Z MrAndria Tourism (JOI23_tourism) C++17
28 / 100
5000 ms 23416 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(){
    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: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++){
      |                 ~^~~~~~~~~~~~~~
tourism.cpp: In function 'int main()':
tourism.cpp:120:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  120 |     scanf("%d%d%d",&n,&m,&q);
      |     ~~~~~^~~~~~~~~~~~~~~~~~~
tourism.cpp:126:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  126 |         scanf("%d%d",&u,&v);
      |         ~~~~~^~~~~~~~~~~~~~
tourism.cpp:132:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  132 |         scanf("%d",&a[i]);
      |         ~~~~~^~~~~~~~~~~~
tourism.cpp:135:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  135 |         scanf("%d%d",&query[i].ff.ff,&query[i].ff.ss);
      |         ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 4 ms 2896 KB Output is correct
2 Correct 3 ms 2812 KB Output is correct
3 Correct 3 ms 2800 KB Output is correct
4 Correct 4 ms 2664 KB Output is correct
5 Correct 4 ms 2896 KB Output is correct
6 Correct 5 ms 2800 KB Output is correct
7 Correct 6 ms 2896 KB Output is correct
8 Correct 3 ms 2896 KB Output is correct
9 Correct 5 ms 2896 KB Output is correct
10 Correct 5 ms 2896 KB Output is correct
11 Correct 4 ms 2896 KB Output is correct
12 Correct 3 ms 2896 KB Output is correct
13 Correct 4 ms 2804 KB Output is correct
14 Correct 3 ms 2896 KB Output is correct
15 Correct 4 ms 2804 KB Output is correct
16 Correct 3 ms 2896 KB Output is correct
17 Correct 4 ms 2896 KB Output is correct
18 Correct 4 ms 2896 KB Output is correct
19 Correct 4 ms 2896 KB Output is correct
20 Correct 4 ms 2896 KB Output is correct
21 Correct 5 ms 2800 KB Output is correct
22 Correct 3 ms 2896 KB Output is correct
23 Correct 5 ms 2896 KB Output is correct
24 Correct 4 ms 2896 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 3 ms 2640 KB Output is correct
29 Correct 3 ms 2808 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 2896 KB Output is correct
2 Correct 3 ms 2812 KB Output is correct
3 Correct 3 ms 2800 KB Output is correct
4 Correct 4 ms 2664 KB Output is correct
5 Correct 4 ms 2896 KB Output is correct
6 Correct 5 ms 2800 KB Output is correct
7 Correct 6 ms 2896 KB Output is correct
8 Correct 3 ms 2896 KB Output is correct
9 Correct 5 ms 2896 KB Output is correct
10 Correct 5 ms 2896 KB Output is correct
11 Correct 4 ms 2896 KB Output is correct
12 Correct 3 ms 2896 KB Output is correct
13 Correct 4 ms 2804 KB Output is correct
14 Correct 3 ms 2896 KB Output is correct
15 Correct 4 ms 2804 KB Output is correct
16 Correct 3 ms 2896 KB Output is correct
17 Correct 4 ms 2896 KB Output is correct
18 Correct 4 ms 2896 KB Output is correct
19 Correct 4 ms 2896 KB Output is correct
20 Correct 4 ms 2896 KB Output is correct
21 Correct 5 ms 2800 KB Output is correct
22 Correct 3 ms 2896 KB Output is correct
23 Correct 5 ms 2896 KB Output is correct
24 Correct 4 ms 2896 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 3 ms 2640 KB Output is correct
29 Correct 3 ms 2808 KB Output is correct
30 Correct 20 ms 3164 KB Output is correct
31 Correct 20 ms 3152 KB Output is correct
32 Correct 34 ms 3164 KB Output is correct
33 Correct 27 ms 3152 KB Output is correct
34 Correct 28 ms 3152 KB Output is correct
35 Correct 6 ms 3068 KB Output is correct
36 Correct 6 ms 3176 KB Output is correct
37 Correct 7 ms 3172 KB Output is correct
38 Correct 15 ms 3164 KB Output is correct
39 Correct 21 ms 3364 KB Output is correct
40 Correct 16 ms 3208 KB Output is correct
41 Correct 7 ms 3420 KB Output is correct
42 Correct 4 ms 3412 KB Output is correct
43 Correct 6 ms 3328 KB Output is correct
44 Correct 22 ms 3160 KB Output is correct
45 Correct 16 ms 3156 KB Output is correct
46 Correct 23 ms 3340 KB Output is correct
47 Correct 6 ms 3328 KB Output is correct
48 Correct 7 ms 3164 KB Output is correct
49 Correct 5 ms 3336 KB Output is correct
50 Correct 32 ms 3336 KB Output is correct
51 Correct 30 ms 3164 KB Output is correct
52 Correct 30 ms 3164 KB Output is correct
53 Correct 34 ms 3168 KB Output is correct
54 Correct 30 ms 3304 KB Output is correct
55 Correct 30 ms 3312 KB Output is correct
56 Correct 3 ms 2908 KB Output is correct
57 Correct 5 ms 3164 KB Output is correct
58 Correct 6 ms 3164 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 2652 KB Output is correct
2 Correct 3 ms 2796 KB Output is correct
3 Correct 5 ms 2808 KB Output is correct
4 Execution timed out 5092 ms 20080 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 79 ms 12444 KB Output is correct
3 Correct 148 ms 13332 KB Output is correct
4 Correct 103 ms 14448 KB Output is correct
5 Correct 176 ms 23416 KB Output is correct
6 Correct 233 ms 22600 KB Output is correct
7 Correct 236 ms 21164 KB Output is correct
8 Correct 219 ms 20548 KB Output is correct
9 Correct 200 ms 20236 KB Output is correct
10 Correct 200 ms 20320 KB Output is correct
11 Correct 188 ms 20352 KB Output is correct
12 Correct 183 ms 20264 KB Output is correct
13 Correct 199 ms 20580 KB Output is correct
14 Correct 198 ms 21268 KB Output is correct
15 Correct 208 ms 23156 KB Output is correct
16 Correct 235 ms 22304 KB Output is correct
17 Correct 217 ms 22240 KB Output is correct
18 Correct 219 ms 22496 KB Output is correct
19 Correct 129 ms 23112 KB Output is correct
20 Correct 138 ms 23028 KB Output is correct
21 Correct 158 ms 21696 KB Output is correct
22 Correct 160 ms 21116 KB Output is correct
23 Correct 179 ms 20692 KB Output is correct
24 Correct 200 ms 20440 KB Output is correct
25 Correct 208 ms 20376 KB Output is correct
26 Correct 211 ms 20180 KB Output is correct
27 Correct 208 ms 20360 KB Output is correct
28 Correct 257 ms 20332 KB Output is correct
29 Correct 249 ms 20372 KB Output is correct
30 Correct 244 ms 20296 KB Output is correct
31 Correct 258 ms 20436 KB Output is correct
32 Correct 219 ms 21052 KB Output is correct
33 Correct 228 ms 21704 KB Output is correct
34 Correct 178 ms 23280 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 2820 KB Output is correct
2 Correct 2 ms 2640 KB Output is correct
3 Correct 4 ms 2864 KB Output is correct
4 Execution timed out 5055 ms 17420 KB Time limit exceeded
5 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 4 ms 2896 KB Output is correct
2 Correct 3 ms 2812 KB Output is correct
3 Correct 3 ms 2800 KB Output is correct
4 Correct 4 ms 2664 KB Output is correct
5 Correct 4 ms 2896 KB Output is correct
6 Correct 5 ms 2800 KB Output is correct
7 Correct 6 ms 2896 KB Output is correct
8 Correct 3 ms 2896 KB Output is correct
9 Correct 5 ms 2896 KB Output is correct
10 Correct 5 ms 2896 KB Output is correct
11 Correct 4 ms 2896 KB Output is correct
12 Correct 3 ms 2896 KB Output is correct
13 Correct 4 ms 2804 KB Output is correct
14 Correct 3 ms 2896 KB Output is correct
15 Correct 4 ms 2804 KB Output is correct
16 Correct 3 ms 2896 KB Output is correct
17 Correct 4 ms 2896 KB Output is correct
18 Correct 4 ms 2896 KB Output is correct
19 Correct 4 ms 2896 KB Output is correct
20 Correct 4 ms 2896 KB Output is correct
21 Correct 5 ms 2800 KB Output is correct
22 Correct 3 ms 2896 KB Output is correct
23 Correct 5 ms 2896 KB Output is correct
24 Correct 4 ms 2896 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 3 ms 2640 KB Output is correct
29 Correct 3 ms 2808 KB Output is correct
30 Correct 20 ms 3164 KB Output is correct
31 Correct 20 ms 3152 KB Output is correct
32 Correct 34 ms 3164 KB Output is correct
33 Correct 27 ms 3152 KB Output is correct
34 Correct 28 ms 3152 KB Output is correct
35 Correct 6 ms 3068 KB Output is correct
36 Correct 6 ms 3176 KB Output is correct
37 Correct 7 ms 3172 KB Output is correct
38 Correct 15 ms 3164 KB Output is correct
39 Correct 21 ms 3364 KB Output is correct
40 Correct 16 ms 3208 KB Output is correct
41 Correct 7 ms 3420 KB Output is correct
42 Correct 4 ms 3412 KB Output is correct
43 Correct 6 ms 3328 KB Output is correct
44 Correct 22 ms 3160 KB Output is correct
45 Correct 16 ms 3156 KB Output is correct
46 Correct 23 ms 3340 KB Output is correct
47 Correct 6 ms 3328 KB Output is correct
48 Correct 7 ms 3164 KB Output is correct
49 Correct 5 ms 3336 KB Output is correct
50 Correct 32 ms 3336 KB Output is correct
51 Correct 30 ms 3164 KB Output is correct
52 Correct 30 ms 3164 KB Output is correct
53 Correct 34 ms 3168 KB Output is correct
54 Correct 30 ms 3304 KB Output is correct
55 Correct 30 ms 3312 KB Output is correct
56 Correct 3 ms 2908 KB Output is correct
57 Correct 5 ms 3164 KB Output is correct
58 Correct 6 ms 3164 KB Output is correct
59 Correct 4 ms 2652 KB Output is correct
60 Correct 3 ms 2796 KB Output is correct
61 Correct 5 ms 2808 KB Output is correct
62 Execution timed out 5092 ms 20080 KB Time limit exceeded
63 Halted 0 ms 0 KB -