답안 #637671

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
637671 2022-09-02T17:02:51 Z Sam_a17 Birthday gift (IZhO18_treearray) C++14
100 / 100
1142 ms 91852 KB
#define _CRT_SECURE_NO_WARNINGS
#include <bits/stdc++.h>
//#include "temp.cpp"
#include <cstdio>
using namespace std;

#ifndef ONLINE_JUDGE
#define dbg(x) cerr << #x <<" "; print(x); cerr << endl;
#else
#define dbg(x)
#endif

#define sz(x) (int((x).size()))
#define len(x) (int)x.length()
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define clr(x) (x).clear()
#define uniq(x) x.resize(unique(all(x)) - x.begin());
#define blt __builtin_popcount

#define pb push_back
#define popf pop_front
#define popb pop_back

void print(long long t) {cerr << t;}
void print(int t) {cerr << t;}
void print(string t) {cerr << t;}
void print(char t) {cerr << t;}
void print(double t) {cerr << t;}
void print(long double t) {cerr << t;}
void print(unsigned long long t) {cerr << t;}

template <class T, class V> void print(pair <T, V> p);
template <class T> void print(vector <T> v);
template <class T> void print(set <T> v);
template <class T, class V> void print(map <T, V> v);
template <class T> void print(multiset <T> v);
template <class T, class V> void print(T v[],V n) {cerr << "["; for(int i = 0; i < n; i++) {print(v[i]); cerr << " "; } cerr << "]";}
template <class T, class V> void print(pair <T, V> p) {cerr << "{"; print(p.first); cerr << ","; print(p.second); cerr << "}";}
template <class T> void print(vector <T> v) {cerr << "[ "; for (T i : v) {print(i); cerr << " ";} cerr << "]";}
template <class T> void print(set <T> v) {cerr << "[ "; for (T i : v) {print(i); cerr << " ";} cerr << "]";}
template <class T> void print(multiset <T> v) {cerr << "[ "; for (T i : v) {print(i); cerr << " ";} cerr << "]";}
template <class T, class V> void print(map <T, V> v) {cerr << "[ "; for (auto i : v) {print(i); cerr << " ";} cerr << "]";}

#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
#define nl '\n'

// for grid problems
int dx[8] = {-1,0,1,0,1,-1,1,-1};
int dy[8] = {0,1,0,-1,1,1,-1,-1};

// lowest / (1 << 17) >= 1e5 / (1 << 18) >= 2e5 / (1 << 21) >= 1e6
void fastIO() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr); cout.tie(nullptr);
}
// file in/out
void setIO(string str = "") {
  fastIO();

  if (str != "") {
    freopen((str + ".in").c_str(), "r", stdin);
    freopen((str + ".out").c_str(), "w", stdout);
  }
}
// Indexed Set
template <class T> using Tree = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
const int N = 2e5 + 10, LOG = 22, inf = 1e8;
int n, m, q, up[N][LOG], depth[N], A[N];
vector<int> adj[N];

int lca(int a, int b) {
  if(a == inf) {
    return b;
  }

  if(b == inf) {
    return a;
  }

  if(a == b) {
    return a;
  }

  if(depth[a] < depth[b]) {
    swap(a, b);
  }

  int delta = depth[a] - depth[b];
  for(int i = 0; i < LOG; i++) {
    if(delta & (1 << i)) {
      a = up[a][i];
    }
  }

  if(a == b) {
    return a;
  }

  for(int i = LOG - 1; i >= 0; i--) {
    if(up[a][i] != up[b][i]) {
      a = up[a][i], b = up[b][i];
    }
  }

  return up[a][0];
}

void dfs(int node, int parent) {
  for(auto i: adj[node]) {
    if(i == parent) continue;
    up[i][0] = node;
    for(int j = 1; j < LOG; j++) {
      up[i][j] = up[up[i][j - 1]][j - 1];
    }
    depth[i] = depth[node] + 1;
    dfs(i, node);
  }
}

set<int> one[N], two[N];

