답안 #537420

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
537420 2022-03-15T05:27:52 Z cig32 Birthday gift (IZhO18_treearray) C++17
100 / 100
927 ms 210600 KB
#include "bits/stdc++.h"
using namespace std;
const int MAXN = 1e6 + 10;
const int MOD = 1e9 + 7;
#define int long long
 
mt19937_64 rng((int)std::chrono::steady_clock::now().time_since_epoch().count());
int rnd(int x, int y) {
  int u = uniform_int_distribution<int>(x, y)(rng); return u;
}
long long bm(long long b, long long p) { 
  if(p==0) return 1;
  long long r = bm(b, p/2);
  if(p&1) return (((r*r) % MOD) * b) % MOD;
  return (r*r) % MOD;
}
vector<int> adj[MAXN];

pair<int, int> st[19][MAXN];

int l[MAXN], r[MAXN];
vector<int> e;

int dep[MAXN];
void dfs(int node, int prv) {
  e.push_back(node);
  dep[node] = (prv == -1 ? 0 : dep[prv] + 1);
  for(int x: adj[node]) {
    if(x != prv) {
      dfs(x, node);
      e.push_back(node);
    }
  }

}

int lca_idx(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;
}
void solve(int tc) {
  int n, m, q;
  cin >> n >> m >> q;
  for(int i=1; i<n; i++) {
    int u, v;
    cin >> u >> v;
    adj[u].push_back(v);
    adj[v].push_back(u);
  }
  dfs(1, -1);
  for(int i=0; i<e.size(); i++) st[0][i] = {dep[e[i]], e[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=1; i<19; i++) {
    for(int j=0; j<e.size(); j++) {
      if(j + (1<<i) - 1 < e.size()) {
        st[i][j] = min(st[i-1][j],st[i-1][j+(1<<(i-1))]);
      }
    }
  }
  int a[m+1];
  for(int i=1; i<=m; i++) cin >> a[i];
  set<int> s[n+1];
  for(int i=1; i<=m; i++) s[a[i]].insert(i);
  for(int i=1; i<=n; i++) s[i].insert(1e7);

  set<int> t[n+1];
  for(int i=1; i<m; i++) t[lca_idx(a[i], a[i+1])].insert(i);
  for(int i=1; i<=n; i++) t[i].insert(1e7);
  for(int i=1; i<=q; i++) {
    int trrr;
    cin >> trrr;
    if(trrr == 1) {
      int pos, v;
      cin >> pos >> v;
      s[a[pos]].erase(pos);
      if(pos > 1) t[lca_idx(a[pos-1], a[pos])].erase(pos-1);
      if(pos < m) t[lca_idx(a[pos], a[pos+1])].erase(pos);
      a[pos] = v;
      s[a[pos]].insert(pos);
      if(pos > 1) t[lca_idx(a[pos-1], a[pos])].insert(pos-1);
      if(pos < m) t[lca_idx(a[pos], a[pos+1])].insert(pos);
    }
    else {
      int l, r, v;
      cin >> l >> r >> v;
      int x = -1, y = -1;
      auto it = s[v].lower_bound(l);
      if((*it) <= r) x = (*it), y = (*it);
      it = t[v].lower_bound(l);
      if((*it) < r) x = (*it), y = (*it) + 1;
      cout << x << ' ' << y << "\n";
    }
  }
}

int32_t main(){
  ios::sync_with_stdio(0); cin.tie(0);
  int t = 1; //cin >> t;
  for(int i=1; i<=t; i++) solve(i);
}
/*
5 4 4
1 2
3 1
3 4
5 3
4 5 2 3
2 1 3 1
1 3 5
2 3 4 5
2 1 3 1
*/

Compilation message

treearray.cpp: In function 'void solve(long long int)':
treearray.cpp:52: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]
   52 |   for(int i=0; i<e.size(); i++) st[0][i] = {dep[e[i]], e[i]};
      |                ~^~~~~~~~~
