結果

問題 No.1505 Zero-Product Ranges
ユーザー kichi2004_kichi2004_
提出日時 2021-04-01 23:54:32
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 40 ms / 2,000 ms
コード長 5,648 bytes
コンパイル時間 8,600 ms
コンパイル使用メモリ 272,868 KB
実行使用メモリ 5,408 KB
最終ジャッジ日時 2023-08-23 07:04:33
合計ジャッジ時間 11,124 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 16 ms
5,408 KB
testcase_04 AC 13 ms
4,376 KB
testcase_05 AC 40 ms
4,496 KB
testcase_06 AC 35 ms
4,380 KB
testcase_07 AC 29 ms
4,376 KB
testcase_08 AC 34 ms
4,380 KB
testcase_09 AC 33 ms
4,380 KB
testcase_10 AC 6 ms
4,376 KB
testcase_11 AC 6 ms
4,376 KB
testcase_12 AC 5 ms
4,376 KB
testcase_13 AC 9 ms
4,376 KB
testcase_14 AC 8 ms
4,380 KB
testcase_15 AC 13 ms
4,376 KB
testcase_16 AC 19 ms
4,504 KB
testcase_17 AC 19 ms
4,380 KB
testcase_18 AC 19 ms
4,428 KB
testcase_19 AC 1 ms
4,376 KB
testcase_20 AC 2 ms
4,380 KB
testcase_21 AC 2 ms
4,380 KB
testcase_22 AC 19 ms
4,500 KB
testcase_23 AC 19 ms
4,376 KB
testcase_24 AC 18 ms
4,380 KB
testcase_25 AC 17 ms
4,408 KB
testcase_26 AC 17 ms
4,412 KB
testcase_27 AC 18 ms
4,376 KB
testcase_28 AC 17 ms
4,376 KB
testcase_29 AC 19 ms
4,392 KB
testcase_30 AC 19 ms
4,404 KB
testcase_31 AC 18 ms
4,380 KB
testcase_32 AC 10 ms
4,376 KB
testcase_33 AC 9 ms
4,376 KB
testcase_34 AC 11 ms
4,380 KB
testcase_35 AC 8 ms
4,380 KB
testcase_36 AC 9 ms
4,376 KB
testcase_37 AC 11 ms
4,376 KB
testcase_38 AC 10 ms
4,376 KB
testcase_39 AC 10 ms
4,376 KB
testcase_40 AC 10 ms
4,380 KB
testcase_41 AC 11 ms
4,380 KB
testcase_42 AC 3 ms
4,376 KB
testcase_43 AC 3 ms
4,380 KB
testcase_44 AC 3 ms
4,380 KB
testcase_45 AC 3 ms
4,376 KB
testcase_46 AC 3 ms
4,380 KB
testcase_47 AC 4 ms
4,380 KB
testcase_48 AC 3 ms
4,376 KB
testcase_49 AC 2 ms
4,380 KB
testcase_50 AC 3 ms
4,380 KB
testcase_51 AC 3 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#ifdef ONLINE_JUDGE
#pragma GCC target("avx")
#endif

#ifndef LOCAL
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#else
#define _GLIBCXX_DEBUG
#endif

#include <iostream>
#include <string>
#include <vector>
#include <iomanip>
#include <algorithm>
#include <queue>
#include <map>
#include <stack>
#include <cmath>
#include <functional>
#include <set>
#include <numeric>
#include <bitset>
#include <cassert>

using std::cerr;
using std::cin;
using std::cout;
using std::pair;
using std::string;
using std::vector;

//region
#define rep(i, n) for (std::uint32_t i = 0; i < (n); ++i)
using ll = long long;
using ld = long double;
using uint = unsigned int;
using ull = unsigned long long;
using usize = std::size_t;
using vint = vector<int>;
using vlong = vector<ll>;
using pii = pair<int, int>;
template <typename T> using VV = vector<vector<T>>;
template <typename T> using priority_queue_g = std::priority_queue<T, vector<T>, std::greater<>>;

//vector<string> split(const string &s, const string &delim) {
//  vector<string> res;
//  string::size_type pos = 0;
//  while (true) {
//    const size_t found = s.find(delim, pos);
//    if (found == std::string::npos) { res.push_back(s.substr(pos)); break; }
//    res.push_back(s.substr(pos, found - pos));
//    pos = found + delim.size();
//  }
//  return res;
//}

template<typename T>
string join(vector<T> &vec, const string &sep) {
  size_t size = vec.size();
  if (!size) return "";
  std::stringstream ss;
  for (size_t i : range(vec.size() - 1)) ss << vec[i] << sep;
  ss << vec.back();
  return ss.str();
}

