QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#878425 | #9695. Trash Problem | ucup-team087# | AC ✓ | 962ms | 4352kb | C++23 | 25.9kb | 2025-02-01 15:15:19 | 2025-02-01 15:15:21 |
Judging History
answer
#line 1 "library/my_template.hpp"
#if defined(LOCAL)
#include <my_template_compiled.hpp>
#else
// https://codeforces.com/blog/entry/96344
// https://codeforces.com/blog/entry/126772?#comment-1154880
#include <bits/allocator.h>
#pragma GCC optimize("Ofast,unroll-loops")
#pragma GCC target("avx2,popcnt")
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using u8 = uint8_t;
using u16 = uint16_t;
using u32 = uint32_t;
using u64 = uint64_t;
using i128 = __int128;
using u128 = unsigned __int128;
using f128 = __float128;
template <class T>
constexpr T infty = 0;
template <>
constexpr int infty<int> = 1'010'000'000;
template <>
constexpr ll infty<ll> = 2'020'000'000'000'000'000;
template <>
constexpr u32 infty<u32> = infty<int>;
template <>
constexpr u64 infty<u64> = infty<ll>;
template <>
constexpr i128 infty<i128> = i128(infty<ll>) * 2'000'000'000'000'000'000;
template <>
constexpr double infty<double> = infty<ll>;
template <>
constexpr long double infty<long double> = infty<ll>;
using pi = pair<ll, ll>;
using vi = vector<ll>;
template <class T>
using vc = vector<T>;
template <class T>
using vvc = vector<vc<T>>;
template <class T>
using vvvc = vector<vvc<T>>;
template <class T>
using vvvvc = vector<vvvc<T>>;
template <class T>
using vvvvvc = vector<vvvvc<T>>;
template <class T>
using pq = priority_queue<T>;
template <class T>
using pqg = priority_queue<T, vector<T>, greater<T>>;
#define vv(type, name, h, ...) vector<vector<type>> name(h, vector<type>(__VA_ARGS__))
#define vvv(type, name, h, w, ...) vector<vector<vector<type>>> name(h, vector<vector<type>>(w, vector<type>(__VA_ARGS__)))
#define vvvv(type, name, a, b, c, ...) \
vector<vector<vector<vector<type>>>> name(a, vector<vector<vector<type>>>(b, vector<vector<type>>(c, vector<type>(__VA_ARGS__))))
// https://trap.jp/post/1224/
#define FOR1(a) for (ll _ = 0; _ < ll(a); ++_)
#define FOR2(i, a) for (ll i = 0; i < ll(a); ++i)
#define FOR3(i, a, b) for (ll i = a; i < ll(b); ++i)
#define FOR4(i, a, b, c) for (ll i = a; i < ll(b); i += (c))
#define FOR1_R(a) for (ll i = (a)-1; i >= ll(0); --i)
#define FOR2_R(i, a) for (ll i = (a)-1; i >= ll(0); --i)
#define FOR3_R(i, a, b) for (ll i = (b)-1; i >= ll(a); --i)
#define overload4(a, b, c, d, e, ...) e
#define overload3(a, b, c, d, ...) d
#define FOR(...) overload4(__VA_ARGS__, FOR4, FOR3, FOR2, FOR1)(__VA_ARGS__)
#define FOR_R(...) overload3(__VA_ARGS__, FOR3_R, FOR2_R, FOR1_R)(__VA_ARGS__)
#define all(x) x.begin(), x.end()
#define len(x) ll(x.size())
#define elif else if
#define eb emplace_back
#define mp make_pair
#define mt make_tuple
#define fi first
#define se second
#define stoi stoll
int popcnt(int x) { return __builtin_popcount(x); }
int popcnt(u32 x) { return __builtin_popcount(x); }
int popcnt(ll x) { return __builtin_popcountll(x); }
int popcnt(u64 x) { return __builtin_popcountll(x); }
int popcnt_sgn(int x) { return (__builtin_parity(unsigned(x)) & 1 ? -1 : 1); }
int popcnt_sgn(u32 x) { return (__builtin_parity(x) & 1 ? -1 : 1); }
int popcnt_sgn(ll x) { return (__builtin_parityll(x) & 1 ? -1 : 1); }
int popcnt_sgn(u64 x) { return (__builtin_parityll(x) & 1 ? -1 : 1); }
// (0, 1, 2, 3, 4) -> (-1, 0, 1, 1, 2)
int topbit(int x) { return (x == 0 ? -1 : 31 - __builtin_clz(x)); }
int topbit(u32 x) { return (x == 0 ? -1 : 31 - __builtin_clz(x)); }
int topbit(ll x) { return (x == 0 ? -1 : 63 - __builtin_clzll(x)); }
int topbit(u64 x) { return (x == 0 ? -1 : 63 - __builtin_clzll(x)); }
// (0, 1, 2, 3, 4) -> (-1, 0, 1, 0, 2)
int lowbit(int x) { return (x == 0 ? -1 : __builtin_ctz(x)); }
int lowbit(u32 x) { return (x == 0 ? -1 : __builtin_ctz(x)); }
int lowbit(ll x) { return (x == 0 ? -1 : __builtin_ctzll(x)); }
int lowbit(u64 x) { return (x == 0 ? -1 : __builtin_ctzll(x)); }
template <typename T>
T kth_bit(int k) {
return T(1) << k;
}
template <typename T>
bool has_kth_bit(T x, int k) {
return x >> k & 1;
}
template <typename UINT>
struct all_bit {
struct iter {
UINT s;
iter(UINT s) : s(s) {}
int operator*() const { return lowbit(s); }
iter &operator++() {
s &= s - 1;
return *this;
}
bool operator!=(const iter) const { return s != 0; }
};
UINT s;
all_bit(UINT s) : s(s) {}
iter begin() const { return iter(s); }
iter end() const { return iter(0); }
};
template <typename UINT>
struct all_subset {
static_assert(is_unsigned<UINT>::value);
struct iter {
UINT s, t;
bool ed;
iter(UINT s) : s(s), t(s), ed(0) {}
int operator*() const { return s ^ t; }
iter &operator++() {
(t == 0 ? ed = 1 : t = (t - 1) & s);
return *this;
}
bool operator!=(const iter) const { return !ed; }
};
UINT s;
all_subset(UINT s) : s(s) {}
iter begin() const { return iter(s); }
iter end() const { return iter(0); }
};
template <typename T>
T floor(T a, T b) {
return a / b - (a % b && (a ^ b) < 0);
}
template <typename T>
T ceil(T x, T y) {
return floor(x + y - 1, y);
}
template <typename T>
T bmod(T x, T y) {
return x - y * floor(x, y);
}
template <typename T>
pair<T, T> divmod(T x, T y) {
T q = floor(x, y);
return {q, x - q * y};
}
template <typename T, typename U>
T SUM(const vector<U> &A) {
T sm = 0;
for (auto &&a: A) sm += a;
return sm;
}
#define MIN(v) *min_element(all(v))
#define MAX(v) *max_element(all(v))
#define LB(c, x) distance((c).begin(), lower_bound(all(c), (x)))
#define UB(c, x) distance((c).begin(), upper_bound(all(c), (x)))
#define UNIQUE(x) sort(all(x)), x.erase(unique(all(x)), x.end()), x.shrink_to_fit()
template <typename T>
T POP(deque<T> &que) {
T a = que.front();
que.pop_front();
return a;
}
template <typename T>
T POP(pq<T> &que) {
T a = que.top();
que.pop();
return a;
}
template <typename T>
T POP(pqg<T> &que) {
T a = que.top();
que.pop();
return a;
}
template <typename T>
T POP(vc<T> &que) {
T a = que.back();
que.pop_back();
return a;
}
template <typename F>
ll binary_search(F check, ll ok, ll ng, bool check_ok = true) {
if (check_ok) assert(check(ok));
while (abs(ok - ng) > 1) {
auto x = (ng + ok) / 2;
(check(x) ? ok : ng) = x;
}
return ok;
}
template <typename F>
double binary_search_real(F check, double ok, double ng, int iter = 100) {
FOR(iter) {
double x = (ok + ng) / 2;
(check(x) ? ok : ng) = x;
}
return (ok + ng) / 2;
}
template <class T, class S>
inline bool chmax(T &a, const S &b) {
return (a < b ? a = b, 1 : 0);
}
template <class T, class S>
inline bool chmin(T &a, const S &b) {
return (a > b ? a = b, 1 : 0);
}
// ? は -1
vc<int> s_to_vi(const string &S, char first_char) {
vc<int> A(S.size());
FOR(i, S.size()) { A[i] = (S[i] != '?' ? S[i] - first_char : -1); }
return A;
}
template <typename T, typename U>
vector<T> cumsum(vector<U> &A, int off = 1) {
int N = A.size();
vector<T> B(N + 1);
FOR(i, N) { B[i + 1] = B[i] + A[i]; }
if (off == 0) B.erase(B.begin());
return B;
}
// stable sort
template <typename T>
vector<int> argsort(const vector<T> &A) {
vector<int> ids(len(A));
iota(all(ids), 0);
sort(all(ids), [&](int i, int j) { return (A[i] == A[j] ? i < j : A[i] < A[j]); });
return ids;
}
// A[I[0]], A[I[1]], ...
template <typename T>
vc<T> rearrange(const vc<T> &A, const vc<int> &I) {
vc<T> B(len(I));
FOR(i, len(I)) B[i] = A[I[i]];
return B;
}
template <typename T, typename... Vectors>
void concat(vc<T> &first, const Vectors &... others) {
vc<T> &res = first;
(res.insert(res.end(), others.begin(), others.end()), ...);
}
#endif
#line 1 "library/other/io.hpp"
#define FASTIO
#include <unistd.h>
// https://judge.yosupo.jp/submission/21623
namespace fastio {
static constexpr uint32_t SZ = 1 << 17;
char ibuf[SZ];
char obuf[SZ];
char out[100];
// pointer of ibuf, obuf
uint32_t pil = 0, pir = 0, por = 0;
struct Pre {
char num[10000][4];
constexpr Pre() : num() {
for (int i = 0; i < 10000; i++) {
int n = i;
for (int j = 3; j >= 0; j--) {
num[i][j] = n % 10 | '0';
n /= 10;
}
}
}
} constexpr pre;
inline void load() {
memcpy(ibuf, ibuf + pil, pir - pil);
pir = pir - pil + fread(ibuf + pir - pil, 1, SZ - pir + pil, stdin);
pil = 0;
if (pir < SZ) ibuf[pir++] = '\n';
}
inline void flush() {
fwrite(obuf, 1, por, stdout);
por = 0;
}
void rd(char &c) {
do {
if (pil + 1 > pir) load();
c = ibuf[pil++];
} while (isspace(c));
}
void rd(string &x) {
x.clear();
char c;
do {
if (pil + 1 > pir) load();
c = ibuf[pil++];
} while (isspace(c));
do {
x += c;
if (pil == pir) load();
c = ibuf[pil++];
} while (!isspace(c));
}
template <typename T>
void rd_real(T &x) {
string s;
rd(s);
x = stod(s);
}
template <typename T>
void rd_integer(T &x) {
if (pil + 100 > pir) load();
char c;
do
c = ibuf[pil++];
while (c < '-');
bool minus = 0;
if constexpr (is_signed<T>::value || is_same_v<T, i128>) {
if (c == '-') { minus = 1, c = ibuf[pil++]; }
}
x = 0;
while ('0' <= c) { x = x * 10 + (c & 15), c = ibuf[pil++]; }
if constexpr (is_signed<T>::value || is_same_v<T, i128>) {
if (minus) x = -x;
}
}
void rd(int &x) { rd_integer(x); }
void rd(ll &x) { rd_integer(x); }
void rd(i128 &x) { rd_integer(x); }
void rd(u32 &x) { rd_integer(x); }
void rd(u64 &x) { rd_integer(x); }
void rd(u128 &x) { rd_integer(x); }
void rd(double &x) { rd_real(x); }
void rd(long double &x) { rd_real(x); }
void rd(f128 &x) { rd_real(x); }
template <class T, class U>
void rd(pair<T, U> &p) {
return rd(p.first), rd(p.second);
}
template <size_t N = 0, typename T>
void rd_tuple(T &t) {
if constexpr (N < std::tuple_size<T>::value) {
auto &x = std::get<N>(t);
rd(x);
rd_tuple<N + 1>(t);
}
}
template <class... T>
void rd(tuple<T...> &tpl) {
rd_tuple(tpl);
}
template <size_t N = 0, typename T>
void rd(array<T, N> &x) {
for (auto &d: x) rd(d);
}
template <class T>
void rd(vc<T> &x) {
for (auto &d: x) rd(d);
}
void read() {}
template <class H, class... T>
void read(H &h, T &... t) {
rd(h), read(t...);
}
void wt(const char c) {
if (por == SZ) flush();
obuf[por++] = c;
}
void wt(const string s) {
for (char c: s) wt(c);
}
void wt(const char *s) {
size_t len = strlen(s);
for (size_t i = 0; i < len; i++) wt(s[i]);
}
template <typename T>
void wt_integer(T x) {
if (por > SZ - 100) flush();
if (x < 0) { obuf[por++] = '-', x = -x; }
int outi;
for (outi = 96; x >= 10000; outi -= 4) {
memcpy(out + outi, pre.num[x % 10000], 4);
x /= 10000;
}
if (x >= 1000) {
memcpy(obuf + por, pre.num[x], 4);
por += 4;
} else if (x >= 100) {
memcpy(obuf + por, pre.num[x] + 1, 3);
por += 3;
} else if (x >= 10) {
int q = (x * 103) >> 10;
obuf[por] = q | '0';
obuf[por + 1] = (x - q * 10) | '0';
por += 2;
} else
obuf[por++] = x | '0';
memcpy(obuf + por, out + outi + 4, 96 - outi);
por += 96 - outi;
}
template <typename T>
void wt_real(T x) {
ostringstream oss;
oss << fixed << setprecision(15) << double(x);
string s = oss.str();
wt(s);
}
void wt(int x) { wt_integer(x); }
void wt(ll x) { wt_integer(x); }
void wt(i128 x) { wt_integer(x); }
void wt(u32 x) { wt_integer(x); }
void wt(u64 x) { wt_integer(x); }
void wt(u128 x) { wt_integer(x); }
void wt(double x) { wt_real(x); }
void wt(long double x) { wt_real(x); }
void wt(f128 x) { wt_real(x); }
template <class T, class U>
void wt(const pair<T, U> val) {
wt(val.first);
wt(' ');
wt(val.second);
}
template <size_t N = 0, typename T>
void wt_tuple(const T t) {
if constexpr (N < std::tuple_size<T>::value) {
if constexpr (N > 0) { wt(' '); }
const auto x = std::get<N>(t);
wt(x);
wt_tuple<N + 1>(t);
}
}
template <class... T>
void wt(tuple<T...> tpl) {
wt_tuple(tpl);
}
template <class T, size_t S>
void wt(const array<T, S> val) {
auto n = val.size();
for (size_t i = 0; i < n; i++) {
if (i) wt(' ');
wt(val[i]);
}
}
template <class T>
void wt(const vector<T> val) {
auto n = val.size();
for (size_t i = 0; i < n; i++) {
if (i) wt(' ');
wt(val[i]);
}
}
void print() { wt('\n'); }
template <class Head, class... Tail>
void print(Head &&head, Tail &&... tail) {
wt(head);
if (sizeof...(Tail)) wt(' ');
print(forward<Tail>(tail)...);
}
// gcc expansion. called automaticall after main.
void __attribute__((destructor)) _d() { flush(); }
} // namespace fastio
using fastio::read;
using fastio::print;
using fastio::flush;
#if defined(LOCAL)
#define SHOW(...) SHOW_IMPL(__VA_ARGS__, SHOW6, SHOW5, SHOW4, SHOW3, SHOW2, SHOW1)(__VA_ARGS__)
#define SHOW_IMPL(_1, _2, _3, _4, _5, _6, NAME, ...) NAME
#define SHOW1(x) print(#x, "=", (x)), flush()
#define SHOW2(x, y) print(#x, "=", (x), #y, "=", (y)), flush()
#define SHOW3(x, y, z) print(#x, "=", (x), #y, "=", (y), #z, "=", (z)), flush()
#define SHOW4(x, y, z, w) print(#x, "=", (x), #y, "=", (y), #z, "=", (z), #w, "=", (w)), flush()
#define SHOW5(x, y, z, w, v) print(#x, "=", (x), #y, "=", (y), #z, "=", (z), #w, "=", (w), #v, "=", (v)), flush()
#define SHOW6(x, y, z, w, v, u) print(#x, "=", (x), #y, "=", (y), #z, "=", (z), #w, "=", (w), #v, "=", (v), #u, "=", (u)), flush()
#else
#define SHOW(...)
#endif
#define INT(...) \
int __VA_ARGS__; \
read(__VA_ARGS__)
#define LL(...) \
ll __VA_ARGS__; \
read(__VA_ARGS__)
#define U32(...) \
u32 __VA_ARGS__; \
read(__VA_ARGS__)
#define U64(...) \
u64 __VA_ARGS__; \
read(__VA_ARGS__)
#define STR(...) \
string __VA_ARGS__; \
read(__VA_ARGS__)
#define CHAR(...) \
char __VA_ARGS__; \
read(__VA_ARGS__)
#define DBL(...) \
double __VA_ARGS__; \
read(__VA_ARGS__)
#define VEC(type, name, size) \
vector<type> name(size); \
read(name)
#define VV(type, name, h, w) \
vector<vector<type>> name(h, vector<type>(w)); \
read(name)
void YES(bool t = 1) { print(t ? "YES" : "NO"); }
void NO(bool t = 1) { YES(!t); }
void Yes(bool t = 1) { print(t ? "Yes" : "No"); }
void No(bool t = 1) { Yes(!t); }
void yes(bool t = 1) { print(t ? "yes" : "no"); }
void no(bool t = 1) { yes(!t); }
void YA(bool t = 1) { print(t ? "YA" : "TIDAK"); }
void TIDAK(bool t = 1) { YES(!t); }
#line 3 "main.cpp"
#line 2 "library/ds/my_bitset.hpp"
// https://codeforces.com/contest/914/problem/F
// https://yukicoder.me/problems/no/142
// わずかに普通の bitset より遅いときもあるようだが,
// 固定長にしたくないときや slice 操作が必要なときに使う
struct My_Bitset {
using T = My_Bitset;
int N;
vc<u64> dat;
// x で埋める
My_Bitset(int N = 0, int x = 0) : N(N) {
assert(x == 0 || x == 1);
u64 v = (x == 0 ? 0 : -1);
dat.assign((N + 63) >> 6, v);
if (N) dat.back() >>= (64 * len(dat) - N);
}
int size() { return N; }
void resize(int size) {
dat.resize((size + 63) >> 6);
int remainingBits = size & 63;
if (remainingBits != 0) {
u64 mask = (u64(1) << remainingBits) - 1;
dat.back() &= mask;
}
N = size;
}
void append(int idx, bool b) {
assert(N == idx);
resize(idx + 1), (*this)[idx] = b;
}
static T from_string(string &S) {
int N = len(S);
T ANS(N);
FOR(i, N) ANS[i] = (S[i] == '1');
return ANS;
}
// thanks to chatgpt!
class Proxy {
public:
Proxy(vc<u64> &d, int i) : dat(d), index(i) {}
operator bool() const { return (dat[index >> 6] >> (index & 63)) & 1; }
Proxy &operator=(u64 value) {
dat[index >> 6] &= ~(u64(1) << (index & 63));
dat[index >> 6] |= (value & 1) << (index & 63);
return *this;
}
void flip() {
dat[index >> 6] ^= (u64(1) << (index & 63)); // XOR to flip the bit
}
private:
vc<u64> &dat;
int index;
};
Proxy operator[](int i) { return Proxy(dat, i); }
bool operator==(const T &p) {
assert(N == p.N);
FOR(i, len(dat)) if (dat[i] != p.dat[i]) return false;
return true;
}
T &operator&=(const T &p) {
assert(N == p.N);
FOR(i, len(dat)) dat[i] &= p.dat[i];
return *this;
}
T &operator|=(const T &p) {
assert(N == p.N);
FOR(i, len(dat)) dat[i] |= p.dat[i];
return *this;
}
T &operator^=(const T &p) {
assert(N == p.N);
FOR(i, len(dat)) dat[i] ^= p.dat[i];
return *this;
}
T operator&(const T &p) const { return T(*this) &= p; }
T operator|(const T &p) const { return T(*this) |= p; }
T operator^(const T &p) const { return T(*this) ^= p; }
T operator~() const {
T p = (*this);
p.flip_range(0, N);
return p;
}
void set_minus_inplace(T &other) {
assert(N == other.N);
FOR(i, len(dat)) dat[i] = dat[i] & (~other.dat[i]);
}
T set_minus(T other) {
assert(N == other.N);
FOR(i, len(dat)) other.dat[i] = dat[i] & (~other.dat[i]);
return other;
}
int count() {
int ans = 0;
for (u64 val: dat) ans += popcnt(val);
return ans;
}
int dot(T &p) {
assert(N == p.N);
int ans = 0;
FOR(i, len(dat)) ans += popcnt(dat[i] & p.dat[i]);
return ans;
}
int next(int i) {
chmax(i, 0);
if (i >= N) return N;
int k = i >> 6;
{
u64 x = dat[k];
int s = i & 63;
x = (x >> s) << s;
if (x) return (k << 6) | lowbit(x);
}
FOR(idx, k + 1, len(dat)) {
if (dat[idx] == 0) continue;
return (idx << 6) | lowbit(dat[idx]);
}
return N;
}
int prev(int i) {
chmin(i, N - 1);
if (i <= -1) return -1;
int k = i >> 6;
if ((i & 63) < 63) {
u64 x = dat[k];
x &= (u64(1) << ((i & 63) + 1)) - 1;
if (x) return (k << 6) | topbit(x);
--k;
}
FOR_R(idx, k + 1) {
if (dat[idx] == 0) continue;
return (idx << 6) | topbit(dat[idx]);
}
return -1;
}
My_Bitset range(int L, int R) {
assert(L <= R);
My_Bitset p(R - L);
int rm = (R - L) & 63;
FOR(rm) {
p[R - L - 1] = bool((*this)[R - 1]);
--R;
}
int n = (R - L) >> 6;
int hi = L & 63;
int lo = 64 - hi;
int s = L >> 6;
if (hi == 0) {
FOR(i, n) { p.dat[i] ^= dat[s + i]; }
} else {
FOR(i, n) { p.dat[i] ^= (dat[s + i] >> hi) ^ (dat[s + i + 1] << lo); }
}
return p;
}
My_Bitset slice(int L, int R) { return range(L, R); }
int count_range(int L, int R) {
assert(L <= R);
int cnt = 0;
while ((L < R) && (L & 63)) cnt += (*this)[L++];
while ((L < R) && (R & 63)) cnt += (*this)[--R];
int l = L >> 6, r = R >> 6;
FOR(i, l, r) cnt += popcnt(dat[i]);
return cnt;
}
// [L,R) に p を代入
void assign_to_range(int L, int R, My_Bitset &p) {
assert(p.N == R - L);
int a = 0, b = p.N;
while (L < R && (L & 63)) { (*this)[L++] = bool(p[a++]); }
while (L < R && (R & 63)) { (*this)[--R] = bool(p[--b]); }
// p[a:b] を [L:R] に
int l = L >> 6, r = R >> 6;
int s = a >> 6, t = b >> t;
int n = r - l;
if (!(a & 63)) {
FOR(i, n) dat[l + i] = p.dat[s + i];
} else {
int hi = a & 63;
int lo = 64 - hi;
FOR(i, n) dat[l + i] = (p.dat[s + i] >> hi) | (p.dat[1 + s + i] << lo);
}
}
// [L,R) に p を xor
void xor_to_range(int L, int R, My_Bitset &p) {
assert(p.N == R - L);
int a = 0, b = p.N;
while (L < R && (L & 63)) {
dat[L >> 6] ^= u64(p[a]) << (L & 63);
++a, ++L;
}
while (L < R && (R & 63)) {
--b, --R;
dat[R >> 6] ^= u64(p[b]) << (R & 63);
}
// p[a:b] を [L:R] に
int l = L >> 6, r = R >> 6;
int s = a >> 6, t = b >> t;
int n = r - l;
if (!(a & 63)) {
FOR(i, n) dat[l + i] ^= p.dat[s + i];
} else {
int hi = a & 63;
int lo = 64 - hi;
FOR(i, n) dat[l + i] ^= (p.dat[s + i] >> hi) | (p.dat[1 + s + i] << lo);
}
}
// 行列基本変形で使うやつ
// p は [i:N) にしかないとして p を xor する
void xor_suffix(int i, My_Bitset &p) {
assert(N == p.N && 0 <= i && i < N);
FOR(k, i / 64, len(dat)) { dat[k] ^= p.dat[k]; }
}
// [L,R) に p を and
void and_to_range(int L, int R, My_Bitset &p) {
assert(p.N == R - L);
int a = 0, b = p.N;
while (L < R && (L & 63)) {
if (!p[a]) (*this)[L] = 0;
a++, L++;
}
while (L < R && (R & 63)) {
--b, --R;
if (!p[b]) (*this)[R] = 0;
}
// p[a:b] を [L:R] に
int l = L >> 6, r = R >> 6;
int s = a >> 6, t = b >> t;
int n = r - l;
if (!(a & 63)) {
FOR(i, n) dat[l + i] &= p.dat[s + i];
} else {
int hi = a & 63;
int lo = 64 - hi;
FOR(i, n) dat[l + i] &= (p.dat[s + i] >> hi) | (p.dat[1 + s + i] << lo);
}
}
// [L,R) に p を or
void or_to_range(int L, int R, My_Bitset &p) {
assert(p.N == R - L);
int a = 0, b = p.N;
while (L < R && (L & 63)) {
dat[L >> 6] |= u64(p[a]) << (L & 63);
++a, ++L;
}
while (L < R && (R & 63)) {
--b, --R;
dat[R >> 6] |= u64(p[b]) << (R & 63);
}
// p[a:b] を [L:R] に
int l = L >> 6, r = R >> 6;
int s = a >> 6, t = b >> t;
int n = r - l;
if (!(a & 63)) {
FOR(i, n) dat[l + i] |= p.dat[s + i];
} else {
int hi = a & 63;
int lo = 64 - hi;
FOR(i, n) dat[l + i] |= (p.dat[s + i] >> hi) | (p.dat[1 + s + i] << lo);
}
}
// 行列基本変形で使うやつ
// p は [i:N) にしかないとして p を or する
void or_suffix(int i, My_Bitset &p) {
assert(N == p.N && 0 <= i && i < N);
FOR(k, i / 64, len(dat)) { dat[k] |= p.dat[k]; }
}
// [L,R) を 1 に変更
void set_range(int L, int R) {
while (L < R && (L & 63)) { set(L++); }
while (L < R && (R & 63)) { set(--R); }
FOR(i, L >> 6, R >> 6) dat[i] = u64(-1);
}
// [L,R) を 1 に変更
void reset_range(int L, int R) {
while (L < R && (L & 63)) { reset(L++); }
while (L < R && (R & 63)) { reset(--R); }
FOR(i, L >> 6, R >> 6) dat[i] = u64(0);
}
// [L,R) を flip
void flip_range(int L, int R) {
while (L < R && (L & 63)) { flip(L++); }
while (L < R && (R & 63)) { flip(--R); }
FOR(i, L >> 6, R >> 6) dat[i] ^= u64(-1);
}
// bitset に仕様を合わせる
void set(int i) { (*this)[i] = 1; }
void reset(int i) { (*this)[i] = 0; }
void flip(int i) { (*this)[i].flip(); }
void set() {
fill(all(dat), u64(-1));
resize(N);
}
void reset() { fill(all(dat), 0); }
void flip() {
FOR(i, len(dat) - 1) { dat[i] = u64(-1) ^ dat[i]; }
int i = len(dat) - 1;
FOR(k, 64) {
if (64 * i + k >= size()) break;
flip(64 * i + k);
}
}
bool any() {
FOR(i, len(dat)) {
if (dat[i]) return true;
}
return false;
}
bool ALL() {
dat.resize((N + 63) >> 6);
int r = N & 63;
if (r != 0) {
u64 mask = (u64(1) << r) - 1;
if (dat.back() != mask) return 0;
}
for (int i = 0; i < N / 64; ++i)
if (dat[i] != u64(-1)) return false;
return true;
}
// bs[i]==true であるような i 全体
vc<int> collect_idx() {
vc<int> I;
FOR(i, N) if ((*this)[i]) I.eb(i);
return I;
}
bool is_subset(T &other) {
assert(len(other) == N);
FOR(i, len(dat)) {
u64 a = dat[i], b = other.dat[i];
if ((a & b) != a) return false;
}
return true;
}
int _Find_first() { return next(0); }
int _Find_next(int p) { return next(p + 1); }
static string TO_STR[256];
string to_string() const {
if (TO_STR[0].empty()) precompute();
string S;
for (auto &x: dat) { FOR(i, 8) S += TO_STR[(x >> (8 * i) & 255)]; }
S.resize(N);
return S;
}
static void precompute() {
FOR(s, 256) {
string x;
FOR(i, 8) x += '0' + (s >> i & 1);
TO_STR[s] = x;
}
}
};
string My_Bitset::TO_STR[256];
#line 5 "main.cpp"
// 左端を決めて解く
ll sub(vc<string> G) {
ll H = len(G), W = len(G[0]);
using BS = My_Bitset;
// L, R, x などを割り当てる
vc<string> LR(H, string(W, '.'));
FOR(i, H) {
FOR(j, W) {
if (LR[i][j] == 'R') continue;
if (G[i][j] == '0') continue;
if (j + 1 < W && G[i][j + 1] == '1') {
LR[i][j] = 'L';
LR[i][j + 1] = 'R';
} else {
LR[i][j] = 'X';
}
}
}
auto from_char = [&](char ch) -> vc<BS> {
vc<BS> ANS(H, BS(W));
FOR(i, H) FOR(j, W) ANS[i][j] = (LR[i][j] == ch);
return ANS;
};
auto L = from_char('L');
auto R = from_char('R');
auto X = from_char('X');
vc<int> FIRST_X(H, W);
FOR(i, H) FOR(j, W) {
if (LR[i][j] == 'X') chmin(FIRST_X[i], j);
}
SHOW(W, FIRST_X);
FOR(i, H) SHOW(X[i].to_string());
// 各行に対して、LR 境界となるところの L は bad
vc<BS> not_L(H);
FOR(i, H) {
not_L[i] = L[i];
not_L[i].flip();
}
ll ANS = 0;
// 上端を決めて解く
FOR(s, H) {
// 各列が最後の列になってもよいかどうかの bitset
BS OK(W, 1);
// 最後の行が L になっているところの bitset
BS last(W, 0);
int n = W;
FOR(t, s, H) {
chmin(n, FIRST_X[t]);
// 新たに悪い「最後の列」が発生
OK &= not_L[t];
// last のところのあとに L 以外がくるとそこ以降は使えない
chmin(n, (last & not_L[t]).next(0));
last ^= L[t];
int k = min(n, last.next(0));
ANS += OK.count_range(0, k);
}
}
return ANS;
}
void solve() {
LL(N);
VEC(string, G, N);
ll ANS = 0;
FOR(j, N) {
vc<string> H(N);
FOR(i, N) H[i] = G[i].substr(j);
ANS += sub(H);
}
print(ANS);
}
signed main() { solve(); }
这程序好像有点Bug,我给组数据试试?
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 3584kb
input:
4 0110 0110 1111 1111
output:
17
result:
ok 1 number(s): "17"
Test #2:
score: 0
Accepted
time: 1ms
memory: 3712kb
input:
20 00110101100010111111 01000101010001111000 11101001001011011010 01000001001001101110 11100011001111111100 01101110111100111100 10000101011110110101 10001001101110000110 11110011110001110010 10001000101101011111 01000010001100110101 00111100100010011010 01000011000111011011 00111010111111010101 000...
output:
549
result:
ok 1 number(s): "549"
Test #3:
score: 0
Accepted
time: 0ms
memory: 3712kb
input:
20 01110011110110000110 10110000111010001100 01111100000001001010 11011111110110011001 01111001001010010111 00111001110110010001 01101011100101011111 10100001100011110001 00101111101011000011 11011011010110101001 01010011011110000111 11111100111100010110 00110111111010011111 00000011000100111011 011...
output:
591
result:
ok 1 number(s): "591"
Test #4:
score: 0
Accepted
time: 0ms
memory: 3712kb
input:
19 1000101101010111010 0001001000101010101 1101111101111110010 0100101100110101010 1111011000111010000 1010000111101101110 0001111010100011010 0011010011011111110 1110111011001001000 0001011010111111010 1010101100000110101 1000000011000111101 1101000011101001001 0011000100011110010 11100010000101001...
output:
558
result:
ok 1 number(s): "558"
Test #5:
score: 0
Accepted
time: 1ms
memory: 3712kb
input:
20 11011001111000000000 11011111111001100000 01111111101101101100 01111001101100111111 11000000011110110011 11000000011110001111 00011000111100111111 00011011111100110011 00011011011110011011 00011011011110011110 11110011110011000110 11110011110011011000 00001111011110011011 00111101111110111111 001...
output:
1629
result:
ok 1 number(s): "1629"
Test #6:
score: 0
Accepted
time: 1ms
memory: 3712kb
input:
20 00011000000001100011 00011110110111101111 11110110110111101100 11110011011111111000 01111011011110011000 01111000111100110000 01111000111100110000 01111011001101101100 11110011001101101100 11111100111101111110 00001111111101111110 00110011110000001100 00110011110111101100 01111011011111111110 011...
output:
1687
result:
ok 1 number(s): "1687"
Test #7:
score: 0
Accepted
time: 0ms
memory: 3712kb
input:
20 01100000000000000000 01100000000000000000 00000000011000000000 00011000011110000000 00011001100110000000 00000001100000000000 00000000110000110000 00000000110000111100 00000000110001101100 11000000110001100000 11001100110000000000 00001100110000000011 00000000000000000011 00000000000000000110 110...
output:
11708
result:
ok 1 number(s): "11708"
Test #8:
score: 0
Accepted
time: 1ms
memory: 3712kb
input:
20 00000000000000011000 00000000000000011000 00000000000000000000 00000000000000000000 00000000000000000000 00000000000000000000 00000000000000000000 00011000110001101100 00011000110001101100 00000000000000000000 00000000000000110000 00001100011000110110 00001100011000000110 00000110011000000011 000...
output:
15512
result:
ok 1 number(s): "15512"
Test #9:
score: 0
Accepted
time: 0ms
memory: 3712kb
input:
20 11001111000000000011 11001111000000000011 11110000001111110000 11110000001111110000 00000000001100110000 00000000001100110000 00000000000000000000 00000000000000000000 00110000000000001100 00110000000000001100 11000000001100000000 11000000001100000000 11000011000000000000 11000011000000000000 000...
output:
13034
result:
ok 1 number(s): "13034"
Test #10:
score: 0
Accepted
time: 0ms
memory: 3584kb
input:
20 00001100000000000000 00001100000000000000 00110011110011000000 00110011110011000000 11110000000000000000 11110000000000000000 11000000000000111100 11000000000000111100 11111100110000000000 11111100110000000000 00110000111111110011 00110000111111110011 00110011000000000000 00110011000000000000 000...
output:
9380
result:
ok 1 number(s): "9380"
Test #11:
score: 0
Accepted
time: 0ms
memory: 3712kb
input:
20 00110000000000000011 00110000000000000011 00000000000011001111 00000000000011001111 00110011000000111111 00110011000000111111 00001100110011110000 00001100110011110000 00001100001111000000 00001100001111000000 00110000001100000000 00110000001100000000 11000011110011000000 11000011110011000000 000...
output:
9048
result:
ok 1 number(s): "9048"
Test #12:
score: 0
Accepted
time: 0ms
memory: 3584kb
input:
20 00000011000000000000 00000011000000000000 00111111001111110011 00111111001111110011 11111100110000001100 11111100110000001100 11001100110000001100 11001100110000001100 11110000001100001100 11110000001100001100 00110000001100000000 00110000001100000000 00000000000011001100 00000000000011001100 000...
output:
9302
result:
ok 1 number(s): "9302"
Test #13:
score: 0
Accepted
time: 3ms
memory: 3584kb
input:
50 10110111111100111011110100110011011100011001010010 01101010010001101000110010101100011110101001011000 00011110101010110011100010101111111001010111101100 01010100110110111000100111110001111011111101010001 01010110110011010100110010100101001110110100111001 100011010011001011101000011110101001100111...
output:
4062
result:
ok 1 number(s): "4062"
Test #14:
score: 0
Accepted
time: 4ms
memory: 3712kb
input:
50 00000100101011100101110000000010000011011001100111 00111101010010100011011000010101101110011010111100 01110111001011001111001001010010110001000101100101 10111000100000000001011110101101001011000010111011 10111100111010111100010101001110101101101011011110 111110000110100001100001001111110011001001...
output:
4693
result:
ok 1 number(s): "4693"
Test #15:
score: 0
Accepted
time: 4ms
memory: 3712kb
input:
49 0000100110001000101010000110110000110110010001100 1011110001010000010011001110010000100101100011001 0000001000011111100001110111111100100100110110100 1010011111110000011100100000110001001001100011001 1100110001100011111010100110010111001100010110001 10011010111110001110011010100011010000110111011...
output:
4104
result:
ok 1 number(s): "4104"
Test #16:
score: 0
Accepted
time: 4ms
memory: 3712kb
input:
50 00000011000110110000011001111110110000000000000000 11000011000110111101111001111110110001111110111111 11000011000001101101111110011110011001111110111111 01111011000001100110011111111110011111101100011110 01111110001100110110000001100001111111101101111110 110111100011111100001111001100011110000011...
output:
11480
result:
ok 1 number(s): "11480"
Test #17:
score: 0
Accepted
time: 5ms
memory: 3712kb
input:
50 00011110000111111111111000000000000000000000000110 11011110011111111111111111100110111101111110000110 11001100011000110000011111100110111101111110000000 11001111110011111100011111100011111101100000001111 11110011111111001100000111111011111101101100001111 111100000011000001100110110110000001111011...
output:
12511
result:
ok 1 number(s): "12511"
Test #18:
score: 0
Accepted
time: 4ms
memory: 3712kb
input:
50 00000000000000000000000000000000001100000000000000 00000000011001100000000000000000001100000000000000 00000000011001100000000000000000000000000110000011 00000000000000000000000000000000011001100110000011 00000000000000000011000000000000011001100000000000 000000000000000000110000000000000000000000...
output:
517453
result:
ok 1 number(s): "517453"
Test #19:
score: 0
Accepted
time: 5ms
memory: 3584kb
input:
50 00000000000000000000000000000000000000000000011000 00000000000000000000000000000000000000000000011000 00000000000000000000000000000000000000000000000000 00000000000000000000000000000000000000000000000000 00000000000000000000000000000000000000000000000000 000000000000110000000000000000000000000000...
output:
510978
result:
ok 1 number(s): "510978"
Test #20:
score: 0
Accepted
time: 4ms
memory: 3712kb
input:
50 00001100000011000000110011001111001100001100000011 00001100000011000000110011001111001100001100000011 11000011000000001100000000000011110000000000001100 11000011000000001100000000000011110000000000001100 00110000000000000000110000000011000000110011000000 001100000000000000001100000000110000001100...
output:
195043
result:
ok 1 number(s): "195043"
Test #21:
score: 0
Accepted
time: 5ms
memory: 3712kb
input:
50 00111100000000000000110000001100001100000011001100 00111100000000000000110000001100001100000011001100 11000000001100000000001111001111000011000000110000 11000000001100000000001111001111000011000000110000 00110000000000000000000000000000110000001100000000 001100000000000000000000000000001100000011...
output:
200866
result:
ok 1 number(s): "200866"
Test #22:
score: 0
Accepted
time: 5ms
memory: 3712kb
input:
50 00000000000000000000000000000000000000000011000011 00000000000000000000000000000000000000000011000011 00000000000000000000000000000000110000001100001100 00000000000000000000000000000000110000001100001100 00000000000000000000000000000000000000000000000000 000000000000000000000000000000000000000000...
output:
411379
result:
ok 1 number(s): "411379"
Test #23:
score: 0
Accepted
time: 5ms
memory: 3712kb
input:
50 00000000000000110000110000000000001100110000000000 00000000000000110000110000000000001100110000000000 00000000110000000000000011000000000000000000000011 00000000110000000000000011000000000000000000000011 00000000000000000000000000000000000000000000000000 000000000000000000000000000000000000000000...
output:
378461
result:
ok 1 number(s): "378461"
Test #24:
score: 0
Accepted
time: 739ms
memory: 4352kb
input:
300 00000000100001101110110010111001010000000101000011011100101110110110100000111110000011000100010111100000101110011011100000000011000001000000111010001110001110110111110011111000000010101111101110000001011101110111010101110001000011100110101010101101001111010011111010100111000100100010001011100010...
output:
158766
result:
ok 1 number(s): "158766"
Test #25:
score: 0
Accepted
time: 732ms
memory: 4224kb
input:
300 01010100101011010011010101111101010100001101101111011011010100111101111101100110110111011000011110010111010111100011110111000100101010010100000101010001011101100000100001011010010000000110111010001101010100110100110110101110001000010011010011111101110001101010101001011011001010101010001100100110...
output:
161123
result:
ok 1 number(s): "161123"
Test #26:
score: 0
Accepted
time: 729ms
memory: 4224kb
input:
300 01001011111100110011101011101100110101011101110111010110111100101000100110100000101011101000101001101011101001110110111110001111011010010011110001010011001011101000011001100111001000001001011010010000101010001001000010010011011000110101010001000000010111011010101010010101000011101101111011101100...
output:
161636
result:
ok 1 number(s): "161636"
Test #27:
score: 0
Accepted
time: 729ms
memory: 4224kb
input:
300 01000110110010010110111000101100110110000000010100111101010111111100001111001010111010011110000111110010000001011111010011001011000010101110101110000110011111100111101001100101100100101110100110110110101011001001001110100101001010001110010010010110011011000110010100101000011011111110101110000110...
output:
163637
result:
ok 1 number(s): "163637"
Test #28:
score: 0
Accepted
time: 719ms
memory: 4352kb
input:
299 11000011001101101010000001111010111111101010101000000111101010011111000001011010100000000001001000100110111011010000111000001111111011010001111101111110101100100010101011111101010011011110000010011101010101101000100011101110011001111101000010000101010011101011100110001000101100011010001010010100...
output:
160948
result:
ok 1 number(s): "160948"
Test #29:
score: 0
Accepted
time: 730ms
memory: 4352kb
input:
300 00110010011001001001110011010011101111011011101000100010010010101001011010110001100101010111001110110010000000101110101111100000011110111000010100010000110000010101100011110100001001010100101100100001100000000010001000101001100001110101101011111010000101111101110010001100001110111100110111000010...
output:
159274
result:
ok 1 number(s): "159274"
Test #30:
score: 0
Accepted
time: 725ms
memory: 4352kb
input:
300 10111110101111011010001001000001001100011101101001110010001101110011110101000100000100111101010110000001111100100110110010001011111110011111011010110101011001010100010111101001001001111110100000100011110111010101011111100010011001001100000111110000100101010010011111111011101111000011101001000000...
output:
163578
result:
ok 1 number(s): "163578"
Test #31:
score: 0
Accepted
time: 721ms
memory: 4352kb
input:
299 11110011011010010001000111011001111111010010101110001111001111101100110010011101101001001011001100111111100000100011001101001010010111000101011101011000101100101111110001011110110110010000010111111001011010101101001011101101110100111101010001100000101111100110010000011010100101100100001010101010...
output:
157983
result:
ok 1 number(s): "157983"
Test #32:
score: 0
Accepted
time: 692ms
memory: 4352kb
input:
300 00000001100110001100000000011111100000000011001111110000111111000000011000000000000001100001101111001100110001101111011000000011000001111011000000000110000000001100000000000011000000000000110001111000000000001101100000000001101101100001111000000011001100011011011110000001100001100110001111000001...
output:
427604
result:
ok 1 number(s): "427604"
Test #33:
score: 0
Accepted
time: 699ms
memory: 4352kb
input:
300 11011110011011000001100110110000000011000110000000110000000000001100110011110000111100011000000000000110011001100110000000000001100011011011000110110001101100011011000011000110001111000011110011110111111000000011000011000000000110000000000001100000000000000110000000001111011110001111110000001111...
output:
429036
result:
ok 1 number(s): "429036"
Test #34:
score: 0
Accepted
time: 940ms
memory: 4352kb
input:
300 00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000...
output:
631185707
result:
ok 1 number(s): "631185707"
Test #35:
score: 0
Accepted
time: 946ms
memory: 4224kb
input:
300 00000000000000000000000000000000000000000000000000000000000011000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000...
output:
640299883
result:
ok 1 number(s): "640299883"
Test #36:
score: 0
Accepted
time: 778ms
memory: 4352kb
input:
300 11001100110000000000110011000000000011000000000000111100111111000000111100000000000000000000110011000011110011000000001100000011001111111111000011111100000011000000111100000011000011000000001100110011000011001100001111001111000000000000000011001100111100000011000000000011110011110011000000111111...
output:
145347042
result:
ok 1 number(s): "145347042"
Test #37:
score: 0
Accepted
time: 766ms
memory: 4352kb
input:
300 00000011000000001100000011001100111111000011000011000011001100111100001100000000110011000000000000000000001111000011110000000000000000110000000000110000111100000000000000000000001100111111001100111111001100111111000011000011000000000000001100001111001111111111000011001100000000110000110011001100...
output:
145414743
result:
ok 1 number(s): "145414743"
Test #38:
score: 0
Accepted
time: 909ms
memory: 4352kb
input:
300 00001100000000000000000000000000000000000000000000000000110000000000000000000000000000000000000000000000000000000011000000000000000000000000000000000000000000000000000000000000000000000011000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000...
output:
476563330
result:
ok 1 number(s): "476563330"
Test #39:
score: 0
Accepted
time: 911ms
memory: 4352kb
input:
300 00000000000000000000000000000000000011000000000000000000000000000000001100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000011000000000000000011000000000000000000000000000000000000000000000000000000000000000000000000000000000000...
output:
491622899
result:
ok 1 number(s): "491622899"
Test #40:
score: 0
Accepted
time: 962ms
memory: 4352kb
input:
300 00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000...
output:
2038522500
result:
ok 1 number(s): "2038522500"
Test #41:
score: 0
Accepted
time: 817ms
memory: 4224kb
input:
300 11111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111...
output:
506250000
result:
ok 1 number(s): "506250000"
Extra Test:
score: 0
Extra Test Passed