답안 #826677

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
826677 2023-08-15T19:57:37 Z Liudas 사탕 분배 (IOI21_candies) C++17
100 / 100
1250 ms 31460 KB
#include <iostream>
#include "candies.h"
#include <cassert>
#include <cstdio>
#include <climits>
#include <vector>
using namespace std;
class segtree{
public:
    struct node{
        long long mini;
        long long maxi;
        long long lazy;
    };
    vector<node> tree;
    int N;
    void init(int n){
        N = 1;
        while(N <= n){
            N*=2;
        }
        tree.assign(2 * N, {0, 0, 0});
    }
    void lazy_upd(int head){
        tree[head].mini += tree[head].lazy;
        tree[head].maxi += tree[head].lazy;
        if(head < N){
            tree[head * 2 + 1].lazy += tree[head].lazy;
            tree[head * 2 + 2].lazy += tree[head].lazy;
        }
        tree[head].lazy = 0;
    }
    void update(int head, int L, int R, int l, int r, int val){
        lazy_upd(head);
        if(l <= L && R <= r){
            tree[head].lazy = val;
            lazy_upd(head); 
            return;
        }
        if(R <= l || r <= L){
            return;
        }
        int mid = (L + R) / 2;
        update(head * 2 + 1, L, mid, l, r, val);
        update(head * 2 + 2, mid, R, l, r, val);
        tree[head].mini = min(tree[head * 2 + 1].mini, tree[head * 2 + 2].mini);
        tree[head].maxi = max(tree[head * 2 + 1].maxi, tree[head * 2 + 2].maxi);
    }
    void update(int l, int val){
        update(0, 0, N, l, N, val);
    }
    long long getmin(int head, int L, int R, int l, int r){
        lazy_upd(head);
        if(l <= L && R <= r){
            return tree[head].mini;
        }
        if(R <= l || r <= L){
            return LLONG_MAX;
        }
        int mid = (L + R)/ 2;
        return min(getmin(head * 2 + 1, L, mid, l, r), getmin(head * 2 + 2, mid, R, l, r));
    }
    long long getmin(int l){
        return getmin(0, 0, N, l, N);
    }
    long long getmax(int head, int L, int R, int l, int r){
        lazy_upd(head);
        if(l <= L && R <= r){
            return tree[head].maxi;
        }
        if(R <= l || r <= L){
            return LLONG_MIN;
        }
        int mid = (L + R)/ 2;
        return max(getmax(head * 2 + 1, L, mid, l, r), getmax(head * 2 + 2, mid, R, l, r));
    }
    long long getmax(int l){
        return getmax(0, 0, N, l, N);
    }
    long long getpoint(int head, int L, int R, int x){
        lazy_upd(head);
        if(R - L == 1){
            return tree[head].mini;
        }
        int mid = (L + R) / 2;
        if(x < mid){
            return getpoint(head * 2 + 1, L, mid, x);
        }
        else{
            return getpoint(head * 2 + 2, mid, R, x);
        }
    }
    long long getpoint(int x){
        return getpoint(0, 0, N, x);
    }
};
vector<int> distribute_candies(vector<int> c, vector<int> l, vector<int> r, vector<int> val){
    int N = c.size();
    int Q = l.size();
    vector<int> ans(N);
    segtree tree;
    tree.init(Q + 5);
    vector<vector<pair<int, int>>> op(N + 5);
    for(int i = 0; i < Q; i ++){
        op[l[i]].push_back({i + 1, val[i]});
        op[r[i]+1].push_back({i + 1, -val[i]});
    }
    for(int i = 0; i < N; i ++){
        for(auto[ll, rr] : op[i]){
            tree.update(ll, rr);
        }
        int l = 0, r = Q + 1;
        if(tree.getmax(0)-tree.getmin(0) < c[i]) { // easy case: range is small
            ans[i] = tree.getpoint(Q) - tree.getmin(0);
            continue;
        }
        while(l + 1 < r){
            int mid = (l + r) / 2;
            if(tree.getmax(mid)-tree.getmin(mid) > c[i]){
                l = mid;
            }
            else{
                r = mid;
            }
        }
        //cout << tree.getpoint(l) << " " <<  tree.getpoint(Q + 1) << " " << l << endl;
        long long a = tree.getpoint(l), b = tree.getpoint(Q);
        if(a < b){
            int d = tree.getmax(l);
            ans[i] = c[i] - (d - b);
        }
        else{
            int d = tree.getmin(l);
            ans[i] = b - d;
        }
    }
    return ans;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 1 ms 508 KB Output is correct
4 Correct 2 ms 468 KB Output is correct
5 Correct 5 ms 468 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1244 ms 31396 KB Output is correct
2 Correct 1249 ms 31396 KB Output is correct
3 Correct 1099 ms 31388 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 185 ms 21840 KB Output is correct
3 Correct 336 ms 7620 KB Output is correct
4 Correct 1246 ms 31392 KB Output is correct
5 Correct 1250 ms 31396 KB Output is correct
6 Correct 1203 ms 31396 KB Output is correct
7 Correct 1192 ms 31420 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 117 ms 20692 KB Output is correct
4 Correct 284 ms 7540 KB Output is correct
5 Correct 798 ms 27724 KB Output is correct
6 Correct 920 ms 27728 KB Output is correct
7 Correct 1033 ms 27728 KB Output is correct
8 Correct 740 ms 27804 KB Output is correct
9 Correct 1004 ms 27728 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 1 ms 508 KB Output is correct
4 Correct 2 ms 468 KB Output is correct
5 Correct 5 ms 468 KB Output is correct
6 Correct 1244 ms 31396 KB Output is correct
7 Correct 1249 ms 31396 KB Output is correct
8 Correct 1099 ms 31388 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 185 ms 21840 KB Output is correct
11 Correct 336 ms 7620 KB Output is correct
12 Correct 1246 ms 31392 KB Output is correct
13 Correct 1250 ms 31396 KB Output is correct
14 Correct 1203 ms 31396 KB Output is correct
15 Correct 1192 ms 31420 KB Output is correct
16 Correct 0 ms 212 KB Output is correct
17 Correct 1 ms 212 KB Output is correct
18 Correct 117 ms 20692 KB Output is correct
19 Correct 284 ms 7540 KB Output is correct
20 Correct 798 ms 27724 KB Output is correct
21 Correct 920 ms 27728 KB Output is correct
22 Correct 1033 ms 27728 KB Output is correct
23 Correct 740 ms 27804 KB Output is correct
24 Correct 1004 ms 27728 KB Output is correct
25 Correct 0 ms 212 KB Output is correct
26 Correct 236 ms 7612 KB Output is correct
27 Correct 178 ms 21736 KB Output is correct
28 Correct 855 ms 31460 KB Output is correct
29 Correct 1037 ms 31428 KB Output is correct
30 Correct 1145 ms 31460 KB Output is correct
31 Correct 1222 ms 31392 KB Output is correct
32 Correct 1158 ms 31384 KB Output is correct