void solve_() {
  cin >> n >> m >> q;

  for(int i = 1; i <= n - 1; i++) {
    int a, b; cin >> a >> b;
    adj[a].push_back(b);
    adj[b].push_back(a);
  }

  dfs(1, 0);

  for(int i = 1; i <= m; i++) {
    cin >> A[i];
    one[A[i]].insert(i);

    if(i >= 2) {
      two[lca(A[i], A[i - 1])].insert(i);
    }
  }

  while(q--) {
    int type; cin >> type;

    if(type == 1) {
      int pos, v; cin >> pos >> v;
      one[A[pos]].erase(pos);
      one[v].insert(pos);

      if(pos >= 2) {
        two[lca(A[pos - 1], A[pos])].erase(pos);
        two[lca(A[pos - 1], v)].insert(pos);
      }

      if(pos <= m - 1) {
        two[lca(A[pos + 1], A[pos])].erase(pos + 1);
        two[lca(A[pos + 1], v)].insert(pos + 1);
      }

      A[pos] = v;
    } else {
      int l, r, v; cin >> l >> r >> v;

      auto it = one[v].lower_bound(l);
      if(it != one[v].end() && *it <= r) {
        cout << *it << " " << *it << endl;
        continue;
      }

      it = two[v].lower_bound(l + 1);
      if(it != two[v].end() && *it <= r) {
        cout << *it - 1 << " " << *it << endl;
        continue;
      }

      cout << -1 << " " << -1 << endl;
    }
  }

}

int main() {
  setIO();

  auto solve = [&](int test_case)-> void {
    while(test_case--) {
      solve_();
    }
  };

  int test_cases = 1;
  solve(test_cases);

  return 0;
}

Compilation message

