Submission #1079498

# Submission time Handle Problem Language Result Execution time Memory
1079498 2024-08-28T15:52:24 Z Khanhcsp2 Birthday gift (IZhO18_treearray) C++14
56 / 100
2444 ms 83148 KB
#include<bits/stdc++.h>
#define el '\n'
#define fi first
#define sc second
#define int ll
#define pii pair<int, int>
#define all(v) v.begin(), v.end()
using namespace std;
using ll=long long;
using ull=unsigned long long;
using ld=long double;
const int mod=1e9+7;
const int N=1e5+11;
int n, m, q, up[23][N], h[N], nod[4*N], lc[2003][2003], a[N];
vector<int> adj[N];
void dfs(int u, int pa)
{
    up[0][u]=pa;
    for(int i=1; i<=20; i++) up[i][u]=up[i-1][up[i-1][u]];
    for(int v:adj[u])
    {
        if(v!=pa)
        {
            h[v]=h[u]+1;
            dfs(v, u);
        }
    }
}
int jump(int k, int u)
{
    for(int i=0; i<=20; i++)
    {
        if(k>>i&1) u=up[i][u];
    }
    return u;
}
int lca(int u, int v)
{
    if(h[u]<h[v]) swap(u, v);
    u=jump(h[u]-h[v], u);
    if(u==v) return u;
    for(int i=20; i>=0; i--)
    {
        if(up[i][u]!=up[i][v])
        {
            u=up[i][u];
            v=up[i][v];
        }
    }
    return up[0][u];
}
void glow(int id, int l, int r)
{
    if(l==r)
    {
        nod[id]=a[l];
        return;
    }
    int mid=(l+r)/2;
    glow(id*2, l, mid);
    glow(id*2+1, mid+1, r);
    nod[id]=lc[nod[id*2]][nod[id*2+1]];
}
void update(int id, int l, int r, int pos, int val)
{
    if(r<pos||pos<l) return;
    if(l==r && l==pos)
    {
        nod[id]=val;
        return;
    }
    int mid=(l+r)/2;
    if(pos<=mid) update(id*2, l, mid, pos, val);
    else update(id*2+1, mid+1, r, pos, val);
    nod[id]=lc[nod[id*2]][nod[id*2+1]];
}
int get(int id, int l, int r, int u, int v)
{
    if(r<u || v<l) return n+1;
    if(u<=l && r<=v) return nod[id];
    int mid=(l+r)/2;
    int s1=get(id*2, l, mid, u, v);
    int s2=get(id*2+1, mid+1, r, u, v);
    return lc[s1][s2];
}
void sub1()
{
    while(q--)
    {
        int tt;
        cin >> tt;
        if(tt==1)
        {
            int pos, v;
            cin >> pos >> v;
            update(1, 1, m, pos, v);
            a[pos]=v;
        }
        else
        {
            int l, r, v;
            cin >> l >> r >> v;
            int ch=0, s1, s2;
            for(int i=l; i<=r; i++)
            {
                if(h[a[i]]<h[v] || ch) continue;
                int low=i, high=r;
                while(low<=high)
                {
                    int mid=(low+high)/2;
                    int cc=get(1, 1, m, i, mid);
                    if(h[cc]>h[v]) low=mid+1;
                    else if(h[cc]<h[v]) high=mid-1;
                    else if(cc==v)
                    {
                        ch=1;
                        s1=i;
                        s2=mid;
                        break;
                    }
                    else break;
                }
            }
            if(ch) cout << s1 << ' ' << s2 << el;
            else cout << -1 << ' ' << -1 << el;
        }
    }
}
void sol()
{
    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);
    }
    h[1]=1;
    dfs(1, 0);
    for(int i=1; i<=m; i++) cin >> a[i];
    for(int i=1; i<=n; i++) for(int j=i; j<=n; j++) lc[i][j]=lc[j][i]=lca(i, j);
    for(int i=1; i<=n; i++) lc[n+1][i]=lc[i][n+1]=i;
    glow(1, 1, m);
//    cout << get(1, 1, m, 1, 1) << el;
    sub1();
