QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#449906 | #8598. AND Масив | bashkort# | 0 | 0ms | 0kb | C++20 | 3.5kb | 2024-06-21 19:03:44 | 2024-06-21 19:03:45 |
answer
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n, b;
cin >> n >> b;
vector<int> a(n);
vector<ll> suf(n + 1);
for (int i = 0; i < n; ++i) {
cin >> a[i];
if (a[i] == 0) {
suf[i] += (i + 1);
}
}
for (int i = n - 1; i >= 0; --i) {
suf[i] += suf[i + 1];
}
vector<ll> ans(n);
for (int i = 0; i < n; ++i) {
ans[i] += b * suf[i];
}
if (n <= 2000 || b <= 20) {
const int nb = 1 << b;
vector stk(nb, vector<int>{});
vector<int> value(n * b), timer(n * b);
int l = 14, r = b - l;
int nl = (1 << l), nr = (1 << r);
auto insert = [&](int id) {
int x = value[id];
int lx = x & (nl - 1);
int ix = lx;
int rx = x >> l << l;
timer[id] += 1;
while (true) {
stk[lx + rx].push_back(id * 21 + timer[id]);
if (lx + 1 == nl) {
break;
}
lx = (lx + 1) | ix;
}
};
auto update = [&](int x, int add, int orz) {
vector<int> now;
int lx = x & (nl - 1);
int rx = x >> l;
int ix = rx;
while (true) {
int f = lx + (rx << l);
now.insert(now.end(), stk[f].begin(), stk[f].end());
stk[f].clear();
stk[f].shrink_to_fit();
if (rx == 0) {
break;
}
rx = (rx - 1) & ix;
}
for (int tt: now) {
int i = tt / 21;
int t = tt - i * 21;
if (timer[i] != t) {
continue;
}
value[i] |= orz;
ans[i % n] += add;
insert(i);
}
};
for (int i = 0; i < n; ++i) {
for (int j = 0; j < b; ++j) {
value[j * n + i] = 1 << j;
insert(j * n + i);
}
if (a[i] != 0) {
update(nb - 1 - a[i], i + 1, a[i]);
}
}
} else {
vector last(b, vector<int>(n + 1, -1));
for (int i = n - 1; i >= 0; --i) {
if (a[i] > 0) {
last[__lg(a[i])][i] = i;
}
for (int j = 0; j < b; ++j) {
if (last[j][i] == -1) {
last[j][i] = last[j][i + 1];
}
}
}
for (int i = 0; i < n; ++i) {
for (int s = 0; s < b; ++s) {
int x = 1 << s;
int t = i;
while (x + 1 < (1 << b) && t < n) {
pair<int, int> mn{1e9, -1};
for (int u = 0; u < b; ++u) {
if ((x >> u & 1) == 0 && last[u][t] != -1) {
mn = min(mn, pair(last[u][t], u));
}
}
if (mn.first == 1e9) {
break;
}
ans[i] += mn.first + 1;
x |= 1 << mn.second;
t = mn.first + 1;
}
}
}
}
for (int i = 0; i < n; ++i) {
cout << ans[i] << " \n"[i == n - 1];
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Subtask #1:
score: 0
Time Limit Exceeded
Test #1:
score: 0
Time Limit Exceeded
input:
2000 20 251931 620255 725521 330111 783060 690627 489092 1019106 84341 631993 231500 920886 604265 342966 152434 588032 469990 805072 809795 12697 699326 433747 754394 567737 603087 199524 539078 775214 872735 454953 106496 93877 933762 36223 211878 168057 53977 782675 171782 455544 869778 47128 955...
output:
result:
Subtask #2:
score: 0
Time Limit Exceeded
Test #4:
score: 0
Time Limit Exceeded
input:
100000 20 262144 16 65536 8 256 1024 32 262144 16 262144 256 1024 1 64 2 131072 4096 2048 2 32 8192 4 2 262144 32768 1 524288 262144 262144 2048 8 64 1 2 8192 131072 256 64 8192 1 262144 4 32 4 524288 1 32768 16 64 128 8192 16 32 4096 16384 16384 4 131072 32768 16384 131072 2 16 2048 32768 16 4 4096...
output:
result:
Subtask #3:
score: 0
Runtime Error
Test #6:
score: 0
Runtime Error
input:
100000 8 98 78 5 190 79 234 162 79 118 176 115 130 10 9 233 56 97 15 148 13 46 87 92 65 150 62 50 46 159 101 48 86 203 71 29 124 23 228 55 161 240 80 139 74 251 143 167 207 183 52 50 252 17 185 40 145 167 164 227 166 172 60 182 62 173 227 232 243 251 134 109 241 44 33 217 149 51 6 110 201 242 196 23...
output:
result:
Subtask #4:
score: 0
Skipped
Dependency #3:
0%
Subtask #5:
score: 0
Skipped
Dependency #4:
0%
Subtask #6:
score: 0
Skipped
Dependency #1:
0%