treearray.cpp: In function 'void setIO(std::string)':
treearray.cpp:63:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   63 |     freopen((str + ".in").c_str(), "r", stdin);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
treearray.cpp:64:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   64 |     freopen((str + ".out").c_str(), "w", stdout);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 13 ms 23764 KB n=5
2 Correct 12 ms 23764 KB n=100
3 Correct 12 ms 23764 KB n=100
4 Correct 14 ms 23772 KB n=100
5 Correct 15 ms 23816 KB n=100
6 Correct 12 ms 23764 KB n=100
7 Correct 12 ms 23796 KB n=100
8 Correct 14 ms 23764 KB n=100
9 Correct 12 ms 23820 KB n=100
10 Correct 12 ms 23796 KB n=100
11 Correct 12 ms 23728 KB n=100
12 Correct 13 ms 23792 KB n=100
13 Correct 12 ms 23848 KB n=100
14 Correct 15 ms 23728 KB n=100
15 Correct 13 ms 23844 KB n=100
16 Correct 15 ms 23752 KB n=100
17 Correct 15 ms 23764 KB n=100
18 Correct 15 ms 23764 KB n=100
19 Correct 12 ms 23824 KB n=100
20 Correct 12 ms 23848 KB n=100
21 Correct 12 ms 23820 KB n=100
22 Correct 12 ms 23916 KB n=100
23 Correct 13 ms 23824 KB n=100
24 Correct 14 ms 23764 KB n=100
25 Correct 12 ms 23764 KB n=100
26 Correct 12 ms 23720 KB n=12
27 Correct 12 ms 23764 KB n=100
# 결과 실행 시간 메모리 Grader output
1 Correct 13 ms 23764 KB n=5
2 Correct 12 ms 23764 KB n=100
3 Correct 12 ms 23764 KB n=100
4 Correct 14 ms 23772 KB n=100
5 Correct 15 ms 23816 KB n=100
6 Correct 12 ms 23764 KB n=100
7 Correct 12 ms 23796 KB n=100
8 Correct 14 ms 23764 KB n=100
9 Correct 12 ms 23820 KB n=100
10 Correct 12 ms 23796 KB n=100
11 Correct 12 ms 23728 KB n=100
12 Correct 13 ms 23792 KB n=100
13 Correct 12 ms 23848 KB n=100
14 Correct 15 ms 23728 KB n=100
15 Correct 13 ms 23844 KB n=100
16 Correct 15 ms 23752 KB n=100
17 Correct 15 ms 23764 KB n=100
18 Correct 15 ms 23764 KB n=100
19 Correct 12 ms 23824 KB n=100
20 Correct 12 ms 23848 KB n=100
21 Correct 12 ms 23820 KB n=100
22 Correct 12 ms 23916 KB n=100
23 Correct 13 ms 23824 KB n=100
24 Correct 14 ms 23764 KB n=100
25 Correct 12 ms 23764 KB n=100
26 Correct 12 ms 23720 KB n=12
27 Correct 12 ms 23764 KB n=100
28 Correct 16 ms 23908 KB n=500
29 Correct 13 ms 23884 KB n=500
30 Correct 13 ms 23852 KB n=500
31 Correct 13 ms 23920 KB n=500
32 Correct 13 ms 23892 KB n=500
33 Correct 13 ms 23848 KB n=500
34 Correct 14 ms 23948 KB n=500
35 Correct 12 ms 23960 KB n=500
36 Correct 14 ms 23892 KB n=500
37 Correct 16 ms 23948 KB n=500
38 Correct 14 ms 23892 KB n=500
39 Correct 13 ms 23948 KB n=500
40 Correct 14 ms 24016 KB n=500
41 Correct 16 ms 23824 KB n=500
42 Correct 15 ms 23892 KB n=500
43 Correct 16 ms 23828 KB n=500
44 Correct 14 ms 23892 KB n=500
45 Correct 13 ms 23856 KB n=500
46 Correct 13 ms 23892 KB n=500
47 Correct 14 ms 23952 KB n=500
48 Correct 12 ms 23828 KB n=500
49 Correct 13 ms 23956 KB n=500
50 Correct 14 ms 23956 KB n=500
51 Correct 12 ms 23856 KB n=500
52 Correct 13 ms 23964 KB n=500
53 Correct 13 ms 23892 KB n=500
54 Correct 13 ms 23892 KB n=500
55 Correct 12 ms 23844 KB n=278
56 Correct 15 ms 23924 KB n=500
57 Correct 13 ms 23956 KB n=500
58 Correct 13 ms 23880 KB n=500
# 결과 실행 시간 메모리 Grader output
1 Correct 13 ms 23764 KB n=5
2 Correct 12 ms 23764 KB n=100
3 Correct 12 ms 23764 KB n=100
4 Correct 14 ms 23772 KB n=100
5 Correct 15 ms 23816 KB n=100
6 Correct 12 ms 23764 KB n=100
7 Correct 12 ms 23796 KB n=100
8 Correct 14 ms 23764 KB n=100
9 Correct 12 ms 23820 KB n=100
10 Correct 12 ms 23796 KB n=100
11 Correct 12 ms 23728 KB n=100
12 Correct 13 ms 23792 KB n=100
13 Correct 12 ms 23848 KB n=100
14 Correct 15 ms 23728 KB n=100
15 Correct 13 ms 23844 KB n=100
16 Correct 15 ms 23752 KB n=100
17 Correct 15 ms 23764 KB n=100
18 Correct 15 ms 23764 KB n=100
19 Correct 12 ms 23824 KB n=100
20 Correct 12 ms 23848 KB n=100
21 Correct 12 ms 23820 KB n=100
22 Correct 12 ms 23916 KB n=100
23 Correct 13 ms 23824 KB n=100
24 Correct 14 ms 23764 KB n=100
25 Correct 12 ms 23764 KB n=100
26 Correct 12 ms 23720 KB n=12
27 Correct 12 ms 23764 KB n=100
28 Correct 16 ms 23908 KB n=500
29 Correct 13 ms 23884 KB n=500
30 Correct 13 ms 23852 KB n=500
31 Correct 13 ms 23920 KB n=500
32 Correct 13 ms 23892 KB n=500
33 Correct 13 ms 23848 KB n=500
34 Correct 14 ms 23948 KB n=500
35 Correct 12 ms 23960 KB n=500
36 Correct 14 ms 23892 KB n=500
37 Correct 16 ms 23948 KB n=500
38 Correct 14 ms 23892 KB n=500
39 Correct 13 ms 23948 KB n=500
40 Correct 14 ms 24016 KB n=500
41 Correct 16 ms 23824 KB n=500
42 Correct 15 ms 23892 KB n=500
43 Correct 16 ms 23828 KB n=500
44 Correct 14 ms 23892 KB n=500
45 Correct 13 ms 23856 KB n=500
46 Correct 13 ms 23892 KB n=500
47 Correct 14 ms 23952 KB n=500
48 Correct 12 ms 23828 KB n=500
49 Correct 13 ms 23956 KB n=500
50 Correct 14 ms 23956 KB n=500
51 Correct 12 ms 23856 KB n=500
52 Correct 13 ms 23964 KB n=500
53 Correct 13 ms 23892 KB n=500
54 Correct 13 ms 23892 KB n=500
55 Correct 12 ms 23844 KB n=278
56 Correct 15 ms 23924 KB n=500
57 Correct 13 ms 23956 KB n=500
58 Correct 13 ms 23880 KB n=500
59 Correct 15 ms 24276 KB n=2000
60 Correct 17 ms 24452 KB n=2000
61 Correct 16 ms 24404 KB n=2000
62 Correct 16 ms 24276 KB n=2000
63 Correct 16 ms 24208 KB n=2000
64 Correct 17 ms 24268 KB n=2000
65 Correct 16 ms 24252 KB n=2000
66 Correct 16 ms 24344 KB n=2000
67 Correct 18 ms 24212 KB n=2000
68 Correct 16 ms 24404 KB n=2000
69 Correct 17 ms 24232 KB n=2000
70 Correct 19 ms 24276 KB n=2000
71 Correct 17 ms 24220 KB n=2000
72 Correct 16 ms 24276 KB n=2000
73 Correct 17 ms 24276 KB n=2000
74 Correct 15 ms 24276 KB n=1844
75 Correct 17 ms 24212 KB n=2000
76 Correct 17 ms 24276 KB n=2000
77 Correct 17 ms 24220 KB n=2000
78 Correct 17 ms 24276 KB n=2000
79 Correct 16 ms 24276 KB n=2000
80 Correct 16 ms 24372 KB n=2000
81 Correct 16 ms 24324 KB n=2000
82 Correct 17 ms 24212 KB n=2000
83 Correct 19 ms 24356 KB n=2000
84 Correct 16 ms 24276 KB n=2000
85 Correct 17 ms 24328 KB n=2000
86 Correct 19 ms 24276 KB n=2000
87 Correct 17 ms 24220 KB n=2000
88 Correct 17 ms 24404 KB n=2000
89 Correct 16 ms 24444 KB n=2000
90 Correct 17 ms 24468 KB n=2000
91 Correct 17 ms 24304 KB n=2000
# 결과 실행 시간 메모리 Grader output
1 Correct 13 ms 23764 KB n=5
2 Correct 12 ms 23764 KB n=100
3 Correct 12 ms 23764 KB n=100
4 Correct 14 ms 23772 KB n=100
5 Correct 15 ms 23816 KB n=100
6 Correct 12 ms 23764 KB n=100
7 Correct 12 ms 23796 KB n=100
8 Correct 14 ms 23764 KB n=100
9 Correct 12 ms 23820 KB n=100
10 Correct 12 ms 23796 KB n=100
11 Correct 12 ms 23728 KB n=100
12 Correct 13 ms 23792 KB n=100
13 Correct 12 ms 23848 KB n=100
14 Correct 15 ms 23728 KB n=100
15 Correct 13 ms 23844 KB n=100
16 Correct 15 ms 23752 KB n=100
17 Correct 15 ms 23764 KB n=100
18 Correct 15 ms 23764 KB n=100
19 Correct 12 ms 23824 KB n=100
20 Correct 12 ms 23848 KB n=100
21 Correct 12 ms 23820 KB n=100
22 Correct 12 ms 23916 KB n=100
23 Correct 13 ms 23824 KB n=100
24 Correct 14 ms 23764 KB n=100
25 Correct 12 ms 23764 KB n=100
26 Correct 12 ms 23720 KB n=12
27 Correct 12 ms 23764 KB n=100
28 Correct 16 ms 23908 KB n=500
29 Correct 13 ms 23884 KB n=500
30 Correct 13 ms 23852 KB n=500
31 Correct 13 ms 23920 KB n=500
32 Correct 13 ms 23892 KB n=500
33 Correct 13 ms 23848 KB n=500
34 Correct 14 ms 23948 KB n=500
35 Correct 12 ms 23960 KB n=500
36 Correct 14 ms 23892 KB n=500
37 Correct 16 ms 23948 KB n=500
38 Correct 14 ms 23892 KB n=500
39 Correct 13 ms 23948 KB n=500
40 Correct 14 ms 24016 KB n=500
41 Correct 16 ms 23824 KB n=500
42 Correct 15 ms 23892 KB n=500
43 Correct 16 ms 23828 KB n=500
44 Correct 14 ms 23892 KB n=500
45 Correct 13 ms 23856 KB n=500
46 Correct 13 ms 23892 KB n=500
47 Correct 14 ms 23952 KB n=500
48 Correct 12 ms 23828 KB n=500
49 Correct 13 ms 23956 KB n=500
50 Correct 14 ms 23956 KB n=500
51 Correct 12 ms 23856 KB n=500
52 Correct 13 ms 23964 KB n=500
53 Correct 13 ms 23892 KB n=500
54 Correct 13 ms 23892 KB n=500
55 Correct 12 ms 23844 KB n=278
56 Correct 15 ms 23924 KB n=500
57 Correct 13 ms 23956 KB n=500
58 Correct 13 ms 23880 KB n=500
59 Correct 15 ms 24276 KB n=2000
60 Correct 17 ms 24452 KB n=2000
61 Correct 16 ms 24404 KB n=2000
62 Correct 16 ms 24276 KB n=2000
63 Correct 16 ms 24208 KB n=2000
64 Correct 17 ms 24268 KB n=2000
65 Correct 16 ms 24252 KB n=2000
66 Correct 16 ms 24344 KB n=2000
67 Correct 18 ms 24212 KB n=2000
68 Correct 16 ms 24404 KB n=2000
69 Correct 17 ms 24232 KB n=2000
70 Correct 19 ms 24276 KB n=2000
71 Correct 17 ms 24220 KB n=2000
72 Correct 16 ms 24276 KB n=2000
73 Correct 17 ms 24276 KB n=2000
74 Correct 15 ms 24276 KB n=1844
75 Correct 17 ms 24212 KB n=2000
76 Correct 17 ms 24276 KB n=2000
77 Correct 17 ms 24220 KB n=2000
78 Correct 17 ms 24276 KB n=2000
79 Correct 16 ms 24276 KB n=2000
80 Correct 16 ms 24372 KB n=2000
81 Correct 16 ms 24324 KB n=2000
82 Correct 17 ms 24212 KB n=2000
83 Correct 19 ms 24356 KB n=2000
84 Correct 16 ms 24276 KB n=2000
85 Correct 17 ms 24328 KB n=2000
86 Correct 19 ms 24276 KB n=2000
87 Correct 17 ms 24220 KB n=2000
88 Correct 17 ms 24404 KB n=2000
89 Correct 16 ms 24444 KB n=2000
90 Correct 17 ms 24468 KB n=2000
91 Correct 17 ms 24304 KB n=2000
92 Correct 823 ms 73164 KB n=200000
93 Correct 1140 ms 84076 KB n=200000
94 Correct 1040 ms 89752 KB n=200000
95 Correct 835 ms 75776 KB n=200000
96 Correct 808 ms 75896 KB n=200000
97 Correct 1086 ms 82600 KB n=200000
98 Correct 803 ms 75952 KB n=200000
99 Correct 1045 ms 76156 KB n=200000
100 Correct 828 ms 76164 KB n=200000
101 Correct 986 ms 91852 KB n=200000
102 Correct 680 ms 77180 KB n=200000
103 Correct 684 ms 77112 KB n=200000
104 Correct 646 ms 77156 KB n=200000
105 Correct 678 ms 77596 KB n=200000
106 Correct 663 ms 77460 KB n=200000
107 Correct 680 ms 77532 KB n=200000
108 Correct 919 ms 76108 KB n=200000
109 Correct 932 ms 76044 KB n=200000
110 Correct 978 ms 76012 KB n=200000
111 Correct 855 ms 75536 KB n=200000
112 Correct 1016 ms 90308 KB n=200000
113 Correct 1072 ms 82508 KB n=200000
114 Correct 808 ms 75376 KB n=200000
115 Correct 1142 ms 78956 KB n=200000
116 Correct 793 ms 76076 KB n=200000
117 Correct 969 ms 90916 KB n=200000
118 Correct 1086 ms 80712 KB n=200000
119 Correct 769 ms 76072 KB n=200000
120 Correct 997 ms 90908 KB n=200000
121 Correct 930 ms 90840 KB n=200000
122 Correct 939 ms 91196 KB n=200000
123 Correct 689 ms 77304 KB n=200000
124 Correct 251 ms 39568 KB n=25264