Problem p02648 (C++) — buggy submission, verdict: Time Limit Exceeded


#pragma GCC target("avx2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#include <algorithm>
#include <array>
#include <cstring>
#include <iostream>
#include <numeric>
#include <utility>
#include <vector>

namespace fast_io {
static constexpr size_t buf_size = 1 << 18;
static constexpr size_t buf_margin = 1;
static constexpr size_t block_size = 10000;
static constexpr size_t integer_size = 20;
size_t in_pos = 0, in_end = buf_size, out_pos = 0;

static char inbuf[buf_size + buf_margin] = {};
static char outbuf[buf_size + buf_margin] = {};
static char block_str[block_size * 4 + buf_margin] = {};

template <class T, class U>
using if_integral =
    typename std::enable_if<std::is_integral<T>::value, U>::type;

static constexpr uint64_t power10[] = {1,
                                       10,
                                       100,
                                       1000,
                                       10000,
                                       100000,
                                       1000000,
                                       10000000,
                                       100000000,
                                       1000000000,
                                       10000000000,
                                       100000000000,
                                       1000000000000,
                                       10000000000000,
                                       100000000000000,
                                       1000000000000000,
                                       10000000000000000,
                                       100000000000000000,
                                       1000000000000000000,
                                       10000000000000000000u};

class scanner {
private:
  void M_load() {
    in_end = fread(inbuf, 1, buf_size, stdin);
    inbuf[in_end] = '\\0';
  }
  void M_reload() {
    size_t length = in_end - in_pos;
    memmove(inbuf, inbuf + in_pos, length);
    in_end = length + fread(inbuf + length, 1, buf_size - length, stdin);
    inbuf[in_end] = '\\0';
    in_pos = 0;
  }

  void M_ignore_space() {
    while (inbuf[in_pos] <= ' ') {
      if (__builtin_expect(++in_pos == in_end, 0))
        M_reload();
    }
  }

  char M_next() { return inbuf[in_pos++]; }
  char M_next_nonspace() {
    M_ignore_space();
    return inbuf[in_pos++];
  }

public:
  scanner() { M_load(); }

  void scan(char &c) { c = M_next_nonspace(); }
  void scan(std::string &s) {
    M_ignore_space();
    s = "";
    do {
      size_t start = in_pos;
      while (inbuf[in_pos] > ' ')
        ++in_pos;
      s += std::string(inbuf + start, inbuf + in_pos);
      if (inbuf[in_pos] != '\\0')
        break;
      M_reload();
    } while (true);
  }

  template <class T> if_integral<T, void> scan(T &x) {
    char c = M_next_nonspace();
    if (__builtin_expect(in_pos + integer_size >= in_end, 0))
      M_reload();
    bool n = false;
    if (c == '-')
      n = true, x = 0;
    else
      x = c & 15;
    while ((c = M_next()) >= '0')
      x = x * 10 + (c & 15);
    if (n)
      x = -x;
  }

  template <class T, class... Args> void scan(T &x, Args &...args) {
    scan(x);
    scan(args...);
  }
};

class printer {
private:
  void M_flush() {
    fwrite(outbuf, 1, out_pos, stdout);
    out_pos = 0;
  }

  void M_precompute() {
    for (size_t i = 0; i < block_size; ++i) {
      size_t j = 4, k = i;
      while (j--) {
        block_str[i * 4 + j] = k % 10 + '0';
        k /= 10;
      }
    }
  }

  static constexpr size_t M_integer_digits(uint64_t n) {
    if (n >= power10[10]) {
      if (n >= power10[19])
        return 20;
      if (n >= power10[18])
        return 19;
      if (n >= power10[17])
        return 18;
      if (n >= power10[16])
        return 17;
      if (n >= power10[15])
        return 16;
      if (n >= power10[14])
        return 15;
      if (n >= power10[13])
        return 14;
      if (n >= power10[12])
        return 13;
      if (n >= power10[11])
        return 12;
      return 11;
    } else {
      if (n >= power10[9])
        return 10;
      if (n >= power10[8])
        return 9;
      if (n >= power10[7])
        return 8;
      if (n >= power10[6])
        return 7;
      if (n >= power10[5])
        return 6;
      if (n >= power10[4])
        return 5;
      if (n >= power10[3])
        return 4;
      if (n >= power10[2])
        return 3;
      if (n >= power10[1])
        return 2;
      return 1;
    }
  }

public:
  printer() { M_precompute(); }
  ~printer() { M_flush(); }

