Submission #339028

# Submission time Handle Problem Language Result Execution time Memory
339028 2020-12-24T11:59:21 Z hackxsaras Birthday gift (IZhO18_treearray) C++14
100 / 100
1361 ms 78188 KB
#include "bits/stdc++.h"
// #include "chrono"

using namespace std;
// using namespace std::chrono; 
 
// #pragma GCC target ("avx2")
// #pragma GCC optimization ("O3")
// #pragma GCC optimization ("unroll-loops")
// #pragma optimization_level 3
// #pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math,O3")
// #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")

#define pb push_back
#define galen_colin {ios_base::sync_with_stdio(false);}
#define orz {cin.tie(NULL); cout.tie(NULL);}
#define fix(prec) {cout << setprecision(prec) << fixed;}
#define mp make_pair
#define f first
#define s second
#define all(v) v.begin(), v.end()

typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<pii> vpi;
typedef vector<pll> vpl;
typedef vector<vi> vvi;
typedef vector<vl> vvl;

template<typename A> ostream& operator<<(ostream &cout, vector<A> const &v);
template<typename A, typename B> ostream& operator<<(ostream &cout, pair<A, B> const &p) { return cout << "(" << p.f << ", " << p.s << ")"; }
template<typename A> ostream& operator<<(ostream &cout, vector<A> const &v) {
    cout << ""; for(int i = 0; i < v.size(); i++) {if (i) cout << " "; cout << v[i];} return cout << "\n";
}
template<typename A, typename B> istream& operator>>(istream& cin, pair<A, B> &p) {
    cin >> p.first;
    return cin >> p.second;
}
template<typename A> istream& operator>>(istream& cin, vector<A> &v) {
    for(auto &x:v)cin>>x;
    return cin;
}

ll min(ll a, int b){return min(a, (ll) b);}
ll min(int a, ll b){return min(b, (ll) a);}
ll max(ll a, int b){return max(a, (ll) b);}
ll max(int a, ll b){return max(b, (ll) a);}

void usaco(string filename) {
  // #pragma message("be careful, freopen may be wrong")
    freopen((filename + ".in").c_str(), "r", stdin);
    freopen((filename + ".out").c_str(), "w", stdout);
}
 
const ld pi = 3.14159265358979323846;
const ll mod = 1000000007;
// const ll mod = 998244353;
// ll mod;
const ll INF = 1e9+7;
 

ll n, m, k, q, l, r, x, y, z;

const ll sz = 2e5 + 1;
string s, t;

const int maxk = 20;

// int up[maxk][sz];
// int timer = 0;
// int tin[sz], tout[sz];
// vi adj[sz];

// void pre(int u, int v){
    
//     tin[u] = timer++;
    
//     up[0][u] = v;

//     for(int i=1;i<maxk;i++){
//         up[i][u] = up[i-1][up[i-1][u]];
//     }

//     for(int i:adj[u]){
//         if(i!=v){
//             pre(i, u);
//         }
//     }

//     tout[u] = timer++;
// }
// bool isancestor(int x, int y){//ask x is ancestor of y ?
//     if(tin[y] > tin[x] && tout[y] < tout[x]) return true;
//     return false;
// }
// int lca(int u, int v){
//     if(u == -1)return v;
//     if(v == -1)return u;
//     if(isancestor(u, v))return u;
//     if(isancestor(v, u))return v;

//     for(int i = maxk-1;i >= 0;i--){
//         if(isancestor(up[u][i], y))continue;
//         u = up[u][i];
//     }
//     return up[u][0];
// }

vvi up(sz, vi(maxk, -1));
vi adj[sz];
int timer = 0;
vi tin(sz), tout(sz);
void pre(int node, int par){    tin[node] = timer++;    up[node][0] = par;    for(int i=1;i<maxk;i++){        int p = up[node][i-1];        up[node][i] = up[p][i-1];    }    for(int i:adj[node]){        if(i != par){            pre(i, node);        }    }    tout[node] = timer++;}bool isancestor(int u,int v){    return tin[u] <= tin[v] && tout[u] >= tout[v];}int lca(int u, int v){    if(isancestor(u, v))return u;    if(isancestor(v, u))return v;    for(int i = maxk-1;i >= 0; i--){        if(!isancestor(up[u][i], v)){            u = up[u][i];        }    }    return up[u][0];}


set<pair<int,int>> ans [sz];

