답안 #786439

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
786439 2023-07-18T07:48:02 Z 반딧불(#10026) Paths (RMI21_paths) C++17
100 / 100
585 ms 68780 KB
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

struct segTree{
    int cnt[800002]; ll sum[800002];

    void update(int i, int l, int r, int x, int v1, ll v2){
        if(l==r){
            cnt[i] += v1, sum[i] += v2;
            return;
        }
        int m = (l+r)>>1;
        if(x<=m) update(i*2, l, m, x, v1, v2);
        else update(i*2+1, m+1, r, x, v1, v2);
        cnt[i] = cnt[i*2] + cnt[i*2+1], sum[i] = sum[i*2] + sum[i*2+1];
    }

    ll query(int i, int l, int r, int x){
        if(l==r) return cnt[i] ? (sum[i] / cnt[i]) * x : 0;
        int m = (l+r)>>1;
        if(cnt[i*2+1] >= x) return query(i*2+1, m+1, r, x);
        else return query(i*2, l, m, x-cnt[i*2+1]) + sum[i*2+1];
    }
} tree;

struct Edge{
    int s, e; ll v; int idx;
    Edge(){}
    Edge(int s, int e, ll v): s(s), e(e), v(v){}
    bool operator<(const Edge &r)const{
        return idx<r.idx;
    }
};

int n, k;
Edge arr[200002];
vector<Edge> link[100002];
int in[100002], out[100002], idx[100002], inCnt;
int par[100002];

void dfs_in(int x, int p=-1){
    in[x] = ++inCnt;
    idx[inCnt] = x;
    par[x] = p;
    for(auto y: link[x]){
        if(y.e == p) continue;
        dfs_in(y.e, x);
    }
    out[x] = inCnt;
}

vector<Edge> linkSet[100002];
ll MX[200002], where[200002]; /// �� ���� �������� ���� �� �ִ밡 ���ΰ�
vector<pair<ll, ll> > options[100002]; /// �� �������� ���� �� �ִ� ��� �ɼ�

pair<ll, int> dfs_getValues(int x, int p=-1){
//    printf("Get value %d %d\n", x, p);
    if(!linkSet[x].empty()){
        vector<Edge> tlst;
        for(Edge y: linkSet[x]){
            if((y.idx ^ p) == 1){
                tlst.push_back(y);
                continue;
            }
            options[x].push_back(make_pair(MX[y.idx] = (dfs_getValues(y.e, y.idx).first + y.v), y.idx));
        }
        linkSet[x].swap(tlst);
        sort(options[x].begin(), options[x].end());
        options[x].erase(unique(options[x].begin(), options[x].end()), options[x].end());
        reverse(options[x].begin(), options[x].end());
    }
    if(options[x].empty()) return make_pair(0, -1);
    else if((options[x][0].second ^ p) != 1) return options[x][0];
    else if((int)options[x].size() == 1) return make_pair(0, -1);
    else return options[x][1];
}

vector<ll> numbers;
vector<ll> inQuery[100002], outQuery[100002];
ll ans[100002];

void putQuery(int s, int e, ll p, int mode){
    if(in[s] < in[e]){
        if(mode == 1){
            inQuery[1].push_back(p), outQuery[in[e]].push_back(p);
            if(out[e]+1 <= n+1) inQuery[out[e]+1].push_back(p), outQuery[n+1].push_back(p);
        }
        else{
            outQuery[1].push_back(p), inQuery[in[e]].push_back(p);
            if(out[e]+1 <= n+1) outQuery[out[e]+1].push_back(p), inQuery[n+1].push_back(p);
        }
    }
    else{
        if(mode == 1) inQuery[in[s]].push_back(p), outQuery[out[s]+1].push_back(p);
        else          outQuery[in[s]].push_back(p), inQuery[out[s]+1].push_back(p);
    }
}

int main(){
    scanf("%d %d", &n, &k);
    for(int i=1; i<n; i++){
        scanf("%d %d %lld", &arr[i*2-2].s, &arr[i*2-2].e, &arr[i*2-2].v);
        arr[i*2-1].s = arr[i*2-2].e, arr[i*2-1].e = arr[i*2-2].s, arr[i*2-1].v = arr[i*2-2].v;
        arr[i*2-2].idx = i*2-2, arr[i*2-1].idx = i*2-1;
        link[arr[i*2-2].s].push_back(arr[i*2-2]);
        link[arr[i*2-1].s].push_back(arr[i*2-1]);
    }
    for(int i=1; i<=n; i++) linkSet[i] = link[i];

    dfs_in(1);
    for(int i=0; i<(n-1)*2; i++){
        if(MX[i]) continue;
        pair<ll, int> p = dfs_getValues(arr[i].e, i);
        MX[i] = p.first + arr[i].v;
        where[i] = p.second;
    }
    for(int i=1; i<=n; i++) options[i].clear();
    for(int i=0; i<(n-1)*2; i++){
        options[arr[i].s].push_back(make_pair(MX[i], i));
    }
    for(int i=1; i<=n; i++) sort(options[i].rbegin(), options[i].rend());

    for(int i=1; i<=n; i++){
        /// ���� �ִ� 2N��
        for(int j=0; j<(int)options[i].size(); j++){
            int p = options[i][j].second; ll v = options[i][j].first;
            int s = arr[p].s, e = arr[p].e;
            putQuery(s, e, v, 1);
            if(where[p] != -1) putQuery(s, e, v-arr[p].v, -1);
        }
    }

    numbers.push_back(0);
    for(int i=1; i<=n; i++) for(ll p: inQuery[i]) numbers.push_back(p);
    for(int i=1; i<=n; i++) for(ll p: outQuery[i]) numbers.push_back(p);
    sort(numbers.begin(), numbers.end());
    numbers.erase(unique(numbers.begin(), numbers.end()), numbers.end());
    for(int i=1; i<=n; i++) for(ll &p: inQuery[i]) p = lower_bound(numbers.begin(), numbers.end(), p) - numbers.begin();
    for(int i=1; i<=n; i++) for(ll &p: outQuery[i]) p = lower_bound(numbers.begin(), numbers.end(), p) - numbers.begin();
    int L = (int)numbers.size();

    for(int i=1; i<=n; i++){
        for(ll p: inQuery[i]){
//            printf("In update %d %lld\n", i, numbers[p]);
            tree.update(1, 0, L-1, p, 1, numbers[p]);
        }
        for(ll p: outQuery[i]){
//            printf("Out update %d %lld\n", i, numbers[p]);
            tree.update(1, 0, L-1, p, -1, -numbers[p]);
        }
        ans[idx[i]] = tree.query(1, 0, L-1, k);
    }

    for(int i=1; i<=n; i++){
        printf("%lld\n", ans[i]);
    }
}

Compilation message

Main.cpp: In function 'int main()':
Main.cpp:103:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  103 |     scanf("%d %d", &n, &k);
      |     ~~~~~^~~~~~~~~~~~~~~~~
Main.cpp:105:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  105 |         scanf("%d %d %lld", &arr[i*2-2].s, &arr[i*2-2].e, &arr[i*2-2].v);
      |         ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 12120 KB Output is correct
2 Correct 5 ms 12116 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 12120 KB Output is correct
2 Correct 5 ms 12116 KB Output is correct
3 Correct 6 ms 12116 KB Output is correct
4 Correct 6 ms 12116 KB Output is correct
5 Correct 6 ms 12116 KB Output is correct
6 Correct 6 ms 12116 KB Output is correct
7 Correct 6 ms 12116 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 12120 KB Output is correct
2 Correct 5 ms 12116 KB Output is correct
3 Correct 6 ms 12116 KB Output is correct
4 Correct 6 ms 12116 KB Output is correct
5 Correct 6 ms 12116 KB Output is correct
6 Correct 6 ms 12116 KB Output is correct
7 Correct 6 ms 12116 KB Output is correct
8 Correct 9 ms 12756 KB Output is correct
9 Correct 10 ms 12744 KB Output is correct
10 Correct 11 ms 12756 KB Output is correct
11 Correct 10 ms 12720 KB Output is correct
12 Correct 9 ms 12748 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 12120 KB Output is correct
2 Correct 5 ms 12116 KB Output is correct
3 Correct 6 ms 12116 KB Output is correct
4 Correct 6 ms 12116 KB Output is correct
5 Correct 6 ms 12116 KB Output is correct
6 Correct 6 ms 12116 KB Output is correct
7 Correct 6 ms 12116 KB Output is correct
8 Correct 9 ms 12756 KB Output is correct
9 Correct 10 ms 12744 KB Output is correct
10 Correct 11 ms 12756 KB Output is correct
11 Correct 10 ms 12720 KB Output is correct
12 Correct 9 ms 12748 KB Output is correct
13 Correct 12 ms 13272 KB Output is correct
14 Correct 12 ms 13300 KB Output is correct
15 Correct 12 ms 13184 KB Output is correct
16 Correct 12 ms 13220 KB Output is correct
17 Correct 11 ms 13264 KB Output is correct
18 Correct 11 ms 12992 KB Output is correct
19 Correct 15 ms 13268 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 565 ms 64504 KB Output is correct
2 Correct 512 ms 66116 KB Output is correct
3 Correct 469 ms 63348 KB Output is correct
4 Correct 585 ms 64316 KB Output is correct
5 Correct 472 ms 66036 KB Output is correct
6 Correct 526 ms 65344 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 12120 KB Output is correct
2 Correct 5 ms 12116 KB Output is correct
3 Correct 6 ms 12116 KB Output is correct
4 Correct 6 ms 12116 KB Output is correct
5 Correct 6 ms 12116 KB Output is correct
6 Correct 6 ms 12116 KB Output is correct
7 Correct 6 ms 12116 KB Output is correct
8 Correct 9 ms 12756 KB Output is correct
9 Correct 10 ms 12744 KB Output is correct
10 Correct 11 ms 12756 KB Output is correct
11 Correct 10 ms 12720 KB Output is correct
12 Correct 9 ms 12748 KB Output is correct
13 Correct 12 ms 13272 KB Output is correct
14 Correct 12 ms 13300 KB Output is correct
15 Correct 12 ms 13184 KB Output is correct
16 Correct 12 ms 13220 KB Output is correct
17 Correct 11 ms 13264 KB Output is correct
18 Correct 11 ms 12992 KB Output is correct
19 Correct 15 ms 13268 KB Output is correct
20 Correct 565 ms 64504 KB Output is correct
21 Correct 512 ms 66116 KB Output is correct
22 Correct 469 ms 63348 KB Output is correct
23 Correct 585 ms 64316 KB Output is correct
24 Correct 472 ms 66036 KB Output is correct
25 Correct 526 ms 65344 KB Output is correct
26 Correct 515 ms 64888 KB Output is correct
27 Correct 492 ms 66520 KB Output is correct
28 Correct 584 ms 67612 KB Output is correct
29 Correct 442 ms 63436 KB Output is correct
30 Correct 517 ms 64648 KB Output is correct
31 Correct 425 ms 59780 KB Output is correct
32 Correct 473 ms 66440 KB Output is correct
33 Correct 532 ms 64880 KB Output is correct
34 Correct 444 ms 63364 KB Output is correct
35 Correct 516 ms 64584 KB Output is correct
36 Correct 461 ms 68780 KB Output is correct