답안 #852589

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
852589 2023-09-22T07:12:03 Z Mher777 Birthday gift (IZhO18_treearray) C++17
100 / 100
928 ms 93520 KB
//////////////////////////////// Mher777
/* -------------------- Includes -------------------- */
#include <iostream>
#include <iomanip>
#include <array>
#include <string>
#include <algorithm>
#include <cmath>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>
#include <vector>
#include <stack>
#include <queue>
#include <deque>
#include <bitset>
#include <list>
#include <iterator>
#include <numeric>
#include <complex>
#include <utility>
#include <random>
#include <fstream>
#pragma warning(disable : 4996)
#pragma warning(disable : 6031)
using namespace std;

/* -------------------- Typedefs -------------------- */

typedef int itn;
typedef long long ll;
typedef unsigned long long ull;
typedef float fl;
typedef double db;
typedef long double ld;

/* -------------------- Usings -------------------- */

using vi = vector<int>;
using vll = vector<ll>;
using mii = map<int, int>;
using mll = map<ll, ll>;
using pii = pair<int, int>;
using pll = pair<ll, ll>;

/* -------------------- Defines -------------------- */

#define ff first
#define ss second
#define pub push_back
#define pob pop_back
#define puf push_front
#define pof pop_front
#define mpr make_pair
#define yes cout<<"YES\n"
#define no cout<<"NO\n"
#define all(x) (x).begin(), (x).end()

/* -------------------- Constants -------------------- */

const int MAX = int(1e9 + 5);
const ll MAXL = ll(1e18 + 5);
const ll MOD = ll(1e9 + 7);
const ll MOD2 = ll(998244353);

/* -------------------- Functions -------------------- */

void fastio() {
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    return;
}

void precision(int x) {
    cout.setf(ios::fixed | ios::showpoint);
    cout.precision(x);
    return;
}

ull gcd(ull a, ull b) {
    while (b) {
        a %= b;
        swap(a, b);
    }
    return a;
}

ull lcm(ull a, ull b) {
    return a / gcd(a, b) * b;
}

ll range_sum(ll a, ll b) {
    ll dif = a - 1, cnt = b - a + 1;
    ll ans = ((b - a + 1) * (b - a + 2)) / 2;
    ans += ((b - a + 1) * dif);
    return ans;
}

string dec_to_bin(ll a) {
    string s = "";
    for (ll i = a; i > 0; ) {
        ll k = i % 2;
        i /= 2;
        char c = k + 48;
        s += c;
    }
    if (a == 0) {
        s = "0";
    }
    reverse(all(s));
    return s;
}

ll bin_to_dec(string s) {
    ll num = 0;
    for (int i = 0; i < s.size(); i++) {
        num *= 2ll;
        num += (s[i] - '0');
    }
    return num;
}

bool isPrime(ll a) {
    if (a == 1) {
        return false;
    }
    for (ll i = 2; i * i <= a; i++) {
        if (a % i == 0) {
            return false;
        }
    }
    return true;
}

int dig_sum(ll a) {
    int sum = 0;
    while (a) {
        sum += int(a % 10);
        a /= 10;
    }
    return sum;
}

ll binpow(ll a, int b) {
    ll ans = 1;
    while (b) {
        if ((b & 1) == 1) {
            ans *= a;
        }
        b >>= 1;
        a *= a;
    }
    return ans;
}

ll binpow_by_mod(ll a, ll b, ll mod) {
    ll ans = 1;
    while (b) {
        if ((b & 1) == 1) {
            ans *= a;
            ans %= mod;
        }
        b >>= 1;
        a *= a;
        a %= mod;
    }
    return ans;
}

ll factorial(int a) {
    ll ans = 1;
    for (int i = 2; i <= a; i++) {
        ans *= ll(i);
    }
    return ans;
}

ll factorial_by_mod(int a, ll mod) {
    ll ans = 1;
    for (int i = 2; i <= a; i++) {
        ans *= ll(i);
        ans %= mod;
    }
    return ans;
}

/* -------------------- Solution -------------------- */

const int N = 200005;
vi g[N];
set<int> ans[N], id[N];
int a[N], used[N], tin[N], tout[N], l, timer;
vector<vi> up;

void dfs(int u, int v = 0) {
    used[u] = 1, tin[u] = timer++;
    up[u][0] = v;
    for (int i = 1; i <= l; i++) {
        up[u][i] = up[up[u][i - 1]][i - 1];
    }
    for (auto to : g[u]) {
        if (!used[to]) dfs(to, u);
    }
    tout[u] = timer++;
}

bool upper(int u, int v) {
    return tin[u] <= tin[v] && tout[u] >= tin[v];
}