void solve(int tc = 0) {
    
    cin>>n>>m>>q;
    

    for(int i=0;i<n-1;i++){
        int u, v;
        cin>>u>>v;
        u--, v--;
        adj[u].pb(v);
        adj[v].pb(u);
    }
    pre(0,0);
    vi a(m);
    for(int i=0;i<m;i++){
        cin>>a[i];
        a[i]--;
    }

    for(int i=0;i<m;i++){
        ans[a[i]].insert({0,i});
        if(i!=m-1)ans[lca(a[i], a[i+1])].insert({1, i});
    }

    for(int i=0;i<q;i++){
        cin>>x;
        if(x == 1){
            cin>>x>>y;
            x--;
            if(x!=m-1){
                int o = lca(a[x], a[x+1]);
                ans[o].erase(ans[o].find({1, x}));
            }
            if(x!=0){
                int o = lca(a[x], a[x-1]);
                ans[o].erase(ans[o].find({1, x-1}));
            }

                ans[a[x]].erase(ans[a[x]].find({0,x}));

            a[x] = y-1;

            if(x!=m-1)
                ans[lca(a[x], a[x+1])].insert({1, x});
            if(x!=0)
                ans[lca(a[x], a[x-1])].insert({1, x-1});

                ans[a[x]].insert({0,x});
        } else {
            int v;
            cin>>x>>y>>v;
            x--, y--, v--;
            auto itr = ans[v].lower_bound({0,x});
            if(itr != ans[v].end()){
                if( (*itr).f == 0 && (*itr).s <= y){
                    cout << (*itr).s+1 << " " << (*itr).s+1 << "\n";
                    continue;
                }
            }
            y--;
            itr = ans[v].lower_bound({1, x});
            if(itr != ans[v].end()){
                if( (*itr).s <= y){
                    cout<< (*itr).s+1 << " " << (*itr).s+2 << "\n";
                    continue;
                }
            }
            cout<<"-1 -1\n";
        }
    }
}
 
int main() {
    #ifdef HACKX
        // auto begin = std::chrono::high_resolution_clock::now();
    #endif
    
    galen_colin orz
    
    // usaco("cowland");
 
    int tc = 1;
    // cin >> tc;
    for (int t = 0; t < tc; t++) solve(t);
    
    #ifdef HACKX
        // auto end = std::chrono::high_resolution_clock::now();
        // cout << setprecision(4) << fixed;
        // cout << "Execution time: " << std::chrono::duration_cast<std::chrono::duration<double>>(end - begin).count() << " seconds" << endl;
    #endif
} 

Compilation message

treearray.cpp: In function 'void solve(int)':
treearray.cpp:167:13: warning: this 'if' clause does not guard... [-Wmisleading-indentation]
  167 |             if(x!=0)
      |             ^~
treearray.cpp:170:17: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'if'
  170 |                 ans[a[x]].insert({0,x});
      |                 ^~~
treearray.cpp: In function 'void usaco(std::string)':
treearray.cpp:56:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
   56 |     freopen((filename + ".in").c_str(), "r", stdin);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
treearray.cpp:57:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
   57 |     freopen((filename + ".out").c_str(), "w", stdout);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 32 ms 39532 KB n=5
