Submission #1094409

# Submission time Handle Problem Language Result Execution time Memory
1094409 2024-09-29T15:01:22 Z cpptowin Birthday gift (IZhO18_treearray) C++17
100 / 100
681 ms 122448 KB
#include <bits/stdc++.h>
#define fo(i, d, c) for (int i = d; i <= c; i++)
#define fod(i, c, d) for (int i = c; i >= d; i--)
#define maxn 500010
#define N 1010
#define fi first
#define se second
#define pb emplace_back
#define en cout << "\n";
#define inf (int)1e18
#define bitcount(x) __builtin_popcountll(x)
#define pii pair<int, int>
#define vii vector<pii>
#define lb(x) x & -x
#define bit(i, j) ((i >> j) & 1)
#define offbit(i, j) (i ^ (1LL << j))
#define onbit(i, j) (i | (1LL << j))
#define vi vector<int>
#define all(x) x.begin(), x.end()
#define ss(x) (int)x.size()
template <typename T1, typename T2>
bool minimize(T1 &a, T2 b)
{
    if (a > b)
    {
        a = b;
        return true;
    }
    return false;
}
template <typename T1, typename T2>
bool maximize(T1 &a, T2 b)
{
    if (a < b)
    {
        a = b;
        return true;
    }
    return false;
}
using namespace std;
const int nsqrt = 450;
const int mod = 1e9 + 7;
void add(int &x, int k)
{
    x += k;
    x %= mod;
    if(x < 0) x += mod;
}
void del(int &x, int k)
{
    x -= k;
    x %= mod;
    if(x < 0) x += mod;
}
int n,m,q,a[maxn],h[maxn],par[maxn][20];
vi ke[maxn];
set<int> s1[maxn],s[maxn];
void dfs(int u)
{
    for(int v : ke[u]) if(v != par[u][0])
    {
        h[v] = h[u] + 1;
        par[v][0] = u;
        fo(i,1,19) par[v][i] = par[par[v][i - 1]][i - 1];
        dfs(v);
    }
}
int lca(int u, int v)
{
    if (h[u] < h[v])
        swap(u, v);
    int del = h[u] - h[v];
    fod(i, 19, 0) if (bit(del, i)) u = par[u][i];
    if (u == v)
        return u;
    fod(i, 19, 0) if (par[u][i] != par[v][i]) u = par[u][i], v = par[v][i];
    return par[u][0];
}
main()
{
#define name "bai1"
    if (fopen(name ".inp", "r"))
    {
        freopen(name ".inp", "r", stdin);
        freopen(name ".out", "w", stdout);
    }
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cin >> n >> m >> q;
    fo(i,1,n - 1)
    {
        int u,v;
        cin >> u >> v;
        ke[u].pb(v);
        ke[v].pb(u);
    }
    dfs(1);
    fo(i,1,m)
    {
        cin >> a[i];
        s[a[i]].insert(i);
    }
    fo(i,1,m - 1)  s1[lca(a[i],a[i + 1])].insert(i);
    while(q--)
    {
        int t;
        cin >> t;
        if(t == 1)
        {
            int i,u;
            cin >> i >> u;
            s[a[i]].erase(i);
            if(i < m)
            s1[lca(a[i],a[i + 1])].erase(i);
            if(i > 1)
            s1[lca(a[i - 1],a[i])].erase(i - 1);
            a[i] = u;
            s[a[i]].insert(i);
            s1[lca(a[i],a[i + 1])].insert(i);
            if(i > 1)
            s1[lca(a[i - 1],a[i])].insert(i - 1);
        }
        else
        {
            int l,r,u;
            cin >> l >> r >> u;
            auto it = s[u].lower_bound(l);
            pii ans = {-1,-1};
            if(it != s[u].end() and *it <= r) ans = {*it,*it};
            else
            {
                it = s1[u].lower_bound(l);
                if(it != s1[u].end() and *it < r) ans = {*it,*it + 1};
            }
            cout << ans.fi << ' ' << ans.se << "\n";
        }
    }
}

