답안 #727531

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
727531 2023-04-21T00:03:54 Z cig32 Tourism (JOI23_tourism) C++17
100 / 100
1518 ms 81128 KB
#include "bits/stdc++.h"
#define int long long
using namespace std;
int n, m, q;
const int MAXN = 1e5+10;
vector<int> e;
vector<int> adj[MAXN];
int dep[MAXN];
int par[MAXN];
int tin[MAXN], cur;
vector<int> ord;
void dfs(int node, int prv) {
  e.push_back(node);
  ord.push_back(node);
  par[node] = prv;
  tin[node] = ++cur;
  dep[node] = (prv == -1 ? 0 : dep[prv] + 1);
  for(int x: adj[node]) {
    if(x != prv) {
      dfs(x, node);
      e.push_back(node);
    }
  }
}
int l[MAXN], r[MAXN];
pair<int,int> st[18][2 * MAXN];
int lca(int x, int y) {
  int m1 = min(l[x], l[y]), m2 = max(r[x], r[y]);
  int k = 32 - __builtin_clz(m2-m1+1) - 1;
  return min(st[k][m1], st[k][m2-(1<<k)+1]).second;
}
int dist(int x, int y) {
  return dep[x]+dep[y]-2*dep[lca(x,y)];
}
int c[MAXN], ans[MAXN];
bool radewoosh(pair<pair<int,int>,int>x,pair<pair<int,int>,int>y) {
  return x.first.second<y.first.second;
}
int dage[MAXN];
int pp[MAXN];
int bit[MAXN];
void add(int x, int v) {
  x++;
  for(;x<MAXN;x+=x&-x) bit[x] += v;
}
int sum(int x) {
  x++;
  int s=0;
  for(;x;x-=x&-x) s += bit[x];
  return s;
}
set<pair<pair<int,int>,int> >ss;
void update_range(int l, int r, int w) { // called n log n times, use at most log n
  auto it = ss.lower_bound({{l, 0}, 0});
  vector<pair<pair<int,int>,int> > rem, ins;
  ins.push_back({{l, r}, w});
  while(it != ss.end() && (*it).first.first <= r) {
    int ll = (*it).first.first, rr = (*it).first.second;
    int ww = (*it).second;
    if(rr <= r) {
      add(ww, -(rr-ll+1));
      rem.push_back(*it);
    }
    else {
      add(ww, -(rr-ll+1));
      rem.push_back(*it);
      ins.push_back({{r+1, rr}, ww});
    }
    it++;
  }
  it = ss.lower_bound({{l, 0}, 0});
  if(it != ss.begin()) {
    it--;
    int ll = (*it).first.first, rr = (*it).first.second, ww = (*it).second;
    if(rr < l) {
      // do nothing
    }
    else if(r < rr) {
      add(ww, -(rr-ll+1));
      rem.push_back(*it);
      ins.push_back({{ll, l-1}, ww});
      ins.push_back({{r+1, rr}, ww});
    }
    else {
      add(ww, -(rr-ll+1));
      rem.push_back(*it);
      ins.push_back({{ll, l-1}, ww});
    }
  }
  for(pair<pair<int,int>,int> pp : rem) {
    ss.erase(pp);
  }
  for(pair<pair<int,int>,int> pp : ins) {
    ss.insert(pp);
    add(pp.second, pp.first.second - pp.first.first + 1);
  }
}
int count_atleast(int x) { // number of >= x
  return sum(MAXN-2) - sum(x-1);
}
void update_path(int u, int v, int w) {
  int lc = lca(u, v);
  int fin = v;
  while(1) {
    int p1 = pp[u];
    int tar = dage[p1];
    if(dep[ord[tar]] < dep[lc]) tar = pp[lc];
    // [tar, p1]
    update_range(tar, p1, w-1);
    if(tar == pp[lc]) break;
    u = par[ord[tar]];
  }
  while(1) {
    int p1 = pp[v];
    int tar = dage[p1];
    if(dep[ord[tar]] < dep[lc]) tar = pp[lc];
    // [tar, p1]
    update_range(tar, p1, w-1);
    if(tar == pp[lc]) break;
    v = par[ord[tar]];
  }
  update_range(pp[fin], pp[fin], w);
}
int sz[MAXN];
void dfs0(int node, int prv) {
  sz[node] = 1;
  for(int x: adj[node]) {
    if(x != prv) {
      dfs0(x, node);
      sz[node] += sz[x];
    }
  }
}
bool hld(int x, int y) {
  return sz[x] > sz[y];
}
int32_t main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cin >> n >> m >> q;
  ss.insert({{1, n}, 0});
  add(0, n);
  for(int i=1; i<n; i++) {
    int a, b;
    cin >> a >> b;
    adj[a].push_back(b);
    adj[b].push_back(a);
  }
  ord.push_back(0);
  dfs0(1, -1);
  for(int i=1; i<=n; i++) sort(adj[i].begin(), adj[i].end(), hld);
  dfs(1, -1);
  for(int i=1; i<=n; i++) {
    if(i == 1 || dep[ord[i]] <= dep[ord[i-1]]) dage[i] = i;
    else dage[i] = dage[i-1];
    pp[ord[i]] = i;
  }
  for(int i=0; i<e.size(); i++) r[e[i]] = i;
  for(int i=e.size()-1; i>=0; i--) l[e[i]] = i;
  for(int i=0; i<e.size(); i++) st[0][i] = {dep[e[i]], e[i]};
  for(int i=1; i<18; i++) {
    for(int j=0; j+(1<<i)-1<e.size(); j++) {
      st[i][j] = min(st[i-1][j], st[i-1][j+(1<<(i-1))]);
    }
  }
  for(int i=1; i<=m; i++) cin >> c[i];
  pair<pair<int,int>,int> qry[q+1];
  for(int i=1; i<=q; i++) {
    cin >> qry[i].first.first >> qry[i].first.second;
    qry[i].second = i;
  }
  sort(qry+1, qry+1+q, radewoosh);
  int rr=0;
  c[0] = c[1];
  for(int i=1; i<=q; i++) {
    while(rr < qry[i].first.second) {
      rr++;
      update_path(c[rr-1], c[rr], rr);
    }
    ans[qry[i].second] = count_atleast(qry[i].first.first);
  }
  for(int i=1; i<=q; i++) {
    cout << ans[i] << "\n";
  }
}
// 2 1 0 0 3 2 0 0
/*
5 5 15
1 2 2 3 3 4 4 5
2 4 5 1 3
1 1 1 2 1 3 1 4 1 5 2 2 2 3 2 4 2 5 3 3 3 4 3 5 4 4 4 5 5 5

g++ C.cpp -std=c++17 -o C
g++ Csmall.cpp -std=c++17 -o Csmall
g++ gen.cpp -std=c++17 -o gen
g++ checker.cpp -std=c++17 -o checker

for((i=1; ; ++i)); do
  ./gen $i > input_file
  ./C < input_file > myAnswer
  ./Csmall < input_file > correctAnswer
  ./checker > checker_log
  echo "Passed test: " $i
done
*/

