Submission #546843

# Submission time Handle Problem Language Result Execution time Memory
546843 2022-04-08T16:20:13 Z Trisanu_Das Distributing Candies (IOI21_candies) C++17
Compilation error
0 ms 0 KB
#include "candies.h"
#include <bits/stdc++.h>

struct SqrtDecomp{
  int n;
  int bucket_size, bucket_cnt;
  std::vector<int> vals;
  std::vector<long long> mini, maxi, sums;
  Buckets(int _n): n(_n) {
    bucket_size = sqrt(n);
    bucket_cnt = (n + bucket_size - 1) / bucket_size;
    vals.assign(n, 0);
    mini.assign(bucket_cnt, 0);
    maxi.assign(bucket_cnt, 0);
    sums.assign(bucket_cnt, 0);
  }

  void upd(int x, int val) {
    vals[x] = val;
    int bucket = x / bucket_size;

    mini[bucket] = maxi[bucket] = sums[bucket] = 0;
    for (int i = std::min(n, (bucket + 1) * bucket_size) - 1; 
         i >= bucket * bucket_size; --i) {
      sums[bucket] += vals[i];
      maxi[bucket] = std::max(maxi[bucket], sums[bucket]);
      mini[bucket] = std::min(mini[bucket], sums[bucket]);
    }
  }

  std::tuple<int, int, int> query(int c) {
    int sufsum = 0, sufmin = 0, sufmax = 0;
    for (int bucket = bucket_cnt - 1; bucket >= 0; --bucket) {
      long long nsufsum = sufsum + sums[bucket],
                nsufmin = std::min(1LL * sufmin, sufsum + mini[bucket]),
                nsufmax = std::max(1LL * sufmax, sufsum + maxi[bucket]);
      if (nsufmax - nsufmin > c) {
        for (int i = std::min(n, (bucket + 1) * bucket_size) - 1; 
             i >= bucket * bucket_size; --i) {
          sufsum += vals[i];
          sufmin = std::min(sufmin, sufsum);
          sufmax = std::max(sufmax, sufsum);
          if (sufmax - sufmin > c) return std::make_tuple(sufmin, sufmax, i);
        }
      }
      sufsum = nsufsum; sufmax = nsufmax; sufmin = nsufmin;
    }
    return std::make_tuple(sufmin, sufmax, 0);
  }

  inline int at(int x) { return vals[x]; }
};

std::vector<int> distribute_candies(std::vector<int> c, std::vector<int> l,
                                    std::vector<int> r, std::vector<int> v) {
  int n = c.size(), q = v.size();
  std::vector<std::vector<std::pair<int, int>>> queriesL(n), queriesR(n);
  for (int i = 0; i < q; ++i) {
    queriesL[l[i]].emplace_back(i, v[i]);
    queriesR[r[i]].emplace_back(i, v[i]);
  }

  std::vector<int> s(n);
  SqrtDecomp tree(q);
  for (int i = 0; i < n; ++i) {
    for (auto [idx, val] : queriesL[i]) {
      tree.upd(idx, -val);
    }

    auto [mini, maxi, idx] = tree.query(c[i]);
    if (maxi - mini <= c[i]) {
      s[i] = -mini;
    } else {
      if (tree.at(idx) < 0) {
        s[i] = c[i] - maxi;
      } else {
        s[i] = -mini;
      }
    }

    for (auto [idx, val] : queriesR[i]) {
      tree.upd(idx, 0);
    }
  }

  return s;
}

Compilation message

candies.cpp:9:3: error: ISO C++ forbids declaration of 'Buckets' with no type [-fpermissive]
    9 |   Buckets(int _n): n(_n) {
      |   ^~~~~~~
candies.cpp: In member function 'int SqrtDecomp::Buckets(int)':
candies.cpp:9:20: error: only constructors take member initializers
    9 |   Buckets(int _n): n(_n) {
      |                    ^
candies.cpp:16:3: warning: no return statement in function returning non-void [-Wreturn-type]
   16 |   }
      |   ^
candies.cpp: In function 'std::vector<int> distribute_candies(std::vector<int>, std::vector<int>, std::vector<int>, std::vector<int>)':
candies.cpp:64:20: error: no matching function for call to 'SqrtDecomp::SqrtDecomp(int&)'
   64 |   SqrtDecomp tree(q);
      |                    ^
candies.cpp:4:8: note: candidate: 'SqrtDecomp::SqrtDecomp()'
    4 | struct SqrtDecomp{
      |        ^~~~~~~~~~
candies.cpp:4:8: note:   candidate expects 0 arguments, 1 provided
candies.cpp:4:8: note: candidate: 'SqrtDecomp::SqrtDecomp(const SqrtDecomp&)'
candies.cpp:4:8: note:   no known conversion for argument 1 from 'int' to 'const SqrtDecomp&'
candies.cpp:4:8: note: candidate: 'SqrtDecomp::SqrtDecomp(SqrtDecomp&&)'
candies.cpp:4:8: note:   no known conversion for argument 1 from 'int' to 'SqrtDecomp&&'