# | 제출 시각 | 아이디 | 문제 | 언어 | 결과 | 실행 시간 | 메모리 |
---|---|---|---|---|---|---|---|
568951 | Dan4Life | Happiness (Balkan15_HAPPINESS) | C++17 | 0 ms | 0 KiB |
이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include "happiness.h"
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define pb push_back
#define fi first
#define se second
int index = 0; ll mx;
vector<ll> segTree;
vector<pair<int,int>> child;
void add_node(ll v=0,int le=-1,int ri=-1){
segTree.pb(v), child.pb({le,ri});
}
void update(ll i, int v, int p=0, ll l=1, ll r=mx){
if(l==r) { segTree[p]+=v; return; }
ll mid = (l+r)/2;
if(i<=mid){
if(child[p].fi==-1) child[p].fi=++index, add_node();
update(i,v,child[p].fi,l,mid);
}
else{
if(child[p].se==-1) child[p].se=++index, add_node();
update(i,v,child[p].se,mid+1,r);
}
segTree[p] = segTree[child[p].fi]+segTree[child[p].se];
}
ll query(ll i, ll j, int p=0, ll l=1, ll r=mx){
if(i>r or j<l or i>j or p==-1) return 0ll;
if(i<=l and r<=j) return segTree[p];
ll mid = (l+r)/2;
return query(i,j,child[p].fi,l,mid)+query(i,j,child[p].se,mid+1,r);
}
bool chk(){
ll coin = 1;
while(1){
int sum = query(1,coin);
if(sum+1>mx) return true;
if(query(1,mx)==sum) return true;
if(query(1,sum+1)==sum) return false;
coin = sum+1;
}
}
bool init(int n, ll M, ll a[]) {
mx = M; add_node();
for(int i = 0; i < n; i++) update(a[i],a[i]);
return chk();
}
bool is_happy(int e, int n, ll a[]) {
for(int i = 0; i < n; i++) update(a[i],a[i]*e);
return chk();
}