Compilation message

tourism.cpp: In function 'int32_t main()':
tourism.cpp:158:17: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  158 |   for(int i=0; i<e.size(); i++) r[e[i]] = i;
      |                ~^~~~~~~~~
tourism.cpp:160:17: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  160 |   for(int i=0; i<e.size(); i++) st[0][i] = {dep[e[i]], e[i]};
      |                ~^~~~~~~~~
tourism.cpp:162:28: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  162 |     for(int j=0; j+(1<<i)-1<e.size(); j++) {
      |                  ~~~~~~~~~~^~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 2772 KB Output is correct
2 Correct 2 ms 2772 KB Output is correct
3 Correct 2 ms 2772 KB Output is correct
4 Correct 2 ms 2900 KB Output is correct
5 Correct 2 ms 2900 KB Output is correct
6 Correct 4 ms 2900 KB Output is correct
7 Correct 4 ms 2920 KB Output is correct
8 Correct 3 ms 2900 KB Output is correct
9 Correct 3 ms 2900 KB Output is correct
10 Correct 3 ms 2900 KB Output is correct
11 Correct 4 ms 2900 KB Output is correct
12 Correct 5 ms 2900 KB Output is correct
13 Correct 3 ms 2900 KB Output is correct
14 Correct 3 ms 2900 KB Output is correct
15 Correct 3 ms 2868 KB Output is correct
16 Correct 2 ms 2900 KB Output is correct
17 Correct 4 ms 2900 KB Output is correct
18 Correct 2 ms 2900 KB Output is correct
19 Correct 3 ms 2900 KB Output is correct
20 Correct 3 ms 2900 KB Output is correct
21 Correct 3 ms 2900 KB Output is correct
22 Correct 4 ms 2900 KB Output is correct
23 Correct 3 ms 2900 KB Output is correct
24 Correct 3 ms 2900 KB Output is correct
25 Correct 4 ms 2900 KB Output is correct
26 Correct 3 ms 2900 KB Output is correct
27 Correct 2 ms 2772 KB Output is correct
28 Correct 2 ms 2900 KB Output is correct
29 Correct 3 ms 2900 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 2772 KB Output is correct
2 Correct 2 ms 2772 KB Output is correct
3 Correct 2 ms 2772 KB Output is correct
4 Correct 2 ms 2900 KB Output is correct
5 Correct 2 ms 2900 KB Output is correct
6 Correct 4 ms 2900 KB Output is correct
7 Correct 4 ms 2920 KB Output is correct
8 Correct 3 ms 2900 KB Output is correct
9 Correct 3 ms 2900 KB Output is correct
10 Correct 3 ms 2900 KB Output is correct
11 Correct 4 ms 2900 KB Output is correct
12 Correct 5 ms 2900 KB Output is correct
13 Correct 3 ms 2900 KB Output is correct
14 Correct 3 ms 2900 KB Output is correct
15 Correct 3 ms 2868 KB Output is correct
16 Correct 2 ms 2900 KB Output is correct
17 Correct 4 ms 2900 KB Output is correct
18 Correct 2 ms 2900 KB Output is correct
19 Correct 3 ms 2900 KB Output is correct
20 Correct 3 ms 2900 KB Output is correct
21 Correct 3 ms 2900 KB Output is correct
22 Correct 4 ms 2900 KB Output is correct
23 Correct 3 ms 2900 KB Output is correct
24 Correct 3 ms 2900 KB Output is correct
25 Correct 4 ms 2900 KB Output is correct
26 Correct 3 ms 2900 KB Output is correct
27 Correct 2 ms 2772 KB Output is correct
28 Correct 2 ms 2900 KB Output is correct
29 Correct 3 ms 2900 KB Output is correct
30 Correct 11 ms 3668 KB Output is correct
31 Correct 11 ms 3668 KB Output is correct
32 Correct 13 ms 3928 KB Output is correct
33 Correct 12 ms 3924 KB Output is correct
34 Correct 14 ms 3924 KB Output is correct
35 Correct 13 ms 3924 KB Output is correct
36 Correct 13 ms 3924 KB Output is correct
37 Correct 14 ms 3984 KB Output is correct
38 Correct 7 ms 3924 KB Output is correct
39 Correct 7 ms 3920 KB Output is correct
40 Correct 7 ms 3924 KB Output is correct
41 Correct 9 ms 3972 KB Output is correct
42 Correct 7 ms 3924 KB Output is correct
43 Correct 9 ms 3968 KB Output is correct
44 Correct 12 ms 3920 KB Output is correct
45 Correct 10 ms 3924 KB Output is correct
46 Correct 9 ms 3924 KB Output is correct
47 Correct 13 ms 3924 KB Output is correct
48 Correct 9 ms 3924 KB Output is correct
49 Correct 10 ms 3924 KB Output is correct
50 Correct 8 ms 3924 KB Output is correct
51 Correct 8 ms 3924 KB Output is correct
52 Correct 8 ms 3924 KB Output is correct
53 Correct 13 ms 3924 KB Output is correct
54 Correct 8 ms 3924 KB Output is correct
55 Correct 9 ms 3932 KB Output is correct
56 Correct 3 ms 2772 KB Output is correct
57 Correct 5 ms 3796 KB Output is correct
58 Correct 12 ms 3924 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 2772 KB Output is correct
2 Correct 2 ms 2772 KB Output is correct
3 Correct 4 ms 2772 KB Output is correct
4 Correct 230 ms 43384 KB Output is correct
5 Correct 163 ms 62772 KB Output is correct
6 Correct 217 ms 70316 KB Output is correct
7 Correct 264 ms 78816 KB Output is correct
8 Correct 287 ms 78388 KB Output is correct
9 Correct 275 ms 78948 KB Output is correct
10 Correct 266 ms 78992 KB Output is correct
11 Correct 293 ms 78732 KB Output is correct
12 Correct 300 ms 77496 KB Output is correct
13 Correct 286 ms 79100 KB Output is correct
14 Correct 305 ms 79196 KB Output is correct
15 Correct 85 ms 75608 KB Output is correct
16 Correct 259 ms 78636 KB Output is correct
17 Correct 83 ms 7880 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2772 KB Output is correct
2 Correct 492 ms 40824 KB Output is correct
3 Correct 749 ms 44064 KB Output is correct
4 Correct 595 ms 49348 KB Output is correct
5 Correct 979 ms 74356 KB Output is correct
6 Correct 1029 ms 73612 KB Output is correct
7 Correct 1002 ms 74120 KB Output is correct
8 Correct 980 ms 73740 KB Output is correct
9 Correct 946 ms 73484 KB Output is correct
10 Correct 945 ms 74120 KB Output is correct
11 Correct 972 ms 74304 KB Output is correct
12 Correct 992 ms 74048 KB Output is correct
13 Correct 1022 ms 75436 KB Output is correct
14 Correct 1052 ms 74836 KB Output is correct
15 Correct 1155 ms 77724 KB Output is correct
16 Correct 1088 ms 73444 KB Output is correct
17 Correct 1213 ms 73564 KB Output is correct
18 Correct 1029 ms 73628 KB Output is correct
19 Correct 715 ms 69824 KB Output is correct
20 Correct 688 ms 68908 KB Output is correct
21 Correct 692 ms 68856 KB Output is correct
22 Correct 694 ms 69804 KB Output is correct
23 Correct 721 ms 70380 KB Output is correct
24 Correct 714 ms 68672 KB Output is correct
25 Correct 647 ms 68920 KB Output is correct
26 Correct 727 ms 69040 KB Output is correct
27 Correct 675 ms 69632 KB Output is correct
28 Correct 739 ms 70380 KB Output is correct
29 Correct 759 ms 69040 KB Output is correct
30 Correct 674 ms 69428 KB Output is correct
31 Correct 702 ms 70124 KB Output is correct
32 Correct 698 ms 69868 KB Output is correct
33 Correct 715 ms 71368 KB Output is correct
34 Correct 724 ms 72348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2772 KB Output is correct
2 Correct 2 ms 2772 KB Output is correct
3 Correct 5 ms 2844 KB Output is correct
4 Correct 1239 ms 47032 KB Output is correct
5 Correct 1171 ms 48052 KB Output is correct
6 Correct 1404 ms 68364 KB Output is correct
7 Correct 1453 ms 80148 KB Output is correct
8 Correct 1518 ms 79756 KB Output is correct
9 Correct 1454 ms 79528 KB Output is correct
10 Correct 1472 ms 80328 KB Output is correct
11 Correct 1459 ms 80316 KB Output is correct
12 Correct 1475 ms 79720 KB Output is correct
13 Correct 1501 ms 80024 KB Output is correct
14 Correct 90 ms 8744 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 2772 KB Output is correct
2 Correct 2 ms 2772 KB Output is correct
3 Correct 2 ms 2772 KB Output is correct
4 Correct 2 ms 2900 KB Output is correct
5 Correct 2 ms 2900 KB Output is correct
6 Correct 4 ms 2900 KB Output is correct
7 Correct 4 ms 2920 KB Output is correct
8 Correct 3 ms 2900 KB Output is correct
9 Correct 3 ms 2900 KB Output is correct
10 Correct 3 ms 2900 KB Output is correct
11 Correct 4 ms 2900 KB Output is correct
12 Correct 5 ms 2900 KB Output is correct
13 Correct 3 ms 2900 KB Output is correct
14 Correct 3 ms 2900 KB Output is correct
15 Correct 3 ms 2868 KB Output is correct
16 Correct 2 ms 2900 KB Output is correct
17 Correct 4 ms 2900 KB Output is correct
18 Correct 2 ms 2900 KB Output is correct
19 Correct 3 ms 2900 KB Output is correct
20 Correct 3 ms 2900 KB Output is correct
21 Correct 3 ms 2900 KB Output is correct
22 Correct 4 ms 2900 KB Output is correct
23 Correct 3 ms 2900 KB Output is correct
24 Correct 3 ms 2900 KB Output is correct
25 Correct 4 ms 2900 KB Output is correct
26 Correct 3 ms 2900 KB Output is correct
27 Correct 2 ms 2772 KB Output is correct
28 Correct 2 ms 2900 KB Output is correct
29 Correct 3 ms 2900 KB Output is correct
30 Correct 11 ms 3668 KB Output is correct
31 Correct 11 ms 3668 KB Output is correct
32 Correct 13 ms 3928 KB Output is correct
33 Correct 12 ms 3924 KB Output is correct
34 Correct 14 ms 3924 KB Output is correct
35 Correct 13 ms 3924 KB Output is correct
36 Correct 13 ms 3924 KB Output is correct
37 Correct 14 ms 3984 KB Output is correct
38 Correct 7 ms 3924 KB Output is correct
39 Correct 7 ms 3920 KB Output is correct
40 Correct 7 ms 3924 KB Output is correct
41 Correct 9 ms 3972 KB Output is correct
42 Correct 7 ms 3924 KB Output is correct
43 Correct 9 ms 3968 KB Output is correct
44 Correct 12 ms 3920 KB Output is correct
45 Correct 10 ms 3924 KB Output is correct
46 Correct 9 ms 3924 KB Output is correct
47 Correct 13 ms 3924 KB Output is correct
48 Correct 9 ms 3924 KB Output is correct
49 Correct 10 ms 3924 KB Output is correct
50 Correct 8 ms 3924 KB Output is correct
51 Correct 8 ms 3924 KB Output is correct
52 Correct 8 ms 3924 KB Output is correct
53 Correct 13 ms 3924 KB Output is correct
54 Correct 8 ms 3924 KB Output is correct
55 Correct 9 ms 3932 KB Output is correct
56 Correct 3 ms 2772 KB Output is correct
57 Correct 5 ms 3796 KB Output is correct
58 Correct 12 ms 3924 KB Output is correct
59 Correct 2 ms 2772 KB Output is correct
60 Correct 2 ms 2772 KB Output is correct
61 Correct 4 ms 2772 KB Output is correct
62 Correct 230 ms 43384 KB Output is correct
63 Correct 163 ms 62772 KB Output is correct
64 Correct 217 ms 70316 KB Output is correct
65 Correct 264 ms 78816 KB Output is correct
66 Correct 287 ms 78388 KB Output is correct
67 Correct 275 ms 78948 KB Output is correct
68 Correct 266 ms 78992 KB Output is correct
69 Correct 293 ms 78732 KB Output is correct
70 Correct 300 ms 77496 KB Output is correct
71 Correct 286 ms 79100 KB Output is correct
72 Correct 305 ms 79196 KB Output is correct
73 Correct 85 ms 75608 KB Output is correct
74 Correct 259 ms 78636 KB Output is correct
75 Correct 83 ms 7880 KB Output is correct
76 Correct 1 ms 2772 KB Output is correct
77 Correct 492 ms 40824 KB Output is correct
78 Correct 749 ms 44064 KB Output is correct
79 Correct 595 ms 49348 KB Output is correct
80 Correct 979 ms 74356 KB Output is correct
81 Correct 1029 ms 73612 KB Output is correct
82 Correct 1002 ms 74120 KB Output is correct
83 Correct 980 ms 73740 KB Output is correct
84 Correct 946 ms 73484 KB Output is correct
85 Correct 945 ms 74120 KB Output is correct
86 Correct 972 ms 74304 KB Output is correct
87 Correct 992 ms 74048 KB Output is correct
88 Correct 1022 ms 75436 KB Output is correct
89 Correct 1052 ms 74836 KB Output is correct
90 Correct 1155 ms 77724 KB Output is correct
91 Correct 1088 ms 73444 KB Output is correct
92 Correct 1213 ms 73564 KB Output is correct
93 Correct 1029 ms 73628 KB Output is correct
94 Correct 715 ms 69824 KB Output is correct
95 Correct 688 ms 68908 KB Output is correct
96 Correct 692 ms 68856 KB Output is correct
97 Correct 694 ms 69804 KB Output is correct
98 Correct 721 ms 70380 KB Output is correct
99 Correct 714 ms 68672 KB Output is correct
100 Correct 647 ms 68920 KB Output is correct
101 Correct 727 ms 69040 KB Output is correct
102 Correct 675 ms 69632 KB Output is correct
103 Correct 739 ms 70380 KB Output is correct
104 Correct 759 ms 69040 KB Output is correct
105 Correct 674 ms 69428 KB Output is correct
106 Correct 702 ms 70124 KB Output is correct
107 Correct 698 ms 69868 KB Output is correct
108 Correct 715 ms 71368 KB Output is correct
109 Correct 724 ms 72348 KB Output is correct
110 Correct 1 ms 2772 KB Output is correct
111 Correct 2 ms 2772 KB Output is correct
112 Correct 5 ms 2844 KB Output is correct
113 Correct 1239 ms 47032 KB Output is correct
114 Correct 1171 ms 48052 KB Output is correct
115 Correct 1404 ms 68364 KB Output is correct
116 Correct 1453 ms 80148 KB Output is correct
117 Correct 1518 ms 79756 KB Output is correct
118 Correct 1454 ms 79528 KB Output is correct
119 Correct 1472 ms 80328 KB Output is correct
120 Correct 1459 ms 80316 KB Output is correct
121 Correct 1475 ms 79720 KB Output is correct
122 Correct 1501 ms 80024 KB Output is correct
123 Correct 90 ms 8744 KB Output is correct
124 Correct 886 ms 79024 KB Output is correct
125 Correct 765 ms 74516 KB Output is correct
126 Correct 1092 ms 79652 KB Output is correct
127 Correct 1113 ms 79748 KB Output is correct
128 Correct 1064 ms 79284 KB Output is correct
129 Correct 1069 ms 79588 KB Output is correct
130 Correct 1037 ms 79840 KB Output is correct
131 Correct 295 ms 78160 KB Output is correct
132 Correct 291 ms 78908 KB Output is correct
133 Correct 292 ms 75824 KB Output is correct
134 Correct 650 ms 75200 KB Output is correct
135 Correct 706 ms 75000 KB Output is correct
136 Correct 687 ms 75168 KB Output is correct
137 Correct 479 ms 81092 KB Output is correct
138 Correct 519 ms 81044 KB Output is correct
139 Correct 486 ms 81128 KB Output is correct
140 Correct 464 ms 80988 KB Output is correct
141 Correct 686 ms 81040 KB Output is correct
142 Correct 565 ms 81028 KB Output is correct
143 Correct 137 ms 72448 KB Output is correct
144 Correct 1038 ms 79252 KB Output is correct