답안 #607720

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
607720 2022-07-27T01:43:32 Z bgnbvnbv Birthday gift (IZhO18_treearray) C++14
100 / 100
859 ms 77184 KB
#include <bits/stdc++.h>
#define prec fixed<<setprecision
#define pb push_back
#define mp make_pair
#define eb emplace_back
#define pf push_front
#define pob pop_back
#define pof pop_front
#define ll long long
#define ld long double
#define fs first
#define sc second
#define all(x) x.begin(),x.end()
#define endl '\n'
#define taskname "tree"
#define pll pair<long long,long long>
#define pld pair<long double,long double>
#define bpc __builtin_popcount
using namespace std;
const ll inf = 1e9;
const int maxN=2e5+9;
const ll mod=1e9+7;
int n,m,q,a[maxN],lc[maxN],pa,plca;
set<int> ap[maxN],lcp[maxN];//xp[i] = positions of i trong x
vector<int> adj[maxN];
//lca
int tg=0, tin[maxN], tout[maxN];
int up[maxN][19];
void dfs(int v, int p){
    tin[v] = ++tg;
    up[v][0] = p;
    for (int i = 1; i <= 18; ++i)
        up[v][i] = up[up[v][i-1]][i-1];
    for (int u : adj[v])
        if (u != p)
            dfs(u, v);
    tout[v] = ++tg;
}
bool act(int u, int v){
    return tin[u] <= tin[v] && tout[u] >= tout[v];
}
int lca(int u, int v){
    if (act(u, v))return u;
    if (act(v, u))return v;
    for (int i = 18; i >= 0; --i)
        if (!act(up[u][i], v))
            u = up[u][i];
    return up[u][0];
}
//*/
void Init(){
    dfs(1,1);
    for(int i=1;i<=m;i++)cin>>a[i],ap[a[i]].insert(i);
    for(int i=1;i<=m-1;i++)lc[i]=lca(a[i],a[i+1]),lcp[lc[i]].insert(i);
}
void Enter(){
    cin>>n>>m>>q;
    for(int i=1;i<n;i++){
        int u,v;
        cin>>u>>v;
        adj[u].pb(v);
        adj[v].pb(u);
    }
}
void Solve(){
    while(q--){
        ll t,l,r,k;
        cin>>t>>l>>r;
        if(t==2){
            //3 log n
            cin>>k;

            pa=*(ap[k].upper_bound(l-1));
            if(l<=pa&&pa<=r){cout<<pa<<" "<<pa<<endl;continue;}

            plca=*(lcp[k].upper_bound(l-1));
            if(l<=plca&&plca+1<=r){cout<<plca<<" "<<plca+1<<endl;continue;}

            cout<<"-1 -1\n";
        } else {
            //8 log n
            ap[a[l]].erase(l);
            if(l>1)lcp[lc[l-1]].erase(l-1);
            if(l<m)lcp[lc[l]].erase(l);

            a[l]=r;
            if(l>1)lc[l-1]=lca(a[l-1],a[l]);
            if(l<m)lc[l]=lca(a[l],a[l+1]);

            ap[a[l]].insert(l);
            if(l>1)lcp[lc[l-1]].insert(l-1);
            if(l<m)lcp[lc[l]].insert(l);
        }
    }
}
///Read every task 3 times
///Connect with previous task
///Hint in subtasks?
///Re-initialize efficiently
///Array bounds
///Int overflow
///Modulo
///Output
///File?
///Multi-tests?
signed main() {
    cin.tie(nullptr);ios_base::sync_with_stdio(NULL);
    if(fopen(taskname".inp","r")){freopen(taskname".inp","r",stdin);freopen(taskname".out","w",stdout);}
    int t=1;
    //cin>>t;
    //getline(cin,s);
    //Init();
    while (t--){
        //Init();
        Enter();
        Init();
        Solve();
    }
}

Compilation message

