QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#300897 | #7967. 二进制 | defyers# | ML | 3ms | 52656kb | C++20 | 2.9kb | 2024-01-08 23:45:30 | 2024-01-08 23:45:30 |
Judging History
answer
#include "bits/stdc++.h"
using namespace std;
#pragma GCC optimize("Ofast")
#pragma GCC target("avx2")
using ll = long long;
using LL = long long;
const int N = 1 << 20;
struct BIT{
int bit[N];
void add(int p, int x){ for(int i = p; i < N; i += i & -i) bit[i] += x; }
int qry(int p){ int ret = 0; for(int i = p; i; i -= i & -i) ret += bit[i]; return ret; }
int bs(int v){ int ret = 0, s = 0; for(int j = 19; j >= 0; j--) if(ret + (1 << j) < N && s + bit[ret + (1 << j)] < v) ret += (1 << j), s += bit[ret]; return ret + 1; }
} bit;
int n;
bool a[N];
int l[N], r[N];
set<int> S[N];
int L[20], R[20];
int cur_len;
void rem(int x){
int sl = 0, sr = 0;
for(int y = l[x], tr = 0; y != 0 && tr < 20; tr++){
L[tr] = y; y = l[y]; sl++;
}
for(int y = r[x], tr = 0; y != n + 1 && tr < 20; tr++){
R[tr] = y; y = r[y]; sr++;
}
for(int i = sl - 1; i >= 0; i--){
if(!a[L[i]]) continue;
int v = 1;
for(int j = i - 1; j >= 0; j--){
v = v * 2 + a[L[j]];
}
// NOT choose middle
int u = v;
for(int j = 0; j < sr; j++){
u = u * 2 + a[R[j]];
if(u >= N) break;
if(u > cur_len)
S[u].insert(L[i]);
}
// DOES choose middle
u = v * 2 + a[x];
if(u < N && u > cur_len) S[u].erase(L[i]);
// cout << "R " << u << ' ' << L[i] << endl;
for(int j = 0; j < sr; j++){
u = u * 2 + a[R[j]];
if(u >= N) break;
// cout << "ADD " << u << ' ' << L[i] << endl;
if(u > cur_len)
S[u].erase(L[i]);
}
}
if(a[x]){
int u = a[x];
if(u > cur_len) S[u].erase(x);
for(int j = 0; j < sr; j++){
u = u * 2 + a[R[j]];
if(u >= N) break;
if(u > cur_len) S[u].erase(x);
}
}
bit.add(x, 1);
l[r[x]] = l[x];
r[l[x]] = r[x];
}
int len(int l){
int x = 0;
while(l){
x++;
l /= 2;
}
return x;
}
void solve(int TC) {
cin >> n;
for(int i = 0; i <= n + 1; i++){
l[i] = i - 1;
r[i] = i + 1;
}
l[0] = 0, r[n + 1] = n + 1;
string s; cin >> s;
for(int i = 1; i <= n; i++){
a[i] = s[i - 1] == '1';
}
for(int i = 1; i <= n; i++){
if(a[i] == 0) continue;
int v = 0;
for(int j = i; j < i + 20 && j <= n; j++){
v = v * 2 + a[j];
S[v].insert(i);
}
}
// for(int l = 1; l <= n; l++){
// for(auto u : S[l]) cout << u << ' '; cout << '\n';
// }
for(int l = 1; l <= n; l++){
cur_len = l;
int cnt = S[l].size();
if(cnt == 0){
cout << -1 << ' ' << 0 << '\n';
}else{
int idx = 0;
assert(cnt > 0);
// cout << "S T" << endl;
// for(auto x : S[l]) cout << x << ' '; cout << '\n';
// for(auto x : T[l]) cout << x << ' '; cout << '\n';
int pos = *S[l].begin();
cout << pos - bit.qry(pos - 1) << ' ' << cnt << '\n';
for(int j = 0; j < len(l); j++){
rem(pos); pos = r[pos];
}
}
S[l].clear();
}
}
int32_t main() {
cin.tie(0)->sync_with_stdio(0);
cout << fixed << setprecision(10);
int t = 1;
// cin >> t;
for (int i = 1; i <= t; i++) {
solve(i);
}
}
詳細信息
Test #1:
score: 100
Accepted
time: 3ms
memory: 52656kb
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
Memory 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...