QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#300905 | #7967. 二进制 | defyers# | ML | 390ms | 233164kb | C++20 | 3.2kb | 2024-01-09 00:01:25 | 2024-01-09 00:01:26 |
Judging History
answer
#include "bits/stdc++.h"
using namespace std;
#define int long long
#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];
vector<int> S[N], T[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].push_back(L[i]);
}
// DOES choose middle
u = v * 2 + a[x];
if(u < N && u > cur_len) T[u].push_back(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)
T[u].push_back(L[i]);
}
}
if(a[x]){
int u = a[x];
if(u > cur_len) T[u].push_back(x);
for(int j = 0; j < sr; j++){
u = u * 2 + a[R[j]];
if(u >= N) break;
if(u > cur_len) T[u].push_back(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].push_back(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;
sort(S[l].begin(), S[l].end());
sort(T[l].begin(), T[l].end());
int cnt = S[l].size() - T[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';
while(idx < T[l].size() && S[l][idx] == T[l][idx]){
++idx;
}
int pos = S[l][idx];
cout << pos - bit.qry(S[l][idx] - 1) << ' ' << cnt << '\n';
for(int j = 0; j < len(l); j++){
rem(pos); pos = r[pos];
}
}
S[l].clear(); T[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);
}
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 4ms
memory: 63520kb
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: 0
Accepted
time: 390ms
memory: 233164kb
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...
result:
ok 1000000 lines
Test #3:
score: -100
Memory Limit Exceeded
input:
1000000 1111111101011101110011011111111111111110110011110111011100111001011011011110101110111111111111111111111111011111101111110110111111111110111010111111100110011101101110011111111101111110111111111110111111111111011011110110111101101101110101100111111111001010111111111111010111011111111011110111...
output:
1 800681 7 159535 1 641144 13 31730 5 127804 5 127761 1 513379 542 6405 25 25324 54 25337 5 102464 30 25561 17 102196 17 102484 1 410890 2647 1324 618 5080 20 5103 99 20219 304 4894 89 20442 21 20308 15 82150 810 5135 82 20422 158 20309 20 81880 83 20567 39 81909 40 82119 1 328769 4222 267 2829 1056...