int lca(int u, int v) {
    if (upper(u, v)) return u;
    if (upper(v, u)) return v;
    for (int i = l; i >= 0; i--) {
        if (!upper(up[u][i], v)) u = up[u][i];
    }
    return up[u][0];
}

void slv() {
    int n, m, q;
    cin >> n >> m >> q;
    for (int i = 0; i < n - 1; i++) {
        int x, y;
        cin >> x >> y;
        x--, y--;
        g[x].pub(y);
        g[y].pub(x);
    }
    l = log2(n) + 2;
    up.resize(n);
    for (int i = 0; i < n; i++) up[i].resize(l + 1);
    dfs(0);
    for (int i = 0; i < m; i++) {
        cin >> a[i];
        a[i]--;
        id[a[i]].insert(i);
        if (i > 0) {
            ans[lca(a[i], a[i - 1])].insert(i - 1);
        }
    }
    while (q--) {
        int tp;
        cin >> tp;
        if (tp == 1) {
            int pos, x;
            cin >> pos >> x;
            pos--, x--;
            id[a[pos]].erase(pos);
            id[x].insert(pos);
            if (pos < m - 1) {
                ans[lca(a[pos], a[pos + 1])].erase(pos);
                ans[lca(x, a[pos + 1])].insert(pos);
            }
            if (pos > 0) {
                ans[lca(a[pos], a[pos - 1])].erase(pos - 1);
                ans[lca(x, a[pos - 1])].insert(pos - 1);
            }
            a[pos] = x;
        }
        else {
            int l, r, x;
            cin >> l >> r >> x;
            x--;
            auto lb1 = id[x].lower_bound(l - 1);
            if (lb1 != id[x].end() && *lb1 < r) {
                cout << *lb1 + 1 << " " << *lb1 + 1 << '\n';
                continue;
            }
            auto lb = ans[x].lower_bound(l - 1);
            if (lb == ans[x].end() || *lb >= r - 1) {
                cout << -1 << " " << -1 << '\n';
            }
            else {
                cout << *lb + 1 << " " << *lb + 2 << '\n';
            }
        }
    }
} 

/*
7 7 1000
1 2
2 3
2 4
1 5
5 6
5 7
1 2 3 4 5 6 7
2 1 7 1
*/

void cs() {
    int tstc = 1;
    //cin >> tstc;
    while (tstc--) {
        slv();
    }
}

void precalc() {
    return;
}

int main() {
    fastio();
    precalc();
    //precision(1);
    cs();
    return 0;
}

Compilation message

treearray.cpp:25: warning: ignoring '#pragma warning ' [-Wunknown-pragmas]
   25 | #pragma warning(disable : 4996)
      | 
treearray.cpp:26: warning: ignoring '#pragma warning ' [-Wunknown-pragmas]
   26 | #pragma warning(disable : 6031)
      | 
treearray.cpp: In function 'll range_sum(ll, ll)':
treearray.cpp:95:21: warning: unused variable 'cnt' [-Wunused-variable]
   95 |     ll dif = a - 1, cnt = b - a + 1;
      |                     ^~~
treearray.cpp: In function 'll bin_to_dec(std::string)':
treearray.cpp:118:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  118 |     for (int i = 0; i < s.size(); i++) {
      |                     ~~^~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 26712 KB n=5
2 Correct 5 ms 26712 KB n=100
3 Correct 6 ms 26712 KB n=100
4 Correct 6 ms 26716 KB n=100
5 Correct 6 ms 26712 KB n=100
6 Correct 6 ms 26712 KB n=100
7 Correct 7 ms 26712 KB n=100
8 Correct 8 ms 26712 KB n=100
9 Correct 6 ms 26712 KB n=100
10 Correct 6 ms 26712 KB n=100
11 Correct 6 ms 26712 KB n=100
12 Correct 5 ms 26900 KB n=100
13 Correct 5 ms 26712 KB n=100
14 Correct 6 ms 26712 KB n=100
15 Correct 5 ms 26712 KB n=100
16 Correct 6 ms 26712 KB n=100
17 Correct 6 ms 26712 KB n=100
18 Correct 6 ms 26712 KB n=100
19 Correct 6 ms 26712 KB n=100
20 Correct 6 ms 26716 KB n=100
21 Correct 6 ms 26712 KB n=100
22 Correct 6 ms 26716 KB n=100
23 Correct 6 ms 26712 KB n=100
24 Correct 5 ms 26712 KB n=100
25 Correct 5 ms 26712 KB n=100
26 Correct 6 ms 26716 KB n=12
27 Correct 6 ms 26716 KB n=100
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 26712 KB n=5
2 Correct 5 ms 26712 KB n=100
3 Correct 6 ms 26712 KB n=100
4 Correct 6 ms 26716 KB n=100
5 Correct 6 ms 26712 KB n=100
6 Correct 6 ms 26712 KB n=100
7 Correct 7 ms 26712 KB n=100
8 Correct 8 ms 26712 KB n=100
9 Correct 6 ms 26712 KB n=100
10 Correct 6 ms 26712 KB n=100
11 Correct 6 ms 26712 KB n=100
12 Correct 5 ms 26900 KB n=100
13 Correct 5 ms 26712 KB n=100
14 Correct 6 ms 26712 KB n=100
15 Correct 5 ms 26712 KB n=100
16 Correct 6 ms 26712 KB n=100
17 Correct 6 ms 26712 KB n=100
18 Correct 6 ms 26712 KB n=100
19 Correct 6 ms 26712 KB n=100
20 Correct 6 ms 26716 KB n=100
21 Correct 6 ms 26712 KB n=100
22 Correct 6 ms 26716 KB n=100
23 Correct 6 ms 26712 KB n=100
24 Correct 5 ms 26712 KB n=100
25 Correct 5 ms 26712 KB n=100
26 Correct 6 ms 26716 KB n=12
27 Correct 6 ms 26716 KB n=100
28 Correct 7 ms 26968 KB n=500
29 Correct 6 ms 26968 KB n=500
30 Correct 6 ms 26968 KB n=500
31 Correct 6 ms 26968 KB n=500
32 Correct 6 ms 26968 KB n=500
33 Correct 6 ms 26968 KB n=500
34 Correct 6 ms 26968 KB n=500
35 Correct 6 ms 26968 KB n=500
36 Correct 6 ms 26968 KB n=500
37 Correct 6 ms 26968 KB n=500
38 Correct 6 ms 26968 KB n=500
39 Correct 6 ms 26972 KB n=500
40 Correct 6 ms 26972 KB n=500
41 Correct 6 ms 26968 KB n=500
42 Correct 6 ms 26968 KB n=500
43 Correct 7 ms 26968 KB n=500
44 Correct 6 ms 26968 KB n=500
45 Correct 6 ms 26968 KB n=500
46 Correct 6 ms 27128 KB n=500
47 Correct 6 ms 26972 KB n=500
48 Correct 6 ms 26968 KB n=500
49 Correct 6 ms 26968 KB n=500
50 Correct 6 ms 26968 KB n=500
51 Correct 6 ms 26972 KB n=500
52 Correct 7 ms 26968 KB n=500
53 Correct 6 ms 26980 KB n=500
54 Correct 7 ms 27168 KB n=500
55 Correct 6 ms 26968 KB n=278
56 Correct 6 ms 26968 KB n=500
57 Correct 6 ms 26968 KB n=500
58 Correct 6 ms 26968 KB n=500
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 26712 KB n=5
2 Correct 5 ms 26712 KB n=100
3 Correct 6 ms 26712 KB n=100
4 Correct 6 ms 26716 KB n=100
5 Correct 6 ms 26712 KB n=100
6 Correct 6 ms 26712 KB n=100
7 Correct 7 ms 26712 KB n=100
8 Correct 8 ms 26712 KB n=100
9 Correct 6 ms 26712 KB n=100
10 Correct 6 ms 26712 KB n=100
11 Correct 6 ms 26712 KB n=100
12 Correct 5 ms 26900 KB n=100
13 Correct 5 ms 26712 KB n=100
14 Correct 6 ms 26712 KB n=100
15 Correct 5 ms 26712 KB n=100
16 Correct 6 ms 26712 KB n=100
17 Correct 6 ms 26712 KB n=100
18 Correct 6 ms 26712 KB n=100
19 Correct 6 ms 26712 KB n=100
20 Correct 6 ms 26716 KB n=100
21 Correct 6 ms 26712 KB n=100
22 Correct 6 ms 26716 KB n=100
23 Correct 6 ms 26712 KB n=100
24 Correct 5 ms 26712 KB n=100
25 Correct 5 ms 26712 KB n=100
26 Correct 6 ms 26716 KB n=12
27 Correct 6 ms 26716 KB n=100
28 Correct 7 ms 26968 KB n=500
29 Correct 6 ms 26968 KB n=500
30 Correct 6 ms 26968 KB n=500
31 Correct 6 ms 26968 KB n=500
32 Correct 6 ms 26968 KB n=500
33 Correct 6 ms 26968 KB n=500
34 Correct 6 ms 26968 KB n=500
35 Correct 6 ms 26968 KB n=500
36 Correct 6 ms 26968 KB n=500
37 Correct 6 ms 26968 KB n=500
38 Correct 6 ms 26968 KB n=500
39 Correct 6 ms 26972 KB n=500
40 Correct 6 ms 26972 KB n=500
41 Correct 6 ms 26968 KB n=500
42 Correct 6 ms 26968 KB n=500
43 Correct 7 ms 26968 KB n=500
44 Correct 6 ms 26968 KB n=500
45 Correct 6 ms 26968 KB n=500
46 Correct 6 ms 27128 KB n=500
47 Correct 6 ms 26972 KB n=500
48 Correct 6 ms 26968 KB n=500
49 Correct 6 ms 26968 KB n=500
50 Correct 6 ms 26968 KB n=500
51 Correct 6 ms 26972 KB n=500
52 Correct 7 ms 26968 KB n=500
53 Correct 6 ms 26980 KB n=500
54 Correct 7 ms 27168 KB n=500
55 Correct 6 ms 26968 KB n=278
56 Correct 6 ms 26968 KB n=500
57 Correct 6 ms 26968 KB n=500
58 Correct 6 ms 26968 KB n=500
59 Correct 8 ms 27224 KB n=2000
60 Correct 8 ms 27480 KB n=2000
61 Correct 8 ms 27228 KB n=2000
62 Correct 8 ms 27224 KB n=2000
63 Correct 8 ms 27224 KB n=2000
64 Correct 8 ms 27228 KB n=2000
65 Correct 8 ms 27224 KB n=2000
66 Correct 7 ms 27228 KB n=2000
67 Correct 8 ms 27228 KB n=2000
68 Correct 8 ms 27224 KB n=2000
69 Correct 7 ms 27480 KB n=2000
70 Correct 7 ms 27228 KB n=2000
71 Correct 7 ms 27224 KB n=2000
72 Correct 8 ms 27228 KB n=2000
73 Correct 9 ms 27484 KB n=2000
74 Correct 8 ms 27228 KB n=1844
75 Correct 8 ms 27224 KB n=2000
76 Correct 8 ms 27264 KB n=2000
77 Correct 8 ms 27224 KB n=2000
78 Correct 8 ms 27224 KB n=2000
79 Correct 8 ms 27224 KB n=2000
80 Correct 8 ms 27420 KB n=2000
81 Correct 8 ms 27420 KB n=2000
82 Correct 8 ms 27224 KB n=2000
83 Correct 8 ms 27228 KB n=2000
84 Correct 8 ms 27228 KB n=2000
85 Correct 8 ms 27224 KB n=2000
86 Correct 9 ms 27224 KB n=2000
87 Correct 8 ms 27224 KB n=2000
88 Correct 8 ms 27480 KB n=2000
89 Correct 8 ms 27480 KB n=2000
90 Correct 8 ms 27480 KB n=2000
91 Correct 7 ms 27428 KB n=2000
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 26712 KB n=5
2 Correct 5 ms 26712 KB n=100
3 Correct 6 ms 26712 KB n=100
4 Correct 6 ms 26716 KB n=100
5 Correct 6 ms 26712 KB n=100
6 Correct 6 ms 26712 KB n=100
7 Correct 7 ms 26712 KB n=100
8 Correct 8 ms 26712 KB n=100
9 Correct 6 ms 26712 KB n=100
10 Correct 6 ms 26712 KB n=100
11 Correct 6 ms 26712 KB n=100
12 Correct 5 ms 26900 KB n=100
13 Correct 5 ms 26712 KB n=100
14 Correct 6 ms 26712 KB n=100
15 Correct 5 ms 26712 KB n=100
16 Correct 6 ms 26712 KB n=100
17 Correct 6 ms 26712 KB n=100
18 Correct 6 ms 26712 KB n=100
19 Correct 6 ms 26712 KB n=100
20 Correct 6 ms 26716 KB n=100
21 Correct 6 ms 26712 KB n=100
22 Correct 6 ms 26716 KB n=100
23 Correct 6 ms 26712 KB n=100
24 Correct 5 ms 26712 KB n=100
25 Correct 5 ms 26712 KB n=100
26 Correct 6 ms 26716 KB n=12
27 Correct 6 ms 26716 KB n=100
28 Correct 7 ms 26968 KB n=500
29 Correct 6 ms 26968 KB n=500
30 Correct 6 ms 26968 KB n=500
31 Correct 6 ms 26968 KB n=500
32 Correct 6 ms 26968 KB n=500
33 Correct 6 ms 26968 KB n=500
34 Correct 6 ms 26968 KB n=500
35 Correct 6 ms 26968 KB n=500
36 Correct 6 ms 26968 KB n=500
37 Correct 6 ms 26968 KB n=500
38 Correct 6 ms 26968 KB n=500
39 Correct 6 ms 26972 KB n=500
40 Correct 6 ms 26972 KB n=500
41 Correct 6 ms 26968 KB n=500
42 Correct 6 ms 26968 KB n=500
43 Correct 7 ms 26968 KB n=500
44 Correct 6 ms 26968 KB n=500
45 Correct 6 ms 26968 KB n=500
46 Correct 6 ms 27128 KB n=500
47 Correct 6 ms 26972 KB n=500
48 Correct 6 ms 26968 KB n=500
49 Correct 6 ms 26968 KB n=500
50 Correct 6 ms 26968 KB n=500
51 Correct 6 ms 26972 KB n=500
52 Correct 7 ms 26968 KB n=500
53 Correct 6 ms 26980 KB n=500
54 Correct 7 ms 27168 KB n=500
55 Correct 6 ms 26968 KB n=278
56 Correct 6 ms 26968 KB n=500
57 Correct 6 ms 26968 KB n=500
58 Correct 6 ms 26968 KB n=500
59 Correct 8 ms 27224 KB n=2000
60 Correct 8 ms 27480 KB n=2000
61 Correct 8 ms 27228 KB n=2000
62 Correct 8 ms 27224 KB n=2000
63 Correct 8 ms 27224 KB n=2000
64 Correct 8 ms 27228 KB n=2000
65 Correct 8 ms 27224 KB n=2000
66 Correct 7 ms 27228 KB n=2000
67 Correct 8 ms 27228 KB n=2000
68 Correct 8 ms 27224 KB n=2000
69 Correct 7 ms 27480 KB n=2000
70 Correct 7 ms 27228 KB n=2000
71 Correct 7 ms 27224 KB n=2000
72 Correct 8 ms 27228 KB n=2000
73 Correct 9 ms 27484 KB n=2000
74 Correct 8 ms 27228 KB n=1844
75 Correct 8 ms 27224 KB n=2000
76 Correct 8 ms 27264 KB n=2000
77 Correct 8 ms 27224 KB n=2000
78 Correct 8 ms 27224 KB n=2000
79 Correct 8 ms 27224 KB n=2000
80 Correct 8 ms 27420 KB n=2000
81 Correct 8 ms 27420 KB n=2000
82 Correct 8 ms 27224 KB n=2000
83 Correct 8 ms 27228 KB n=2000
84 Correct 8 ms 27228 KB n=2000
85 Correct 8 ms 27224 KB n=2000
86 Correct 9 ms 27224 KB n=2000
87 Correct 8 ms 27224 KB n=2000
88 Correct 8 ms 27480 KB n=2000
89 Correct 8 ms 27480 KB n=2000
90 Correct 8 ms 27480 KB n=2000
91 Correct 7 ms 27428 KB n=2000
92 Correct 569 ms 84308 KB n=200000
93 Correct 668 ms 88824 KB n=200000
94 Correct 451 ms 92240 KB n=200000
95 Correct 560 ms 83648 KB n=200000
96 Correct 558 ms 83908 KB n=200000
97 Correct 705 ms 88128 KB n=200000
98 Correct 597 ms 83908 KB n=200000
99 Correct 666 ms 84004 KB n=200000
100 Correct 571 ms 83880 KB n=200000
101 Correct 384 ms 93520 KB n=200000
102 Correct 346 ms 85116 KB n=200000
103 Correct 338 ms 85072 KB n=200000
104 Correct 366 ms 85072 KB n=200000
105 Correct 373 ms 85508 KB n=200000
106 Correct 371 ms 85328 KB n=200000
107 Correct 397 ms 85440 KB n=200000
108 Correct 560 ms 84048 KB n=200000
109 Correct 586 ms 83792 KB n=200000
110 Correct 598 ms 83768 KB n=200000
111 Correct 553 ms 83572 KB n=200000
112 Correct 478 ms 92620 KB n=200000
113 Correct 691 ms 87880 KB n=200000
114 Correct 568 ms 83364 KB n=200000
115 Correct 928 ms 86096 KB n=200000
116 Correct 570 ms 83848 KB n=200000
117 Correct 406 ms 92752 KB n=200000
118 Correct 793 ms 86796 KB n=200000
119 Correct 542 ms 83876 KB n=200000
120 Correct 367 ms 92496 KB n=200000
121 Correct 402 ms 92500 KB n=200000
122 Correct 356 ms 92752 KB n=200000
123 Correct 400 ms 85236 KB n=200000
124 Correct 104 ms 42020 KB n=25264