Submission #287507

# Submission time Handle Problem Language Result Execution time Memory
287507 2020-08-31T18:21:40 Z jainbot27 Happiness (Balkan15_HAPPINESS) C++17
0 / 100
454 ms 67064 KB
//author: JainBot27
//Created on: 08/31/20
#pragma GCC optimize("Ofast")
#pragma GCC target("sse4,avx,avx2,fma")
#include <bits/stdc++.h>
//#include "happiness.h"
using namespace std;


#define f first
#define s second
#define pb push_back
#define mp make_pair
#define ts to_string
#define ub upper_bound
#define lb lower_bound
#define ar array
#define all(x) x.begin(), x.end()
#define siz(x) (int)x.size()
#define FOR(x, y, z) for(int x = (y); x < (z); x++)
#define ROF(x, y, z) for(int x = (y-1); x >= (z); x--)
#define F0R(x, z) FOR(x, 0, z)
#define R0F(x, z) ROF(x, z, 0)
#define trav(x, y) for(auto&x:y)


const string nl = "\n";
using ll = int64_t;
using vi = vector<int>;
using vl = vector<ll>;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using str = string;
using vpii = vector<pii>;
using vpll = vector<pll>;


template<class T> inline bool ckmin(T&a, T b) {return b < a ? a = b, 1 : 0;}
template<class T> inline bool ckmax(T&a, T b) {return b > a ? a = b, 1 : 0;}
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());


str ts(char c) { return str(1,c); }
str ts(bool b) { return b ? "true" : "false"; }
str ts(const char* s) { return (str)s; }
str ts(str s) { return s; }
template<class A> str ts(complex<A> c) { 
    stringstream ss; ss << c; return ss.str(); }
str ts(vector<bool> v) { 
    str res = "{"; for(int i = 0;i < (int)v.size(); i++) res += char('0'+v[i]);
    res += "}"; return res; }
template<size_t SZ> str ts(bitset<SZ> b) {
    str res = ""; for(int i = 0; i < b.size(); i++) res += char('0'+b[i]);
    return res; }
template<class A, class B> str ts(pair<A,B> p);
template<class T> str ts(T v) { 
    bool fst = 1; str res = "{";
    for (const auto& x: v) {
        if (!fst) res += ", ";
        fst = 0; res += ts(x);
    }
    res += "}"; return res;
}
template<class A, class B> str ts(pair<A,B> p) {
    return "("+ts(p.f)+", "+ts(p.s)+")"; }
void DBG() { cerr << "]" << endl; }
template<class H, class... T> void DBG(H h, T... t) {
    cerr << ts(h); if (sizeof...(t)) cerr << ", ";
    DBG(t...); }


#ifdef D 
#define dbg(...) cerr << "LINE(" << __LINE__ << ") -> [" << #__VA_ARGS__ << "]: [", DBG(__VA_ARGS__)
#else
#define dbg(...) 0
#endif
using node = ll;

