QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#615411 | #7967. 二进制 | yzj12345 | ML | 3ms | 57040kb | C++20 | 4.3kb | 2024-10-05 18:29:28 | 2024-10-05 18:29:30 |
Judging History
answer
#include <bits/stdc++.h>
#pragma GCC optimize(2)
#define ls(v) v << 1
#define rs(v) v << 1 | 1
using namespace std;
int read()
{
int res = 0, bj = 1;
char ch = getchar();
while (ch < '0' || ch > '9')
{
if (ch == '-') bj = -1;
ch = getchar();
}
while (ch >= '0' && ch <= '9')
{
res = res * 10 + ch - '0';
ch = getchar();
}
return res * bj;
}
const int MAXN = 1e6 + 5;
int n;
int a[MAXN];
char s[MAXN];
set<int> pos[MAXN];
int nxt[MAXN], pre[MAXN];
int vis[MAXN];
int sum[MAXN * 4];
void pushup(int v)
{
sum[v] = sum[ls(v)] + sum[rs(v)];
}
void pushdown(int v)
{
if (sum[v] == 0) sum[ls(v)] = sum[rs(v)] = 0;
}
void build(int v, int l, int r)
{
if (l == r)
{
sum[v] = 1;
return;
}
int mid = l + r >> 1;
build(ls(v), l, mid);
build(rs(v), mid + 1, r);
pushup(v);
}
void add(int v, int l, int r, int ql, int qr)
{
if (l > qr || r < ql) return;
if (l >= ql && r <= qr)
{
sum[v] = 0;
return;
}
int mid = l + r >> 1;
pushdown(v);
add(ls(v), l, mid, ql, qr);
add(rs(v), mid + 1, r, ql, qr);
pushup(v);
}
int ask(int v, int l, int r, int ql, int qr)
{
if (l > qr || r < ql) return 0;
if (l >= ql && r <= qr) return sum[v];
int mid = l + r >> 1;
pushdown(v);
int res1 = ask(ls(v), l, mid, ql, qr), res2 = ask(rs(v), mid + 1, r, ql, qr);
return res1 + res2;
}
int main()
{
// freopen("1.in", "r", stdin);
n = read();
build(1, 1, n);
scanf("%s", s + 1);
for (int i = 1; i <= n; i++) nxt[i] = i + 1, pre[i] = i - 1;
nxt[n] = 0;
for (int i = 1; i <= n; i++)
{
int x = 0;
if (s[i] != '0')
{
for (int j = 1; j <= 20 && i + j - 1 <= n; j++)
{
x = x * 2 + s[i + j - 1] - '0';
// cout << "x:" << i << " " << i + j - 1 << " " << x << "\n";
if (x <= n) pos[x].insert(i);
else break;
}
}
}
// for (int i = 1; i <= 10; i++)
// {
// cout << "pos " << i << ": ";
// for (auto x : pos[i]) cout << x << " ";
// cout << "\n";
// }
// cout << "!!!!\n";
for (int i = 1; i <= n; i++)
{
// cout << "i:" << i << "\n";
int d = 0, tp = i;
while (tp) d++, tp /= 2;
if (pos[i].size())
{
int st = *(pos[i].begin()), p = st;
cout << ask(1, 1, n, 1, st) << " " << pos[i].size();
cout << "\n";
// cout << " " << d << "\n";
// vis[p] = 1;
// for (int j = 1; j < d; j++) p = nxt[p], vis[p] = 1;
// p = st;
// for (int j = 1; j < 20 && pre[p] != 0; j++)
// {
// p = pre[p];
// int x = 0, l = p;
// for (int k = 1; k <= 20 && l != 0; k++)
// {
// x = x * 10 + s[l] - '0';
// if (vis[l]) pos[x].erase(p);
// l = nxt[l];
// }
// }
// p = st;
// for (int j = 1; j <= d; j++)
// {
// int x = 0, l = p;
// for (int k = 1; k <= d && l != 0; k++)
// {
// x = x * 10 + s[l] - '0';
// pos[x].erase(p);
// l = nxt[l];
// }
// p = nxt[p];
// }
for (int j = 1; j < d; j++) p = nxt[p];
int ed = p;
add(1, 1, n, st, ed);
//向前跳19步
p = st;
for (int j = 1; j < 20 && pre[p] != 0; j++) p = pre[p];
// cout << "step1:" << p << " " << ed << "\n";
int st2 = p, ed2 = pre[st];
while (1)
{
int x = 0, l = p;
if (s[p] != '0')
{
for (int j = 1; j <= 20 && l != 0; j++)
{
x = x * 2 + s[l] - '0';
if (x <= n) pos[x].erase(p);
else break;
l = nxt[l];
}
}
if (p == ed) break;
p = nxt[p];
}
// cout << "ed2:" << ed2 << " " << nxt[ed] << "\n";
nxt[pre[st]] = nxt[ed];
pre[nxt[ed]] = pre[st];
p = st2;
if (p <= ed2)
{
// cout << "step2:" << p << " " << ed2 << "\n";
// system("pause");
while (1)
{
int x = 0, l = p;
if (s[p] != '0')
{
for (int j = 1; j <= 20 && l != 0; j++)
{
x = x * 2 + s[l] - '0';
// cout << "x:" << p << " " << l << " " << x << "\n";
if (x <= n) pos[x].insert(p);
else break;
l = nxt[l];
}
}
if (p == ed2) break;
p = nxt[p];
}
}
}
else cout << -1 << " " << 0 << "\n";
// for (int i = 1; i <= 10; i++)
// {
// cout << "pos " << i << ": ";
// int cc = 0;
// for (auto x : pos[i])
// {
// cout << x << " ";
// cc++;
// if (cc == 10) break;
// }
// cout << "\n";
// }
// cout << "\n";
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 3ms
memory: 57040kb
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...