Submission #850625

# Submission time Handle Problem Language Result Execution time Memory
850625 2023-09-17T07:43:19 Z huutuan Tourism (JOI23_tourism) C++14
28 / 100
5000 ms 76356 KB
#include<bits/stdc++.h>

using namespace std;

#define int long long
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define sz(x) ((int)x.size())
#define sumof(x) accumulate(all(x), 0ll)

const int N=1e5+1, S=320, LG=18;

struct query{
   int l, r, i;

   query(int a=0, int b=0, int c=0): l(a), r(b), i(c){}

   bool operator<(const query& x){
      return l/S!=x.l/S?l<x.l:r<x.r;
   }
} qq[N];

int n, m, q, a[N], tdfs, tin[N], tout[N], dep[N], ans[N];
pair<int, int> st[N*2][LG];
vector<int> g[N];

void dfs(int u, int p){
   tin[u]=++tdfs;
   dep[u]=dep[p]+1;
   st[tdfs][0]={dep[u], u};
   for (int v:g[u]) if (v!=p){
      dfs(v, u);
      ++tdfs;
      st[tdfs][0]={dep[u], u};
   }
   tout[u]=tdfs;
}

void build(){
   for (int k=1; k<LG; ++k) for (int i=1; i+(1<<k)-1<=tdfs; ++i) st[i][k]=min(st[i][k-1], st[i+(1<<(k-1))][k-1]);
}

int lca(int u, int v){
   u=tin[u]; v=tin[v];
   if (u>v) swap(u, v);
   int lg=__lg(v-u+1);
   return min(st[u][lg], st[v-(1<<lg)+1][lg]).second;
}

void solve(int tc){
   // cout << "Case #" << tc << ": ";
   cin >> n >> m >> q;
   for (int i=1; i<n; ++i){
      int x, y; cin >> x >> y;
      g[x].push_back(y);
      g[y].push_back(x);
   }
   dfs(1, 0);
   build();
   for (int i=1; i<=m; ++i) cin >> a[i];
   for (int i=1; i<=q; ++i){
      int x, y; cin >> x >> y;
      qq[i]={x, y, i};
   }
   sort(qq+1, qq+q+1);
   multiset<pair<int, int>> st;
   int val=0;
   function<int(int, int)> calc=[&](int x, int y) -> int {
      return dep[y]-dep[lca(x, y)];
   };
   function<void(int)> add=[&](int x) -> void {
      if (st.empty()){
         st.emplace(tin[x], x);
         return;
      }
      auto it=st.lower_bound({tin[x], x});
      auto previt=it==st.begin()?prev(st.end()):prev(it);
      auto nextit=it==st.end()?st.begin():it;
      val-=calc(previt->second, nextit->second);
      val+=calc(previt->second, x);
      val+=calc(x, nextit->second);
      st.emplace(tin[x], x);
   };
   function<void(int)> del=[&](int x) -> void {
      if (sz(st)==1){
         st.erase({tin[x], x});
         return;
      }
      auto it=st.find({tin[x], x});
      auto previt=it==st.begin()?prev(st.end()):prev(it);
      auto nextit=it==prev(st.end())?st.begin():next(it);
      val+=calc(previt->second, nextit->second);
      val-=calc(previt->second, x);
      val-=calc(x, nextit->second);
      st.erase(it);
   };
   int cl=1, cr=0;
   for (int i=1; i<=q; ++i){
      int l=qq[i].l, r=qq[i].r;
      while (l<cl){
         --cl;
         add(a[cl]);
      }
      while (r>cr){
         ++cr;
         add(a[cr]);
      }
      while (l>cl){
         del(a[cl]);
         ++cl;
      }
      while (r<cr){
         del(a[cr]);
         --cr;
      }
      ans[qq[i].i]=val;
   }
   for (int i=1; i<=q; ++i) cout << ans[i]+1 << '\n';
}