Compilation message

treearray.cpp:80:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
   80 | main()
      | ^~~~
treearray.cpp: In function 'int main()':
treearray.cpp:85:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   85 |         freopen(name ".inp", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
treearray.cpp:86:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   86 |         freopen(name ".out", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 22 ms 58972 KB n=5
2 Correct 27 ms 59312 KB n=100
3 Correct 23 ms 58968 KB n=100
4 Correct 23 ms 58968 KB n=100
5 Correct 23 ms 58972 KB n=100
6 Correct 23 ms 58972 KB n=100
7 Correct 25 ms 59144 KB n=100
8 Correct 23 ms 59140 KB n=100
9 Correct 23 ms 59124 KB n=100
10 Correct 23 ms 59224 KB n=100
11 Correct 23 ms 58972 KB n=100
12 Correct 23 ms 58972 KB n=100
13 Correct 23 ms 58972 KB n=100
14 Correct 34 ms 59028 KB n=100
15 Correct 27 ms 58972 KB n=100
16 Correct 25 ms 59176 KB n=100
17 Correct 27 ms 59012 KB n=100
18 Correct 27 ms 58972 KB n=100
19 Correct 25 ms 58972 KB n=100
20 Correct 28 ms 58972 KB n=100
21 Correct 25 ms 58972 KB n=100
22 Correct 25 ms 58972 KB n=100
23 Correct 27 ms 58960 KB n=100
24 Correct 26 ms 58972 KB n=100
25 Correct 26 ms 58972 KB n=100
26 Correct 30 ms 59092 KB n=12
27 Correct 29 ms 59176 KB n=100
# Verdict Execution time Memory Grader output
1 Correct 22 ms 58972 KB n=5
2 Correct 27 ms 59312 KB n=100
3 Correct 23 ms 58968 KB n=100
4 Correct 23 ms 58968 KB n=100
5 Correct 23 ms 58972 KB n=100
6 Correct 23 ms 58972 KB n=100
7 Correct 25 ms 59144 KB n=100
8 Correct 23 ms 59140 KB n=100
9 Correct 23 ms 59124 KB n=100
10 Correct 23 ms 59224 KB n=100
11 Correct 23 ms 58972 KB n=100
12 Correct 23 ms 58972 KB n=100
13 Correct 23 ms 58972 KB n=100
14 Correct 34 ms 59028 KB n=100
15 Correct 27 ms 58972 KB n=100
16 Correct 25 ms 59176 KB n=100
17 Correct 27 ms 59012 KB n=100
18 Correct 27 ms 58972 KB n=100
19 Correct 25 ms 58972 KB n=100
20 Correct 28 ms 58972 KB n=100
21 Correct 25 ms 58972 KB n=100
22 Correct 25 ms 58972 KB n=100
23 Correct 27 ms 58960 KB n=100
24 Correct 26 ms 58972 KB n=100
25 Correct 26 ms 58972 KB n=100
26 Correct 30 ms 59092 KB n=12
27 Correct 29 ms 59176 KB n=100
28 Correct 24 ms 59200 KB n=500
29 Correct 25 ms 59228 KB n=500
30 Correct 25 ms 59228 KB n=500
31 Correct 23 ms 59228 KB n=500
32 Correct 23 ms 59172 KB n=500
33 Correct 22 ms 59228 KB n=500
34 Correct 25 ms 59228 KB n=500
35 Correct 26 ms 59228 KB n=500
36 Correct 33 ms 59180 KB n=500
37 Correct 27 ms 59164 KB n=500
38 Correct 28 ms 59228 KB n=500
39 Correct 26 ms 59224 KB n=500
40 Correct 30 ms 59228 KB n=500
41 Correct 27 ms 59224 KB n=500
42 Correct 26 ms 59224 KB n=500
43 Correct 26 ms 59216 KB n=500
44 Correct 26 ms 59216 KB n=500
45 Correct 30 ms 59096 KB n=500
46 Correct 28 ms 59228 KB n=500
47 Correct 27 ms 59228 KB n=500
48 Correct 26 ms 59228 KB n=500
49 Correct 26 ms 59228 KB n=500
50 Correct 26 ms 59176 KB n=500
51 Correct 25 ms 59316 KB n=500
52 Correct 26 ms 59296 KB n=500
53 Correct 26 ms 59224 KB n=500
54 Correct 28 ms 59220 KB n=500
55 Correct 27 ms 59220 KB n=278
56 Correct 26 ms 59216 KB n=500
57 Correct 26 ms 59228 KB n=500
58 Correct 28 ms 59220 KB n=500
# Verdict Execution time Memory Grader output
1 Correct 22 ms 58972 KB n=5
2 Correct 27 ms 59312 KB n=100
3 Correct 23 ms 58968 KB n=100
4 Correct 23 ms 58968 KB n=100
5 Correct 23 ms 58972 KB n=100
6 Correct 23 ms 58972 KB n=100
7 Correct 25 ms 59144 KB n=100
8 Correct 23 ms 59140 KB n=100
9 Correct 23 ms 59124 KB n=100
10 Correct 23 ms 59224 KB n=100
11 Correct 23 ms 58972 KB n=100
12 Correct 23 ms 58972 KB n=100
13 Correct 23 ms 58972 KB n=100
14 Correct 34 ms 59028 KB n=100
15 Correct 27 ms 58972 KB n=100
16 Correct 25 ms 59176 KB n=100
17 Correct 27 ms 59012 KB n=100
18 Correct 27 ms 58972 KB n=100
19 Correct 25 ms 58972 KB n=100
20 Correct 28 ms 58972 KB n=100
21 Correct 25 ms 58972 KB n=100
22 Correct 25 ms 58972 KB n=100
23 Correct 27 ms 58960 KB n=100
24 Correct 26 ms 58972 KB n=100
25 Correct 26 ms 58972 KB n=100
26 Correct 30 ms 59092 KB n=12
27 Correct 29 ms 59176 KB n=100
28 Correct 24 ms 59200 KB n=500
29 Correct 25 ms 59228 KB n=500
30 Correct 25 ms 59228 KB n=500
31 Correct 23 ms 59228 KB n=500
32 Correct 23 ms 59172 KB n=500
33 Correct 22 ms 59228 KB n=500
34 Correct 25 ms 59228 KB n=500
35 Correct 26 ms 59228 KB n=500
36 Correct 33 ms 59180 KB n=500
37 Correct 27 ms 59164 KB n=500
38 Correct 28 ms 59228 KB n=500
39 Correct 26 ms 59224 KB n=500
40 Correct 30 ms 59228 KB n=500
41 Correct 27 ms 59224 KB n=500
42 Correct 26 ms 59224 KB n=500
43 Correct 26 ms 59216 KB n=500
44 Correct 26 ms 59216 KB n=500
45 Correct 30 ms 59096 KB n=500
46 Correct 28 ms 59228 KB n=500
47 Correct 27 ms 59228 KB n=500
48 Correct 26 ms 59228 KB n=500
49 Correct 26 ms 59228 KB n=500
50 Correct 26 ms 59176 KB n=500
51 Correct 25 ms 59316 KB n=500
52 Correct 26 ms 59296 KB n=500
53 Correct 26 ms 59224 KB n=500
54 Correct 28 ms 59220 KB n=500
55 Correct 27 ms 59220 KB n=278
56 Correct 26 ms 59216 KB n=500
57 Correct 26 ms 59228 KB n=500
58 Correct 28 ms 59220 KB n=500
59 Correct 28 ms 59480 KB n=2000
60 Correct 35 ms 59544 KB n=2000
61 Correct 30 ms 59984 KB n=2000
62 Correct 29 ms 59484 KB n=2000
63 Correct 30 ms 59476 KB n=2000
64 Correct 29 ms 59480 KB n=2000
65 Correct 30 ms 59476 KB n=2000
66 Correct 31 ms 59540 KB n=2000
67 Correct 30 ms 59568 KB n=2000
68 Correct 30 ms 59736 KB n=2000
69 Correct 31 ms 59484 KB n=2000
70 Correct 28 ms 59484 KB n=2000
71 Correct 27 ms 59476 KB n=2000
72 Correct 28 ms 59480 KB n=2000
73 Correct 27 ms 59484 KB n=2000
74 Correct 29 ms 59576 KB n=1844
75 Correct 29 ms 59484 KB n=2000
76 Correct 28 ms 59472 KB n=2000
77 Correct 29 ms 59476 KB n=2000
78 Correct 29 ms 59484 KB n=2000
79 Correct 29 ms 59472 KB n=2000
80 Correct 29 ms 59728 KB n=2000
81 Correct 35 ms 59480 KB n=2000
82 Correct 29 ms 59484 KB n=2000
83 Correct 30 ms 59740 KB n=2000
84 Correct 29 ms 59676 KB n=2000
85 Correct 33 ms 59476 KB n=2000
86 Correct 30 ms 59644 KB n=2000
87 Correct 30 ms 59480 KB n=2000
88 Correct 30 ms 59728 KB n=2000
89 Correct 30 ms 59740 KB n=2000
90 Correct 31 ms 59740 KB n=2000
91 Correct 33 ms 59484 KB n=2000
# Verdict Execution time Memory Grader output
1 Correct 22 ms 58972 KB n=5
2 Correct 27 ms 59312 KB n=100
3 Correct 23 ms 58968 KB n=100
4 Correct 23 ms 58968 KB n=100
5 Correct 23 ms 58972 KB n=100
6 Correct 23 ms 58972 KB n=100
7 Correct 25 ms 59144 KB n=100
8 Correct 23 ms 59140 KB n=100
9 Correct 23 ms 59124 KB n=100
10 Correct 23 ms 59224 KB n=100
11 Correct 23 ms 58972 KB n=100
12 Correct 23 ms 58972 KB n=100
13 Correct 23 ms 58972 KB n=100
14 Correct 34 ms 59028 KB n=100
15 Correct 27 ms 58972 KB n=100
16 Correct 25 ms 59176 KB n=100
17 Correct 27 ms 59012 KB n=100
18 Correct 27 ms 58972 KB n=100
19 Correct 25 ms 58972 KB n=100
20 Correct 28 ms 58972 KB n=100
21 Correct 25 ms 58972 KB n=100
22 Correct 25 ms 58972 KB n=100
23 Correct 27 ms 58960 KB n=100
24 Correct 26 ms 58972 KB n=100
25 Correct 26 ms 58972 KB n=100
26 Correct 30 ms 59092 KB n=12
27 Correct 29 ms 59176 KB n=100
28 Correct 24 ms 59200 KB n=500
29 Correct 25 ms 59228 KB n=500
30 Correct 25 ms 59228 KB n=500
31 Correct 23 ms 59228 KB n=500
32 Correct 23 ms 59172 KB n=500
33 Correct 22 ms 59228 KB n=500
34 Correct 25 ms 59228 KB n=500
35 Correct 26 ms 59228 KB n=500
36 Correct 33 ms 59180 KB n=500
37 Correct 27 ms 59164 KB n=500
38 Correct 28 ms 59228 KB n=500
39 Correct 26 ms 59224 KB n=500
40 Correct 30 ms 59228 KB n=500
41 Correct 27 ms 59224 KB n=500
42 Correct 26 ms 59224 KB n=500
43 Correct 26 ms 59216 KB n=500
44 Correct 26 ms 59216 KB n=500
45 Correct 30 ms 59096 KB n=500
46 Correct 28 ms 59228 KB n=500
47 Correct 27 ms 59228 KB n=500
48 Correct 26 ms 59228 KB n=500
49 Correct 26 ms 59228 KB n=500
50 Correct 26 ms 59176 KB n=500
51 Correct 25 ms 59316 KB n=500
52 Correct 26 ms 59296 KB n=500
53 Correct 26 ms 59224 KB n=500
54 Correct 28 ms 59220 KB n=500
55 Correct 27 ms 59220 KB n=278
56 Correct 26 ms 59216 KB n=500
57 Correct 26 ms 59228 KB n=500
58 Correct 28 ms 59220 KB n=500
59 Correct 28 ms 59480 KB n=2000
60 Correct 35 ms 59544 KB n=2000
61 Correct 30 ms 59984 KB n=2000
62 Correct 29 ms 59484 KB n=2000
63 Correct 30 ms 59476 KB n=2000
64 Correct 29 ms 59480 KB n=2000
65 Correct 30 ms 59476 KB n=2000
66 Correct 31 ms 59540 KB n=2000
67 Correct 30 ms 59568 KB n=2000
68 Correct 30 ms 59736 KB n=2000
69 Correct 31 ms 59484 KB n=2000
70 Correct 28 ms 59484 KB n=2000
71 Correct 27 ms 59476 KB n=2000
72 Correct 28 ms 59480 KB n=2000
73 Correct 27 ms 59484 KB n=2000
74 Correct 29 ms 59576 KB n=1844
75 Correct 29 ms 59484 KB n=2000
76 Correct 28 ms 59472 KB n=2000
77 Correct 29 ms 59476 KB n=2000
78 Correct 29 ms 59484 KB n=2000
79 Correct 29 ms 59472 KB n=2000
80 Correct 29 ms 59728 KB n=2000
81 Correct 35 ms 59480 KB n=2000
82 Correct 29 ms 59484 KB n=2000
83 Correct 30 ms 59740 KB n=2000
84 Correct 29 ms 59676 KB n=2000
85 Correct 33 ms 59476 KB n=2000
86 Correct 30 ms 59644 KB n=2000
87 Correct 30 ms 59480 KB n=2000
88 Correct 30 ms 59728 KB n=2000
89 Correct 30 ms 59740 KB n=2000
90 Correct 31 ms 59740 KB n=2000
91 Correct 33 ms 59484 KB n=2000
92 Correct 454 ms 109728 KB n=200000
93 Correct 574 ms 116208 KB n=200000
94 Correct 572 ms 120656 KB n=200000
95 Correct 459 ms 109548 KB n=200000
96 Correct 442 ms 109680 KB n=200000
97 Correct 571 ms 115028 KB n=200000
98 Correct 462 ms 109620 KB n=200000
99 Correct 510 ms 109912 KB n=200000
100 Correct 440 ms 109816 KB n=200000
101 Correct 548 ms 122448 KB n=200000
102 Correct 272 ms 110928 KB n=200000
103 Correct 281 ms 110932 KB n=200000
104 Correct 276 ms 110932 KB n=200000
105 Correct 276 ms 111364 KB n=200000
106 Correct 268 ms 111160 KB n=200000
107 Correct 285 ms 111184 KB n=200000
108 Correct 504 ms 109652 KB n=200000
109 Correct 510 ms 109724 KB n=200000
110 Correct 493 ms 109648 KB n=200000
111 Correct 447 ms 109124 KB n=200000
112 Correct 573 ms 121168 KB n=200000
113 Correct 582 ms 114992 KB n=200000
114 Correct 417 ms 109252 KB n=200000
115 Correct 661 ms 112208 KB n=200000
116 Correct 509 ms 109764 KB n=200000
117 Correct 662 ms 121536 KB n=200000
118 Correct 567 ms 113496 KB n=200000
119 Correct 472 ms 109768 KB n=200000
120 Correct 681 ms 121424 KB n=200000
121 Correct 531 ms 121484 KB n=200000
122 Correct 501 ms 121672 KB n=200000
123 Correct 281 ms 110928 KB n=200000
124 Correct 132 ms 74320 KB n=25264