Submission #537414

# Submission time Handle Problem Language Result Execution time Memory
537414 2022-03-15T05:18:52 Z cig32 Birthday gift (IZhO18_treearray) C++17
56 / 100
4000 ms 155868 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];
  for(int i=1; i<=q; i++) {
    int t;
    cin >> t;
    if(t == 1) {
      int pos, v;
      cin >> pos >> v;
      a[pos] = v;
    }
    else {
      int l, r, v;
      cin >> l >> r >> v;
      int x = -1, y = -1;
      for(int j=l; j<=r; j++) {
        if(a[j] == v) {
          x = j, y = j;
        }
      }
      for(int j=l; j<r; j++) {
        if(lca_idx(a[j], a[j+1]) == v) {
          x = j, y = j + 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()) {
      |          ~~~~~~~~~~~~~~~^~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 14 ms 23768 KB n=5
2 Correct 13 ms 23892 KB n=100
3 Correct 13 ms 23824 KB n=100
4 Correct 13 ms 23820 KB n=100
5 Correct 12 ms 23816 KB n=100
6 Correct 13 ms 23932 KB n=100
7 Correct 16 ms 23832 KB n=100
8 Correct 16 ms 23816 KB n=100
9 Correct 13 ms 23892 KB n=100
10 Correct 13 ms 23892 KB n=100
11 Correct 13 ms 23892 KB n=100
12 Correct 13 ms 23824 KB n=100
13 Correct 13 ms 23816 KB n=100
14 Correct 16 ms 23892 KB n=100
15 Correct 14 ms 23892 KB n=100
16 Correct 16 ms 23936 KB n=100
17 Correct 13 ms 23892 KB n=100
18 Correct 13 ms 23820 KB n=100
19 Correct 14 ms 23892 KB n=100
20 Correct 14 ms 23864 KB n=100
21 Correct 13 ms 23892 KB n=100
22 Correct 14 ms 23892 KB n=100
23 Correct 14 ms 23892 KB n=100
24 Correct 14 ms 23888 KB n=100
25 Correct 13 ms 23844 KB n=100
26 Correct 13 ms 23764 KB n=12
27 Correct 14 ms 23820 KB n=100
# Verdict Execution time Memory Grader output
1 Correct 14 ms 23768 KB n=5
2 Correct 13 ms 23892 KB n=100
3 Correct 13 ms 23824 KB n=100
4 Correct 13 ms 23820 KB n=100
5 Correct 12 ms 23816 KB n=100
6 Correct 13 ms 23932 KB n=100
7 Correct 16 ms 23832 KB n=100
8 Correct 16 ms 23816 KB n=100
9 Correct 13 ms 23892 KB n=100
10 Correct 13 ms 23892 KB n=100
11 Correct 13 ms 23892 KB n=100
12 Correct 13 ms 23824 KB n=100
13 Correct 13 ms 23816 KB n=100
14 Correct 16 ms 23892 KB n=100
15 Correct 14 ms 23892 KB n=100
16 Correct 16 ms 23936 KB n=100
17 Correct 13 ms 23892 KB n=100
18 Correct 13 ms 23820 KB n=100
19 Correct 14 ms 23892 KB n=100
20 Correct 14 ms 23864 KB n=100
21 Correct 13 ms 23892 KB n=100
22 Correct 14 ms 23892 KB n=100
23 Correct 14 ms 23892 KB n=100
24 Correct 14 ms 23888 KB n=100
25 Correct 13 ms 23844 KB n=100
26 Correct 13 ms 23764 KB n=12
27 Correct 14 ms 23820 KB n=100
28 Correct 15 ms 24056 KB n=500
29 Correct 15 ms 24112 KB n=500
30 Correct 14 ms 24020 KB n=500
31 Correct 16 ms 24020 KB n=500
32 Correct 14 ms 24020 KB n=500
33 Correct 14 ms 24088 KB n=500
34 Correct 15 ms 24020 KB n=500
35 Correct 15 ms 24020 KB n=500
36 Correct 17 ms 24068 KB n=500
37 Correct 16 ms 24092 KB n=500
38 Correct 16 ms 24092 KB n=500
39 Correct 15 ms 24216 KB n=500
40 Correct 15 ms 24020 KB n=500
41 Correct 15 ms 24096 KB n=500
42 Correct 19 ms 24076 KB n=500
43 Correct 15 ms 24020 KB n=500
44 Correct 14 ms 24020 KB n=500
45 Correct 14 ms 24084 KB n=500
46 Correct 14 ms 24040 KB n=500
47 Correct 14 ms 24020 KB n=500
48 Correct 17 ms 24020 KB n=500
49 Correct 14 ms 24020 KB n=500
50 Correct 13 ms 24080 KB n=500
51 Correct 16 ms 24100 KB n=500
52 Correct 17 ms 24020 KB n=500
53 Correct 14 ms 24020 KB n=500
54 Correct 16 ms 24092 KB n=500
55 Correct 14 ms 23952 KB n=278
56 Correct 15 ms 24084 KB n=500
57 Correct 14 ms 24148 KB n=500
58 Correct 15 ms 24088 KB n=500
# Verdict Execution time Memory Grader output
1 Correct 14 ms 23768 KB n=5
2 Correct 13 ms 23892 KB n=100
3 Correct 13 ms 23824 KB n=100
4 Correct 13 ms 23820 KB n=100
5 Correct 12 ms 23816 KB n=100
6 Correct 13 ms 23932 KB n=100
7 Correct 16 ms 23832 KB n=100
8 Correct 16 ms 23816 KB n=100
9 Correct 13 ms 23892 KB n=100
10 Correct 13 ms 23892 KB n=100
11 Correct 13 ms 23892 KB n=100
12 Correct 13 ms 23824 KB n=100
13 Correct 13 ms 23816 KB n=100
14 Correct 16 ms 23892 KB n=100
15 Correct 14 ms 23892 KB n=100
16 Correct 16 ms 23936 KB n=100
17 Correct 13 ms 23892 KB n=100
18 Correct 13 ms 23820 KB n=100
19 Correct 14 ms 23892 KB n=100
20 Correct 14 ms 23864 KB n=100
21 Correct 13 ms 23892 KB n=100
22 Correct 14 ms 23892 KB n=100
23 Correct 14 ms 23892 KB n=100
24 Correct 14 ms 23888 KB n=100
25 Correct 13 ms 23844 KB n=100
26 Correct 13 ms 23764 KB n=12
27 Correct 14 ms 23820 KB n=100
28 Correct 15 ms 24056 KB n=500
29 Correct 15 ms 24112 KB n=500
30 Correct 14 ms 24020 KB n=500
31 Correct 16 ms 24020 KB n=500
32 Correct 14 ms 24020 KB n=500
33 Correct 14 ms 24088 KB n=500
34 Correct 15 ms 24020 KB n=500
35 Correct 15 ms 24020 KB n=500
36 Correct 17 ms 24068 KB n=500
37 Correct 16 ms 24092 KB n=500
38 Correct 16 ms 24092 KB n=500
39 Correct 15 ms 24216 KB n=500
40 Correct 15 ms 24020 KB n=500
41 Correct 15 ms 24096 KB n=500
42 Correct 19 ms 24076 KB n=500
43 Correct 15 ms 24020 KB n=500
44 Correct 14 ms 24020 KB n=500
45 Correct 14 ms 24084 KB n=500
46 Correct 14 ms 24040 KB n=500
47 Correct 14 ms 24020 KB n=500
48 Correct 17 ms 24020 KB n=500
49 Correct 14 ms 24020 KB n=500
50 Correct 13 ms 24080 KB n=500
51 Correct 16 ms 24100 KB n=500
52 Correct 17 ms 24020 KB n=500
53 Correct 14 ms 24020 KB n=500
54 Correct 16 ms 24092 KB n=500
55 Correct 14 ms 23952 KB n=278
56 Correct 15 ms 24084 KB n=500
57 Correct 14 ms 24148 KB n=500
58 Correct 15 ms 24088 KB n=500
59 Correct 22 ms 24880 KB n=2000
60 Correct 23 ms 24908 KB n=2000
61 Correct 24 ms 24852 KB n=2000
62 Correct 24 ms 24788 KB n=2000
63 Correct 21 ms 24788 KB n=2000
64 Correct 25 ms 24856 KB n=2000
65 Correct 25 ms 24856 KB n=2000
66 Correct 24 ms 24924 KB n=2000
67 Correct 24 ms 24856 KB n=2000
68 Correct 24 ms 24916 KB n=2000
69 Correct 63 ms 24872 KB n=2000
70 Correct 66 ms 24856 KB n=2000
71 Correct 64 ms 24788 KB n=2000
72 Correct 26 ms 24788 KB n=2000
73 Correct 26 ms 24836 KB n=2000
74 Correct 19 ms 24728 KB n=1844
75 Correct 30 ms 24788 KB n=2000
76 Correct 50 ms 24780 KB n=2000
77 Correct 40 ms 24860 KB n=2000
78 Correct 43 ms 24848 KB n=2000
79 Correct 20 ms 24868 KB n=2000
80 Correct 22 ms 24836 KB n=2000
81 Correct 23 ms 24852 KB n=2000
82 Correct 21 ms 24852 KB n=2000
83 Correct 27 ms 24836 KB n=2000
84 Correct 24 ms 24788 KB n=2000
85 Correct 23 ms 24896 KB n=2000
86 Correct 25 ms 24868 KB n=2000
87 Correct 24 ms 24824 KB n=2000
88 Correct 30 ms 24920 KB n=2000
89 Correct 33 ms 24960 KB n=2000
90 Correct 28 ms 24968 KB n=2000
91 Correct 43 ms 24864 KB n=2000
# Verdict Execution time Memory Grader output
1 Correct 14 ms 23768 KB n=5
2 Correct 13 ms 23892 KB n=100
3 Correct 13 ms 23824 KB n=100
4 Correct 13 ms 23820 KB n=100
5 Correct 12 ms 23816 KB n=100
6 Correct 13 ms 23932 KB n=100
7 Correct 16 ms 23832 KB n=100
8 Correct 16 ms 23816 KB n=100
9 Correct 13 ms 23892 KB n=100
10 Correct 13 ms 23892 KB n=100
11 Correct 13 ms 23892 KB n=100
12 Correct 13 ms 23824 KB n=100
13 Correct 13 ms 23816 KB n=100
14 Correct 16 ms 23892 KB n=100
15 Correct 14 ms 23892 KB n=100
16 Correct 16 ms 23936 KB n=100
17 Correct 13 ms 23892 KB n=100
18 Correct 13 ms 23820 KB n=100
19 Correct 14 ms 23892 KB n=100
20 Correct 14 ms 23864 KB n=100
21 Correct 13 ms 23892 KB n=100
22 Correct 14 ms 23892 KB n=100
23 Correct 14 ms 23892 KB n=100
24 Correct 14 ms 23888 KB n=100
25 Correct 13 ms 23844 KB n=100
26 Correct 13 ms 23764 KB n=12
27 Correct 14 ms 23820 KB n=100
28 Correct 15 ms 24056 KB n=500
29 Correct 15 ms 24112 KB n=500
30 Correct 14 ms 24020 KB n=500
31 Correct 16 ms 24020 KB n=500
32 Correct 14 ms 24020 KB n=500
33 Correct 14 ms 24088 KB n=500
34 Correct 15 ms 24020 KB n=500
35 Correct 15 ms 24020 KB n=500
36 Correct 17 ms 24068 KB n=500
37 Correct 16 ms 24092 KB n=500
38 Correct 16 ms 24092 KB n=500
39 Correct 15 ms 24216 KB n=500
40 Correct 15 ms 24020 KB n=500
41 Correct 15 ms 24096 KB n=500
42 Correct 19 ms 24076 KB n=500
43 Correct 15 ms 24020 KB n=500
44 Correct 14 ms 24020 KB n=500
45 Correct 14 ms 24084 KB n=500
46 Correct 14 ms 24040 KB n=500
47 Correct 14 ms 24020 KB n=500
48 Correct 17 ms 24020 KB n=500
49 Correct 14 ms 24020 KB n=500
50 Correct 13 ms 24080 KB n=500
51 Correct 16 ms 24100 KB n=500
52 Correct 17 ms 24020 KB n=500
53 Correct 14 ms 24020 KB n=500
54 Correct 16 ms 24092 KB n=500
55 Correct 14 ms 23952 KB n=278
56 Correct 15 ms 24084 KB n=500
57 Correct 14 ms 24148 KB n=500
58 Correct 15 ms 24088 KB n=500
59 Correct 22 ms 24880 KB n=2000
60 Correct 23 ms 24908 KB n=2000
61 Correct 24 ms 24852 KB n=2000
62 Correct 24 ms 24788 KB n=2000
63 Correct 21 ms 24788 KB n=2000
64 Correct 25 ms 24856 KB n=2000
65 Correct 25 ms 24856 KB n=2000
66 Correct 24 ms 24924 KB n=2000
67 Correct 24 ms 24856 KB n=2000
68 Correct 24 ms 24916 KB n=2000
69 Correct 63 ms 24872 KB n=2000
70 Correct 66 ms 24856 KB n=2000
71 Correct 64 ms 24788 KB n=2000
72 Correct 26 ms 24788 KB n=2000
73 Correct 26 ms 24836 KB n=2000
74 Correct 19 ms 24728 KB n=1844
75 Correct 30 ms 24788 KB n=2000
76 Correct 50 ms 24780 KB n=2000
77 Correct 40 ms 24860 KB n=2000
78 Correct 43 ms 24848 KB n=2000
79 Correct 20 ms 24868 KB n=2000
80 Correct 22 ms 24836 KB n=2000
81 Correct 23 ms 24852 KB n=2000
82 Correct 21 ms 24852 KB n=2000
83 Correct 27 ms 24836 KB n=2000
84 Correct 24 ms 24788 KB n=2000
85 Correct 23 ms 24896 KB n=2000
86 Correct 25 ms 24868 KB n=2000
87 Correct 24 ms 24824 KB n=2000
88 Correct 30 ms 24920 KB n=2000
89 Correct 33 ms 24960 KB n=2000
90 Correct 28 ms 24968 KB n=2000
91 Correct 43 ms 24864 KB n=2000
92 Execution timed out 4048 ms 155868 KB Time limit exceeded
93 Halted 0 ms 0 KB -