Submission #850679

# Submission time Handle Problem Language Result Execution time Memory
850679 2023-09-17T09:16:56 Z huutuan Tourism (JOI23_tourism) C++14
28 / 100
5000 ms 45032 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=400, 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], cnt[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);
   set<pair<pair<int, 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.insert({{tin[x], x}, ++cnt[x]});
         return;
      }
      auto it=st.lower_bound({{tin[x], x}, cnt[x]});
      auto previt=it==st.begin()?prev(st.end()):prev(it);
      auto nextit=it==st.end()?st.begin():it;
      val-=calc(previt->first.second, nextit->first.second);
      val+=calc(previt->first.second, x);
      val+=calc(x, nextit->first.second);
      st.insert(it, {{tin[x], x}, ++cnt[x]});
   };
   function<void(int)> del=[&](int x) -> void {
      auto it=st.lower_bound({{tin[x], x}, 0});
      if (sz(st)==1){
         st.erase(it);
         return;
      }
      auto previt=it==st.begin()?prev(st.end()):prev(it);
      auto nextit=it==prev(st.end())?st.begin():next(it);
      val+=calc(previt->first.second, nextit->first.second);
      val-=calc(previt->first.second, x);
      val-=calc(x, nextit->first.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 7772 KB Output is correct
2 Correct 2 ms 7772 KB Output is correct
3 Correct 2 ms 7772 KB Output is correct
4 Correct 3 ms 7772 KB Output is correct
5 Correct 3 ms 7772 KB Output is correct
6 Correct 3 ms 7772 KB Output is correct
7 Correct 3 ms 7772 KB Output is correct
8 Correct 3 ms 7772 KB Output is correct
9 Correct 5 ms 7772 KB Output is correct
10 Correct 4 ms 7772 KB Output is correct
11 Correct 4 ms 7792 KB Output is correct
12 Correct 2 ms 8024 KB Output is correct
13 Correct 2 ms 7772 KB Output is correct
14 Correct 2 ms 7772 KB Output is correct
15 Correct 5 ms 7772 KB Output is correct
16 Correct 5 ms 7772 KB Output is correct
17 Correct 4 ms 7888 KB Output is correct
18 Correct 5 ms 7772 KB Output is correct
19 Correct 5 ms 7772 KB Output is correct
20 Correct 4 ms 7836 KB Output is correct
21 Correct 5 ms 7772 KB Output is correct
22 Correct 4 ms 7872 KB Output is correct
23 Correct 4 ms 7836 KB Output is correct
24 Correct 4 ms 7772 KB Output is correct
25 Correct 4 ms 7884 KB Output is correct
26 Correct 5 ms 7772 KB Output is correct
27 Correct 3 ms 7768 KB Output is correct
28 Correct 2 ms 7768 KB Output is correct
29 Correct 2 ms 7772 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 7772 KB Output is correct
2 Correct 2 ms 7772 KB Output is correct
3 Correct 2 ms 7772 KB Output is correct
4 Correct 3 ms 7772 KB Output is correct
5 Correct 3 ms 7772 KB Output is correct
6 Correct 3 ms 7772 KB Output is correct
7 Correct 3 ms 7772 KB Output is correct
8 Correct 3 ms 7772 KB Output is correct
9 Correct 5 ms 7772 KB Output is correct
10 Correct 4 ms 7772 KB Output is correct
11 Correct 4 ms 7792 KB Output is correct
12 Correct 2 ms 8024 KB Output is correct
13 Correct 2 ms 7772 KB Output is correct
14 Correct 2 ms 7772 KB Output is correct
15 Correct 5 ms 7772 KB Output is correct
16 Correct 5 ms 7772 KB Output is correct
17 Correct 4 ms 7888 KB Output is correct
18 Correct 5 ms 7772 KB Output is correct
19 Correct 5 ms 7772 KB Output is correct
20 Correct 4 ms 7836 KB Output is correct
21 Correct 5 ms 7772 KB Output is correct
22 Correct 4 ms 7872 KB Output is correct
23 Correct 4 ms 7836 KB Output is correct
24 Correct 4 ms 7772 KB Output is correct
25 Correct 4 ms 7884 KB Output is correct
26 Correct 5 ms 7772 KB Output is correct
27 Correct 3 ms 7768 KB Output is correct
28 Correct 2 ms 7768 KB Output is correct
29 Correct 2 ms 7772 KB Output is correct
30 Correct 27 ms 8028 KB Output is correct
31 Correct 37 ms 8024 KB Output is correct
32 Correct 42 ms 8088 KB Output is correct
33 Correct 46 ms 8028 KB Output is correct
34 Correct 38 ms 8100 KB Output is correct
35 Correct 6 ms 8028 KB Output is correct
36 Correct 5 ms 8028 KB Output is correct
37 Correct 7 ms 7912 KB Output is correct
38 Correct 39 ms 8028 KB Output is correct
39 Correct 42 ms 8384 KB Output is correct
40 Correct 43 ms 8028 KB Output is correct
41 Correct 5 ms 8028 KB Output is correct
42 Correct 6 ms 8028 KB Output is correct
43 Correct 5 ms 8028 KB Output is correct
44 Correct 38 ms 8120 KB Output is correct
45 Correct 39 ms 8028 KB Output is correct
46 Correct 40 ms 8088 KB Output is correct
47 Correct 6 ms 8028 KB Output is correct
48 Correct 5 ms 8024 KB Output is correct
49 Correct 5 ms 8028 KB Output is correct
50 Correct 37 ms 8048 KB Output is correct
51 Correct 34 ms 8024 KB Output is correct
52 Correct 36 ms 8024 KB Output is correct
53 Correct 36 ms 8280 KB Output is correct
54 Correct 38 ms 8008 KB Output is correct
55 Correct 39 ms 8092 KB Output is correct
56 Correct 21 ms 7772 KB Output is correct
57 Correct 2 ms 7772 KB Output is correct
58 Correct 3 ms 7772 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 7772 KB Output is correct
2 Correct 3 ms 7804 KB Output is correct
3 Correct 22 ms 7828 KB Output is correct
4 Execution timed out 5031 ms 34316 KB Time limit exceeded
5 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 2 ms 7768 KB Output is correct
2 Correct 50 ms 24788 KB Output is correct
3 Correct 62 ms 27040 KB Output is correct
4 Correct 71 ms 29464 KB Output is correct
5 Correct 113 ms 44908 KB Output is correct
6 Correct 133 ms 42848 KB Output is correct
7 Correct 132 ms 40020 KB Output is correct
8 Correct 119 ms 39004 KB Output is correct
9 Correct 117 ms 38840 KB Output is correct
10 Correct 112 ms 38804 KB Output is correct
11 Correct 112 ms 38552 KB Output is correct
12 Correct 111 ms 38756 KB Output is correct
13 Correct 109 ms 39004 KB Output is correct
14 Correct 104 ms 39008 KB Output is correct
15 Correct 111 ms 39764 KB Output is correct
16 Correct 122 ms 41808 KB Output is correct
17 Correct 126 ms 41812 KB Output is correct
18 Correct 123 ms 41808 KB Output is correct
19 Correct 103 ms 45032 KB Output is correct
20 Correct 123 ms 45008 KB Output is correct
21 Correct 129 ms 41056 KB Output is correct
22 Correct 133 ms 40052 KB Output is correct
23 Correct 127 ms 39264 KB Output is correct
24 Correct 108 ms 38996 KB Output is correct
25 Correct 109 ms 38740 KB Output is correct
26 Correct 115 ms 38748 KB Output is correct
27 Correct 112 ms 39020 KB Output is correct
28 Correct 105 ms 38748 KB Output is correct
29 Correct 102 ms 38600 KB Output is correct
30 Correct 102 ms 38784 KB Output is correct
31 Correct 103 ms 38992 KB Output is correct
32 Correct 95 ms 39004 KB Output is correct
33 Correct 100 ms 38996 KB Output is correct
34 Correct 98 ms 39580 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 7768 KB Output is correct
2 Correct 5 ms 7772 KB Output is correct
3 Correct 22 ms 7772 KB Output is correct
4 Execution timed out 5013 ms 31032 KB Time limit exceeded
5 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 2 ms 7772 KB Output is correct
2 Correct 2 ms 7772 KB Output is correct
3 Correct 2 ms 7772 KB Output is correct
4 Correct 3 ms 7772 KB Output is correct
5 Correct 3 ms 7772 KB Output is correct
6 Correct 3 ms 7772 KB Output is correct
7 Correct 3 ms 7772 KB Output is correct
8 Correct 3 ms 7772 KB Output is correct
9 Correct 5 ms 7772 KB Output is correct
10 Correct 4 ms 7772 KB Output is correct
11 Correct 4 ms 7792 KB Output is correct
12 Correct 2 ms 8024 KB Output is correct
13 Correct 2 ms 7772 KB Output is correct
14 Correct 2 ms 7772 KB Output is correct
15 Correct 5 ms 7772 KB Output is correct
16 Correct 5 ms 7772 KB Output is correct
17 Correct 4 ms 7888 KB Output is correct
18 Correct 5 ms 7772 KB Output is correct
19 Correct 5 ms 7772 KB Output is correct
20 Correct 4 ms 7836 KB Output is correct
21 Correct 5 ms 7772 KB Output is correct
22 Correct 4 ms 7872 KB Output is correct
23 Correct 4 ms 7836 KB Output is correct
24 Correct 4 ms 7772 KB Output is correct
25 Correct 4 ms 7884 KB Output is correct
26 Correct 5 ms 7772 KB Output is correct
27 Correct 3 ms 7768 KB Output is correct
28 Correct 2 ms 7768 KB Output is correct
29 Correct 2 ms 7772 KB Output is correct
30 Correct 27 ms 8028 KB Output is correct
31 Correct 37 ms 8024 KB Output is correct
32 Correct 42 ms 8088 KB Output is correct
33 Correct 46 ms 8028 KB Output is correct
34 Correct 38 ms 8100 KB Output is correct
35 Correct 6 ms 8028 KB Output is correct
36 Correct 5 ms 8028 KB Output is correct
37 Correct 7 ms 7912 KB Output is correct
38 Correct 39 ms 8028 KB Output is correct
39 Correct 42 ms 8384 KB Output is correct
40 Correct 43 ms 8028 KB Output is correct
41 Correct 5 ms 8028 KB Output is correct
42 Correct 6 ms 8028 KB Output is correct
43 Correct 5 ms 8028 KB Output is correct
44 Correct 38 ms 8120 KB Output is correct
45 Correct 39 ms 8028 KB Output is correct
46 Correct 40 ms 8088 KB Output is correct
47 Correct 6 ms 8028 KB Output is correct
48 Correct 5 ms 8024 KB Output is correct
49 Correct 5 ms 8028 KB Output is correct
50 Correct 37 ms 8048 KB Output is correct
51 Correct 34 ms 8024 KB Output is correct
52 Correct 36 ms 8024 KB Output is correct
53 Correct 36 ms 8280 KB Output is correct
54 Correct 38 ms 8008 KB Output is correct
55 Correct 39 ms 8092 KB Output is correct
56 Correct 21 ms 7772 KB Output is correct
57 Correct 2 ms 7772 KB Output is correct
58 Correct 3 ms 7772 KB Output is correct
59 Correct 1 ms 7772 KB Output is correct
60 Correct 3 ms 7804 KB Output is correct
61 Correct 22 ms 7828 KB Output is correct
62 Execution timed out 5031 ms 34316 KB Time limit exceeded
63 Halted 0 ms 0 KB -