2 Correct 32 ms 39532 KB n=100
3 Correct 32 ms 39532 KB n=100
4 Correct 33 ms 39532 KB n=100
5 Correct 33 ms 39532 KB n=100
6 Correct 39 ms 39532 KB n=100
7 Correct 38 ms 39532 KB n=100
8 Correct 32 ms 39532 KB n=100
9 Correct 32 ms 39532 KB n=100
10 Correct 33 ms 39532 KB n=100
11 Correct 33 ms 39532 KB n=100
12 Correct 34 ms 39532 KB n=100
13 Correct 33 ms 39552 KB n=100
14 Correct 34 ms 39532 KB n=100
15 Correct 32 ms 39552 KB n=100
16 Correct 32 ms 39532 KB n=100
17 Correct 32 ms 39532 KB n=100
18 Correct 32 ms 39532 KB n=100
19 Correct 33 ms 39532 KB n=100
20 Correct 33 ms 39532 KB n=100
21 Correct 33 ms 39532 KB n=100
22 Correct 38 ms 39532 KB n=100
23 Correct 32 ms 39532 KB n=100
24 Correct 32 ms 39532 KB n=100
25 Correct 33 ms 39532 KB n=100
26 Correct 34 ms 39532 KB n=12
27 Correct 32 ms 39532 KB n=100
# Verdict Execution time Memory Grader output
1 Correct 32 ms 39532 KB n=5
2 Correct 32 ms 39532 KB n=100
3 Correct 32 ms 39532 KB n=100
4 Correct 33 ms 39532 KB n=100
5 Correct 33 ms 39532 KB n=100
6 Correct 39 ms 39532 KB n=100
7 Correct 38 ms 39532 KB n=100
8 Correct 32 ms 39532 KB n=100
9 Correct 32 ms 39532 KB n=100
10 Correct 33 ms 39532 KB n=100
11 Correct 33 ms 39532 KB n=100
12 Correct 34 ms 39532 KB n=100
13 Correct 33 ms 39552 KB n=100
14 Correct 34 ms 39532 KB n=100
15 Correct 32 ms 39552 KB n=100
16 Correct 32 ms 39532 KB n=100
17 Correct 32 ms 39532 KB n=100
18 Correct 32 ms 39532 KB n=100
19 Correct 33 ms 39532 KB n=100
20 Correct 33 ms 39532 KB n=100
21 Correct 33 ms 39532 KB n=100
22 Correct 38 ms 39532 KB n=100
23 Correct 32 ms 39532 KB n=100
24 Correct 32 ms 39532 KB n=100
25 Correct 33 ms 39532 KB n=100
26 Correct 34 ms 39532 KB n=12
27 Correct 32 ms 39532 KB n=100
28 Correct 33 ms 39532 KB n=500
29 Correct 33 ms 39532 KB n=500
30 Correct 34 ms 39532 KB n=500
31 Correct 33 ms 39532 KB n=500
32 Correct 33 ms 39532 KB n=500
33 Correct 33 ms 39532 KB n=500
34 Correct 35 ms 39532 KB n=500
35 Correct 33 ms 39660 KB n=500
36 Correct 33 ms 39532 KB n=500
37 Correct 34 ms 39532 KB n=500
38 Correct 33 ms 39668 KB n=500
39 Correct 34 ms 39660 KB n=500
40 Correct 33 ms 39532 KB n=500
41 Correct 32 ms 39532 KB n=500
42 Correct 34 ms 39532 KB n=500
43 Correct 33 ms 39532 KB n=500
44 Correct 33 ms 39532 KB n=500
45 Correct 33 ms 39532 KB n=500
46 Correct 33 ms 39532 KB n=500
47 Correct 33 ms 39532 KB n=500
48 Correct 35 ms 39532 KB n=500
49 Correct 35 ms 39532 KB n=500
50 Correct 34 ms 39532 KB n=500
51 Correct 41 ms 39680 KB n=500
52 Correct 33 ms 39532 KB n=500
53 Correct 33 ms 39532 KB n=500
54 Correct 34 ms 39532 KB n=500
55 Correct 32 ms 39532 KB n=278
56 Correct 34 ms 39532 KB n=500
57 Correct 34 ms 39532 KB n=500
58 Correct 32 ms 39532 KB n=500
# Verdict Execution time Memory Grader output
1 Correct 32 ms 39532 KB n=5
2 Correct 32 ms 39532 KB n=100
3 Correct 32 ms 39532 KB n=100
4 Correct 33 ms 39532 KB n=100
5 Correct 33 ms 39532 KB n=100
6 Correct 39 ms 39532 KB n=100
7 Correct 38 ms 39532 KB n=100
8 Correct 32 ms 39532 KB n=100
9 Correct 32 ms 39532 KB n=100
10 Correct 33 ms 39532 KB n=100
11 Correct 33 ms 39532 KB n=100
12 Correct 34 ms 39532 KB n=100
13 Correct 33 ms 39552 KB n=100
14 Correct 34 ms 39532 KB n=100
15 Correct 32 ms 39552 KB n=100
16 Correct 32 ms 39532 KB n=100
17 Correct 32 ms 39532 KB n=100
18 Correct 32 ms 39532 KB n=100
19 Correct 33 ms 39532 KB n=100
20 Correct 33 ms 39532 KB n=100
21 Correct 33 ms 39532 KB n=100
22 Correct 38 ms 39532 KB n=100
23 Correct 32 ms 39532 KB n=100
24 Correct 32 ms 39532 KB n=100
25 Correct 33 ms 39532 KB n=100
26 Correct 34 ms 39532 KB n=12
27 Correct 32 ms 39532 KB n=100
28 Correct 33 ms 39532 KB n=500
29 Correct 33 ms 39532 KB n=500
30 Correct 34 ms 39532 KB n=500
31 Correct 33 ms 39532 KB n=500
32 Correct 33 ms 39532 KB n=500
33 Correct 33 ms 39532 KB n=500
34 Correct 35 ms 39532 KB n=500
35 Correct 33 ms 39660 KB n=500
36 Correct 33 ms 39532 KB n=500
37 Correct 34 ms 39532 KB n=500
38 Correct 33 ms 39668 KB n=500
39 Correct 34 ms 39660 KB n=500
40 Correct 33 ms 39532 KB n=500
41 Correct 32 ms 39532 KB n=500
42 Correct 34 ms 39532 KB n=500
43 Correct 33 ms 39532 KB n=500
44 Correct 33 ms 39532 KB n=500
45 Correct 33 ms 39532 KB n=500
46 Correct 33 ms 39532 KB n=500
47 Correct 33 ms 39532 KB n=500
48 Correct 35 ms 39532 KB n=500
49 Correct 35 ms 39532 KB n=500
50 Correct 34 ms 39532 KB n=500
51 Correct 41 ms 39680 KB n=500
52 Correct 33 ms 39532 KB n=500
53 Correct 33 ms 39532 KB n=500
54 Correct 34 ms 39532 KB n=500
55 Correct 32 ms 39532 KB n=278
56 Correct 34 ms 39532 KB n=500
57 Correct 34 ms 39532 KB n=500
58 Correct 32 ms 39532 KB n=500
59 Correct 36 ms 39788 KB n=2000
60 Correct 35 ms 39916 KB n=2000
61 Correct 36 ms 39788 KB n=2000
62 Correct 40 ms 39788 KB n=2000
63 Correct 37 ms 39788 KB n=2000
64 Correct 36 ms 39788 KB n=2000
65 Correct 37 ms 39808 KB n=2000
66 Correct 38 ms 39916 KB n=2000
67 Correct 36 ms 39788 KB n=2000
68 Correct 36 ms 39788 KB n=2000
69 Correct 35 ms 39788 KB n=2000
70 Correct 36 ms 39788 KB n=2000
71 Correct 35 ms 39788 KB n=2000
72 Correct 35 ms 39788 KB n=2000
73 Correct 35 ms 39788 KB n=2000
74 Correct 37 ms 39916 KB n=1844
75 Correct 35 ms 39788 KB n=2000
76 Correct 36 ms 39788 KB n=2000
77 Correct 36 ms 39788 KB n=2000
78 Correct 38 ms 39916 KB n=2000
79 Correct 36 ms 39788 KB n=2000
80 Correct 35 ms 39916 KB n=2000
81 Correct 36 ms 39788 KB n=2000
82 Correct 37 ms 39788 KB n=2000
83 Correct 35 ms 39916 KB n=2000
84 Correct 35 ms 39788 KB n=2000
85 Correct 36 ms 39788 KB n=2000
86 Correct 43 ms 39788 KB n=2000
87 Correct 36 ms 39788 KB n=2000
88 Correct 35 ms 39916 KB n=2000
89 Correct 35 ms 39916 KB n=2000
90 Correct 35 ms 39916 KB n=2000
91 Correct 36 ms 39788 KB n=2000
# Verdict Execution time Memory Grader output
1 Correct 32 ms 39532 KB n=5
2 Correct 32 ms 39532 KB n=100
3 Correct 32 ms 39532 KB n=100
4 Correct 33 ms 39532 KB n=100
5 Correct 33 ms 39532 KB n=100
6 Correct 39 ms 39532 KB n=100
7 Correct 38 ms 39532 KB n=100
8 Correct 32 ms 39532 KB n=100
9 Correct 32 ms 39532 KB n=100
10 Correct 33 ms 39532 KB n=100
11 Correct 33 ms 39532 KB n=100
12 Correct 34 ms 39532 KB n=100
13 Correct 33 ms 39552 KB n=100
14 Correct 34 ms 39532 KB n=100
15 Correct 32 ms 39552 KB n=100
16 Correct 32 ms 39532 KB n=100
17 Correct 32 ms 39532 KB n=100
18 Correct 32 ms 39532 KB n=100
19 Correct 33 ms 39532 KB n=100
20 Correct 33 ms 39532 KB n=100
21 Correct 33 ms 39532 KB n=100
22 Correct 38 ms 39532 KB n=100
23 Correct 32 ms 39532 KB n=100
24 Correct 32 ms 39532 KB n=100
25 Correct 33 ms 39532 KB n=100
26 Correct 34 ms 39532 KB n=12
27 Correct 32 ms 39532 KB n=100
28 Correct 33 ms 39532 KB n=500
29 Correct 33 ms 39532 KB n=500
30 Correct 34 ms 39532 KB n=500
31 Correct 33 ms 39532 KB n=500
32 Correct 33 ms 39532 KB n=500
33 Correct 33 ms 39532 KB n=500
34 Correct 35 ms 39532 KB n=500
35 Correct 33 ms 39660 KB n=500
36 Correct 33 ms 39532 KB n=500
37 Correct 34 ms 39532 KB n=500
38 Correct 33 ms 39668 KB n=500
39 Correct 34 ms 39660 KB n=500
40 Correct 33 ms 39532 KB n=500
41 Correct 32 ms 39532 KB n=500
42 Correct 34 ms 39532 KB n=500
43 Correct 33 ms 39532 KB n=500
44 Correct 33 ms 39532 KB n=500
45 Correct 33 ms 39532 KB n=500
46 Correct 33 ms 39532 KB n=500
47 Correct 33 ms 39532 KB n=500
48 Correct 35 ms 39532 KB n=500
49 Correct 35 ms 39532 KB n=500
50 Correct 34 ms 39532 KB n=500
51 Correct 41 ms 39680 KB n=500
52 Correct 33 ms 39532 KB n=500
53 Correct 33 ms 39532 KB n=500
54 Correct 34 ms 39532 KB n=500
55 Correct 32 ms 39532 KB n=278
56 Correct 34 ms 39532 KB n=500
57 Correct 34 ms 39532 KB n=500
58 Correct 32 ms 39532 KB n=500
59 Correct 36 ms 39788 KB n=2000
60 Correct 35 ms 39916 KB n=2000
61 Correct 36 ms 39788 KB n=2000
62 Correct 40 ms 39788 KB n=2000
63 Correct 37 ms 39788 KB n=2000
64 Correct 36 ms 39788 KB n=2000
65 Correct 37 ms 39808 KB n=2000
66 Correct 38 ms 39916 KB n=2000
67 Correct 36 ms 39788 KB n=2000
68 Correct 36 ms 39788 KB n=2000
69 Correct 35 ms 39788 KB n=2000
70 Correct 36 ms 39788 KB n=2000
71 Correct 35 ms 39788 KB n=2000
72 Correct 35 ms 39788 KB n=2000
73 Correct 35 ms 39788 KB n=2000
74 Correct 37 ms 39916 KB n=1844
75 Correct 35 ms 39788 KB n=2000
76 Correct 36 ms 39788 KB n=2000
77 Correct 36 ms 39788 KB n=2000
78 Correct 38 ms 39916 KB n=2000
79 Correct 36 ms 39788 KB n=2000
80 Correct 35 ms 39916 KB n=2000
81 Correct 36 ms 39788 KB n=2000
82 Correct 37 ms 39788 KB n=2000
83 Correct 35 ms 39916 KB n=2000
84 Correct 35 ms 39788 KB n=2000
85 Correct 36 ms 39788 KB n=2000
86 Correct 43 ms 39788 KB n=2000
87 Correct 36 ms 39788 KB n=2000
88 Correct 35 ms 39916 KB n=2000
89 Correct 35 ms 39916 KB n=2000
90 Correct 35 ms 39916 KB n=2000
91 Correct 36 ms 39788 KB n=2000
92 Correct 888 ms 69484 KB n=200000
93 Correct 971 ms 73452 KB n=200000
94 Correct 672 ms 76524 KB n=200000
95 Correct 883 ms 69472 KB n=200000
96 Correct 825 ms 69600 KB n=200000
97 Correct 1101 ms 72428 KB n=200000
98 Correct 902 ms 69728 KB n=200000
99 Correct 1049 ms 69356 KB n=200000
100 Correct 853 ms 69604 KB n=200000
101 Correct 590 ms 78188 KB n=200000
102 Correct 559 ms 70436 KB n=200000
103 Correct 566 ms 70520 KB n=200000
104 Correct 574 ms 70508 KB n=200000
105 Correct 570 ms 70380 KB n=200000
106 Correct 556 ms 70252 KB n=200000
107 Correct 543 ms 70124 KB n=200000
108 Correct 986 ms 69116 KB n=200000
109 Correct 960 ms 69268 KB n=200000
110 Correct 959 ms 69100 KB n=200000
111 Correct 894 ms 69320 KB n=200000
112 Correct 645 ms 77036 KB n=200000
113 Correct 1010 ms 72556 KB n=200000
114 Correct 868 ms 69472 KB n=200000
115 Correct 1361 ms 70496 KB n=200000
116 Correct 835 ms 69216 KB n=200000
117 Correct 606 ms 77292 KB n=200000
118 Correct 1220 ms 71404 KB n=200000
119 Correct 800 ms 69264 KB n=200000
120 Correct 522 ms 77804 KB n=200000
121 Correct 528 ms 77804 KB n=200000
122 Correct 539 ms 78060 KB n=200000
123 Correct 573 ms 69868 KB n=200000
124 Correct 216 ms 52332 KB n=25264