답안 #246359

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
246359 2020-07-08T21:19:42 Z fivefourthreeone Construction of Highway (JOI18_construction) C++17
16 / 100
2000 ms 32888 KB
#include <bits/stdc++.h>
#define owo(i,a, b) for(int i=(a); i<(b); ++i)
#define uwu(i,a, b) for(int i=(a)-1; i>=(b); --i)
#define senpai push_back
#define ttgl pair<int, int>
#define ayaya cout<<"debug"<<endl
 
using namespace std;
using ll = long long;
using ld = long double;
const ll MOD = 1e9+7;
const double PI = acos(-1);
const int INF = 0x3f3f3f3f;
const int NINF = 0x3f3f3f40;
const ll INFLL = 0x3f3f3f3f3f3f3f3f;
const ll NINFLL = 0x3f3f3f3f3f3f3f40;
const int mxN = 100001;
int n;
int cnt = 0;
ttgl inout[mxN];
ttgl edges[mxN];
set<int> stt;
map<int, int> mp;
int arr[mxN];
int seg[4*mxN];
int par[mxN][18];
vector<int> adj[mxN];
void upd(int p, int val) {
    for(seg[p+=2*n] = val; p>1; p>>=1) seg[p>>1] = max(seg[p], seg[p^1]);
}
int qmax(int l, int r) {
    int res = 0;
    r++;
    for(l+=2*n, r+=2*n; l<r; l>>=1, r>>=1) {
        if(l&1) res= max(res, seg[l++]);
        if(r&1) res = max(res, seg[--r]);
    }
    return res;
}
int BIT[mxN];
void add(int i, int val) {
    while(i<=n) {
        BIT[i]+= val;
        i +=i&-i;
    }
}
void del(int i) {
    while(i<=n) {
        BIT[i] = 0;
        i +=i&-i;
    }
}
int sum (int i) {
    int res = 0;
    while(i>0) {
        res+=BIT[i];
        i-=i&-i;
    }
    return res;
}
void dfs(int u, int p) {
    par[u][0] = p;
    inout[u].first = cnt++;
    for(int v: adj[u]) {
        if(v==p)continue;
        dfs(v, u);
    }
    inout[u].second = cnt++;
}
int query(int k) {
    //cout<<"\n";
    //cout<<k<<" "<<inout[k].first<<" "<<inout[k].second<<" "<<qmax(inout[k].first, inout[k].second)<<" hi\n";
    return qmax(inout[k].first, inout[k].second);
}
int main()
{
    //freopen("filename.in", "r", stdin);
    //freopen("filename.out", "w", stdout);
    cin.tie(0)->sync_with_stdio(0);
    cin>>n;
    owo(i, 0, n) {
        cin>>arr[i];
        stt.insert(arr[i]);
    }
    int idx = 0;
    auto it = stt.begin();
    while(it!=stt.end()) {
        mp[*it] = idx++;
        it++;
    }
    owo(i, 0, n) {
        arr[i] = mp[arr[i]];
    }
    owo(i, 0, n-1) {
        cin>>edges[i].first>>edges[i].second;
        edges[i].first--;
        edges[i].second--;
        adj[edges[i].first].senpai(edges[i].second);
        adj[edges[i].second].senpai(edges[i].first);
    }
    dfs(0, -1);
    owo(lg, 1, 18) {
        owo(i, 0, n) {
            if(par[i][lg-1]==-1) {
                par[i][lg] = -1;
            }else {
                par[i][lg] = par[par[i][lg-1]][lg-1];
            }
        }
    }
    owo(i, 0, 2*mxN) {
        seg[i] = -1;
    }
    upd(inout[0].first, 0);
    owo(i, 1, n) {
        int a = edges[i-1].first;
        int b = edges[i-1].second;
        int ans = 0;
        vector<int> path;
        //cout<<i<<"\n";
        int val = query(a)==0 ? arr[a] : arr[edges[query(a)-1].second];
        while(a!=-1) {
            int num= 1;
            //cout<<a<<" ";
            uwu(i, 18, 0) {
                if(par[a][i]!=-1&&query(par[a][i])==query(a)) {
                    a = par[a][i];
                    num+=(1<<i);
                }
            }
            //cout<<a<<" "<<num<<" "<<val<<" "<<sum(val+1)<<"\n";
            ans+=num*sum(val);
            add(val+1, num);
            path.senpai(val+1);
            a = par[a][0];
            val = arr[edges[query(a)-1].second];
            //cout<<query(a)<<"\n";
        }
        memset(BIT, 0, sizeof(BIT));
        cout<<ans<<"\n";
        upd(inout[b].first, i);
        //cout<<inout[b].first<<" "<<i<<" cool\n";
    }
    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 3860 KB Output is correct
2 Correct 7 ms 3840 KB Output is correct
3 Correct 8 ms 3840 KB Output is correct
4 Correct 11 ms 3968 KB Output is correct
5 Correct 13 ms 3968 KB Output is correct
6 Correct 14 ms 3968 KB Output is correct
7 Correct 13 ms 3968 KB Output is correct
8 Correct 15 ms 4096 KB Output is correct
9 Correct 13 ms 4096 KB Output is correct
10 Correct 13 ms 3968 KB Output is correct
11 Correct 13 ms 3968 KB Output is correct
12 Correct 14 ms 4096 KB Output is correct
13 Correct 13 ms 3968 KB Output is correct
14 Correct 13 ms 3968 KB Output is correct
15 Correct 13 ms 3968 KB Output is correct
16 Correct 14 ms 3968 KB Output is correct
17 Correct 13 ms 3968 KB Output is correct
18 Correct 14 ms 3968 KB Output is correct
19 Correct 13 ms 3968 KB Output is correct
20 Correct 13 ms 3968 KB Output is correct
21 Correct 13 ms 3968 KB Output is correct
22 Correct 13 ms 3968 KB Output is correct
23 Correct 13 ms 3968 KB Output is correct
24 Correct 13 ms 3968 KB Output is correct
25 Correct 14 ms 3968 KB Output is correct
26 Correct 13 ms 3968 KB Output is correct
27 Correct 14 ms 4096 KB Output is correct
28 Correct 14 ms 4096 KB Output is correct
29 Correct 13 ms 3968 KB Output is correct
30 Correct 13 ms 4096 KB Output is correct
31 Correct 14 ms 3968 KB Output is correct
32 Correct 13 ms 3968 KB Output is correct
33 Correct 13 ms 3968 KB Output is correct
34 Correct 13 ms 3968 KB Output is correct
35 Correct 13 ms 3968 KB Output is correct
36 Correct 13 ms 3968 KB Output is correct
37 Correct 13 ms 3968 KB Output is correct
38 Correct 13 ms 3968 KB Output is correct
39 Correct 13 ms 3968 KB Output is correct
40 Correct 13 ms 4096 KB Output is correct
41 Correct 14 ms 4096 KB Output is correct
42 Correct 13 ms 3968 KB Output is correct
43 Correct 13 ms 3968 KB Output is correct
44 Correct 17 ms 4096 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 3860 KB Output is correct
2 Correct 7 ms 3840 KB Output is correct
3 Correct 8 ms 3840 KB Output is correct
4 Correct 11 ms 3968 KB Output is correct
5 Correct 13 ms 3968 KB Output is correct
6 Correct 14 ms 3968 KB Output is correct
7 Correct 13 ms 3968 KB Output is correct
8 Correct 15 ms 4096 KB Output is correct
9 Correct 13 ms 4096 KB Output is correct
10 Correct 13 ms 3968 KB Output is correct
11 Correct 13 ms 3968 KB Output is correct
12 Correct 14 ms 4096 KB Output is correct
13 Correct 13 ms 3968 KB Output is correct
14 Correct 13 ms 3968 KB Output is correct
15 Correct 13 ms 3968 KB Output is correct
16 Correct 14 ms 3968 KB Output is correct
17 Correct 13 ms 3968 KB Output is correct
18 Correct 14 ms 3968 KB Output is correct
19 Correct 13 ms 3968 KB Output is correct
20 Correct 13 ms 3968 KB Output is correct
21 Correct 13 ms 3968 KB Output is correct
22 Correct 13 ms 3968 KB Output is correct
23 Correct 13 ms 3968 KB Output is correct
24 Correct 13 ms 3968 KB Output is correct
25 Correct 14 ms 3968 KB Output is correct
26 Correct 13 ms 3968 KB Output is correct
27 Correct 14 ms 4096 KB Output is correct
28 Correct 14 ms 4096 KB Output is correct
29 Correct 13 ms 3968 KB Output is correct
30 Correct 13 ms 4096 KB Output is correct
31 Correct 14 ms 3968 KB Output is correct
32 Correct 13 ms 3968 KB Output is correct
33 Correct 13 ms 3968 KB Output is correct
34 Correct 13 ms 3968 KB Output is correct
35 Correct 13 ms 3968 KB Output is correct
36 Correct 13 ms 3968 KB Output is correct
37 Correct 13 ms 3968 KB Output is correct
38 Correct 13 ms 3968 KB Output is correct
39 Correct 13 ms 3968 KB Output is correct
40 Correct 13 ms 4096 KB Output is correct
41 Correct 14 ms 4096 KB Output is correct
42 Correct 13 ms 3968 KB Output is correct
43 Correct 13 ms 3968 KB Output is correct
44 Correct 17 ms 4096 KB Output is correct
45 Correct 20 ms 4224 KB Output is correct
46 Correct 63 ms 4864 KB Output is correct
47 Correct 82 ms 4864 KB Output is correct
48 Correct 62 ms 4864 KB Output is correct
49 Correct 61 ms 5112 KB Output is correct
50 Correct 66 ms 5116 KB Output is correct
51 Correct 60 ms 5112 KB Output is correct
52 Correct 106 ms 4984 KB Output is correct
53 Correct 66 ms 4992 KB Output is correct
54 Correct 61 ms 4984 KB Output is correct
55 Correct 64 ms 4992 KB Output is correct
56 Correct 61 ms 4964 KB Output is correct
57 Correct 83 ms 4864 KB Output is correct
58 Correct 71 ms 4752 KB Output is correct
59 Correct 70 ms 4864 KB Output is correct
60 Correct 70 ms 4872 KB Output is correct
61 Correct 63 ms 4992 KB Output is correct
62 Correct 63 ms 4984 KB Output is correct
63 Correct 65 ms 4992 KB Output is correct
64 Correct 60 ms 4480 KB Output is correct
65 Correct 65 ms 4480 KB Output is correct
66 Correct 60 ms 4480 KB Output is correct
67 Correct 62 ms 4608 KB Output is correct
68 Correct 57 ms 4656 KB Output is correct
69 Correct 62 ms 4984 KB Output is correct
70 Correct 74 ms 4600 KB Output is correct
71 Correct 61 ms 4728 KB Output is correct
72 Correct 70 ms 4864 KB Output is correct
73 Correct 71 ms 4480 KB Output is correct
74 Correct 62 ms 4480 KB Output is correct
75 Correct 62 ms 4864 KB Output is correct
76 Correct 61 ms 4864 KB Output is correct
77 Correct 61 ms 4864 KB Output is correct
78 Correct 73 ms 4480 KB Output is correct
79 Correct 58 ms 4480 KB Output is correct
80 Correct 60 ms 4480 KB Output is correct
81 Correct 62 ms 4864 KB Output is correct
82 Correct 63 ms 4864 KB Output is correct
83 Correct 68 ms 4864 KB Output is correct
84 Correct 61 ms 4480 KB Output is correct
85 Correct 63 ms 4480 KB Output is correct
86 Correct 61 ms 4480 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 3860 KB Output is correct
2 Correct 7 ms 3840 KB Output is correct
3 Correct 8 ms 3840 KB Output is correct
4 Correct 11 ms 3968 KB Output is correct
5 Correct 13 ms 3968 KB Output is correct
6 Correct 14 ms 3968 KB Output is correct
7 Correct 13 ms 3968 KB Output is correct
8 Correct 15 ms 4096 KB Output is correct
9 Correct 13 ms 4096 KB Output is correct
10 Correct 13 ms 3968 KB Output is correct
11 Correct 13 ms 3968 KB Output is correct
12 Correct 14 ms 4096 KB Output is correct
13 Correct 13 ms 3968 KB Output is correct
14 Correct 13 ms 3968 KB Output is correct
15 Correct 13 ms 3968 KB Output is correct
16 Correct 14 ms 3968 KB Output is correct
17 Correct 13 ms 3968 KB Output is correct
18 Correct 14 ms 3968 KB Output is correct
19 Correct 13 ms 3968 KB Output is correct
20 Correct 13 ms 3968 KB Output is correct
21 Correct 13 ms 3968 KB Output is correct
22 Correct 13 ms 3968 KB Output is correct
23 Correct 13 ms 3968 KB Output is correct
24 Correct 13 ms 3968 KB Output is correct
25 Correct 14 ms 3968 KB Output is correct
26 Correct 13 ms 3968 KB Output is correct
27 Correct 14 ms 4096 KB Output is correct
28 Correct 14 ms 4096 KB Output is correct
29 Correct 13 ms 3968 KB Output is correct
30 Correct 13 ms 4096 KB Output is correct
31 Correct 14 ms 3968 KB Output is correct
32 Correct 13 ms 3968 KB Output is correct
33 Correct 13 ms 3968 KB Output is correct
34 Correct 13 ms 3968 KB Output is correct
35 Correct 13 ms 3968 KB Output is correct
36 Correct 13 ms 3968 KB Output is correct
37 Correct 13 ms 3968 KB Output is correct
38 Correct 13 ms 3968 KB Output is correct
39 Correct 13 ms 3968 KB Output is correct
40 Correct 13 ms 4096 KB Output is correct
41 Correct 14 ms 4096 KB Output is correct
42 Correct 13 ms 3968 KB Output is correct
43 Correct 13 ms 3968 KB Output is correct
44 Correct 17 ms 4096 KB Output is correct
45 Correct 20 ms 4224 KB Output is correct
46 Correct 63 ms 4864 KB Output is correct
47 Correct 82 ms 4864 KB Output is correct
48 Correct 62 ms 4864 KB Output is correct
49 Correct 61 ms 5112 KB Output is correct
50 Correct 66 ms 5116 KB Output is correct
51 Correct 60 ms 5112 KB Output is correct
52 Correct 106 ms 4984 KB Output is correct
53 Correct 66 ms 4992 KB Output is correct
54 Correct 61 ms 4984 KB Output is correct
55 Correct 64 ms 4992 KB Output is correct
56 Correct 61 ms 4964 KB Output is correct
57 Correct 83 ms 4864 KB Output is correct
58 Correct 71 ms 4752 KB Output is correct
59 Correct 70 ms 4864 KB Output is correct
60 Correct 70 ms 4872 KB Output is correct
61 Correct 63 ms 4992 KB Output is correct
62 Correct 63 ms 4984 KB Output is correct
63 Correct 65 ms 4992 KB Output is correct
64 Correct 60 ms 4480 KB Output is correct
65 Correct 65 ms 4480 KB Output is correct
66 Correct 60 ms 4480 KB Output is correct
67 Correct 62 ms 4608 KB Output is correct
68 Correct 57 ms 4656 KB Output is correct
69 Correct 62 ms 4984 KB Output is correct
70 Correct 74 ms 4600 KB Output is correct
71 Correct 61 ms 4728 KB Output is correct
72 Correct 70 ms 4864 KB Output is correct
73 Correct 71 ms 4480 KB Output is correct
74 Correct 62 ms 4480 KB Output is correct
75 Correct 62 ms 4864 KB Output is correct
76 Correct 61 ms 4864 KB Output is correct
77 Correct 61 ms 4864 KB Output is correct
78 Correct 73 ms 4480 KB Output is correct
79 Correct 58 ms 4480 KB Output is correct
80 Correct 60 ms 4480 KB Output is correct
81 Correct 62 ms 4864 KB Output is correct
82 Correct 63 ms 4864 KB Output is correct
83 Correct 68 ms 4864 KB Output is correct
84 Correct 61 ms 4480 KB Output is correct
85 Correct 63 ms 4480 KB Output is correct
86 Correct 61 ms 4480 KB Output is correct
87 Correct 152 ms 6272 KB Output is correct
88 Correct 457 ms 11132 KB Output is correct
89 Correct 1720 ms 28944 KB Output is correct
90 Correct 1738 ms 28904 KB Output is correct
91 Correct 1721 ms 28936 KB Output is correct
92 Correct 1514 ms 32888 KB Output is correct
93 Correct 1623 ms 32888 KB Output is correct
94 Correct 1540 ms 32824 KB Output is correct
95 Correct 1625 ms 31224 KB Output is correct
96 Correct 1592 ms 31440 KB Output is correct
97 Correct 1630 ms 31376 KB Output is correct
98 Correct 1640 ms 31360 KB Output is correct
99 Correct 1693 ms 31208 KB Output is correct
100 Execution timed out 2079 ms 28664 KB Time limit exceeded
101 Halted 0 ms 0 KB -