treearray.cpp: In function 'int main()':
treearray.cpp:108:42: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  108 |     if(fopen(taskname".inp","r")){freopen(taskname".inp","r",stdin);freopen(taskname".out","w",stdout);}
      |                                   ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
treearray.cpp:108:76: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  108 |     if(fopen(taskname".inp","r")){freopen(taskname".inp","r",stdin);freopen(taskname".out","w",stdout);}
      |                                                                     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 12 ms 23764 KB n=5
2 Correct 13 ms 23764 KB n=100
3 Correct 13 ms 23820 KB n=100
4 Correct 13 ms 23764 KB n=100
5 Correct 12 ms 23820 KB n=100
6 Correct 14 ms 23828 KB n=100
7 Correct 12 ms 23776 KB n=100
8 Correct 12 ms 23836 KB n=100
9 Correct 12 ms 23796 KB n=100
10 Correct 12 ms 23820 KB n=100
11 Correct 12 ms 23816 KB n=100
12 Correct 14 ms 23764 KB n=100
13 Correct 12 ms 23808 KB n=100
14 Correct 12 ms 23816 KB n=100
15 Correct 11 ms 23764 KB n=100
16 Correct 12 ms 23832 KB n=100
17 Correct 12 ms 23764 KB n=100
18 Correct 16 ms 23820 KB n=100
19 Correct 12 ms 23824 KB n=100
20 Correct 14 ms 23776 KB n=100
21 Correct 13 ms 23764 KB n=100
22 Correct 13 ms 23808 KB n=100
23 Correct 12 ms 23764 KB n=100
24 Correct 14 ms 23780 KB n=100
25 Correct 12 ms 23812 KB n=100
26 Correct 12 ms 23816 KB n=12
27 Correct 12 ms 23764 KB n=100
# 결과 실행 시간 메모리 Grader output
1 Correct 12 ms 23764 KB n=5
2 Correct 13 ms 23764 KB n=100
3 Correct 13 ms 23820 KB n=100
4 Correct 13 ms 23764 KB n=100
5 Correct 12 ms 23820 KB n=100
6 Correct 14 ms 23828 KB n=100
7 Correct 12 ms 23776 KB n=100
8 Correct 12 ms 23836 KB n=100
9 Correct 12 ms 23796 KB n=100
10 Correct 12 ms 23820 KB n=100
11 Correct 12 ms 23816 KB n=100
12 Correct 14 ms 23764 KB n=100
13 Correct 12 ms 23808 KB n=100
14 Correct 12 ms 23816 KB n=100
15 Correct 11 ms 23764 KB n=100
16 Correct 12 ms 23832 KB n=100
17 Correct 12 ms 23764 KB n=100
18 Correct 16 ms 23820 KB n=100
19 Correct 12 ms 23824 KB n=100
20 Correct 14 ms 23776 KB n=100
21 Correct 13 ms 23764 KB n=100
22 Correct 13 ms 23808 KB n=100
23 Correct 12 ms 23764 KB n=100
24 Correct 14 ms 23780 KB n=100
25 Correct 12 ms 23812 KB n=100
26 Correct 12 ms 23816 KB n=12
27 Correct 12 ms 23764 KB n=100
28 Correct 14 ms 23892 KB n=500
29 Correct 14 ms 24024 KB n=500
30 Correct 14 ms 23968 KB n=500
31 Correct 15 ms 23892 KB n=500
32 Correct 16 ms 23824 KB n=500
33 Correct 13 ms 23892 KB n=500
34 Correct 13 ms 23892 KB n=500
35 Correct 13 ms 23892 KB n=500
36 Correct 13 ms 23868 KB n=500
37 Correct 13 ms 23892 KB n=500
38 Correct 13 ms 23892 KB n=500
39 Correct 16 ms 23852 KB n=500
40 Correct 15 ms 23892 KB n=500
41 Correct 13 ms 23852 KB n=500
42 Correct 14 ms 23892 KB n=500
43 Correct 13 ms 23892 KB n=500
44 Correct 12 ms 23892 KB n=500
45 Correct 13 ms 23956 KB n=500
46 Correct 13 ms 23892 KB n=500
47 Correct 15 ms 23916 KB n=500
48 Correct 15 ms 23892 KB n=500
49 Correct 12 ms 23860 KB n=500
50 Correct 13 ms 23892 KB n=500
51 Correct 13 ms 23896 KB n=500
52 Correct 13 ms 23940 KB n=500
53 Correct 13 ms 23892 KB n=500
54 Correct 13 ms 23892 KB n=500
55 Correct 12 ms 23892 KB n=278
56 Correct 13 ms 23924 KB n=500
57 Correct 13 ms 23892 KB n=500
58 Correct 12 ms 23892 KB n=500
# 결과 실행 시간 메모리 Grader output
1 Correct 12 ms 23764 KB n=5
2 Correct 13 ms 23764 KB n=100
3 Correct 13 ms 23820 KB n=100
4 Correct 13 ms 23764 KB n=100
5 Correct 12 ms 23820 KB n=100
6 Correct 14 ms 23828 KB n=100
7 Correct 12 ms 23776 KB n=100
8 Correct 12 ms 23836 KB n=100
9 Correct 12 ms 23796 KB n=100
10 Correct 12 ms 23820 KB n=100
11 Correct 12 ms 23816 KB n=100
12 Correct 14 ms 23764 KB n=100
13 Correct 12 ms 23808 KB n=100
14 Correct 12 ms 23816 KB n=100
15 Correct 11 ms 23764 KB n=100
16 Correct 12 ms 23832 KB n=100
17 Correct 12 ms 23764 KB n=100
18 Correct 16 ms 23820 KB n=100
19 Correct 12 ms 23824 KB n=100
20 Correct 14 ms 23776 KB n=100
21 Correct 13 ms 23764 KB n=100
22 Correct 13 ms 23808 KB n=100
23 Correct 12 ms 23764 KB n=100
24 Correct 14 ms 23780 KB n=100
25 Correct 12 ms 23812 KB n=100
26 Correct 12 ms 23816 KB n=12
27 Correct 12 ms 23764 KB n=100
28 Correct 14 ms 23892 KB n=500
29 Correct 14 ms 24024 KB n=500
30 Correct 14 ms 23968 KB n=500
31 Correct 15 ms 23892 KB n=500
32 Correct 16 ms 23824 KB n=500
33 Correct 13 ms 23892 KB n=500
34 Correct 13 ms 23892 KB n=500
35 Correct 13 ms 23892 KB n=500
36 Correct 13 ms 23868 KB n=500
37 Correct 13 ms 23892 KB n=500
38 Correct 13 ms 23892 KB n=500
39 Correct 16 ms 23852 KB n=500
40 Correct 15 ms 23892 KB n=500
41 Correct 13 ms 23852 KB n=500
42 Correct 14 ms 23892 KB n=500
43 Correct 13 ms 23892 KB n=500
44 Correct 12 ms 23892 KB n=500
45 Correct 13 ms 23956 KB n=500
46 Correct 13 ms 23892 KB n=500
47 Correct 15 ms 23916 KB n=500
48 Correct 15 ms 23892 KB n=500
49 Correct 12 ms 23860 KB n=500
50 Correct 13 ms 23892 KB n=500
51 Correct 13 ms 23896 KB n=500
52 Correct 13 ms 23940 KB n=500
53 Correct 13 ms 23892 KB n=500
54 Correct 13 ms 23892 KB n=500
55 Correct 12 ms 23892 KB n=278
56 Correct 13 ms 23924 KB n=500
57 Correct 13 ms 23892 KB n=500
58 Correct 12 ms 23892 KB n=500
59 Correct 14 ms 24240 KB n=2000
60 Correct 14 ms 24472 KB n=2000
61 Correct 16 ms 24276 KB n=2000
62 Correct 17 ms 24276 KB n=2000
63 Correct 16 ms 24220 KB n=2000
64 Correct 15 ms 24360 KB n=2000
65 Correct 18 ms 24304 KB n=2000
66 Correct 15 ms 24276 KB n=2000
67 Correct 15 ms 24216 KB n=2000
68 Correct 15 ms 24364 KB n=2000
69 Correct 15 ms 24220 KB n=2000
70 Correct 18 ms 24284 KB n=2000
71 Correct 15 ms 24268 KB n=2000
72 Correct 15 ms 24300 KB n=2000
73 Correct 14 ms 24316 KB n=2000
74 Correct 15 ms 24220 KB n=1844
75 Correct 14 ms 24276 KB n=2000
76 Correct 17 ms 24404 KB n=2000
77 Correct 16 ms 24212 KB n=2000
78 Correct 16 ms 24288 KB n=2000
79 Correct 16 ms 24324 KB n=2000
80 Correct 15 ms 24288 KB n=2000
81 Correct 17 ms 24324 KB n=2000
82 Correct 16 ms 24212 KB n=2000
83 Correct 15 ms 24340 KB n=2000
84 Correct 15 ms 24240 KB n=2000
85 Correct 15 ms 24276 KB n=2000
86 Correct 17 ms 24336 KB n=2000
87 Correct 15 ms 24212 KB n=2000
88 Correct 14 ms 24324 KB n=2000
89 Correct 14 ms 24356 KB n=2000
90 Correct 14 ms 24348 KB n=2000
91 Correct 14 ms 24316 KB n=2000
# 결과 실행 시간 메모리 Grader output
1 Correct 12 ms 23764 KB n=5
2 Correct 13 ms 23764 KB n=100
3 Correct 13 ms 23820 KB n=100
4 Correct 13 ms 23764 KB n=100
5 Correct 12 ms 23820 KB n=100
6 Correct 14 ms 23828 KB n=100
7 Correct 12 ms 23776 KB n=100
8 Correct 12 ms 23836 KB n=100
9 Correct 12 ms 23796 KB n=100
10 Correct 12 ms 23820 KB n=100
11 Correct 12 ms 23816 KB n=100
12 Correct 14 ms 23764 KB n=100
13 Correct 12 ms 23808 KB n=100
14 Correct 12 ms 23816 KB n=100
15 Correct 11 ms 23764 KB n=100
16 Correct 12 ms 23832 KB n=100
17 Correct 12 ms 23764 KB n=100
18 Correct 16 ms 23820 KB n=100
19 Correct 12 ms 23824 KB n=100
20 Correct 14 ms 23776 KB n=100
21 Correct 13 ms 23764 KB n=100
22 Correct 13 ms 23808 KB n=100
23 Correct 12 ms 23764 KB n=100
24 Correct 14 ms 23780 KB n=100
25 Correct 12 ms 23812 KB n=100
26 Correct 12 ms 23816 KB n=12
27 Correct 12 ms 23764 KB n=100
28 Correct 14 ms 23892 KB n=500
29 Correct 14 ms 24024 KB n=500
30 Correct 14 ms 23968 KB n=500
31 Correct 15 ms 23892 KB n=500
32 Correct 16 ms 23824 KB n=500
33 Correct 13 ms 23892 KB n=500
34 Correct 13 ms 23892 KB n=500
35 Correct 13 ms 23892 KB n=500
36 Correct 13 ms 23868 KB n=500
37 Correct 13 ms 23892 KB n=500
38 Correct 13 ms 23892 KB n=500
39 Correct 16 ms 23852 KB n=500
40 Correct 15 ms 23892 KB n=500
41 Correct 13 ms 23852 KB n=500
42 Correct 14 ms 23892 KB n=500
43 Correct 13 ms 23892 KB n=500
44 Correct 12 ms 23892 KB n=500
45 Correct 13 ms 23956 KB n=500
46 Correct 13 ms 23892 KB n=500
47 Correct 15 ms 23916 KB n=500
48 Correct 15 ms 23892 KB n=500
49 Correct 12 ms 23860 KB n=500
50 Correct 13 ms 23892 KB n=500
51 Correct 13 ms 23896 KB n=500
52 Correct 13 ms 23940 KB n=500
53 Correct 13 ms 23892 KB n=500
54 Correct 13 ms 23892 KB n=500
55 Correct 12 ms 23892 KB n=278
56 Correct 13 ms 23924 KB n=500
57 Correct 13 ms 23892 KB n=500
58 Correct 12 ms 23892 KB n=500
59 Correct 14 ms 24240 KB n=2000
60 Correct 14 ms 24472 KB n=2000
61 Correct 16 ms 24276 KB n=2000
62 Correct 17 ms 24276 KB n=2000
63 Correct 16 ms 24220 KB n=2000
64 Correct 15 ms 24360 KB n=2000
65 Correct 18 ms 24304 KB n=2000
66 Correct 15 ms 24276 KB n=2000
67 Correct 15 ms 24216 KB n=2000
68 Correct 15 ms 24364 KB n=2000
69 Correct 15 ms 24220 KB n=2000
70 Correct 18 ms 24284 KB n=2000
71 Correct 15 ms 24268 KB n=2000
72 Correct 15 ms 24300 KB n=2000
73 Correct 14 ms 24316 KB n=2000
74 Correct 15 ms 24220 KB n=1844
75 Correct 14 ms 24276 KB n=2000
76 Correct 17 ms 24404 KB n=2000
77 Correct 16 ms 24212 KB n=2000
78 Correct 16 ms 24288 KB n=2000
79 Correct 16 ms 24324 KB n=2000
80 Correct 15 ms 24288 KB n=2000
81 Correct 17 ms 24324 KB n=2000
82 Correct 16 ms 24212 KB n=2000
83 Correct 15 ms 24340 KB n=2000
84 Correct 15 ms 24240 KB n=2000
85 Correct 15 ms 24276 KB n=2000
86 Correct 17 ms 24336 KB n=2000
87 Correct 15 ms 24212 KB n=2000
88 Correct 14 ms 24324 KB n=2000
89 Correct 14 ms 24356 KB n=2000
90 Correct 14 ms 24348 KB n=2000
91 Correct 14 ms 24316 KB n=2000
92 Correct 614 ms 68912 KB n=200000
93 Correct 699 ms 72612 KB n=200000
94 Correct 538 ms 75992 KB n=200000
95 Correct 622 ms 68664 KB n=200000
96 Correct 567 ms 68784 KB n=200000
97 Correct 733 ms 71712 KB n=200000
98 Correct 588 ms 68744 KB n=200000
99 Correct 692 ms 68060 KB n=200000
100 Correct 596 ms 68676 KB n=200000
101 Correct 499 ms 77004 KB n=200000
102 Correct 451 ms 69580 KB n=200000
103 Correct 425 ms 69640 KB n=200000
104 Correct 417 ms 69616 KB n=200000
105 Correct 381 ms 69480 KB n=200000
106 Correct 365 ms 69388 KB n=200000
107 Correct 379 ms 69364 KB n=200000
108 Correct 678 ms 68400 KB n=200000
109 Correct 703 ms 68324 KB n=200000
110 Correct 764 ms 68408 KB n=200000
111 Correct 607 ms 68624 KB n=200000
112 Correct 524 ms 76004 KB n=200000
113 Correct 698 ms 71484 KB n=200000
114 Correct 626 ms 68688 KB n=200000
115 Correct 859 ms 69524 KB n=200000
116 Correct 629 ms 68548 KB n=200000
117 Correct 502 ms 76392 KB n=200000
118 Correct 774 ms 70328 KB n=200000
119 Correct 558 ms 68404 KB n=200000
120 Correct 473 ms 76804 KB n=200000
121 Correct 433 ms 76960 KB n=200000
122 Correct 463 ms 77184 KB n=200000
123 Correct 413 ms 69028 KB n=200000
124 Correct 134 ms 37068 KB n=25264