Submission #275701

# Submission time Handle Problem Language Result Execution time Memory
275701 2020-08-20T07:12:44 Z 반딧불(#5115) Happiness (Balkan15_HAPPINESS) C++17
60 / 100
2000 ms 264220 KB
#include <unordered_set>
#include <climits>
#include <vector>
#pragma GCC optimize("O3")
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#include "happiness.h"

using namespace std;

typedef long long ll;

unordered_set<ll> st;
struct Node{
    ll s, e;
    ll sum = 0;
    int l=0, r=0;

    Node(){}
    Node(ll s, ll e): s(s), e(e){}
};
vector<Node> vec;

inline ll minSum(int i, ll idx){ /// returning sum
    if(idx <= vec[i].s) return 0;
    if(vec[i].e < idx) return vec[i].sum;
    ll ret = 0;
    if(vec[i].l && vec[vec[i].l].sum) ret += minSum(vec[i].l, idx);
    if(vec[i].r && vec[vec[i].r].sum) ret += minSum(vec[i].r, idx);
    return ret;
}

inline ll minMax(int i, ll idx){ /// returning value
    if(idx <= vec[i].s) return 0;
    if(vec[i].s==vec[i].e) return vec[i].s;
    ll m = (vec[i].s+vec[i].e)>>1;
    if(idx <= m+1){
        if(vec[i].l && vec[vec[i].l].sum) return minMax(vec[i].l, idx);
        return 0;
    }
    if(vec[i].r && vec[vec[i].r].sum){
        ll tmp = minMax(vec[i].r, idx);
        if(tmp) return tmp;
    }
    if(vec[i].l && vec[vec[i].l].sum){
        ll tmp = minMax(vec[i].l, idx);
        if(tmp) return tmp;
    }
    return 0;
}

inline ll maxMin(int i, ll idx){ /// returning value
    if(vec[i].e <= idx) return LLONG_MAX;
    if(vec[i].s==vec[i].e) return vec[i].s;
    ll m = (vec[i].s+vec[i].e)>>1;
    if(m <= idx){
        if(vec[i].r && vec[vec[i].r].sum) return maxMin(vec[i].r, idx);
        return LLONG_MAX;
    }
    if(vec[i].l && vec[vec[i].l].sum){
        ll tmp = maxMin(vec[i].l, idx);
        if(tmp != LLONG_MAX) return tmp;
    }
    if(vec[i].r && vec[vec[i].r].sum){
        ll tmp = maxMin(vec[i].r, idx);
        if(tmp != LLONG_MAX) return tmp;
    }
    return LLONG_MAX;
}

void addValue(int i, ll idx, ll val){
    if(vec[i].s==vec[i].e){
        vec[i].sum += val;

        ll lv = minMax(0, idx);
        ll rv = maxMin(0, idx);

        if(vec[i].sum){ /// adding
            if(lv*2 < idx) st.insert(idx);
            else st.erase(idx);
            if(rv != LLONG_MAX){
                if(idx*2 < rv) st.insert(rv);
                else st.erase(rv);
            }
        }
        else{ /// deleted
            st.erase(idx);
            if(rv != LLONG_MAX){
                if(lv*2 < rv) st.insert(rv);
                else st.erase(rv);
            }
        }
        return;
    }
    ll m = (vec[i].s+vec[i].e)>>1;
    if(idx <= m){
        if(!vec[i].l) vec[i].l = (int)vec.size(), vec.push_back(Node(vec[i].s, m));
        addValue(vec[i].l, idx, val);
    }
    else{
        if(!vec[i].r) vec[i].r = (int)vec.size(), vec.push_back(Node(m+1, vec[i].e));
        addValue(vec[i].r, idx, val);
    }
    vec[i].sum = (!vec[i].l ? 0 : vec[vec[i].l].sum) + (!vec[i].r ? 0 : vec[vec[i].r].sum);
}

int n, cnt;

bool isAble(){
    for(auto &x: st){
//        printf("strange(%lld) ", x);
        if(minSum(0, x) + 1 < x) return false;
    }
    return true;
}

bool init(int coinsCount, ll maxCoinSize, ll coins[]) {
    n = coinsCount;
    vec.push_back(Node(1, maxCoinSize));

    for(int i=0; i<n; i++){
        addValue(0, coins[i], coins[i]);
    }
    return isAble();
}

bool is_happy(int event, int coinsCount, ll coins[]) {
    n += coinsCount * event;
    for(int i=0; i<coinsCount; i++) addValue(0, coins[i], coins[i] * event);
    return isAble();
}

Compilation message

grader.cpp: In function 'int main()':
grader.cpp:16:12: warning: unused variable 'max_code' [-Wunused-variable]
   16 |  long long max_code;
      |            ^~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 0 ms 256 KB Output is correct
2 Correct 0 ms 256 KB Output is correct
3 Correct 1 ms 256 KB Output is correct
4 Correct 1 ms 256 KB Output is correct
5 Correct 1 ms 256 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 256 KB Output is correct
2 Correct 0 ms 256 KB Output is correct
3 Correct 1 ms 256 KB Output is correct
4 Correct 1 ms 256 KB Output is correct
5 Correct 1 ms 256 KB Output is correct
6 Correct 5 ms 1528 KB Output is correct
7 Correct 6 ms 2548 KB Output is correct
8 Correct 56 ms 16984 KB Output is correct
9 Correct 66 ms 16984 KB Output is correct
10 Correct 54 ms 16992 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 256 KB Output is correct
2 Correct 0 ms 256 KB Output is correct
3 Correct 1 ms 256 KB Output is correct
4 Correct 1 ms 256 KB Output is correct
5 Correct 1 ms 256 KB Output is correct
6 Correct 916 ms 34272 KB Output is correct
7 Correct 896 ms 34108 KB Output is correct
8 Correct 916 ms 34276 KB Output is correct
9 Correct 1395 ms 34388 KB Output is correct
10 Correct 1192 ms 34104 KB Output is correct
11 Correct 430 ms 33468 KB Output is correct
12 Correct 485 ms 33468 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 256 KB Output is correct
2 Correct 0 ms 256 KB Output is correct
3 Correct 1 ms 256 KB Output is correct
4 Correct 1 ms 256 KB Output is correct
5 Correct 1 ms 256 KB Output is correct
6 Correct 5 ms 1528 KB Output is correct
7 Correct 6 ms 2548 KB Output is correct
8 Correct 56 ms 16984 KB Output is correct
9 Correct 66 ms 16984 KB Output is correct
10 Correct 54 ms 16992 KB Output is correct
11 Correct 916 ms 34272 KB Output is correct
12 Correct 896 ms 34108 KB Output is correct
13 Correct 916 ms 34276 KB Output is correct
14 Correct 1395 ms 34388 KB Output is correct
15 Correct 1192 ms 34104 KB Output is correct
16 Correct 430 ms 33468 KB Output is correct
17 Correct 485 ms 33468 KB Output is correct
18 Execution timed out 2035 ms 264220 KB Time limit exceeded
19 Halted 0 ms 0 KB -