QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#643837 | #7967. 二进制 | hos_lyric# | TL | 0ms | 3864kb | C++14 | 5.8kb | 2024-10-16 02:21:12 | 2024-10-16 02:21:13 |
Judging History
answer
#include <cassert>
#include <cmath>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <functional>
#include <iostream>
#include <limits>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
using namespace std;
using Int = long long;
template <class T1, class T2> ostream &operator<<(ostream &os, const pair<T1, T2> &a) { return os << "(" << a.first << ", " << a.second << ")"; };
template <class T> ostream &operator<<(ostream &os, const vector<T> &as) { const int sz = as.size(); os << "["; for (int i = 0; i < sz; ++i) { if (i >= 256) { os << ", ..."; break; } if (i > 0) { os << ", "; } os << as[i]; } return os << "]"; }
template <class T> void pv(T a, T b) { for (T i = a; i != b; ++i) cerr << *i << " "; cerr << endl; }
template <class T> bool chmin(T &t, const T &f) { if (t > f) { t = f; return true; } return false; }
template <class T> bool chmax(T &t, const T &f) { if (t < f) { t = f; return true; } return false; }
#define COLOR(s) ("\x1b[" s "m")
template <class T> void bAdd(vector<T> &bit, int pos, const T &val) {
const int bitN = bit.size();
for (int x = pos; x < bitN; x |= x + 1) bit[x] += val;
}
template <class T> T bSum(const vector<T> &bit, int pos) {
T ret = 0;
for (int x = pos; x > 0; x &= x - 1) ret += bit[x - 1];
return ret;
}
template <class T> T bSum(const vector<T> &bit, int pos0, int pos1) {
return bSum(bit, pos1) - bSum(bit, pos0);
}
struct Set {
// max{ceil(log_64(n)), 1}
int log64N, n;
vector<unsigned long long> a[6];
explicit Set(int n_ = 0) : n(n_) {
assert(n >= 0);
int m = n ? n : 1;
for (int d = 0; ; ++d) {
m = (m + 63) >> 6;
a[d].assign(m, 0);
if (m == 1) {
log64N = d + 1;
break;
}
}
}
bool empty() const {
return !a[log64N - 1][0];
}
bool contains(int x) const {
return (a[0][x >> 6] >> (x & 63)) & 1;
}
void insert(int x) {
for (int d = 0; d < log64N; ++d) {
const int q = x >> 6, r = x & 63;
a[d][q] |= 1ULL << r;
x = q;
}
}
void erase(int x) {
for (int d = 0; d < log64N; ++d) {
const int q = x >> 6, r = x & 63;
if ((a[d][q] &= ~(1ULL << r))) break;
x = q;
}
}
// max s.t. <= x (or -1)
int prev(int x) const {
if (x > n - 1) x = n - 1;
for (int d = 0; d <= log64N; ++d) {
if (x < 0) break;
const int q = x >> 6, r = x & 63;
const unsigned long long lower = a[d][q] << (63 - r);
if (lower) {
x -= __builtin_clzll(lower);
for (int e = d; --e >= 0; ) x = x << 6 | (63 - __builtin_clzll(a[e][x]));
return x;
}
x = q - 1;
}
return -1;
}
// min s.t. >= x (or n)
int next(int x) const {
if (x < 0) x = 0;
for (int d = 0; d < log64N; ++d) {
const int q = x >> 6, r = x & 63;
if (static_cast<unsigned>(q) >= a[d].size()) break;
const unsigned long long upper = a[d][q] >> r;
if (upper) {
x += __builtin_ctzll(upper);
for (int e = d; --e >= 0; ) x = x << 6 | __builtin_ctzll(a[e][x]);
return x;
}
x = q + 1;
}
return n;
}
};
#ifdef LOCAL
constexpr int E = 5;
#else
constexpr int E = 20;
#endif
int N;
char S[1'000'010];
int main() {
for (; ~scanf("%d", &N); ) {
scanf("%s", S + 1);
vector<int> bit(N + 2, 0);
Set on(N + 2);
for (int i = 0; i <= N + 1; ++i) on.insert(i);
vector<set<int>> candss(N + 1);
for (int i = 1; i <= N; ++i) if (S[i] == '1') {
int y = 0;
for (int j = i; j <= N; ++j) {
(y <<= 1) |= (S[j] - '0');
if (y > N) break;
candss[y].insert(i);
}
}
for (int x = 1; x <= N; ++x) {
if (candss[x].size()) {
const int i0 = *candss[x].begin();
printf("%d %d\n", i0 - bSum(bit, i0), (int)candss[x].size());
// cerr<<"candss["<<x<<"] = ";pv(candss[x].begin(),candss[x].end());
const int len = 31 - __builtin_clz(x) + 1;
vector<int> is(E + len + E, -1);
fill(is.begin(), is.begin() + E, 0);
fill(is.begin() + (E + len), is.end(), N + 1);
{
int i = i0;
for (int j = 0; j < len; ++j) {
is[E + j] = i;
bAdd(bit, i, +1);
on.erase(i);
i = on.next(i);
}
}
for (int j = E; --j >= 0; ) {
is[j] = on.prev(is[j + 1] - 1);
if (is[j] < 1) break;
}
for (int j = E + len; j < E + len + E; ++j) {
is[j] = on.next(is[j - 1] + 1);
if (N < is[j]) break;
}
// cerr<<"x = "<<x<<": is = "<<is<<endl;
for (int l = 0; l < E + len; ++l) if (1 <= is[l] && S[is[l]] == '1') {
int y = 0;
for (int j = l; j < E + len + E && is[j] <= N; ++j) {
(y <<= 1) |= (S[is[j]] - '0');
if (y > N) break;
if (y <= x) continue;
if (j >= E) {
// cerr<<" erase "<<y<<" "<<is[l]<<endl;
candss[y].erase(is[l]);
}
}
}
is.erase(is.begin() + E, is.begin() + (E + len));
for (int l = 0; l < E; ++l) if (1 <= is[l] && S[is[l]] == '1') {
int y = 0;
for (int j = l; j < E + E && is[j] <= N; ++j) {
(y <<= 1) |= (S[is[j]] - '0');
if (y > N) break;
if (y <= x) continue;
if (j >= E) {
// cerr<<" insert "<<y<<" "<<is[l]<<endl;
candss[y].insert(is[l]);
}
}
}
} else {
puts("-1 0");
}
}
}
return 0;
}
详细
Test #1:
score: 100
Accepted
time: 0ms
memory: 3864kb
input:
20 01001101101101110010
output:
2 11 5 5 4 5 11 1 4 2 7 1 -1 0 -1 0 -1 0 -1 0 -1 0 -1 0 -1 0 -1 0 -1 0 -1 0 -1 0 -1 0 -1 0 -1 0
result:
ok 20 lines
Test #2:
score: -100
Time Limit Exceeded
input:
1000000 1111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111...
output:
1 1000000 -1 0 1 999998 -1 0 -1 0 -1 0 1 999995 -1 0 -1 0 -1 0 -1 0 -1 0 -1 0 -1 0 1 999991 -1 0 -1 0 -1 0 -1 0 -1 0 -1 0 -1 0 -1 0 -1 0 -1 0 -1 0 -1 0 -1 0 -1 0 -1 0 1 999986 -1 0 -1 0 -1 0 -1 0 -1 0 -1 0 -1 0 -1 0 -1 0 -1 0 -1 0 -1 0 -1 0 -1 0 -1 0 -1 0 -1 0 -1 0 -1 0 -1 0 -1 0 -1 0 -1 0 -1 0 -1 0...