int32_t main(){
   ios_base::sync_with_stdio(false);
   cin.tie(nullptr);
   int ntests=1;
   // cin >> ntests;
   for (int i=1; i<=ntests; ++i) solve(i);
   return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 2 ms 9820 KB Output is correct
2 Correct 2 ms 9820 KB Output is correct
3 Correct 2 ms 9820 KB Output is correct
4 Correct 4 ms 9820 KB Output is correct
5 Correct 3 ms 9820 KB Output is correct
6 Correct 3 ms 9820 KB Output is correct
7 Correct 4 ms 9820 KB Output is correct
8 Correct 3 ms 10016 KB Output is correct
9 Correct 5 ms 10024 KB Output is correct
10 Correct 5 ms 9820 KB Output is correct
11 Correct 5 ms 10076 KB Output is correct
12 Correct 2 ms 10076 KB Output is correct
13 Correct 2 ms 10076 KB Output is correct
14 Correct 3 ms 9820 KB Output is correct
15 Correct 5 ms 10072 KB Output is correct
16 Correct 5 ms 9980 KB Output is correct
17 Correct 5 ms 10072 KB Output is correct
18 Correct 5 ms 10076 KB Output is correct
19 Correct 5 ms 10076 KB Output is correct
20 Correct 5 ms 10076 KB Output is correct
21 Correct 5 ms 9988 KB Output is correct
22 Correct 5 ms 10076 KB Output is correct
23 Correct 4 ms 10076 KB Output is correct
24 Correct 5 ms 10080 KB Output is correct
25 Correct 5 ms 10076 KB Output is correct
26 Correct 5 ms 10072 KB Output is correct
27 Correct 3 ms 9820 KB Output is correct
28 Correct 2 ms 9820 KB Output is correct
29 Correct 3 ms 9820 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 9820 KB Output is correct
2 Correct 2 ms 9820 KB Output is correct
3 Correct 2 ms 9820 KB Output is correct
4 Correct 4 ms 9820 KB Output is correct
5 Correct 3 ms 9820 KB Output is correct
6 Correct 3 ms 9820 KB Output is correct
7 Correct 4 ms 9820 KB Output is correct
8 Correct 3 ms 10016 KB Output is correct
9 Correct 5 ms 10024 KB Output is correct
10 Correct 5 ms 9820 KB Output is correct
11 Correct 5 ms 10076 KB Output is correct
12 Correct 2 ms 10076 KB Output is correct
13 Correct 2 ms 10076 KB Output is correct
14 Correct 3 ms 9820 KB Output is correct
15 Correct 5 ms 10072 KB Output is correct
16 Correct 5 ms 9980 KB Output is correct
17 Correct 5 ms 10072 KB Output is correct
18 Correct 5 ms 10076 KB Output is correct
19 Correct 5 ms 10076 KB Output is correct
20 Correct 5 ms 10076 KB Output is correct
21 Correct 5 ms 9988 KB Output is correct
22 Correct 5 ms 10076 KB Output is correct
23 Correct 4 ms 10076 KB Output is correct
24 Correct 5 ms 10080 KB Output is correct
25 Correct 5 ms 10076 KB Output is correct
26 Correct 5 ms 10072 KB Output is correct
27 Correct 3 ms 9820 KB Output is correct
28 Correct 2 ms 9820 KB Output is correct
29 Correct 3 ms 9820 KB Output is correct
30 Correct 23 ms 10196 KB Output is correct
31 Correct 27 ms 10076 KB Output is correct
32 Correct 35 ms 10284 KB Output is correct
33 Correct 36 ms 10284 KB Output is correct
34 Correct 34 ms 10076 KB Output is correct
35 Correct 6 ms 10076 KB Output is correct
36 Correct 6 ms 10076 KB Output is correct
37 Correct 6 ms 10072 KB Output is correct
38 Correct 35 ms 10332 KB Output is correct
39 Correct 34 ms 10328 KB Output is correct
40 Correct 34 ms 10396 KB Output is correct
41 Correct 6 ms 10328 KB Output is correct
42 Correct 6 ms 10332 KB Output is correct
43 Correct 6 ms 10332 KB Output is correct
44 Correct 36 ms 10332 KB Output is correct
45 Correct 34 ms 10076 KB Output is correct
46 Correct 35 ms 10328 KB Output is correct
47 Correct 6 ms 10328 KB Output is correct
48 Correct 6 ms 10332 KB Output is correct
49 Correct 7 ms 10344 KB Output is correct
50 Correct 32 ms 10320 KB Output is correct
51 Correct 31 ms 10076 KB Output is correct
52 Correct 32 ms 10328 KB Output is correct
53 Correct 31 ms 10300 KB Output is correct
54 Correct 37 ms 10312 KB Output is correct
55 Correct 32 ms 10072 KB Output is correct
56 Correct 18 ms 10072 KB Output is correct
57 Correct 3 ms 10076 KB Output is correct
58 Correct 3 ms 10076 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 9820 KB Output is correct
2 Correct 3 ms 9820 KB Output is correct
3 Correct 18 ms 10076 KB Output is correct
4 Execution timed out 5045 ms 54708 KB Time limit exceeded
5 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 2 ms 9816 KB Output is correct
2 Correct 68 ms 43784 KB Output is correct
3 Correct 102 ms 46472 KB Output is correct
4 Correct 77 ms 50664 KB Output is correct
5 Correct 132 ms 76356 KB Output is correct
6 Correct 153 ms 74072 KB Output is correct
7 Correct 140 ms 71348 KB Output is correct
8 Correct 165 ms 70408 KB Output is correct
9 Correct 140 ms 70228 KB Output is correct
10 Correct 124 ms 69968 KB Output is correct
11 Correct 134 ms 70228 KB Output is correct
12 Correct 129 ms 70140 KB Output is correct
13 Correct 126 ms 70144 KB Output is correct
14 Correct 112 ms 70592 KB Output is correct
15 Correct 126 ms 72020 KB Output is correct
16 Correct 136 ms 73012 KB Output is correct
17 Correct 138 ms 73132 KB Output is correct
18 Correct 142 ms 73092 KB Output is correct
19 Correct 126 ms 75852 KB Output is correct
20 Correct 150 ms 75872 KB Output is correct
21 Correct 136 ms 72016 KB Output is correct
22 Correct 146 ms 71092 KB Output is correct
23 Correct 148 ms 70476 KB Output is correct
24 Correct 128 ms 69716 KB Output is correct
25 Correct 135 ms 69716 KB Output is correct
26 Correct 126 ms 69892 KB Output is correct
27 Correct 125 ms 69656 KB Output is correct
28 Correct 116 ms 69460 KB Output is correct
29 Correct 115 ms 69656 KB Output is correct
30 Correct 118 ms 69712 KB Output is correct
31 Correct 116 ms 69784 KB Output is correct
32 Correct 111 ms 69968 KB Output is correct
33 Correct 108 ms 70664 KB Output is correct
34 Correct 112 ms 71652 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 10012 KB Output is correct
2 Correct 3 ms 10076 KB Output is correct
3 Correct 18 ms 10196 KB Output is correct
4 Execution timed out 5094 ms 51536 KB Time limit exceeded
5 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 2 ms 9820 KB Output is correct
2 Correct 2 ms 9820 KB Output is correct
3 Correct 2 ms 9820 KB Output is correct
4 Correct 4 ms 9820 KB Output is correct
5 Correct 3 ms 9820 KB Output is correct
6 Correct 3 ms 9820 KB Output is correct
7 Correct 4 ms 9820 KB Output is correct
8 Correct 3 ms 10016 KB Output is correct
9 Correct 5 ms 10024 KB Output is correct
10 Correct 5 ms 9820 KB Output is correct
11 Correct 5 ms 10076 KB Output is correct
12 Correct 2 ms 10076 KB Output is correct
13 Correct 2 ms 10076 KB Output is correct
14 Correct 3 ms 9820 KB Output is correct
15 Correct 5 ms 10072 KB Output is correct
16 Correct 5 ms 9980 KB Output is correct
17 Correct 5 ms 10072 KB Output is correct
18 Correct 5 ms 10076 KB Output is correct
19 Correct 5 ms 10076 KB Output is correct
20 Correct 5 ms 10076 KB Output is correct
21 Correct 5 ms 9988 KB Output is correct
22 Correct 5 ms 10076 KB Output is correct
23 Correct 4 ms 10076 KB Output is correct
24 Correct 5 ms 10080 KB Output is correct
25 Correct 5 ms 10076 KB Output is correct
26 Correct 5 ms 10072 KB Output is correct
27 Correct 3 ms 9820 KB Output is correct
28 Correct 2 ms 9820 KB Output is correct
29 Correct 3 ms 9820 KB Output is correct
30 Correct 23 ms 10196 KB Output is correct
31 Correct 27 ms 10076 KB Output is correct
32 Correct 35 ms 10284 KB Output is correct
33 Correct 36 ms 10284 KB Output is correct
34 Correct 34 ms 10076 KB Output is correct
35 Correct 6 ms 10076 KB Output is correct
36 Correct 6 ms 10076 KB Output is correct
37 Correct 6 ms 10072 KB Output is correct
38 Correct 35 ms 10332 KB Output is correct
39 Correct 34 ms 10328 KB Output is correct
40 Correct 34 ms 10396 KB Output is correct
41 Correct 6 ms 10328 KB Output is correct
42 Correct 6 ms 10332 KB Output is correct
43 Correct 6 ms 10332 KB Output is correct
44 Correct 36 ms 10332 KB Output is correct
45 Correct 34 ms 10076 KB Output is correct
46 Correct 35 ms 10328 KB Output is correct
47 Correct 6 ms 10328 KB Output is correct
48 Correct 6 ms 10332 KB Output is correct
49 Correct 7 ms 10344 KB Output is correct
50 Correct 32 ms 10320 KB Output is correct
51 Correct 31 ms 10076 KB Output is correct
52 Correct 32 ms 10328 KB Output is correct
53 Correct 31 ms 10300 KB Output is correct
54 Correct 37 ms 10312 KB Output is correct
55 Correct 32 ms 10072 KB Output is correct
56 Correct 18 ms 10072 KB Output is correct
57 Correct 3 ms 10076 KB Output is correct
58 Correct 3 ms 10076 KB Output is correct
59 Correct 2 ms 9820 KB Output is correct
60 Correct 3 ms 9820 KB Output is correct
61 Correct 18 ms 10076 KB Output is correct
62 Execution timed out 5045 ms 54708 KB Time limit exceeded
63 Halted 0 ms 0 KB -