답안 #91358

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
91358 2018-12-27T08:45:19 Z toloraia Birthday gift (IZhO18_treearray) C++14
56 / 100
4000 ms 43052 KB
#include <bits/stdc++.h>
#define F first
#define S second
#define mp make_pair
#define pb push_back
#define ll long long
#define LEFT(a) ((a)<<1)
#define RIGHT(a) (LEFT(a) + 1)
#define MID(a,b) ((a+b)>>1)
#define MAX(a,b) ((a)>(b)?(a):(b))
#define MIN(a,b) ((a)<(b)?(a):(b))

using namespace std;
const int N = 4e5 + 5;

int n, m, Q;
vector < int > g[N];
int in[N], out[N];
int P[N][20];
int T;

void dfs (int k, int p){
    in[k] = ++T;
    P[k][0] = p;
    for (int i = 1; i < 20; i++)
        P[k][i] = P[P[k][i - 1]][i - 1];
    for (int i = 0; i < g[k].size(); i++)
        if (g[k][i] != p)
            dfs (g[k][i], k);
    out[k] = ++T;
}

int LCA (int u, int v){
    if (u == 0 || v == 0)return u + v;
    if (in[u] <= in[v] && out[v] <= out[u])
        return u;
    if (in[v] <= in[u] && out[u] <= out[v])
        return v;
    int I = u;
    for (int i = 19; i >= 0; i--){
        if (P[I][i] == 0)
            continue;
        if (in[P[I][i]] <= in[v] && out[v] <= out[P[I][i]])
            continue;
        I = P[I][i];
    }
    return P[I][0];
}

int a[N], S;
int le[N], ri[N];

int main()
{
    ios::sync_with_stdio(false);
    cin>>n>>m>>Q;
    for (int i = 1; i < n; i++){
        int u, v;
        cin>>u>>v;
        g[u].pb (v);
        g[v].pb (u);
    }
    dfs (1, 0);
    for (int i = 1; i <= m; i++)
        cin>>a[i];
    S = sqrt(m - 1) + 1;
    for (int i = 0; i < S * S; i += S){
        le[i + 1] = a[i + 1];
        for (int j = i + 2; j <= i + S; j++)
            le[j] = LCA (a[j], le[j - 1]);
        ri[i + S] = a[i + S];
        for (int j = i + S - 1; j >= i + 1; j--)
            ri[j] = LCA (a[j], ri[j + 1]);
    }
    while (Q--){
        int t;
        cin>>t;
        if (t == 1){
            int ind, x;
            cin>>ind>>x;
            a[ind] = x;
            int i = (ind - 1) / S * S;
            le[i + 1] = a[i + 1];
            for (int j = i + 2; j <= i + S; j++)
                le[j] = LCA (a[j], le[j - 1]);
            ri[i + S] = a[i + S];
            for (int j = i + S - 1; j >= i + 1; j--)
                ri[j] = LCA (a[j], ri[j + 1]);
            continue;
        }
        int l, r, v;
        cin>>l>>r>>v;
        int p1 = -1, p2 = -1;
        /*for (int L = 1; L <= S * S && p1 == -1; L += S){
            if (r < L - 1 || L < l)
                continue;
            int R = L - 1;
            int X = 0;
            while (R + S <= r && in[v] <= in[le[R + S]] && out[le[R + S]] <= out[v]){
                R += S;
                X = LCA (X, le[R]);
            }
            int x = L, y = R, xx, yy;
            for (int i = 10; i >= 0; i--){
                xx = x - (1<<i);
                if (xx >= L - S && xx >= l && in[v] <= in[ri[xx]] && out[ri[xx]] <= out[v])
                    x = xx;
            }
            for (int i = 10; i >= 0; i--){
                yy = y + (1<<i);
                if (yy <= R + S && yy <= r && in[v] <= in[le[yy]] && out[le[yy]] <= out[v])
                    y = yy;
            }
            if (x < L)
                X = LCA (X, ri[x]);
            if (R < y)
                X = LCA (X, le[y]);
            if (X == v && l <= x && y <= r){
                p1 = x;
                p2 = y;
            }
            //cout<<x<<" "<<y<<"   "<<X<<" "<<S<<" "<<L<<" "<<R<<" "<<le[R + S]<<endl;
            L = max (L, R + 1 - S);
        }
        if (p1 != -1){
            cout<<p1<<" "<<p2<<endl;
            continue;
        }*/
        //for (int i = 0; i < S * S && p1 == -1; i += S){
            for (int L = l; L <= r && p1 == -1; L++){
                int R = L - 1;
                int X = 0;
                while (R < r && in[v] <= in[a[R + 1]] && out[a[R + 1]] <= out[v]){
                    R++;
                    X = LCA (X, a[R]);
                }
                if (X == v){
                    p1 = L;
                    p2 = R;
                }
                L = max (L, R);
            }
        //}
        cout<<p1<<" "<<p2<<endl;
    }
    return 0;
}