  void print(char c) {
    outbuf[out_pos++] = c;
    if (__builtin_expect(out_pos == buf_size, 0))
      M_flush();
  }
  void print(const char *s) {
    while (*s != 0) {
      outbuf[out_pos++] = *s++;
      if (out_pos == buf_size)
        M_flush();
    }
  }
  void print(const std::string &s) {
    for (auto c : s) {
      outbuf[out_pos++] = c;
      if (out_pos == buf_size)
        M_flush();
    }
  }

  template <class T> if_integral<T, void> print(T x) {
    if (__builtin_expect(out_pos + integer_size >= buf_size, 0))
      M_flush();
    if (x < 0)
      print('-'), x = -x;
    size_t digit = M_integer_digits(x);
    size_t len = digit;
    while (len >= 4) {
      len -= 4;
      memcpy(outbuf + out_pos + len, block_str + (x % block_size) * 4, 4);
      x /= 10000;
    }
    memcpy(outbuf + out_pos, block_str + x * 4 + 4 - len, len);
    out_pos += digit;
  }

  template <class T, class... Args>
  void print(const T &x, const Args &...args) {
    print(x);
    print(' ');
    print(args...);
  }

  template <class... Args> void println(const Args &...args) {
    print(args...);
    print('\\n');
  }
};

}; // namespace fast_io

fast_io::scanner cin;
fast_io::printer cout;

using i32 = int32_t;
using i64 = int64_t;
using u32 = uint32_t;
using u64 = uint64_t;

constexpr i32 inf32 = (i32(1) << 30) - 1;
constexpr i64 inf64 = (i64(1) << 62) - 1;

i32 N, Q;
i32 V[1 << 18], W[1 << 18];
std::pair<i32, i32> top[1 << 9];
i32 botv[1 << 9], botw[1 << 9];
i32 best[1 << 9];

void chmax(i32 &lhs, i32 rhs) {
  if (lhs < rhs)
    lhs = rhs;
}
void chmax(i64 &lhs, i64 rhs) {
  if (lhs < rhs)
    lhs = rhs;
}

int main() {
  cin.scan(N);
  for (i32 i = 1; i <= N; ++i) {
    cin.scan(V[i], W[i]);
  }
  cin.scan(Q);
  while (Q--) {
    i32 v, l;
    cin.scan(v, l);
    i32 topsize = 1, botsize = 1;
    while (v >= 512) {
      for (i32 i = 0; i < botsize; ++i) {
        botv[i + botsize] = botv[i] + V[v];
        botw[i + botsize] = botw[i] + W[v];
      }
      botsize <<= 1;
      v >>= 1;
    }
    while (v >= 1) {
      for (i32 i = 0; i < topsize; ++i) {
        top[i + topsize] = {top[i].first + W[v], top[i].second + V[v]};
      }
      topsize <<= 1;
      v >>= 1;
    }
    std::sort(top, top + topsize);
    best[0] = top[0].second;
    for (i32 i = 1; i < topsize; ++i) {
      best[i] = best[i - 1];
      chmax(best[i], top[i].second);
    }
    i64 ans = 0;
    for (i32 i = 0; i < botsize; ++i) {
      if (botw[i] > l)
        continue;
      chmax(ans, (i64)botv[i] +
                     best[std::lower_bound(top, top + topsize,
                                           std::make_pair(l - botw[i] + 1, 0)) -
                          top - 1]);
    }
    cout.println(ans);
  }
  return 0;
}


Error: TLE

Fixed submission:

#pragma GCC target("avx2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#include <algorithm>
#include <array>
#include <cstring>
#include <iostream>
#include <numeric>
#include <utility>
#include <vector>

