답안 #470388

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
470388 2021-09-03T16:12:06 Z wiwiho 사탕 분배 (IOI21_candies) C++17
0 / 100
345 ms 33620 KB
#include "candies.h"

#include <bits/stdc++.h>
#include <bits/extc++.h>

#define StarBurstStream ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#define iter(a) a.begin(), a.end()
#define riter(a) a.rbegin(), a.rend()
#define lsort(a) sort(iter(a))
#define gsort(a) sort(riter(a))
#define pb(a) push_back(a)
#define eb(a) emplace_back(a)
#define pf(a) push_front(a)
#define ef(a) emplace_front(a)
#define pob pop_back()
#define pof pop_front()
#define mp(a, b) make_pair(a, b)
#define F first
#define S second
#define mt make_tuple
#define gt(t, i) get<i>(t)
#define tomax(a, b) ((a) = max((a), (b)))
#define tomin(a, b) ((a) = min((a), (b)))
#define topos(a) ((a) = (((a) % MOD + MOD) % MOD))
#define uni(a) a.resize(unique(iter(a)) - a.begin())
#define printv(a, b) {bool pvaspace=false; \
for(auto pva : a){ \
    if(pvaspace) b << " "; pvaspace=true;\
    b << pva;\
}\
b << "\n";}

using namespace std;
using namespace __gnu_pbds;

typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;

using pii = pair<int, int>;
using pll = pair<ll, ll>;
using pdd = pair<ld, ld>;
using tiii = tuple<int, int, int>;

const ll MOD = 1000000007;
const ll MAX = 2147483647;
const ll LLMAX = 1LL << 60;

template<typename A, typename B>
ostream& operator<<(ostream& o, pair<A, B> p){
    return o << '(' << p.F << ',' << p.S << ')';
}

ll ifloor(ll a, ll b){
    if(b < 0) a *= -1, b *= -1;
    if(a < 0) return (a - b + 1) / b;
    else return a / b;
}

ll iceil(ll a, ll b){
    if(b < 0) a *= -1, b *= -1;
    if(a > 0) return (a + b - 1) / b;
    else return a / b;
}

struct Node{
    ll mn = 0, mx = 0, tag = 0;
};

vector<Node> st;

void addtag(int id, ll v){
    st[id].mn += v;
    st[id].mx += v;
    st[id].tag += v;
}

void push(int id){
    addtag(2 * id + 1, st[id].tag);
    addtag(2 * id + 2, st[id].tag);
    st[id].tag = 0;
}

void modify(int l, int r, ll v, int L, int R, int id){
    if(l == L && r == R){
        addtag(id, v);
        return;
    }
    push(id);
    int M = (L + R) / 2;
    if(r <= M) modify(l, r, v, L, M, 2 * id + 1);
    else if(l > M) modify(l, r, v, M + 1, R, 2 * id + 2);
    else{
        modify(l, M, v, L, M, 2 * id + 1);
        modify(M + 1, r, v, M + 1, R, 2 * id + 2);
    }
    st[id].mn = min(st[2 * id + 1].mn, st[2 * id + 2].mn);
    st[id].mx = max(st[2 * id + 1].mx, st[2 * id + 2].mx);
}

bool type;
ll val;
int query(ll c, int L, int R, int id, ll nmn = LLMAX, ll nmx = -LLMAX){
    if(L == R){
        nmn = min(nmn, st[id].mn);
        nmx = max(nmx, st[id].mx);
//        cerr << "query " << L << " " << R << " " << st[id].mn << " " << nmn << " " << nmx << "\n";
        type = st[id].mn == nmn;
        val = type ? nmx : nmn;
        return L;
    }
    push(id);
    int M = (L + R) / 2;
    ll tmn = min(nmn, st[2 * id + 2].mn);
    ll tmx = max(nmx, st[2 * id + 2].mx);
    if(tmx - tmn >= c) return query(c, M + 1, R, 2 * id + 2, nmn, nmx);
    else return query(c, L, M, 2 * id + 1, tmn, tmx);
}

ll queryp(ll x, int L, int R, int id){
    if(L == R) return st[id].mn;
    push(id);
    int M = (L + R) / 2;
    if(x <= M) return queryp(x, L, M, 2 * id + 1);
    else return queryp(x, M + 1, R, 2 * id + 2);
}

void print(int L, int R, int id){
    if(L == R){
//        cerr << st[id].mn << " ";
        return;
    }
    push(id);
    int M = (L + R) / 2;
    print(L, M, 2 * id + 1);
    print(M + 1, R, 2 * id + 2);
}

vector<int> distribute_candies(vector<int> C, vector<int> L, vector<int> R, vector<int> V){
    int q = L.size();
    int n = C.size();

    st.resize(4 * (q + 1));

    vector<pair<pii, ll>> qry;

    for(int i = 0; i < q; i++){
        qry.eb(mp(mp(L[i], i + 1), V[i]));
        if(R[i] < n - 1) qry.eb(mp(mp(R[i] + 1, i + 1), -V[i]));
    }
    lsort(qry);

    vector<int> ans(n);
    int qp = 0;
    ll sum = 0;
    for(int i = 0; i < n; i++){
        while(qp < n && qry[qp].F.F == i){
            modify(qry[qp].F.S, q, qry[qp].S, 0, q, 0);
            sum += qry[qp].S;
            qp++;
        }

//        cerr << "test " << i << " " << st[0].mx << " " << st[0].mn << "\n";
//        print(0, q, 0);
//        cerr << "\n";

        if(st[0].mx - st[0].mn < C[i]){
            type = false;
            val = st[0].mn;
            assert(val == 0);
        }
        else{
            int t = query(C[i], 0, q, 0);
            assert(type && val == sum);
//            cerr << t << "\n";
        }
//        cerr << type << " " << val << " " << sum << "\n";

        if(type) ans[i] = sum - (val - C[i]);
        else ans[i] = sum - val;
    }

    return ans;
}

Compilation message

candies.cpp: In function 'std::vector<int> distribute_candies(std::vector<int>, std::vector<int>, std::vector<int>, std::vector<int>)':
candies.cpp:173:17: warning: unused variable 't' [-Wunused-variable]
  173 |             int t = query(C[i], 0, q, 0);
      |                 ^
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 204 KB Output is correct
2 Runtime error 1 ms 332 KB Execution killed with signal 6
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 345 ms 33620 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 1 ms 460 KB Execution killed with signal 6
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 1 ms 332 KB Execution killed with signal 6
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 204 KB Output is correct
2 Runtime error 1 ms 332 KB Execution killed with signal 6
3 Halted 0 ms 0 KB -