CF121E Lucky Array
题意
给定一个序列,维护下列操作。
- 区间加
- 区间查询数中只包含 \(4, 7\) 数的个数。
所有数前后不超过 \(1e4\)。
Sol
块块版。
\(1e4\),发现满足条件的数的个数只有 \(30\) 个。
对于每个块开一个桶,记录每种数有多少个。
查询时暴力枚举 \(30\) 个数,暴力判断即可。
修改是平凡的。
Code
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <array>
#include <vector>
using namespace std;
#ifdef ONLINE_JUDGE
#define getchar() (p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1 << 21, stdin), p1 == p2) ? EOF : *p1++)
char buf[1 << 23], *p1 = buf, *p2 = buf, ubuf[1 << 23], *u = ubuf;
#endif
int read() {
int p = 0, flg = 1;
char c = getchar();
while (c < '0' || c > '9') {
if (c == '-') flg = -1;
c = getchar();
}
while (c >= '0' && c <= '9') {
p = p * 10 + c - '0';
c = getchar();
}
return p * flg;
}
string read_() {
string ans;
char c = getchar();
while (c < 'a' || c > 'z')
c = getchar();
while (c >= 'a' && c <= 'z')
ans += c, c = getchar();
return ans;
}
void write(int x) {
if (x < 0) {
x = -x;
putchar('-');
}
if (x > 9) {
write(x / 10);
}
putchar(x % 10 + '0');
}
const int N = 1e5 + 5, blk = 327;
array <array <int, N>, blk> bk;
array <int, blk> tag;
array <int, N> s;
int calc(int x) {
return (x - 1) / blk + 1;
}
vector <int> isl;
void modify(int l, int r, int x) {
if (calc(l) == calc(r)) {
for (int i = l; i <= r; i++)
bk[calc(i)][s[i]]--, s[i] += x, bk[calc(i)][s[i]]++;
return;
}
for (int i = l; i <= calc(l) * blk; i++)
bk[calc(i)][s[i]]--, s[i] += x, bk[calc(i)][s[i]]++;
for (int i = (calc(r) - 1) * blk + 1; i <= r; i++)
bk[calc(i)][s[i]]--, s[i] += x, bk[calc(i)][s[i]]++;
for (int i = calc(l) + 1; i <= calc(r) - 1; i++)
tag[i] += x;
}
bool check(int x) {
for (auto y : isl)
if (x == y) return 1;
return 0;
}
int query(int l, int r) {
int ans = 0;
if (calc(l) == calc(r)) {
for (int i = l; i <= r; i++)
if (check(s[i] + tag[calc(l)]))
ans++;
return ans;
}
for (int i = l; i <= calc(l) * blk; i++)
ans += check(s[i] + tag[calc(l)]);
for (int i = (calc(r) - 1) * blk + 1; i <= r; i++)
ans += check(s[i] + tag[calc(r)]);
for (int i = calc(l) + 1; i <= calc(r) - 1; i++) {
for (auto x : isl) {
if (x - tag[i] <= 0) continue;
ans += bk[i][x - tag[i]];
}
}
return ans;
}
int main() {
int n = read(), q = read();
for (int i = 1; i <= n; i++)
s[i] = read();
for (int i = 1; i <= calc(n); i++)
for (int j = (i - 1) * blk + 1; j <= i * blk; j++)
bk[i][s[j]]++;
for (int l = 1; l <= 4; l++) {
for (int T = 0; T < 1 << l; T++) {
int idx = 0, tp = 1;
for (int i = 1; i <= l; i++) {
idx += (T & (1 << (i - 1)) ? 7 : 4) * tp;
tp *= 10;
}
isl.push_back(idx);
}
}
/* for (auto x : isl) */
/* write(x), putchar(32); */
/* puts(""); */
while (q--) {
string tp = read_();
int l = read(), r = read();
if (tp == "add") {
int x = read();
modify(l, r, x);
}
else write(query(l, r)), puts("");
}
return 0;
}