struct lazy{
    int size;
    vector<node> operations;
    vector<node> vals;
    const node NEUTRAL_ELEMENT = 1e18;
    const node NO_OPERATION = 0;
    const node START_VAL = 0;
    ll query_op(node a, node b, int len){
        len = len;
        return (a + b);
    }
    ll calc_op(node a, node b){
        return min(a, b);
    }
    void apply_mod_op(node & a, node b, int c){
        a = query_op(a, b, c);
    }
    void propogate(int x, int lx, int rx){
        if(rx - lx == 1) return;
        int m = (lx + rx)/2;
        apply_mod_op(operations[2*x+1], operations[x], 1);
        apply_mod_op(vals[2*x+1], operations[x], m-lx);
        apply_mod_op(operations[2*x+2], operations[x], 1);
        apply_mod_op(vals[2*x+2], operations[x], rx-m);
        operations[x] = NO_OPERATION;
    }
    void build(int x, int lx, int rx){
        if(rx == lx + 1){
            vals[x] = START_VAL;
            return;
        }
        int m = (lx + rx)/2;
        build(2*x + 1, lx, m);
        build(2*x + 2, m, rx);
        vals[x] = calc_op(vals[x * 2 + 1], vals[x * 2 + 2]);
    }
    void init(int n){
        size = 1;
        while(size < n) size *= 2;
        operations.assign(2 * size, 0);
        vals.assign(2 * size, 0);
        // build(0, 0, size);
    }
    void update(int l, int r, node v, int x, int lx, int rx){
        propogate(x, lx, rx);
        if(lx >= r || l >= rx) return;
        if(lx >= l && rx <= r){
            apply_mod_op(operations[x], v, 1);
            apply_mod_op(vals[x], v, rx - lx);
            return;
        }
        int m = (lx + rx)/2;
        update(l, r, v, 2 * x + 1, lx , m);
        update(l, r, v, 2 * x + 2, m , rx);
        vals[x] = calc_op(vals[x * 2 + 1], vals[x * 2 + 2]);
    }
    void update(int l, int r, node v){
        update(l, r, v, 0, 0, size);
    }
    node query(int l, int r, int x, int lx, int rx){
        propogate(x, lx, rx);
        if(lx >= r || l >= rx) return NEUTRAL_ELEMENT;
        if(lx >= l && rx <= r){
            return vals[x];
        }
        int m = (lx + rx)/2;
        node m1 = query(l, r, 2 * x + 1, lx, m);
        node m2 = query(l, r, 2 * x + 2, m, rx);
        return calc_op(m1, m2);
    }
    node query(int l , int r){
        return query(l, r, 0, 0, size);
    }
} st;
multiset<int> e;
bool init(int coinsCount, long long maxCoinSize, long long coins[]){
    st.init(1000001);
    //assert(maxCoinSize<=1000001);
    FOR(i, 1, 1000001){
        st.update(i, i+1, -i);
    }
    F0R(i, coinsCount){
        st.update(coins[i],1000001,coins[i]);
        e.insert(coins[i]);
    }
    //return (st.query(1, *(e.rbegin())+1)>=0?"YES":"NO") << nl;
    return st.query(1, *(e.rbegin())+1)>=0;
}
bool is_happy(int event, int coinsCount, long long coins[]){
    dbg(event);
    if(event == -1){
        F0R(i, coinsCount) coins[i]*=-1;
    }
    F0R(i, coinsCount){
        int x; x = coins[i];
        dbg(x);
        if(x < 0){ 
            assert(e.find(-x) != e.end());
            e.erase(e.find(-x));
        }
        else e.insert((x));
        if(x < 0){
            st.update(-x, 1000001, x);
        }
        else{
            st.update(x, 1000001, x);
        }
    }
    F0R(i, coinsCount) if(coins[i]<0) coins[i]*=-1;
    return st.query(1, *(e.rbegin())+1)>=0;
}

/*
signed main(){
    ios_base::sync_with_stdio(0); cin.tie(0);
    long long x[] = {4,8,1,2,6};
    cout << (init(5, 100, x)?"YES":"NO")<< nl; 
    long long y[] = {2, 8};
    cout << (is_happy(-1, 2, y)?"YES":"NO")<< nl; 
    long long z[] = {7, 9, 2};
    cout << (is_happy(1, 2, y)?"YES":"NO")<< nl; 
}
/*
 * Stuff I should try:
 * Is it Monotonic? -> Binary Search
 * Is N small? -> Bitsets or other exponential
 * Edge Cases? Look Carefully 
 * Can't figure anything out? Brute Force and see patterns
 * Try to prove greedies
 * Prefix Sums, Amortization, DP, Data Structures
*/

Compilation message

happiness.cpp:201:1: warning: "/*" within comment [-Wcomment]
  201 | /*
      |  
happiness.cpp: In function 'bool is_happy(int, int, long long int*)':
happiness.cpp:75:18: warning: statement has no effect [-Wunused-value]
   75 | #define dbg(...) 0
      |                  ^
happiness.cpp:168:5: note: in expansion of macro 'dbg'
  168 |     dbg(event);
      |     ^~~
happiness.cpp:75:18: warning: statement has no effect [-Wunused-value]
   75 | #define dbg(...) 0
      |                  ^
happiness.cpp:174:9: note: in expansion of macro 'dbg'
  174 |         dbg(x);
      |         ^~~
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 Runtime error 454 ms 67064 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 454 ms 67064 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 454 ms 67064 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 454 ms 67064 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -