답안 #543031

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
543031 2022-03-28T22:09:24 Z Danilo21 Birthday gift (IZhO18_treearray) C++14
56 / 100
4000 ms 43820 KB
#include <bits/stdc++.h>

#define ll long long
#define ld long double
#define pb push_back
#define fi first
#define se second
#define en '\n'
#define sp ' '
#define tb '\t'
#define ri(n) int n; cin >> n
#define rl(n) ll n; cin >> n
#define rs(s) string s; cin >> s
#define rc(c) char c; cin >> c
#define rv(v) for (auto &x : v) cin >> x
#define pven(v) for (auto x : v) cout << x << en
#define pv(v) for (auto x : v) cout << x << sp; cout << en
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
#define yes cout << "YES" << en
#define no cout << "NO" << en
#define smin(a, b) a = min(a, b)
#define smax(a, b) a = max(a, b)
#define ssort(a, b) if (a < b) swap(a, b)
#define bitcnt(a) __builtin_popcountll(a)
#define bithigh(a) 63-__builtin_clzll(a)
#define lg bithigh
ll highpow(ll a) { return 1LL << (ll)lg(a); }

using namespace std;

class Lca{
private:
    int n;
    vector<vector<int> > g, par;
    vector<int> in, out, depth;

    int dfs(int s, int p = -1, int d = 0, int t = 0){
        par[s][0] = p;
        in[s] = t;
        depth[s] = d;
        for (int u : g[s])
            if (u^p)
                t = dfs(u, s, d+1, t+1);
        return out[s] = t;
    }

public:
    Lca(int n = 0){
        this->n = n;
        g = vector<vector<int> >(n);
        par = vector<vector<int> >(n, vector<int>(lg(n)+1, -1));
        in = out = depth = vector<int>(n);
    }

    void edge(int u, int v){
        g[u].pb(v);
        g[v].pb(u);
    }

    void build(int root = 0){
        dfs(root);
        for (int d = 1; (1<<d) <= n; d++)
            for (int s = 0; s < n; s++)
                if (~par[s][d-1])
                    par[s][d] = par[par[s][d-1]][d-1];
    }

    int Par(int s, int d) const {
        if (!d) return s;
        return Par(par[s][lg(d)], d - highpow(d));
    }

    bool Ancestor(int s, int p) const {
        return in[s] >= in[p] && in[s] <= out[p];
    }

    int lca(int u, int v){
        if (depth[u] > depth[v]) swap(u, v);
        if (Ancestor(v, u)) return u;
        v = Par(v, depth[v] - depth[u]);
        for (int d = lg(n); ~d; d--){
            if (par[u][d]^par[v][d]){
                u = par[u][d];
                v = par[v][d];
            }
        }
        return par[u][0];
    }
};

Lca* lc;

struct node{

    int s;
    node(){ s = -1; }
    node(int u, int v){ s = lc->lca(u, v); }

    static node op(const node& a, const node& b){
        if (!~a.s) return b;
        if (!~b.s) return a;
        return node(a.s, b.s);
    }
};

struct seg{
    int s, l, r;
    seg(){ s = l = r = -1; }
    seg(int _s, int _l, int _r): s(_s), l(_l), r(_r){}
    static seg concate(const seg& a, const seg& b, bool f){
        if (!~a.s){
            if (!~b.s || !f) return seg();
            return b;
        }
        if (!~b.s){
            if (f) return seg();
            return a;
        }
        if (a.r+1 != b.l){
            if (f) return b;
            return a;
        }
        return seg(lc->lca(a.s, b.s), a.l, b.r);
    }
};

struct response{
    seg left, right, sol;
    bool solved = 0;
    response(){}
    response(int u, int l, int r, int s){
        left = right = seg(u, l, r);
        if (u==s){ sol = seg(u, l, r); solved = 1; }
    }
    response(const response& a, const response& b, int s){
        left = seg::concate(a.left, b.left, 0);
        right = seg::concate(a.right, b.right, 1);
        if (a.solved) sol = a.sol;
        if (b.solved) sol = b.sol;
        if (left.s == s) sol = left;
        if (right.s == s) sol = right;
        seg t = seg::concate(a.right, b.left, 0);
        if (t.s == s) sol = t;
        if (~sol.s) solved = 1;
    }
};

class segtree{
private:
    int n;
    vector<node> tree;

