답안 #1060068

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1060068 2024-08-15T10:24:52 Z thangdz2k7 사탕 분배 (IOI21_candies) C++17
29 / 100
174 ms 39508 KB
#include <bits/stdc++.h>

using namespace std;

const int N = 2e5 + 5;

int n, q;
vector <int> event[N];
long long sum[4 * N], Max[4 * N], Min[4 * N];

void upd(int u, int val, int s = 1, int l = 0, int r = q - 1){
    if (l == r){
        sum[s] = val;
        Max[s] = max(0, val);
        Min[s] = min(0, val);
        return;
    }

    int mid = l + r >> 1;
    if (mid >= u) upd(u, val, 2 * s, l, mid);
    else upd(u, val, 2 * s + 1, mid + 1, r);

    sum[s] = sum[2 * s] + sum[2 * s + 1];
    Max[s] = max(Max[2 * s], sum[2 * s] + Max[2 * s + 1]);
    Min[s] = min(Min[2 * s], sum[2 * s] + Min[2 * s + 1]);
}

long long get(int time, int s = 1, int l = 0, int r = q - 1){
    if (l == r){
        if (sum[s] < 0) return 0;
        if (sum[s] > time) return time;
        return sum[s];
    }

    int mid = l + r >> 1;
    if (Max[2 * s + 1] - Min[2 * s + 1] > time) return get(time, 2 * s + 1, mid + 1, r);
    long long rt = get(time, 2 * s, l, mid);
    if (rt + Max[2 * s + 1] > time) return time - (Max[2 * s + 1] - sum[2 * s + 1]);
    if (rt + Min[2 * s + 1] < 0) return sum[2 * s + 1] - Min[2 * s + 1];
    return rt + sum[2 * s + 1];
}

vector <int> distribute_candies(vector <int> c, vector <int> l, vector <int> r, vector <int> v){
    n = c.size(), q = l.size();
    for (int i = 0; i < q; ++ i){
        event[l[i]].push_back(i);
        event[r[i] + 1].push_back(-i);
    }

    vector <int> ans;
    for (int i = 0; i < n; ++ i){
        for (int id : event[i]){
            if (id < 0) upd(-id, 0);
            else upd(id, v[id]);
        }
        ans.push_back(get(c[i]));
    }
    return ans;
}

Compilation message

candies.cpp: In function 'void upd(int, int, int, int, int)':
candies.cpp:19:17: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   19 |     int mid = l + r >> 1;
      |               ~~^~~
candies.cpp: In function 'long long int get(int, int, int, int)':
candies.cpp:35:17: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   35 |     int mid = l + r >> 1;
      |               ~~^~~
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 9304 KB Output is correct
2 Incorrect 2 ms 9304 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 174 ms 39508 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 1 ms 5288 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 9304 KB Output is correct
2 Correct 1 ms 9308 KB Output is correct
3 Correct 56 ms 30876 KB Output is correct
4 Correct 46 ms 13204 KB Output is correct
5 Correct 95 ms 34748 KB Output is correct
6 Correct 95 ms 35204 KB Output is correct
7 Correct 95 ms 35772 KB Output is correct
8 Correct 97 ms 34492 KB Output is correct
9 Correct 99 ms 36112 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 9304 KB Output is correct
2 Incorrect 2 ms 9304 KB Output isn't correct
3 Halted 0 ms 0 KB -