답안 #682392

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
682392 2023-01-16T07:58:26 Z MilosMilutinovic Happiness (Balkan15_HAPPINESS) C++14
30 / 100
2000 ms 8300 KB
#include "happiness.h"
#include <bits/stdc++.h>

using namespace std;

const int MAX = 200005;
const long long inf = (long long) 1.01e12;

int root, ls[MAX * 20], rs[MAX * 20], tsz;
long long sum[MAX * 20];
long long tot[MAX];
long long f[MAX];

void modify(int& v, long long l, long long r, long long i, int x) {
  if (!v) {
    v = ++tsz;
  }
  sum[v] += x * i;
  if (l == r) {
    return;
  }
  long long mid = l + r >> 1;
  if (i <= mid) {
    modify(ls[v], l, mid, i, x);
  } else {
    modify(rs[v], mid + 1, r, i, x);
  }
}

long long query(int v, long long l, long long r, long long ql, long long qr) {
  if (ql <= l && r <= qr) {
    return sum[v];
  }
  if (l > qr || r < ql || l > r) {
    return 0LL;
  }
  long long mid = l + r >> 1;
  return query(ls[v], l, mid, ql, qr) + query(rs[v], mid + 1, r, ql, qr);
}

long long walk(long long s) {
  long long l = s, r = inf, pos = inf;
  long long target = query(root, 1, inf, s, inf);
  while (l <= r) {
    long long mid = l + r >> 1;
    if (query(root, 1, inf, mid, inf) == target) {
      pos = mid;
      l = mid + 1;
    } else {
      r = mid - 1;
    }
  }
  return pos;
}

bool Check() {
  if (query(root, 1, inf, 1, inf) == 0) {
    return true;
  }
  if (query(root, 1, inf, 1, 1) == 0) {
    return false;
  }
  long long p = 1;
  while (true) {
    if (query(root, 1, inf, p + 1, inf) == 0) {
      break;
    }
    long long s = query(root, 1, inf, 1, p);
    long long nxt = walk(s + 2);
    if (nxt == inf) {
      break;
    }
    if (query(root, 1, inf, 1, nxt - 1) + 1 < nxt) {
      return false;
    }
    p = nxt;
  }
  return true;
}

bool init(int coinsCount, long long maxCoinSize, long long coins[]) {
  for (int i = 0; i < coinsCount; i++) {
    modify(root, 1, inf, coins[i], +1);
  }
  return Check();
}

bool is_happy(int event, int coinsCount, long long coins[]) {
  if (event == -1) {
    for (int i = 0; i < coinsCount; i++) {
      assert(query(root, 1, inf, coins[i], coins[i]) > 0);
      modify(root, 1, inf, coins[i], -1);
    }
  } else {
    for (int i = 0; i < coinsCount; i++) {
      modify(root, 1, inf, coins[i], +1);
    }
  }
  return Check();
}

Compilation message

happiness.cpp: In function 'void modify(int&, long long int, long long int, long long int, int)':
happiness.cpp:22:21: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   22 |   long long mid = l + r >> 1;
      |                   ~~^~~
happiness.cpp: In function 'long long int query(int, long long int, long long int, long long int, long long int)':
happiness.cpp:37:21: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   37 |   long long mid = l + r >> 1;
      |                   ~~^~~
happiness.cpp: In function 'long long int walk(long long int)':
happiness.cpp:45:23: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   45 |     long long mid = l + r >> 1;
      |                     ~~^~~
grader.cpp: In function 'int main()':
grader.cpp:16:12: warning: unused variable 'max_code' [-Wunused-variable]
   16 |  long long max_code;
      |            ^~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 4 ms 340 KB Output is correct
4 Correct 3 ms 340 KB Output is correct
5 Correct 3 ms 348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 4 ms 340 KB Output is correct
4 Correct 3 ms 340 KB Output is correct
5 Correct 3 ms 348 KB Output is correct
6 Correct 18 ms 844 KB Output is correct
7 Correct 16 ms 876 KB Output is correct
8 Correct 199 ms 5068 KB Output is correct
9 Correct 208 ms 5160 KB Output is correct
10 Correct 207 ms 4928 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 4 ms 340 KB Output is correct
4 Correct 3 ms 340 KB Output is correct
5 Correct 3 ms 348 KB Output is correct
6 Execution timed out 2072 ms 8300 KB Time limit exceeded
7 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 4 ms 340 KB Output is correct
4 Correct 3 ms 340 KB Output is correct
5 Correct 3 ms 348 KB Output is correct
6 Correct 18 ms 844 KB Output is correct
7 Correct 16 ms 876 KB Output is correct
8 Correct 199 ms 5068 KB Output is correct
9 Correct 208 ms 5160 KB Output is correct
10 Correct 207 ms 4928 KB Output is correct
11 Execution timed out 2072 ms 8300 KB Time limit exceeded
12 Halted 0 ms 0 KB -