    void init(int n){
        this->n = highpow(n);
        if (bitcnt(n)>1) this->n <<= 1;
        tree = vector<node>(2*this->n);
    }

    node build(int s, int l, int r, int* arr){
        if (l==r) return tree[s] = node(arr[l], arr[l]);
        int m = (l + r)>>1;
        node a = build(2*s, l, m, arr);
        node b = build(2*s+1, m+1, r, arr);
        return tree[s] = node::op(a, b);
    }

    node update(int s, int l, int r, int pos, int u){
        if (pos < l || pos > r) return tree[s];
        if (l==r) return tree[s] = node(u, u);
        int m = (l + r)>>1;
        node a = update(2*s, l, m, pos, u);
        node b = update(2*s+1, m+1, r, pos, u);
        return tree[s] = node::op(a, b);
    }

    response query(int s, int l, int r, int ql, int qr, int u) const {
        if (ql > r || qr < l) return response();
        if (!lc->Ancestor(u, tree[s].s) && !lc->Ancestor(tree[s].s, u)) return response();
        if (l >= ql && r <= qr && lc->Ancestor(tree[s].s, u))
            return response(tree[s].s, l, r, u);
        if (l == r) return response();
        int m = (l + r)>>1;
        auto a = query(2*s, l, m, ql, qr, u);
        auto b = query(2*s+1, m+1, r, ql, qr, u);
        return response(a, b, u);
    }
public:
    segtree(int n, int* arr){ init(n); build(1, 0, this->n-1, arr); }
    void update(int pos, int u){ update(1, 0, n-1, pos, u); }
    array<int, 2> query(int l, int r, int u){
        auto q = query(1, 0, n-1, l, r, u);
        if (!q.solved) return {-2, -2};
        return {q.sol.l, q.sol.r};
    }
};

const ll LINF = 4e18;
const int mxN = 2e5+10, INF = 2e9, mod = (1 ? 1e9+7 : 998244353);
int n, m, q, a[2*mxN];
segtree* st;

void Solve(){

    cin >> n >> m >> q;
    lc = new Lca(n);
    for (int i = 1; i < n; i++){
        ri(u); ri(v);
        u--; v--;
        lc->edge(u, v);
    }
    lc->build();
    memset(a, -1, sizeof(a));
    for (int i = 0; i < m; i++)
        cin >> a[i], a[i]--;
    st = new segtree(m, a);
    while (q--){
        ri(t); t--;
        if (t){
            ri(l); ri(r); ri(s);
            l--; r--; s--;
            auto ans = st->query(l, r, s);
            cout << ans[0]+1 << sp << ans[1]+1 << en;
        }
        else{
            ri(i); ri(s);
            i--; s--;
            st->update(i, s);
        }
    }
}

int main(){

    ios::sync_with_stdio(false);
    cin.tie(0); cout.tie(0); cerr.tie(0);
    cout << setprecision(12) << fixed;
    cerr << setprecision(12) << fixed;
    cerr << "Started!" << endl;

    int t = 1;
    //cin >> t;
    while (t--)
        Solve();

    return 0;
}

Compilation message

treearray.cpp: In function 'long long int highpow(long long int)':
treearray.cpp:26:22: warning: suggest parentheses around '-' inside '<<' [-Wparentheses]
   26 | #define bithigh(a) 63-__builtin_clzll(a)
      |                      ^
treearray.cpp:27:12: note: in expansion of macro 'bithigh'
   27 | #define lg bithigh
      |            ^~~~~~~
treearray.cpp:28:38: note: in expansion of macro 'lg'
   28 | ll highpow(ll a) { return 1LL << (ll)lg(a); }
      |                                      ^~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 1876 KB n=5
2 Correct 1 ms 1876 KB n=100
3 Correct 1 ms 1868 KB n=100
4 Correct 1 ms 1876 KB n=100
5 Correct 1 ms 1868 KB n=100
6 Correct 2 ms 1876 KB n=100
7 Correct 2 ms 1876 KB n=100
8 Correct 1 ms 1868 KB n=100
9 Correct 1 ms 1876 KB n=100
10 Correct 2 ms 1876 KB n=100
11 Correct 1 ms 1876 KB n=100
12 Correct 1 ms 1868 KB n=100
13 Correct 1 ms 1876 KB n=100
14 Correct 1 ms 1876 KB n=100
15 Correct 1 ms 1876 KB n=100
16 Correct 2 ms 1876 KB n=100
17 Correct 2 ms 1876 KB n=100
18 Correct 1 ms 1868 KB n=100
19 Correct 1 ms 1876 KB n=100
20 Correct 2 ms 1876 KB n=100
21 Correct 1 ms 1864 KB n=100
22 Correct 1 ms 1876 KB n=100
23 Correct 1 ms 1876 KB n=100
24 Correct 1 ms 1868 KB n=100
25 Correct 1 ms 1876 KB n=100
26 Correct 1 ms 1868 KB n=12
27 Correct 1 ms 1876 KB n=100
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 1876 KB n=5
2 Correct 1 ms 1876 KB n=100
3 Correct 1 ms 1868 KB n=100
4 Correct 1 ms 1876 KB n=100
5 Correct 1 ms 1868 KB n=100
6 Correct 2 ms 1876 KB n=100
7 Correct 2 ms 1876 KB n=100
8 Correct 1 ms 1868 KB n=100
9 Correct 1 ms 1876 KB n=100
10 Correct 2 ms 1876 KB n=100
11 Correct 1 ms 1876 KB n=100
12 Correct 1 ms 1868 KB n=100
13 Correct 1 ms 1876 KB n=100
14 Correct 1 ms 1876 KB n=100
15 Correct 1 ms 1876 KB n=100
16 Correct 2 ms 1876 KB n=100
17 Correct 2 ms 1876 KB n=100
18 Correct 1 ms 1868 KB n=100
19 Correct 1 ms 1876 KB n=100
20 Correct 2 ms 1876 KB n=100
21 Correct 1 ms 1864 KB n=100
22 Correct 1 ms 1876 KB n=100
23 Correct 1 ms 1876 KB n=100
24 Correct 1 ms 1868 KB n=100
25 Correct 1 ms 1876 KB n=100
26 Correct 1 ms 1868 KB n=12
27 Correct 1 ms 1876 KB n=100
28 Correct 2 ms 1876 KB n=500
29 Correct 3 ms 1876 KB n=500
30 Correct 2 ms 1880 KB n=500
31 Correct 3 ms 1876 KB n=500
32 Correct 3 ms 1876 KB n=500
33 Correct 3 ms 1876 KB n=500
34 Correct 2 ms 1876 KB n=500
35 Correct 3 ms 1876 KB n=500
36 Correct 10 ms 1876 KB n=500
37 Correct 8 ms 1876 KB n=500
38 Correct 7 ms 1988 KB n=500
39 Correct 3 ms 1876 KB n=500
40 Correct 3 ms 1876 KB n=500
41 Correct 3 ms 1988 KB n=500
42 Correct 4 ms 1876 KB n=500
43 Correct 4 ms 1876 KB n=500
44 Correct 6 ms 1876 KB n=500
45 Correct 2 ms 1876 KB n=500
46 Correct 3 ms 1876 KB n=500
47 Correct 3 ms 1876 KB n=500
48 Correct 2 ms 1880 KB n=500
49 Correct 2 ms 1876 KB n=500
50 Correct 3 ms 1976 KB n=500
51 Correct 3 ms 1876 KB n=500
52 Correct 3 ms 1876 KB n=500
53 Correct 3 ms 1876 KB n=500
54 Correct 2 ms 1876 KB n=500
55 Correct 2 ms 1876 KB n=278
56 Correct 2 ms 1876 KB n=500
57 Correct 2 ms 1876 KB n=500
58 Correct 5 ms 1876 KB n=500
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 1876 KB n=5
2 Correct 1 ms 1876 KB n=100
3 Correct 1 ms 1868 KB n=100
4 Correct 1 ms 1876 KB n=100
5 Correct 1 ms 1868 KB n=100
6 Correct 2 ms 1876 KB n=100
7 Correct 2 ms 1876 KB n=100
8 Correct 1 ms 1868 KB n=100
9 Correct 1 ms 1876 KB n=100
10 Correct 2 ms 1876 KB n=100
11 Correct 1 ms 1876 KB n=100
12 Correct 1 ms 1868 KB n=100
13 Correct 1 ms 1876 KB n=100
14 Correct 1 ms 1876 KB n=100
15 Correct 1 ms 1876 KB n=100
16 Correct 2 ms 1876 KB n=100
17 Correct 2 ms 1876 KB n=100
18 Correct 1 ms 1868 KB n=100
19 Correct 1 ms 1876 KB n=100
20 Correct 2 ms 1876 KB n=100
21 Correct 1 ms 1864 KB n=100
22 Correct 1 ms 1876 KB n=100
23 Correct 1 ms 1876 KB n=100
24 Correct 1 ms 1868 KB n=100
25 Correct 1 ms 1876 KB n=100
26 Correct 1 ms 1868 KB n=12
27 Correct 1 ms 1876 KB n=100
28 Correct 2 ms 1876 KB n=500
29 Correct 3 ms 1876 KB n=500
30 Correct 2 ms 1880 KB n=500
31 Correct 3 ms 1876 KB n=500
32 Correct 3 ms 1876 KB n=500
33 Correct 3 ms 1876 KB n=500
34 Correct 2 ms 1876 KB n=500
35 Correct 3 ms 1876 KB n=500
36 Correct 10 ms 1876 KB n=500
37 Correct 8 ms 1876 KB n=500
38 Correct 7 ms 1988 KB n=500
39 Correct 3 ms 1876 KB n=500
40 Correct 3 ms 1876 KB n=500
41 Correct 3 ms 1988 KB n=500
42 Correct 4 ms 1876 KB n=500
43 Correct 4 ms 1876 KB n=500
44 Correct 6 ms 1876 KB n=500
45 Correct 2 ms 1876 KB n=500
46 Correct 3 ms 1876 KB n=500
47 Correct 3 ms 1876 KB n=500
48 Correct 2 ms 1880 KB n=500
49 Correct 2 ms 1876 KB n=500
50 Correct 3 ms 1976 KB n=500
51 Correct 3 ms 1876 KB n=500
52 Correct 3 ms 1876 KB n=500
53 Correct 3 ms 1876 KB n=500
54 Correct 2 ms 1876 KB n=500
55 Correct 2 ms 1876 KB n=278
56 Correct 2 ms 1876 KB n=500
57 Correct 2 ms 1876 KB n=500
58 Correct 5 ms 1876 KB n=500
59 Correct 4 ms 2260 KB n=2000
60 Correct 24 ms 2336 KB n=2000
61 Correct 20 ms 2260 KB n=2000
62 Correct 22 ms 2288 KB n=2000
63 Correct 5 ms 2260 KB n=2000
64 Correct 32 ms 2292 KB n=2000
65 Correct 4 ms 2260 KB n=2000
66 Correct 21 ms 2316 KB n=2000
67 Correct 15 ms 2260 KB n=2000
68 Correct 22 ms 2276 KB n=2000
69 Correct 118 ms 2264 KB n=2000
70 Correct 87 ms 2260 KB n=2000
71 Correct 88 ms 2388 KB n=2000
72 Correct 26 ms 2260 KB n=2000
73 Correct 27 ms 2276 KB n=2000
74 Correct 3 ms 2132 KB n=1844
75 Correct 27 ms 2260 KB n=2000
76 Correct 47 ms 2260 KB n=2000
77 Correct 46 ms 2268 KB n=2000
78 Correct 64 ms 2260 KB n=2000
79 Correct 5 ms 2260 KB n=2000
80 Correct 19 ms 2320 KB n=2000
81 Correct 19 ms 2260 KB n=2000
82 Correct 3 ms 2264 KB n=2000
83 Correct 17 ms 2260 KB n=2000
84 Correct 22 ms 2308 KB n=2000
85 Correct 23 ms 2260 KB n=2000
86 Correct 25 ms 2260 KB n=2000
87 Correct 22 ms 2260 KB n=2000
88 Correct 4 ms 2260 KB n=2000
89 Correct 4 ms 2260 KB n=2000
90 Correct 4 ms 2260 KB n=2000
91 Correct 54 ms 2276 KB n=2000
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 1876 KB n=5
2 Correct 1 ms 1876 KB n=100
3 Correct 1 ms 1868 KB n=100
4 Correct 1 ms 1876 KB n=100
5 Correct 1 ms 1868 KB n=100
6 Correct 2 ms 1876 KB n=100
7 Correct 2 ms 1876 KB n=100
8 Correct 1 ms 1868 KB n=100
9 Correct 1 ms 1876 KB n=100
10 Correct 2 ms 1876 KB n=100
11 Correct 1 ms 1876 KB n=100
12 Correct 1 ms 1868 KB n=100
13 Correct 1 ms 1876 KB n=100
14 Correct 1 ms 1876 KB n=100
15 Correct 1 ms 1876 KB n=100
16 Correct 2 ms 1876 KB n=100
17 Correct 2 ms 1876 KB n=100
18 Correct 1 ms 1868 KB n=100
19 Correct 1 ms 1876 KB n=100
20 Correct 2 ms 1876 KB n=100
21 Correct 1 ms 1864 KB n=100
22 Correct 1 ms 1876 KB n=100
23 Correct 1 ms 1876 KB n=100
24 Correct 1 ms 1868 KB n=100
25 Correct 1 ms 1876 KB n=100
26 Correct 1 ms 1868 KB n=12
27 Correct 1 ms 1876 KB n=100
28 Correct 2 ms 1876 KB n=500
29 Correct 3 ms 1876 KB n=500
30 Correct 2 ms 1880 KB n=500
31 Correct 3 ms 1876 KB n=500
32 Correct 3 ms 1876 KB n=500
33 Correct 3 ms 1876 KB n=500
34 Correct 2 ms 1876 KB n=500
35 Correct 3 ms 1876 KB n=500
36 Correct 10 ms 1876 KB n=500
37 Correct 8 ms 1876 KB n=500
38 Correct 7 ms 1988 KB n=500
39 Correct 3 ms 1876 KB n=500
40 Correct 3 ms 1876 KB n=500
41 Correct 3 ms 1988 KB n=500
42 Correct 4 ms 1876 KB n=500
43 Correct 4 ms 1876 KB n=500
44 Correct 6 ms 1876 KB n=500
45 Correct 2 ms 1876 KB n=500
46 Correct 3 ms 1876 KB n=500
47 Correct 3 ms 1876 KB n=500
48 Correct 2 ms 1880 KB n=500
49 Correct 2 ms 1876 KB n=500
50 Correct 3 ms 1976 KB n=500
51 Correct 3 ms 1876 KB n=500
52 Correct 3 ms 1876 KB n=500
53 Correct 3 ms 1876 KB n=500
54 Correct 2 ms 1876 KB n=500
55 Correct 2 ms 1876 KB n=278
56 Correct 2 ms 1876 KB n=500
57 Correct 2 ms 1876 KB n=500
58 Correct 5 ms 1876 KB n=500
59 Correct 4 ms 2260 KB n=2000
60 Correct 24 ms 2336 KB n=2000
61 Correct 20 ms 2260 KB n=2000
62 Correct 22 ms 2288 KB n=2000
63 Correct 5 ms 2260 KB n=2000
64 Correct 32 ms 2292 KB n=2000
65 Correct 4 ms 2260 KB n=2000
66 Correct 21 ms 2316 KB n=2000
67 Correct 15 ms 2260 KB n=2000
68 Correct 22 ms 2276 KB n=2000
69 Correct 118 ms 2264 KB n=2000
70 Correct 87 ms 2260 KB n=2000
71 Correct 88 ms 2388 KB n=2000
72 Correct 26 ms 2260 KB n=2000
73 Correct 27 ms 2276 KB n=2000
74 Correct 3 ms 2132 KB n=1844
75 Correct 27 ms 2260 KB n=2000
76 Correct 47 ms 2260 KB n=2000
77 Correct 46 ms 2268 KB n=2000
78 Correct 64 ms 2260 KB n=2000
79 Correct 5 ms 2260 KB n=2000
80 Correct 19 ms 2320 KB n=2000
81 Correct 19 ms 2260 KB n=2000
82 Correct 3 ms 2264 KB n=2000
83 Correct 17 ms 2260 KB n=2000
84 Correct 22 ms 2308 KB n=2000
85 Correct 23 ms 2260 KB n=2000
86 Correct 25 ms 2260 KB n=2000
87 Correct 22 ms 2260 KB n=2000
88 Correct 4 ms 2260 KB n=2000
89 Correct 4 ms 2260 KB n=2000
90 Correct 4 ms 2260 KB n=2000
91 Correct 54 ms 2276 KB n=2000
92 Execution timed out 4034 ms 43820 KB Time limit exceeded
93 Halted 0 ms 0 KB -