//    cout << lca(5, 3);
}
signed main()
{
//    freopen("divisor.INP", "r", stdin);
//    freopen("divisor.OUT", "w", stdout);
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    int t=1;
    //cin >> t;
    while(t--)
    {
        sol();
    }
}
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2904 KB n=5
2 Correct 2 ms 3420 KB n=100
3 Correct 2 ms 3416 KB n=100
4 Correct 2 ms 3420 KB n=100
5 Correct 3 ms 3416 KB n=100
6 Correct 3 ms 3328 KB n=100
7 Correct 5 ms 3416 KB n=100
8 Correct 2 ms 3416 KB n=100
9 Correct 3 ms 3420 KB n=100
10 Correct 3 ms 3420 KB n=100
11 Correct 2 ms 3420 KB n=100
12 Correct 2 ms 3672 KB n=100
13 Correct 2 ms 3420 KB n=100
14 Correct 3 ms 3420 KB n=100
15 Correct 2 ms 3420 KB n=100
16 Correct 2 ms 3452 KB n=100
17 Correct 3 ms 3420 KB n=100
18 Correct 2 ms 3420 KB n=100
19 Correct 2 ms 3328 KB n=100
20 Correct 2 ms 3420 KB n=100
21 Correct 2 ms 3416 KB n=100
22 Correct 3 ms 3420 KB n=100
23 Correct 4 ms 3324 KB n=100
24 Correct 3 ms 3416 KB n=100
25 Correct 2 ms 3420 KB n=100
26 Correct 2 ms 2908 KB n=12
27 Correct 4 ms 3420 KB n=100
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2904 KB n=5
2 Correct 2 ms 3420 KB n=100
3 Correct 2 ms 3416 KB n=100
4 Correct 2 ms 3420 KB n=100
5 Correct 3 ms 3416 KB n=100
6 Correct 3 ms 3328 KB n=100
7 Correct 5 ms 3416 KB n=100
8 Correct 2 ms 3416 KB n=100
9 Correct 3 ms 3420 KB n=100
10 Correct 3 ms 3420 KB n=100
11 Correct 2 ms 3420 KB n=100
12 Correct 2 ms 3672 KB n=100
13 Correct 2 ms 3420 KB n=100
14 Correct 3 ms 3420 KB n=100
15 Correct 2 ms 3420 KB n=100
16 Correct 2 ms 3452 KB n=100
17 Correct 3 ms 3420 KB n=100
18 Correct 2 ms 3420 KB n=100
19 Correct 2 ms 3328 KB n=100
20 Correct 2 ms 3420 KB n=100
21 Correct 2 ms 3416 KB n=100
22 Correct 3 ms 3420 KB n=100
23 Correct 4 ms 3324 KB n=100
24 Correct 3 ms 3416 KB n=100
25 Correct 2 ms 3420 KB n=100
26 Correct 2 ms 2908 KB n=12
27 Correct 4 ms 3420 KB n=100
28 Correct 11 ms 7000 KB n=500
29 Correct 20 ms 7004 KB n=500
30 Correct 28 ms 7000 KB n=500
31 Correct 19 ms 6932 KB n=500
32 Correct 12 ms 7004 KB n=500
33 Correct 16 ms 7004 KB n=500
34 Correct 11 ms 6920 KB n=500
35 Correct 15 ms 6892 KB n=500
36 Correct 113 ms 7004 KB n=500
37 Correct 108 ms 7104 KB n=500
38 Correct 124 ms 7004 KB n=500
39 Correct 59 ms 7000 KB n=500
40 Correct 41 ms 7036 KB n=500
41 Correct 40 ms 7000 KB n=500
42 Correct 71 ms 7008 KB n=500
43 Correct 75 ms 7000 KB n=500
44 Correct 61 ms 7000 KB n=500
45 Correct 11 ms 7004 KB n=500
46 Correct 15 ms 6924 KB n=500
47 Correct 19 ms 7004 KB n=500
48 Correct 17 ms 7000 KB n=500
49 Correct 14 ms 7004 KB n=500
50 Correct 33 ms 6848 KB n=500
51 Correct 20 ms 7004 KB n=500
52 Correct 28 ms 7036 KB n=500
53 Correct 19 ms 7000 KB n=500
54 Correct 23 ms 7040 KB n=500
55 Correct 8 ms 4700 KB n=278
56 Correct 83 ms 7000 KB n=500
57 Correct 102 ms 7252 KB n=500
58 Correct 38 ms 7000 KB n=500
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2904 KB n=5
2 Correct 2 ms 3420 KB n=100
3 Correct 2 ms 3416 KB n=100
4 Correct 2 ms 3420 KB n=100
5 Correct 3 ms 3416 KB n=100
6 Correct 3 ms 3328 KB n=100
7 Correct 5 ms 3416 KB n=100
8 Correct 2 ms 3416 KB n=100
9 Correct 3 ms 3420 KB n=100
10 Correct 3 ms 3420 KB n=100
11 Correct 2 ms 3420 KB n=100
12 Correct 2 ms 3672 KB n=100
13 Correct 2 ms 3420 KB n=100
14 Correct 3 ms 3420 KB n=100
15 Correct 2 ms 3420 KB n=100
16 Correct 2 ms 3452 KB n=100
17 Correct 3 ms 3420 KB n=100
18 Correct 2 ms 3420 KB n=100
19 Correct 2 ms 3328 KB n=100
20 Correct 2 ms 3420 KB n=100
21 Correct 2 ms 3416 KB n=100
22 Correct 3 ms 3420 KB n=100
23 Correct 4 ms 3324 KB n=100
24 Correct 3 ms 3416 KB n=100
25 Correct 2 ms 3420 KB n=100
26 Correct 2 ms 2908 KB n=12
27 Correct 4 ms 3420 KB n=100
28 Correct 11 ms 7000 KB n=500
29 Correct 20 ms 7004 KB n=500
30 Correct 28 ms 7000 KB n=500
31 Correct 19 ms 6932 KB n=500
32 Correct 12 ms 7004 KB n=500
33 Correct 16 ms 7004 KB n=500
34 Correct 11 ms 6920 KB n=500
35 Correct 15 ms 6892 KB n=500
36 Correct 113 ms 7004 KB n=500
37 Correct 108 ms 7104 KB n=500
38 Correct 124 ms 7004 KB n=500
39 Correct 59 ms 7000 KB n=500
40 Correct 41 ms 7036 KB n=500
41 Correct 40 ms 7000 KB n=500
42 Correct 71 ms 7008 KB n=500
43 Correct 75 ms 7000 KB n=500
44 Correct 61 ms 7000 KB n=500
45 Correct 11 ms 7004 KB n=500
46 Correct 15 ms 6924 KB n=500
47 Correct 19 ms 7004 KB n=500
48 Correct 17 ms 7000 KB n=500
49 Correct 14 ms 7004 KB n=500
50 Correct 33 ms 6848 KB n=500
51 Correct 20 ms 7004 KB n=500
52 Correct 28 ms 7036 KB n=500
53 Correct 19 ms 7000 KB n=500
54 Correct 23 ms 7040 KB n=500
55 Correct 8 ms 4700 KB n=278
56 Correct 83 ms 7000 KB n=500
57 Correct 102 ms 7252 KB n=500
58 Correct 38 ms 7000 KB n=500
59 Correct 181 ms 34640 KB n=2000
60 Correct 385 ms 34644 KB n=2000
61 Correct 395 ms 34640 KB n=2000
62 Correct 328 ms 34836 KB n=2000
63 Correct 235 ms 34640 KB n=2000
64 Correct 344 ms 34928 KB n=2000
65 Correct 163 ms 34692 KB n=2000
66 Correct 434 ms 34856 KB n=2000
67 Correct 193 ms 34752 KB n=2000
68 Correct 343 ms 34604 KB n=2000
69 Correct 2444 ms 34796 KB n=2000
70 Correct 2244 ms 34800 KB n=2000
71 Correct 2267 ms 34572 KB n=2000
72 Correct 954 ms 34640 KB n=2000
73 Correct 890 ms 34796 KB n=2000
74 Correct 137 ms 32280 KB n=1844
75 Correct 951 ms 34812 KB n=2000
76 Correct 1208 ms 34792 KB n=2000
77 Correct 1212 ms 34796 KB n=2000
78 Correct 1278 ms 34788 KB n=2000
79 Correct 159 ms 34640 KB n=2000
80 Correct 311 ms 34640 KB n=2000
81 Correct 300 ms 34592 KB n=2000
82 Correct 157 ms 34640 KB n=2000
83 Correct 306 ms 34712 KB n=2000
84 Correct 544 ms 34900 KB n=2000
85 Correct 417 ms 34640 KB n=2000
86 Correct 385 ms 34816 KB n=2000
87 Correct 514 ms 34728 KB n=2000
88 Correct 2353 ms 34872 KB n=2000
89 Correct 2202 ms 34644 KB n=2000
90 Correct 413 ms 34640 KB n=2000
91 Correct 1279 ms 34812 KB n=2000
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2904 KB n=5
2 Correct 2 ms 3420 KB n=100
3 Correct 2 ms 3416 KB n=100
4 Correct 2 ms 3420 KB n=100
5 Correct 3 ms 3416 KB n=100
6 Correct 3 ms 3328 KB n=100
7 Correct 5 ms 3416 KB n=100
8 Correct 2 ms 3416 KB n=100
9 Correct 3 ms 3420 KB n=100
10 Correct 3 ms 3420 KB n=100
11 Correct 2 ms 3420 KB n=100
12 Correct 2 ms 3672 KB n=100
13 Correct 2 ms 3420 KB n=100
14 Correct 3 ms 3420 KB n=100
15 Correct 2 ms 3420 KB n=100
16 Correct 2 ms 3452 KB n=100
17 Correct 3 ms 3420 KB n=100
18 Correct 2 ms 3420 KB n=100
19 Correct 2 ms 3328 KB n=100
20 Correct 2 ms 3420 KB n=100
21 Correct 2 ms 3416 KB n=100
22 Correct 3 ms 3420 KB n=100
23 Correct 4 ms 3324 KB n=100
24 Correct 3 ms 3416 KB n=100
25 Correct 2 ms 3420 KB n=100
26 Correct 2 ms 2908 KB n=12
27 Correct 4 ms 3420 KB n=100
28 Correct 11 ms 7000 KB n=500
29 Correct 20 ms 7004 KB n=500
30 Correct 28 ms 7000 KB n=500
31 Correct 19 ms 6932 KB n=500
32 Correct 12 ms 7004 KB n=500
33 Correct 16 ms 7004 KB n=500
34 Correct 11 ms 6920 KB n=500
35 Correct 15 ms 6892 KB n=500
36 Correct 113 ms 7004 KB n=500
37 Correct 108 ms 7104 KB n=500
38 Correct 124 ms 7004 KB n=500
39 Correct 59 ms 7000 KB n=500
40 Correct 41 ms 7036 KB n=500
41 Correct 40 ms 7000 KB n=500
42 Correct 71 ms 7008 KB n=500
43 Correct 75 ms 7000 KB n=500
44 Correct 61 ms 7000 KB n=500
45 Correct 11 ms 7004 KB n=500
46 Correct 15 ms 6924 KB n=500
47 Correct 19 ms 7004 KB n=500
48 Correct 17 ms 7000 KB n=500
49 Correct 14 ms 7004 KB n=500
50 Correct 33 ms 6848 KB n=500
51 Correct 20 ms 7004 KB n=500
52 Correct 28 ms 7036 KB n=500
53 Correct 19 ms 7000 KB n=500
54 Correct 23 ms 7040 KB n=500
55 Correct 8 ms 4700 KB n=278
56 Correct 83 ms 7000 KB n=500
57 Correct 102 ms 7252 KB n=500
58 Correct 38 ms 7000 KB n=500
59 Correct 181 ms 34640 KB n=2000
60 Correct 385 ms 34644 KB n=2000
61 Correct 395 ms 34640 KB n=2000
62 Correct 328 ms 34836 KB n=2000
63 Correct 235 ms 34640 KB n=2000
64 Correct 344 ms 34928 KB n=2000
65 Correct 163 ms 34692 KB n=2000
66 Correct 434 ms 34856 KB n=2000
67 Correct 193 ms 34752 KB n=2000
68 Correct 343 ms 34604 KB n=2000
69 Correct 2444 ms 34796 KB n=2000
70 Correct 2244 ms 34800 KB n=2000
71 Correct 2267 ms 34572 KB n=2000
72 Correct 954 ms 34640 KB n=2000
73 Correct 890 ms 34796 KB n=2000
74 Correct 137 ms 32280 KB n=1844
75 Correct 951 ms 34812 KB n=2000
76 Correct 1208 ms 34792 KB n=2000
77 Correct 1212 ms 34796 KB n=2000
78 Correct 1278 ms 34788 KB n=2000
79 Correct 159 ms 34640 KB n=2000
80 Correct 311 ms 34640 KB n=2000
81 Correct 300 ms 34592 KB n=2000
82 Correct 157 ms 34640 KB n=2000
83 Correct 306 ms 34712 KB n=2000
84 Correct 544 ms 34900 KB n=2000
85 Correct 417 ms 34640 KB n=2000
86 Correct 385 ms 34816 KB n=2000
87 Correct 514 ms 34728 KB n=2000
88 Correct 2353 ms 34872 KB n=2000
89 Correct 2202 ms 34644 KB n=2000
90 Correct 413 ms 34640 KB n=2000
91 Correct 1279 ms 34812 KB n=2000
92 Runtime error 126 ms 83148 KB Execution killed with signal 11
93 Halted 0 ms 0 KB -