template<typename T, typename U>
std::istream &operator>>(std::istream &is, pair<T, U> &pair) { return is >> pair.first >> pair.second; }

template<typename T>
std::istream &operator>>(std::istream &is, vector<T> &vec) { for (T &x : vec) is >> x; return is; }

template<typename Iter>
inline void print(const Iter &first, const Iter &last, const std::string &d = " ", bool endline = true) {
  cout << *first;
  for (Iter iter = first + 1; iter < last; ++iter) cout << d << *iter;
  if (endline) cout << "\n";
}

constexpr ll powmod(ll a, ull b, uint p) {
  ll res = 1;
  while (b > 0) {
    if (b % 2) res = res * a % p;
    a = a * a % p;
    b >>= 1u;
  }
  return res;
}

constexpr ll pow(ll a, ll b) {
  ll res = 1;
  while (b > 0) {
    if (b % 2) res = res * a;
    a = a * a;
    b >>= 1u;
  }
  return res;
}

template<typename T, std::enable_if_t<std::is_integral_v<T>, nullptr_t>* = nullptr>
struct RangeIterator {
    RangeIterator(T current, T step) : _current(current), _step(step) {}

    constexpr bool operator!=(RangeIterator& other) const noexcept {
      return _step < 0 ? _current > other._current : _current < other._current;
    }

    RangeIterator<T> operator++() noexcept { _current += _step; return *this; }

    constexpr T operator*() const noexcept { return _current; }

private:
    T _current, _step;
};

template<typename T, std::enable_if_t<std::is_integral_v<T>, nullptr_t>* = nullptr>
struct range {
    range(T stop) : range(0, stop) {}
    range(T start, T stop, T step = 1) : _start(start), _stop(stop), _step(step) {}

    RangeIterator<T> begin() const noexcept { return RangeIterator(_start, _step); }
    RangeIterator<T> end() const noexcept { return RangeIterator(_stop, _step); }
private:
    T _start, _stop, _step;
};

template<typename T>
range<T> rev_range(T stop) { return range(stop - 1, -1, -1); }

template<class T, class U, typename std::enable_if_t<std::is_convertible<U, T>::value, nullptr_t>* = nullptr>
bool chmax(T &a, const U &b) { return a < T(b) && (a = T(b), true); }

template<class T, class U, typename std::enable_if_t<std::is_convertible<U, T>::value, nullptr_t>* = nullptr>
bool chmin(T &a, const U &b) { return a > T(b) && (a = T(b), true); }

template<typename T>
void bsort(vector<T> &v) { std::sort(v.begin(), v.end()); }

template<typename T>
void rsort(vector<T> &v) { std::sort(v.begin(), v.end(), std::greater<T>()); }

struct io_init {
    io_init() {
      cin.tie(nullptr); cout.tie(nullptr);
      std::ios::sync_with_stdio(false);
      cout << std::fixed << std::setprecision(16);
    }
} io_init_nouse;
//endregion

template <typename T>
vector<T> input_vec(usize n, std::make_signed_t<T> offset = 0) {
  vector<T> res(n); cin >> res;
  for (usize i : range(n)) res[i] += offset;
  return res;
}

template <typename T> std::vector<T> make_vec(std::size_t n) { return std::vector<T>(n); }

template <typename T, typename ...Tail>
auto make_vec(std::size_t n, Tail... tail) { return std::vector(n, make_vec<T>(tail...)); }

void solve();

int main() {
  solve();
}

#include "testlib.h"

void solve() {

  int n; cin >> n;
  vector<bool> a(n);
  rep(i, n) {
    bool tmp; cin >> tmp;
    a[i] = tmp;
  }

  vector<usize> one_indexes;
  for (uint_fast32_t i : range(n)) {
    if (a[i]) one_indexes.push_back(i);
  }
  ll all = ll(n) * (n + 1) / 2;
  if (one_indexes.empty());
  else if (one_indexes.size() == 1) {
    all -= 1;
  } else if (one_indexes.size() == 2) {
    if (one_indexes[0] + 1 == one_indexes[1]) {
        all -= 3;
    } else {
      all -= 2;
    }
  } else if (one_indexes.size() == n) {
    all = 0;
  } else if (one_indexes.size() == n - 1) {
    all = (one_indexes[0] + 1) * (n - one_indexes[0]);
  } else if (one_indexes.size() == n - 2) {
    all = (one_indexes[0] + 1) * (n - one_indexes[0]);
    all += (one_indexes[1] - one_indexes[0]) * (n - one_indexes[1]);
  } else {
    rep(i, n) {
      if (!a[i]) continue;
      for (uint_fast32_t j = i; j < n; ++j) {
        if (!a[j]) break;
        all -= 1;
      }
    }
  }
  std::printf("%lld\n", all);
}
0