Compilation message

treearray.cpp: In function 'void dfs(int, int)':
treearray.cpp:27:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for (int i = 0; i < g[k].size(); i++)
                     ~~^~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 9720 KB n=5
2 Correct 10 ms 9972 KB n=100
3 Correct 9 ms 9972 KB n=100
4 Correct 9 ms 9972 KB n=100
5 Correct 9 ms 9972 KB n=100
6 Correct 10 ms 10024 KB n=100
7 Correct 9 ms 10048 KB n=100
8 Correct 9 ms 10048 KB n=100
9 Correct 8 ms 10048 KB n=100
10 Correct 10 ms 10048 KB n=100
11 Correct 9 ms 10048 KB n=100
12 Correct 10 ms 10048 KB n=100
13 Correct 10 ms 10048 KB n=100
14 Correct 10 ms 10048 KB n=100
15 Correct 10 ms 10048 KB n=100
16 Correct 10 ms 10108 KB n=100
17 Correct 10 ms 10108 KB n=100
18 Correct 10 ms 10108 KB n=100
19 Correct 10 ms 10120 KB n=100
20 Correct 9 ms 10120 KB n=100
21 Correct 10 ms 10120 KB n=100
22 Correct 10 ms 10120 KB n=100
23 Correct 10 ms 10120 KB n=100
24 Correct 9 ms 10120 KB n=100
25 Correct 9 ms 10120 KB n=100
26 Correct 9 ms 10120 KB n=12
27 Correct 10 ms 10172 KB n=100
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 9720 KB n=5
2 Correct 10 ms 9972 KB n=100
3 Correct 9 ms 9972 KB n=100
4 Correct 9 ms 9972 KB n=100
5 Correct 9 ms 9972 KB n=100
6 Correct 10 ms 10024 KB n=100
7 Correct 9 ms 10048 KB n=100
8 Correct 9 ms 10048 KB n=100
9 Correct 8 ms 10048 KB n=100
10 Correct 10 ms 10048 KB n=100
11 Correct 9 ms 10048 KB n=100
12 Correct 10 ms 10048 KB n=100
13 Correct 10 ms 10048 KB n=100
14 Correct 10 ms 10048 KB n=100
15 Correct 10 ms 10048 KB n=100
16 Correct 10 ms 10108 KB n=100
17 Correct 10 ms 10108 KB n=100
18 Correct 10 ms 10108 KB n=100
19 Correct 10 ms 10120 KB n=100
20 Correct 9 ms 10120 KB n=100
21 Correct 10 ms 10120 KB n=100
22 Correct 10 ms 10120 KB n=100
23 Correct 10 ms 10120 KB n=100
24 Correct 9 ms 10120 KB n=100
25 Correct 9 ms 10120 KB n=100
26 Correct 9 ms 10120 KB n=12
27 Correct 10 ms 10172 KB n=100
28 Correct 12 ms 10176 KB n=500
29 Correct 10 ms 10188 KB n=500
30 Correct 11 ms 10204 KB n=500
31 Correct 10 ms 10220 KB n=500
32 Correct 10 ms 10236 KB n=500
33 Correct 11 ms 10248 KB n=500
34 Correct 11 ms 10264 KB n=500
35 Correct 10 ms 10276 KB n=500
36 Correct 11 ms 10296 KB n=500
37 Correct 11 ms 10296 KB n=500
38 Correct 11 ms 10300 KB n=500
39 Correct 11 ms 10312 KB n=500
40 Correct 10 ms 10328 KB n=500
41 Correct 11 ms 10344 KB n=500
42 Correct 11 ms 10360 KB n=500
43 Correct 11 ms 10360 KB n=500
44 Correct 11 ms 10388 KB n=500
45 Correct 11 ms 10396 KB n=500
46 Correct 11 ms 10408 KB n=500
47 Correct 11 ms 10424 KB n=500
48 Correct 11 ms 10496 KB n=500
49 Correct 10 ms 10496 KB n=500
50 Correct 11 ms 10496 KB n=500
51 Correct 11 ms 10496 KB n=500
52 Correct 10 ms 10500 KB n=500
53 Correct 10 ms 10512 KB n=500
54 Correct 11 ms 10528 KB n=500
55 Correct 10 ms 10540 KB n=278
56 Correct 11 ms 10612 KB n=500
57 Correct 10 ms 10612 KB n=500
58 Correct 11 ms 10612 KB n=500
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 9720 KB n=5
2 Correct 10 ms 9972 KB n=100
3 Correct 9 ms 9972 KB n=100
4 Correct 9 ms 9972 KB n=100
5 Correct 9 ms 9972 KB n=100
6 Correct 10 ms 10024 KB n=100
7 Correct 9 ms 10048 KB n=100
8 Correct 9 ms 10048 KB n=100
9 Correct 8 ms 10048 KB n=100
10 Correct 10 ms 10048 KB n=100
11 Correct 9 ms 10048 KB n=100
12 Correct 10 ms 10048 KB n=100
13 Correct 10 ms 10048 KB n=100
14 Correct 10 ms 10048 KB n=100
15 Correct 10 ms 10048 KB n=100
16 Correct 10 ms 10108 KB n=100
17 Correct 10 ms 10108 KB n=100
18 Correct 10 ms 10108 KB n=100
19 Correct 10 ms 10120 KB n=100
20 Correct 9 ms 10120 KB n=100
21 Correct 10 ms 10120 KB n=100
22 Correct 10 ms 10120 KB n=100
23 Correct 10 ms 10120 KB n=100
24 Correct 9 ms 10120 KB n=100
25 Correct 9 ms 10120 KB n=100
26 Correct 9 ms 10120 KB n=12
27 Correct 10 ms 10172 KB n=100
28 Correct 12 ms 10176 KB n=500
29 Correct 10 ms 10188 KB n=500
30 Correct 11 ms 10204 KB n=500
31 Correct 10 ms 10220 KB n=500
32 Correct 10 ms 10236 KB n=500
33 Correct 11 ms 10248 KB n=500
34 Correct 11 ms 10264 KB n=500
35 Correct 10 ms 10276 KB n=500
36 Correct 11 ms 10296 KB n=500
37 Correct 11 ms 10296 KB n=500
38 Correct 11 ms 10300 KB n=500
39 Correct 11 ms 10312 KB n=500
40 Correct 10 ms 10328 KB n=500
41 Correct 11 ms 10344 KB n=500
42 Correct 11 ms 10360 KB n=500
43 Correct 11 ms 10360 KB n=500
44 Correct 11 ms 10388 KB n=500
45 Correct 11 ms 10396 KB n=500
46 Correct 11 ms 10408 KB n=500
47 Correct 11 ms 10424 KB n=500
48 Correct 11 ms 10496 KB n=500
49 Correct 10 ms 10496 KB n=500
50 Correct 11 ms 10496 KB n=500
51 Correct 11 ms 10496 KB n=500
52 Correct 10 ms 10500 KB n=500
53 Correct 10 ms 10512 KB n=500
54 Correct 11 ms 10528 KB n=500
55 Correct 10 ms 10540 KB n=278
56 Correct 11 ms 10612 KB n=500
57 Correct 10 ms 10612 KB n=500
58 Correct 11 ms 10612 KB n=500
59 Correct 15 ms 10716 KB n=2000
60 Correct 17 ms 10972 KB n=2000
61 Correct 18 ms 10972 KB n=2000
62 Correct 18 ms 11032 KB n=2000
63 Correct 16 ms 11036 KB n=2000
64 Correct 18 ms 11088 KB n=2000
65 Correct 15 ms 11160 KB n=2000
66 Correct 18 ms 11212 KB n=2000
67 Correct 15 ms 11288 KB n=2000
68 Correct 17 ms 11340 KB n=2000
69 Correct 21 ms 11340 KB n=2000
70 Correct 21 ms 11448 KB n=2000
71 Correct 21 ms 11628 KB n=2000
72 Correct 17 ms 11680 KB n=2000
73 Correct 17 ms 11680 KB n=2000
74 Correct 15 ms 11680 KB n=1844
75 Correct 19 ms 11708 KB n=2000
76 Correct 18 ms 11764 KB n=2000
77 Correct 20 ms 11816 KB n=2000
78 Correct 20 ms 11828 KB n=2000
79 Correct 16 ms 11880 KB n=2000
80 Correct 17 ms 11968 KB n=2000
81 Correct 18 ms 12024 KB n=2000
82 Correct 15 ms 12080 KB n=2000
83 Correct 17 ms 12216 KB n=2000
84 Correct 17 ms 12216 KB n=2000
85 Correct 18 ms 12236 KB n=2000
86 Correct 19 ms 12292 KB n=2000
87 Correct 16 ms 12348 KB n=2000
88 Correct 19 ms 12432 KB n=2000
89 Correct 19 ms 12576 KB n=2000
90 Correct 18 ms 12628 KB n=2000
91 Correct 26 ms 12628 KB n=2000
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 9720 KB n=5
2 Correct 10 ms 9972 KB n=100
3 Correct 9 ms 9972 KB n=100
4 Correct 9 ms 9972 KB n=100
5 Correct 9 ms 9972 KB n=100
6 Correct 10 ms 10024 KB n=100
7 Correct 9 ms 10048 KB n=100
8 Correct 9 ms 10048 KB n=100
9 Correct 8 ms 10048 KB n=100
10 Correct 10 ms 10048 KB n=100
11 Correct 9 ms 10048 KB n=100
12 Correct 10 ms 10048 KB n=100
13 Correct 10 ms 10048 KB n=100
14 Correct 10 ms 10048 KB n=100
15 Correct 10 ms 10048 KB n=100
16 Correct 10 ms 10108 KB n=100
17 Correct 10 ms 10108 KB n=100
18 Correct 10 ms 10108 KB n=100
19 Correct 10 ms 10120 KB n=100
20 Correct 9 ms 10120 KB n=100
21 Correct 10 ms 10120 KB n=100
22 Correct 10 ms 10120 KB n=100
23 Correct 10 ms 10120 KB n=100
24 Correct 9 ms 10120 KB n=100
25 Correct 9 ms 10120 KB n=100
26 Correct 9 ms 10120 KB n=12
27 Correct 10 ms 10172 KB n=100
28 Correct 12 ms 10176 KB n=500
29 Correct 10 ms 10188 KB n=500
30 Correct 11 ms 10204 KB n=500
31 Correct 10 ms 10220 KB n=500
32 Correct 10 ms 10236 KB n=500
33 Correct 11 ms 10248 KB n=500
34 Correct 11 ms 10264 KB n=500
35 Correct 10 ms 10276 KB n=500
36 Correct 11 ms 10296 KB n=500
37 Correct 11 ms 10296 KB n=500
38 Correct 11 ms 10300 KB n=500
39 Correct 11 ms 10312 KB n=500
40 Correct 10 ms 10328 KB n=500
41 Correct 11 ms 10344 KB n=500
42 Correct 11 ms 10360 KB n=500
43 Correct 11 ms 10360 KB n=500
44 Correct 11 ms 10388 KB n=500
45 Correct 11 ms 10396 KB n=500
46 Correct 11 ms 10408 KB n=500
47 Correct 11 ms 10424 KB n=500
48 Correct 11 ms 10496 KB n=500
49 Correct 10 ms 10496 KB n=500
50 Correct 11 ms 10496 KB n=500
51 Correct 11 ms 10496 KB n=500
52 Correct 10 ms 10500 KB n=500
53 Correct 10 ms 10512 KB n=500
54 Correct 11 ms 10528 KB n=500
55 Correct 10 ms 10540 KB n=278
56 Correct 11 ms 10612 KB n=500
57 Correct 10 ms 10612 KB n=500
58 Correct 11 ms 10612 KB n=500
59 Correct 15 ms 10716 KB n=2000
60 Correct 17 ms 10972 KB n=2000
61 Correct 18 ms 10972 KB n=2000
62 Correct 18 ms 11032 KB n=2000
63 Correct 16 ms 11036 KB n=2000
64 Correct 18 ms 11088 KB n=2000
65 Correct 15 ms 11160 KB n=2000
66 Correct 18 ms 11212 KB n=2000
67 Correct 15 ms 11288 KB n=2000
68 Correct 17 ms 11340 KB n=2000
69 Correct 21 ms 11340 KB n=2000
70 Correct 21 ms 11448 KB n=2000
71 Correct 21 ms 11628 KB n=2000
72 Correct 17 ms 11680 KB n=2000
73 Correct 17 ms 11680 KB n=2000
74 Correct 15 ms 11680 KB n=1844
75 Correct 19 ms 11708 KB n=2000
76 Correct 18 ms 11764 KB n=2000
77 Correct 20 ms 11816 KB n=2000
78 Correct 20 ms 11828 KB n=2000
79 Correct 16 ms 11880 KB n=2000
80 Correct 17 ms 11968 KB n=2000
81 Correct 18 ms 12024 KB n=2000
82 Correct 15 ms 12080 KB n=2000
83 Correct 17 ms 12216 KB n=2000
84 Correct 17 ms 12216 KB n=2000
85 Correct 18 ms 12236 KB n=2000
86 Correct 19 ms 12292 KB n=2000
87 Correct 16 ms 12348 KB n=2000
88 Correct 19 ms 12432 KB n=2000
89 Correct 19 ms 12576 KB n=2000
90 Correct 18 ms 12628 KB n=2000
91 Correct 26 ms 12628 KB n=2000
92 Execution timed out 4061 ms 43052 KB Time limit exceeded
93 Halted 0 ms 0 KB -