struct IO {
  static constexpr size_t buf_size = 1 << 18;
  char buf_in[buf_size], buf_out[buf_size];
  size_t pt_in = 0, pt_out = 0, tail_in = 0;
  char strs[10000 * 4];
  inline size_t num_digits(long long x) {
    if (x >= (long long)1e9) {
      if (x >= (long long)1e18)
        return 19;
      if (x >= (long long)1e17)
        return 18;
      if (x >= (long long)1e16)
        return 17;
      if (x >= (long long)1e15)
        return 16;
      if (x >= (long long)1e14)
        return 15;
      if (x >= (long long)1e13)
        return 14;
      if (x >= (long long)1e12)
        return 13;
      if (x >= (long long)1e11)
        return 12;
      if (x >= (long long)1e10)
        return 11;
      return 10;
    } else {
      if (x >= (long long)1e8)
        return 9;
      if (x >= (long long)1e7)
        return 8;
      if (x >= (long long)1e6)
        return 7;
      if (x >= (long long)1e5)
        return 6;
      if (x >= (long long)1e4)
        return 5;
      if (x >= (long long)1e3)
        return 4;
      if (x >= (long long)1e2)
        return 3;
      if (x >= (long long)1e1)
        return 2;
      return 1;
    }
  }
  IO() {
    load();
    for (int i = 0; i < 10000; ++i) {
      int j = i;
      for (int t = 3; t >= 0; --t) {
        strs[i * 4 + t] = j % 10 + '0';
        j /= 10;
      }
    }
  }
  ~IO() { flush(); }
  inline void load() {
    memcpy(buf_in, buf_in + pt_in, tail_in - pt_in);
    size_t width = tail_in - pt_in;
    tail_in = width + fread(buf_in + width, 1, buf_size - width, stdin);
    pt_in = 0;
  }
  inline void flush() {
    fwrite(buf_out, 1, pt_out, stdout);
    pt_out = 0;
  }
  inline void read(char &c) { c = buf_in[pt_in++]; }
  template <class T> inline void read(T &x) {
    if (pt_in + 32 > tail_in)
      load();
    char c;
    do {
      read(c);
    } while (c < '0');
    bool minus = 0;
    if (c == '-') {
      minus = 1;
      read(c);
    }
    x = 0;
    while (c >= '0') {
      x = x * 10 + (c & 15);
      read(c);
    }
    if (minus)
      x = -x;
  }
  inline void write(char c) { buf_out[pt_out++] = c; }
  template <class T> inline void write(T x) {
    if (pt_out > buf_size - 32)
      flush();
    if (x < 0) {
      write('-');
      x = -x;
    }
    size_t digits = num_digits(x);
    int i;
    for (i = pt_out + digits - 4; i > (int)pt_out; i -= 4) {
      memcpy(buf_out + i, strs + (x % 10000) * 4, 4);
      x /= 10000;
    }
    memcpy(buf_out + pt_out, strs + x * 4 + (pt_out - i), 4 + i - pt_out);
    pt_out += digits;
  }
  inline void write(const char *s) {
    if (pt_out > buf_size - 32)
      flush();
    for (int i = 0; s[i] != 0; ++i)
      write(s[i]);
  }
  template <class T> inline void writeln(T x) {
    write(x);
    write('\\n');
  }
} io;

using i32 = int32_t;
using i64 = int64_t;
using u32 = uint32_t;
using u64 = uint64_t;

constexpr i32 inf32 = (i32(1) << 30) - 1;
constexpr i64 inf64 = (i64(1) << 62) - 1;

i32 N, Q;
i32 V[1 << 18], W[1 << 18];
std::pair<i32, i32> top[1 << 9];
i32 botv[1 << 9], botw[1 << 9];
i32 best[1 << 9];

void chmax(i32 &lhs, i32 rhs) {
  if (lhs < rhs)
    lhs = rhs;
}
void chmax(i64 &lhs, i64 rhs) {
  if (lhs < rhs)
    lhs = rhs;
}

int main() {
  io.read(N);
  for (i32 i = 1; i <= N; ++i) {
    io.read(V[i]);
    io.read(W[i]);
  }
  io.read(Q);
  while (Q--) {
    i32 v, l;
    io.read(v);
    io.read(l);
    i32 topsize = 1, botsize = 1;
    while (v >= 512) {
      for (i32 i = 0; i < botsize; ++i) {
        botv[i + botsize] = botv[i] + V[v];
        botw[i + botsize] = botw[i] + W[v];
      }
      botsize <<= 1;
      v >>= 1;
    }
    while (v >= 1) {
      for (i32 i = 0; i < topsize; ++i) {
        top[i + topsize] = {top[i].first + W[v], top[i].second + V[v]};
      }
      topsize <<= 1;
      v >>= 1;
    }
    std::sort(top, top + topsize);
    best[0] = top[0].second;
    for (i32 i = 1; i < topsize; ++i) {
      best[i] = best[i - 1];
      chmax(best[i], top[i].second);
    }
    i64 ans = 0;
    for (i32 i = 0; i < botsize; ++i) {
      if (botw[i] > l)
        continue;
      chmax(ans, (i64)botv[i] +
                     best[std::lower_bound(top, top + topsize,
                                           std::make_pair(l - botw[i] + 1, 0)) -
                          top - 1]);
    }
    io.writeln(ans);
  }
  return 0;
}