treearray.cpp:53: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]
   53 |   for(int i=0; i<e.size(); i++) r[e[i]] = i;
      |                ~^~~~~~~~~
treearray.cpp:57:19: 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]
   57 |     for(int j=0; j<e.size(); j++) {
      |                  ~^~~~~~~~~
treearray.cpp:58:25: 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]
   58 |       if(j + (1<<i) - 1 < e.size()) {
      |          ~~~~~~~~~~~~~~~^~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 12 ms 23796 KB n=5
2 Correct 12 ms 23892 KB n=100
3 Correct 14 ms 23892 KB n=100
4 Correct 13 ms 23948 KB n=100
5 Correct 13 ms 23892 KB n=100
6 Correct 14 ms 23892 KB n=100
7 Correct 13 ms 23892 KB n=100
8 Correct 14 ms 23940 KB n=100
9 Correct 12 ms 23892 KB n=100
10 Correct 13 ms 23892 KB n=100
11 Correct 13 ms 23920 KB n=100
12 Correct 12 ms 23908 KB n=100
13 Correct 15 ms 23892 KB n=100
14 Correct 12 ms 23852 KB n=100
15 Correct 14 ms 23892 KB n=100
16 Correct 14 ms 23944 KB n=100
17 Correct 14 ms 23840 KB n=100
18 Correct 13 ms 23892 KB n=100
19 Correct 13 ms 23828 KB n=100
20 Correct 13 ms 23832 KB n=100
21 Correct 16 ms 23892 KB n=100
22 Correct 13 ms 23912 KB n=100
23 Correct 16 ms 23872 KB n=100
24 Correct 13 ms 23892 KB n=100
25 Correct 13 ms 23848 KB n=100
26 Correct 12 ms 23764 KB n=12
27 Correct 12 ms 23952 KB n=100
# 결과 실행 시간 메모리 Grader output
1 Correct 12 ms 23796 KB n=5
2 Correct 12 ms 23892 KB n=100
3 Correct 14 ms 23892 KB n=100
4 Correct 13 ms 23948 KB n=100
5 Correct 13 ms 23892 KB n=100
6 Correct 14 ms 23892 KB n=100
7 Correct 13 ms 23892 KB n=100
8 Correct 14 ms 23940 KB n=100
9 Correct 12 ms 23892 KB n=100
10 Correct 13 ms 23892 KB n=100
11 Correct 13 ms 23920 KB n=100
12 Correct 12 ms 23908 KB n=100
13 Correct 15 ms 23892 KB n=100
14 Correct 12 ms 23852 KB n=100
15 Correct 14 ms 23892 KB n=100
16 Correct 14 ms 23944 KB n=100
17 Correct 14 ms 23840 KB n=100
18 Correct 13 ms 23892 KB n=100
19 Correct 13 ms 23828 KB n=100
20 Correct 13 ms 23832 KB n=100
21 Correct 16 ms 23892 KB n=100
22 Correct 13 ms 23912 KB n=100
23 Correct 16 ms 23872 KB n=100
24 Correct 13 ms 23892 KB n=100
25 Correct 13 ms 23848 KB n=100
26 Correct 12 ms 23764 KB n=12
27 Correct 12 ms 23952 KB n=100
28 Correct 15 ms 24148 KB n=500
29 Correct 14 ms 24148 KB n=500
30 Correct 15 ms 24124 KB n=500
31 Correct 14 ms 24148 KB n=500
32 Correct 15 ms 24148 KB n=500
33 Correct 14 ms 24148 KB n=500
34 Correct 13 ms 24148 KB n=500
35 Correct 13 ms 24148 KB n=500
36 Correct 13 ms 24148 KB n=500
37 Correct 13 ms 24148 KB n=500
38 Correct 14 ms 24148 KB n=500
39 Correct 14 ms 24216 KB n=500
40 Correct 13 ms 24148 KB n=500
41 Correct 14 ms 24148 KB n=500
42 Correct 13 ms 24148 KB n=500
43 Correct 17 ms 24148 KB n=500
44 Correct 13 ms 24148 KB n=500
45 Correct 14 ms 24232 KB n=500
46 Correct 15 ms 24148 KB n=500
47 Correct 16 ms 24168 KB n=500
48 Correct 14 ms 24148 KB n=500
49 Correct 16 ms 24128 KB n=500
50 Correct 13 ms 24148 KB n=500
51 Correct 13 ms 24148 KB n=500
52 Correct 13 ms 24148 KB n=500
53 Correct 13 ms 24148 KB n=500
54 Correct 13 ms 24220 KB n=500
55 Correct 14 ms 24104 KB n=278
56 Correct 13 ms 24148 KB n=500
57 Correct 13 ms 24148 KB n=500
58 Correct 16 ms 24148 KB n=500
# 결과 실행 시간 메모리 Grader output
1 Correct 12 ms 23796 KB n=5
2 Correct 12 ms 23892 KB n=100
3 Correct 14 ms 23892 KB n=100
4 Correct 13 ms 23948 KB n=100
5 Correct 13 ms 23892 KB n=100
6 Correct 14 ms 23892 KB n=100
7 Correct 13 ms 23892 KB n=100
8 Correct 14 ms 23940 KB n=100
9 Correct 12 ms 23892 KB n=100
10 Correct 13 ms 23892 KB n=100
11 Correct 13 ms 23920 KB n=100
12 Correct 12 ms 23908 KB n=100
13 Correct 15 ms 23892 KB n=100
14 Correct 12 ms 23852 KB n=100
15 Correct 14 ms 23892 KB n=100
16 Correct 14 ms 23944 KB n=100
17 Correct 14 ms 23840 KB n=100
18 Correct 13 ms 23892 KB n=100
19 Correct 13 ms 23828 KB n=100
20 Correct 13 ms 23832 KB n=100
21 Correct 16 ms 23892 KB n=100
22 Correct 13 ms 23912 KB n=100
23 Correct 16 ms 23872 KB n=100
24 Correct 13 ms 23892 KB n=100
25 Correct 13 ms 23848 KB n=100
26 Correct 12 ms 23764 KB n=12
27 Correct 12 ms 23952 KB n=100
28 Correct 15 ms 24148 KB n=500
29 Correct 14 ms 24148 KB n=500
30 Correct 15 ms 24124 KB n=500
31 Correct 14 ms 24148 KB n=500
32 Correct 15 ms 24148 KB n=500
33 Correct 14 ms 24148 KB n=500
34 Correct 13 ms 24148 KB n=500
35 Correct 13 ms 24148 KB n=500
36 Correct 13 ms 24148 KB n=500
37 Correct 13 ms 24148 KB n=500
38 Correct 14 ms 24148 KB n=500
39 Correct 14 ms 24216 KB n=500
40 Correct 13 ms 24148 KB n=500
41 Correct 14 ms 24148 KB n=500
42 Correct 13 ms 24148 KB n=500
43 Correct 17 ms 24148 KB n=500
44 Correct 13 ms 24148 KB n=500
45 Correct 14 ms 24232 KB n=500
46 Correct 15 ms 24148 KB n=500
47 Correct 16 ms 24168 KB n=500
48 Correct 14 ms 24148 KB n=500
49 Correct 16 ms 24128 KB n=500
50 Correct 13 ms 24148 KB n=500
51 Correct 13 ms 24148 KB n=500
52 Correct 13 ms 24148 KB n=500
53 Correct 13 ms 24148 KB n=500
54 Correct 13 ms 24220 KB n=500
55 Correct 14 ms 24104 KB n=278
56 Correct 13 ms 24148 KB n=500
57 Correct 13 ms 24148 KB n=500
58 Correct 16 ms 24148 KB n=500
59 Correct 20 ms 25300 KB n=2000
60 Correct 17 ms 25228 KB n=2000
61 Correct 19 ms 25300 KB n=2000
62 Correct 18 ms 25300 KB n=2000
63 Correct 17 ms 25300 KB n=2000
64 Correct 17 ms 25332 KB n=2000
65 Correct 18 ms 25300 KB n=2000
66 Correct 21 ms 25288 KB n=2000
67 Correct 17 ms 25300 KB n=2000
68 Correct 16 ms 25300 KB n=2000
69 Correct 15 ms 25300 KB n=2000
70 Correct 16 ms 25300 KB n=2000
71 Correct 16 ms 25368 KB n=2000
72 Correct 17 ms 25360 KB n=2000
73 Correct 17 ms 25300 KB n=2000
74 Correct 16 ms 25260 KB n=1844
75 Correct 16 ms 25348 KB n=2000
76 Correct 16 ms 25296 KB n=2000
77 Correct 15 ms 25300 KB n=2000
78 Correct 17 ms 25296 KB n=2000
79 Correct 17 ms 25260 KB n=2000
80 Correct 20 ms 25300 KB n=2000
81 Correct 16 ms 25300 KB n=2000
82 Correct 15 ms 25300 KB n=2000
83 Correct 17 ms 25428 KB n=2000
84 Correct 16 ms 25308 KB n=2000
85 Correct 17 ms 25300 KB n=2000
86 Correct 17 ms 25300 KB n=2000
87 Correct 20 ms 25300 KB n=2000
88 Correct 16 ms 25308 KB n=2000
89 Correct 16 ms 25300 KB n=2000
90 Correct 16 ms 25220 KB n=2000
91 Correct 17 ms 25284 KB n=2000
# 결과 실행 시간 메모리 Grader output
1 Correct 12 ms 23796 KB n=5
2 Correct 12 ms 23892 KB n=100
3 Correct 14 ms 23892 KB n=100
4 Correct 13 ms 23948 KB n=100
5 Correct 13 ms 23892 KB n=100
6 Correct 14 ms 23892 KB n=100
7 Correct 13 ms 23892 KB n=100
8 Correct 14 ms 23940 KB n=100
9 Correct 12 ms 23892 KB n=100
10 Correct 13 ms 23892 KB n=100
11 Correct 13 ms 23920 KB n=100
12 Correct 12 ms 23908 KB n=100
13 Correct 15 ms 23892 KB n=100
14 Correct 12 ms 23852 KB n=100
15 Correct 14 ms 23892 KB n=100
16 Correct 14 ms 23944 KB n=100
17 Correct 14 ms 23840 KB n=100
18 Correct 13 ms 23892 KB n=100
19 Correct 13 ms 23828 KB n=100
20 Correct 13 ms 23832 KB n=100
21 Correct 16 ms 23892 KB n=100
22 Correct 13 ms 23912 KB n=100
23 Correct 16 ms 23872 KB n=100
24 Correct 13 ms 23892 KB n=100
25 Correct 13 ms 23848 KB n=100
26 Correct 12 ms 23764 KB n=12
27 Correct 12 ms 23952 KB n=100
28 Correct 15 ms 24148 KB n=500
29 Correct 14 ms 24148 KB n=500
30 Correct 15 ms 24124 KB n=500
31 Correct 14 ms 24148 KB n=500
32 Correct 15 ms 24148 KB n=500
33 Correct 14 ms 24148 KB n=500
34 Correct 13 ms 24148 KB n=500
35 Correct 13 ms 24148 KB n=500
36 Correct 13 ms 24148 KB n=500
37 Correct 13 ms 24148 KB n=500
38 Correct 14 ms 24148 KB n=500
39 Correct 14 ms 24216 KB n=500
40 Correct 13 ms 24148 KB n=500
41 Correct 14 ms 24148 KB n=500
42 Correct 13 ms 24148 KB n=500
43 Correct 17 ms 24148 KB n=500
44 Correct 13 ms 24148 KB n=500
45 Correct 14 ms 24232 KB n=500
46 Correct 15 ms 24148 KB n=500
47 Correct 16 ms 24168 KB n=500
48 Correct 14 ms 24148 KB n=500
49 Correct 16 ms 24128 KB n=500
50 Correct 13 ms 24148 KB n=500
51 Correct 13 ms 24148 KB n=500
52 Correct 13 ms 24148 KB n=500
53 Correct 13 ms 24148 KB n=500
54 Correct 13 ms 24220 KB n=500
55 Correct 14 ms 24104 KB n=278
56 Correct 13 ms 24148 KB n=500
57 Correct 13 ms 24148 KB n=500
58 Correct 16 ms 24148 KB n=500
59 Correct 20 ms 25300 KB n=2000
60 Correct 17 ms 25228 KB n=2000
61 Correct 19 ms 25300 KB n=2000
62 Correct 18 ms 25300 KB n=2000
63 Correct 17 ms 25300 KB n=2000
64 Correct 17 ms 25332 KB n=2000
65 Correct 18 ms 25300 KB n=2000
66 Correct 21 ms 25288 KB n=2000
67 Correct 17 ms 25300 KB n=2000
68 Correct 16 ms 25300 KB n=2000
69 Correct 15 ms 25300 KB n=2000
70 Correct 16 ms 25300 KB n=2000
71 Correct 16 ms 25368 KB n=2000
72 Correct 17 ms 25360 KB n=2000
73 Correct 17 ms 25300 KB n=2000
74 Correct 16 ms 25260 KB n=1844
75 Correct 16 ms 25348 KB n=2000
76 Correct 16 ms 25296 KB n=2000
77 Correct 15 ms 25300 KB n=2000
78 Correct 17 ms 25296 KB n=2000
79 Correct 17 ms 25260 KB n=2000
80 Correct 20 ms 25300 KB n=2000
81 Correct 16 ms 25300 KB n=2000
82 Correct 15 ms 25300 KB n=2000
83 Correct 17 ms 25428 KB n=2000
84 Correct 16 ms 25308 KB n=2000
85 Correct 17 ms 25300 KB n=2000
86 Correct 17 ms 25300 KB n=2000
87 Correct 20 ms 25300 KB n=2000
88 Correct 16 ms 25308 KB n=2000
89 Correct 16 ms 25300 KB n=2000
90 Correct 16 ms 25220 KB n=2000
91 Correct 17 ms 25284 KB n=2000
92 Correct 807 ms 209496 KB n=200000
93 Correct 813 ms 208724 KB n=200000
94 Correct 776 ms 208176 KB n=200000
95 Correct 827 ms 209352 KB n=200000
96 Correct 824 ms 209376 KB n=200000
97 Correct 834 ms 208696 KB n=200000
98 Correct 813 ms 209396 KB n=200000
99 Correct 921 ms 209184 KB n=200000
100 Correct 885 ms 209516 KB n=200000
101 Correct 799 ms 207796 KB n=200000
102 Correct 461 ms 210600 KB n=200000
103 Correct 522 ms 210520 KB n=200000
104 Correct 453 ms 210596 KB n=200000
105 Correct 514 ms 210432 KB n=200000
106 Correct 505 ms 210324 KB n=200000
107 Correct 456 ms 210252 KB n=200000
108 Correct 810 ms 209268 KB n=200000
109 Correct 822 ms 209348 KB n=200000
110 Correct 819 ms 209212 KB n=200000
111 Correct 816 ms 209380 KB n=200000
112 Correct 765 ms 208052 KB n=200000
113 Correct 810 ms 208748 KB n=200000
114 Correct 808 ms 209460 KB n=200000
115 Correct 927 ms 208692 KB n=200000
116 Correct 771 ms 209084 KB n=200000
117 Correct 706 ms 207668 KB n=200000
118 Correct 850 ms 208464 KB n=200000
119 Correct 805 ms 209096 KB n=200000
120 Correct 707 ms 207344 KB n=200000
121 Correct 727 ms 207444 KB n=200000
122 Correct 693 ms 207840 KB n=200000
123 Correct 484 ms 209960 KB n=200000
124 Correct 195 ms 51544 KB n=25264