답안 #537418

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
537418 2022-03-15T05:25:56 Z cig32 Birthday gift (IZhO18_treearray) C++17
56 / 100
849 ms 217652 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 < n) 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 < n) 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 13 ms 23764 KB n=5
2 Correct 12 ms 23892 KB n=100
3 Correct 13 ms 23928 KB n=100
4 Correct 13 ms 23892 KB n=100
5 Correct 13 ms 23828 KB n=100
6 Correct 14 ms 23876 KB n=100
7 Correct 16 ms 23892 KB n=100
8 Correct 13 ms 23892 KB n=100
9 Correct 13 ms 23892 KB n=100
10 Correct 13 ms 23892 KB n=100
11 Correct 16 ms 23888 KB n=100
12 Correct 14 ms 23884 KB n=100
13 Correct 14 ms 23892 KB n=100
14 Correct 16 ms 23916 KB n=100
15 Correct 13 ms 23892 KB n=100
16 Correct 13 ms 23924 KB n=100
17 Correct 13 ms 23892 KB n=100
18 Correct 13 ms 23828 KB n=100
19 Correct 13 ms 23892 KB n=100
20 Correct 16 ms 23856 KB n=100
21 Correct 13 ms 23892 KB n=100
22 Correct 13 ms 23892 KB n=100
23 Correct 13 ms 23892 KB n=100
24 Correct 13 ms 23892 KB n=100
25 Correct 13 ms 23840 KB n=100
26 Correct 14 ms 23804 KB n=12
27 Correct 13 ms 23904 KB n=100
# 결과 실행 시간 메모리 Grader output
1 Correct 13 ms 23764 KB n=5
2 Correct 12 ms 23892 KB n=100
3 Correct 13 ms 23928 KB n=100
4 Correct 13 ms 23892 KB n=100
5 Correct 13 ms 23828 KB n=100
6 Correct 14 ms 23876 KB n=100
7 Correct 16 ms 23892 KB n=100
8 Correct 13 ms 23892 KB n=100
9 Correct 13 ms 23892 KB n=100
10 Correct 13 ms 23892 KB n=100
11 Correct 16 ms 23888 KB n=100
12 Correct 14 ms 23884 KB n=100
13 Correct 14 ms 23892 KB n=100
14 Correct 16 ms 23916 KB n=100
15 Correct 13 ms 23892 KB n=100
16 Correct 13 ms 23924 KB n=100
17 Correct 13 ms 23892 KB n=100
18 Correct 13 ms 23828 KB n=100
19 Correct 13 ms 23892 KB n=100
20 Correct 16 ms 23856 KB n=100
21 Correct 13 ms 23892 KB n=100
22 Correct 13 ms 23892 KB n=100
23 Correct 13 ms 23892 KB n=100
24 Correct 13 ms 23892 KB n=100
25 Correct 13 ms 23840 KB n=100
26 Correct 14 ms 23804 KB n=12
27 Correct 13 ms 23904 KB n=100
28 Correct 14 ms 24148 KB n=500
29 Correct 14 ms 24148 KB n=500
30 Correct 14 ms 24148 KB n=500
31 Correct 13 ms 24196 KB n=500
32 Correct 14 ms 24188 KB n=500
33 Correct 14 ms 24164 KB n=500
34 Correct 13 ms 24236 KB n=500
35 Correct 14 ms 24228 KB n=500
36 Correct 16 ms 24144 KB n=500
37 Correct 14 ms 24148 KB n=500
38 Correct 14 ms 24148 KB n=500
39 Correct 13 ms 24236 KB n=500
40 Correct 13 ms 24148 KB n=500
41 Correct 13 ms 24148 KB n=500
42 Correct 13 ms 24212 KB n=500
43 Correct 15 ms 24148 KB n=500
44 Correct 16 ms 24140 KB n=500
45 Correct 17 ms 24148 KB n=500
46 Correct 14 ms 24148 KB n=500
47 Correct 14 ms 24128 KB n=500
48 Correct 14 ms 24128 KB n=500
49 Correct 13 ms 24228 KB n=500
50 Correct 14 ms 24148 KB n=500
51 Correct 15 ms 24160 KB n=500
52 Correct 17 ms 24148 KB n=500
53 Correct 14 ms 24196 KB n=500
54 Correct 13 ms 24192 KB n=500
55 Correct 13 ms 24104 KB n=278
56 Correct 13 ms 24144 KB n=500
57 Correct 15 ms 24136 KB n=500
58 Correct 13 ms 24148 KB n=500
# 결과 실행 시간 메모리 Grader output
1 Correct 13 ms 23764 KB n=5
2 Correct 12 ms 23892 KB n=100
3 Correct 13 ms 23928 KB n=100
4 Correct 13 ms 23892 KB n=100
5 Correct 13 ms 23828 KB n=100
6 Correct 14 ms 23876 KB n=100
7 Correct 16 ms 23892 KB n=100
8 Correct 13 ms 23892 KB n=100
9 Correct 13 ms 23892 KB n=100
10 Correct 13 ms 23892 KB n=100
11 Correct 16 ms 23888 KB n=100
12 Correct 14 ms 23884 KB n=100
13 Correct 14 ms 23892 KB n=100
14 Correct 16 ms 23916 KB n=100
15 Correct 13 ms 23892 KB n=100
16 Correct 13 ms 23924 KB n=100
17 Correct 13 ms 23892 KB n=100
18 Correct 13 ms 23828 KB n=100
19 Correct 13 ms 23892 KB n=100
20 Correct 16 ms 23856 KB n=100
21 Correct 13 ms 23892 KB n=100
22 Correct 13 ms 23892 KB n=100
23 Correct 13 ms 23892 KB n=100
24 Correct 13 ms 23892 KB n=100
25 Correct 13 ms 23840 KB n=100
26 Correct 14 ms 23804 KB n=12
27 Correct 13 ms 23904 KB n=100
28 Correct 14 ms 24148 KB n=500
29 Correct 14 ms 24148 KB n=500
30 Correct 14 ms 24148 KB n=500
31 Correct 13 ms 24196 KB n=500
32 Correct 14 ms 24188 KB n=500
33 Correct 14 ms 24164 KB n=500
34 Correct 13 ms 24236 KB n=500
35 Correct 14 ms 24228 KB n=500
36 Correct 16 ms 24144 KB n=500
37 Correct 14 ms 24148 KB n=500
38 Correct 14 ms 24148 KB n=500
39 Correct 13 ms 24236 KB n=500
40 Correct 13 ms 24148 KB n=500
41 Correct 13 ms 24148 KB n=500
42 Correct 13 ms 24212 KB n=500
43 Correct 15 ms 24148 KB n=500
44 Correct 16 ms 24140 KB n=500
45 Correct 17 ms 24148 KB n=500
46 Correct 14 ms 24148 KB n=500
47 Correct 14 ms 24128 KB n=500
48 Correct 14 ms 24128 KB n=500
49 Correct 13 ms 24228 KB n=500
50 Correct 14 ms 24148 KB n=500
51 Correct 15 ms 24160 KB n=500
52 Correct 17 ms 24148 KB n=500
53 Correct 14 ms 24196 KB n=500
54 Correct 13 ms 24192 KB n=500
55 Correct 13 ms 24104 KB n=278
56 Correct 13 ms 24144 KB n=500
57 Correct 15 ms 24136 KB n=500
58 Correct 13 ms 24148 KB n=500
59 Correct 17 ms 25300 KB n=2000
60 Correct 20 ms 25292 KB n=2000
61 Correct 17 ms 25300 KB n=2000
62 Correct 18 ms 25300 KB n=2000
63 Correct 17 ms 25268 KB n=2000
64 Correct 16 ms 25296 KB n=2000
65 Correct 16 ms 25240 KB n=2000
66 Correct 16 ms 25276 KB n=2000
67 Correct 16 ms 25300 KB n=2000
68 Correct 15 ms 25224 KB n=2000
69 Correct 16 ms 25304 KB n=2000
70 Correct 17 ms 25300 KB n=2000
71 Correct 17 ms 25300 KB n=2000
72 Correct 15 ms 25360 KB n=2000
73 Correct 15 ms 25324 KB n=2000
74 Correct 15 ms 25208 KB n=1844
75 Correct 18 ms 25428 KB n=2000
76 Correct 16 ms 25360 KB n=2000
77 Correct 16 ms 25360 KB n=2000
78 Correct 21 ms 25300 KB n=2000
79 Correct 17 ms 25360 KB n=2000
80 Correct 16 ms 25340 KB n=2000
81 Correct 15 ms 25224 KB n=2000
82 Correct 16 ms 25332 KB n=2000
83 Correct 16 ms 25228 KB n=2000
84 Correct 16 ms 25300 KB n=2000
85 Correct 18 ms 25344 KB n=2000
86 Correct 20 ms 25316 KB n=2000
87 Correct 21 ms 25304 KB n=2000
88 Correct 16 ms 25220 KB n=2000
89 Correct 16 ms 25336 KB n=2000
90 Correct 16 ms 25228 KB n=2000
91 Correct 15 ms 25352 KB n=2000
# 결과 실행 시간 메모리 Grader output
1 Correct 13 ms 23764 KB n=5
2 Correct 12 ms 23892 KB n=100
3 Correct 13 ms 23928 KB n=100
4 Correct 13 ms 23892 KB n=100
5 Correct 13 ms 23828 KB n=100
6 Correct 14 ms 23876 KB n=100
7 Correct 16 ms 23892 KB n=100
8 Correct 13 ms 23892 KB n=100
9 Correct 13 ms 23892 KB n=100
10 Correct 13 ms 23892 KB n=100
11 Correct 16 ms 23888 KB n=100
12 Correct 14 ms 23884 KB n=100
13 Correct 14 ms 23892 KB n=100
14 Correct 16 ms 23916 KB n=100
15 Correct 13 ms 23892 KB n=100
16 Correct 13 ms 23924 KB n=100
17 Correct 13 ms 23892 KB n=100
18 Correct 13 ms 23828 KB n=100
19 Correct 13 ms 23892 KB n=100
20 Correct 16 ms 23856 KB n=100
21 Correct 13 ms 23892 KB n=100
22 Correct 13 ms 23892 KB n=100
23 Correct 13 ms 23892 KB n=100
24 Correct 13 ms 23892 KB n=100
25 Correct 13 ms 23840 KB n=100
26 Correct 14 ms 23804 KB n=12
27 Correct 13 ms 23904 KB n=100
28 Correct 14 ms 24148 KB n=500
29 Correct 14 ms 24148 KB n=500
30 Correct 14 ms 24148 KB n=500
31 Correct 13 ms 24196 KB n=500
32 Correct 14 ms 24188 KB n=500
33 Correct 14 ms 24164 KB n=500
34 Correct 13 ms 24236 KB n=500
35 Correct 14 ms 24228 KB n=500
36 Correct 16 ms 24144 KB n=500
37 Correct 14 ms 24148 KB n=500
38 Correct 14 ms 24148 KB n=500
39 Correct 13 ms 24236 KB n=500
40 Correct 13 ms 24148 KB n=500
41 Correct 13 ms 24148 KB n=500
42 Correct 13 ms 24212 KB n=500
43 Correct 15 ms 24148 KB n=500
44 Correct 16 ms 24140 KB n=500
45 Correct 17 ms 24148 KB n=500
46 Correct 14 ms 24148 KB n=500
47 Correct 14 ms 24128 KB n=500
48 Correct 14 ms 24128 KB n=500
49 Correct 13 ms 24228 KB n=500
50 Correct 14 ms 24148 KB n=500
51 Correct 15 ms 24160 KB n=500
52 Correct 17 ms 24148 KB n=500
53 Correct 14 ms 24196 KB n=500
54 Correct 13 ms 24192 KB n=500
55 Correct 13 ms 24104 KB n=278
56 Correct 13 ms 24144 KB n=500
57 Correct 15 ms 24136 KB n=500
58 Correct 13 ms 24148 KB n=500
59 Correct 17 ms 25300 KB n=2000
60 Correct 20 ms 25292 KB n=2000
61 Correct 17 ms 25300 KB n=2000
62 Correct 18 ms 25300 KB n=2000
63 Correct 17 ms 25268 KB n=2000
64 Correct 16 ms 25296 KB n=2000
65 Correct 16 ms 25240 KB n=2000
66 Correct 16 ms 25276 KB n=2000
67 Correct 16 ms 25300 KB n=2000
68 Correct 15 ms 25224 KB n=2000
69 Correct 16 ms 25304 KB n=2000
70 Correct 17 ms 25300 KB n=2000
71 Correct 17 ms 25300 KB n=2000
72 Correct 15 ms 25360 KB n=2000
73 Correct 15 ms 25324 KB n=2000
74 Correct 15 ms 25208 KB n=1844
75 Correct 18 ms 25428 KB n=2000
76 Correct 16 ms 25360 KB n=2000
77 Correct 16 ms 25360 KB n=2000
78 Correct 21 ms 25300 KB n=2000
79 Correct 17 ms 25360 KB n=2000
80 Correct 16 ms 25340 KB n=2000
81 Correct 15 ms 25224 KB n=2000
82 Correct 16 ms 25332 KB n=2000
83 Correct 16 ms 25228 KB n=2000
84 Correct 16 ms 25300 KB n=2000
85 Correct 18 ms 25344 KB n=2000
86 Correct 20 ms 25316 KB n=2000
87 Correct 21 ms 25304 KB n=2000
88 Correct 16 ms 25220 KB n=2000
89 Correct 16 ms 25336 KB n=2000
90 Correct 16 ms 25228 KB n=2000
91 Correct 15 ms 25352 KB n=2000
92 Correct 849 ms 212688 KB n=200000
93 Correct 831 ms 216404 KB n=200000
94 Correct 805 ms 215980 KB n=200000
95 Correct 803 ms 215904 KB n=200000
96 Correct 834 ms 215784 KB n=200000
97 Correct 823 ms 216412 KB n=200000
98 Correct 802 ms 215772 KB n=200000
99 Correct 848 ms 216260 KB n=200000
100 Correct 789 ms 215848 KB n=200000
101 Correct 725 ms 215512 KB n=200000
102 Correct 452 ms 217260 KB n=200000
103 Correct 453 ms 217324 KB n=200000
104 Correct 451 ms 217288 KB n=200000
105 Correct 446 ms 217652 KB n=200000
106 Correct 459 ms 217592 KB n=200000
107 Correct 451 ms 217580 KB n=200000
108 Correct 775 ms 216128 KB n=200000
109 Correct 774 ms 216100 KB n=200000
110 Correct 771 ms 216120 KB n=200000
111 Correct 734 ms 215348 KB n=200000
112 Correct 716 ms 215852 KB n=200000
113 Correct 781 ms 216436 KB n=200000
114 Correct 744 ms 215396 KB n=200000
115 Correct 830 ms 216432 KB n=200000
116 Correct 735 ms 216100 KB n=200000
117 Correct 709 ms 215548 KB n=200000
118 Correct 776 ms 216236 KB n=200000
119 Correct 729 ms 216116 KB n=200000
120 Correct 670 ms 214232 KB n=200000
121 Correct 662 ms 214268 KB n=200000
122 Correct 663 ms 214544 KB n=200000
123 Correct 460 ms 217532 KB n=200000
124 Incorrect 162 ms 53572 KB Wrong range.
125 Halted 0 ms 0 KB -