Submission #275883

# Submission time Handle Problem Language Result Execution time Memory
275883 2020-08-20T08:14:12 Z 반딧불(#5115) Happiness (Balkan15_HAPPINESS) C++17
30 / 100
2000 ms 40084 KB
#include <bits/stdc++.h>
#pragma GCC target("avx2")
#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;
    Node *l=nullptr, *r=nullptr, *root=nullptr;

    Node(){}
    Node(ll s, ll e, Node *root): s(s), e(e), root(root){}
    ~Node(){
        if(l) delete l;
        if(r) delete r;
    }

    ll minSum(ll idx){ /// returning sum
        if(idx <= s) return 0;
        if(e < idx) return sum;
        ll ret = 0;
        if(l) ret += l->minSum(idx);
        if(r) ret += r->minSum(idx);
        return ret;
    }
    ll minMax(ll idx){ /// returning value
        if(idx <= s) return 0;
        if(s==e) return s;
        ll m = (s+e)/2;
        if(idx <= m+1){
            if(l) return l->minMax(idx);
            return 0;
        }
        if(r){
            ll tmp = r->minMax(idx);
            if(tmp) return tmp;
        }
        if(l){
            ll tmp = l->minMax(idx);
            if(tmp) return tmp;
        }
        return 0;
    }
    ll maxMin(ll idx){ /// returning value
        if(e <= idx) return LLONG_MAX;
        if(s==e) return s;
        ll m = (s+e)/2;
        if(m <= idx){
            if(r) return r->maxMin(idx);
            return LLONG_MAX;
        }
        if(l){
            ll tmp = l->maxMin(idx);
            if(tmp != LLONG_MAX) return tmp;
        }
        if(r){
            ll tmp = r->maxMin(idx);
            if(tmp != LLONG_MAX) return tmp;
        }
        return LLONG_MAX;
    }

    void addValue(ll idx, ll val){
        if(s==e){
            sum += val;

            ll lv = root->minMax(idx);
            ll rv = root->maxMin(idx);

            if(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{ /// deleted
                st.erase(idx);
                if(rv != LLONG_MAX){
                    if(lv*2 < rv) st.insert(rv);
                }
            }

            return;
        }
        ll m = (s+e)/2;
        if(idx <= m){
            if(!l) l = new Node(s, m, root);
            l->addValue(idx, val);
            if(!l->sum) delete l, l=nullptr;
        }
        else{
            if(!r) r = new Node(m+1, e, root);
            r->addValue(idx, val);
            if(!r->sum) delete r, r=nullptr;
        }
        sum = (!l ? 0 : l->sum) + (!r ? 0 : r->sum);
    }
};
Node* root;

bool isAble(){
    for(auto it=st.begin(); it!=st.end(); ++it){
        if(root->minSum(*it) + 1 < *it) return false;
    }
    return true;
}

bool init(int coinsCount, ll maxCoinSize, ll coins[]) {
    root = new Node();
    root->s= 1, root->e = maxCoinSize;
    root->root = root;

    for(int i=0; i<coinsCount; i++){
        root->addValue(coins[i], coins[i]);
    }
    return isAble();
}

bool is_happy(int event, int coinsCount, ll coins[]){
    for(int i=0; i<coinsCount; i++) root->addValue(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 1 ms 384 KB Output is correct
2 Correct 1 ms 384 KB Output is correct
3 Correct 1 ms 384 KB Output is correct
4 Correct 1 ms 384 KB Output is correct
5 Correct 1 ms 384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 384 KB Output is correct
2 Correct 1 ms 384 KB Output is correct
3 Correct 1 ms 384 KB Output is correct
4 Correct 1 ms 384 KB Output is correct
5 Correct 1 ms 384 KB Output is correct
6 Correct 7 ms 2304 KB Output is correct
7 Correct 9 ms 2304 KB Output is correct
8 Correct 78 ms 18040 KB Output is correct
9 Correct 79 ms 18040 KB Output is correct
10 Correct 68 ms 17912 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 384 KB Output is correct
2 Correct 1 ms 384 KB Output is correct
3 Correct 1 ms 384 KB Output is correct
4 Correct 1 ms 384 KB Output is correct
5 Correct 1 ms 384 KB Output is correct
6 Correct 1180 ms 30200 KB Output is correct
7 Correct 1152 ms 30588 KB Output is correct
8 Correct 1191 ms 30476 KB Output is correct
9 Correct 1753 ms 30656 KB Output is correct
10 Correct 1536 ms 40084 KB Output is correct
11 Correct 412 ms 35960 KB Output is correct
12 Execution timed out 2076 ms 35928 KB Time limit exceeded
# Verdict Execution time Memory Grader output
1 Correct 1 ms 384 KB Output is correct
2 Correct 1 ms 384 KB Output is correct
3 Correct 1 ms 384 KB Output is correct
4 Correct 1 ms 384 KB Output is correct
5 Correct 1 ms 384 KB Output is correct
6 Correct 7 ms 2304 KB Output is correct
7 Correct 9 ms 2304 KB Output is correct
8 Correct 78 ms 18040 KB Output is correct
9 Correct 79 ms 18040 KB Output is correct
10 Correct 68 ms 17912 KB Output is correct
11 Correct 1180 ms 30200 KB Output is correct
12 Correct 1152 ms 30588 KB Output is correct
13 Correct 1191 ms 30476 KB Output is correct
14 Correct 1753 ms 30656 KB Output is correct
15 Correct 1536 ms 40084 KB Output is correct
16 Correct 412 ms 35960 KB Output is correct
17 Execution timed out 2076 ms 35928 KB Time limit exceeded