洛谷P2787,分块只有7分,求调:
// # pragma GCC optimize("Ofast,no-stack-protector")
//
// # pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
# include <bits/stdc++.h>
# define I return
# define AK 0
# define IOI ;
using namespace std;
typedef long long ll;
typedef pair <int, int> pii;
int n, m, op, l, r, t, col[50005], st[230], ed[230], lazy[230], s[230][30], a[50005];
string str;
char c;
void init () {
t = sqrt (n);
for (int i = 1; i <= t; ++ i)
st[i] = n / t * (i - 1) + 1, ed[i] = n / t * i;
ed[t] = n;
for (int i = 1; i <= t; lazy[i] = -1, ++ i)
for (int j = st[i]; j <= ed[i]; ++ j)
col[j] = i, ++ s[i][a[j]];
return ;
}
void pushdown (int x) {
if (~lazy[x]) {
for (int i = st[x]; i <= ed[x]; ++ i)
a[i] = lazy[x];
lazy[x] = -1;
}
return ;
}
int find (const int l, const int r, const int k) {
const int cl = col[l], cr = col[r];
int sum = 0;
pushdown (cl);
if (cl == cr) {
for (int i = l; i <= r; ++ i)
sum += (a[i] == k);
return sum;
}
for (int i = l; i <= ed[cl]; ++ i)
sum += (a[i] == k);
for (int i = cl + 1; i < cr; ++ i)
sum += s[i][k];
pushdown (cr);
for (int i = st[cr]; i <= r; ++ i)
sum += (a[i] == k);
return sum;
}
void change (const int l, const int r, const int k) {
const int cl = col[l], cr = col[r];
pushdown (cl);
if (cl == cr) {
for (int i = l; i <= r; ++ i)
-- s[cl][a[i]], ++ s[cl][k], a[i] = k;
return ;
}
for (int i = l; i <= ed[cl]; ++ i)
-- s[cl][a[i]], ++ s[cl][k], a[i] = k;
for (int i = cl + 1; i < cr; ++ i) {
for (int j = 0; j < 26; ++ j)
s[i][j] = 0;
s[i][k] = ed[i] - st[i] + 1;
lazy[i] = k;
}
pushdown (cr);
for (int i = st[cr]; i <= r; ++ i)
-- s[cr][a[i]], ++ s[cr][k], a[i] += k;
return ;
}
void sort (int l, int r) {
const int cl = col[l], cr = col[r];
pushdown (cl);
if (cl == cr) {
sort (a + l, a + r + 1);
return ;
}
int sum[30] = {}, now = 0;
for (int i = l; i <= ed[cl]; ++ i)
++ sum[a[i]];
for (int i = cl + 1; i < cr; ++ i)
for (int j = 0; j < 26; ++ j)
sum[j] += s[i][j];
pushdown (cr);
for (int i = st[cr]; i <= r; ++ i)
++ sum[a[i]];
for (int i = l; i <= ed[cl]; ++ i) {
-- s[cl][a[i]], ++ s[cl][now], a[i] = now, -- sum[now];
while (now < 26 && ! sum[now])
++ now;
}
for (int i = cl + 1; i < cr; ++ i)
if (sum[now] >= ed[i] - st[i] + 1) {
for (int j = 0; j < 26; ++ j)
s[i][j] = 0;
s[i][now] = ed[i] - st[i] + 1;
sum[now] -= ed[i] - st[i] + 1;
lazy[i] = now;
while (now < 26 && ! sum[now])
++ now;
} else for (int j = st[i]; j <= ed[i]; ++ j) {
-- s[cr][a[j]], ++ s[cr][now], a[j] = now, -- sum[now];
while (now < 26 && ! sum[now])
++ now;
}
for (int i = st[cr]; i <= r; ++ i) {
-- s[cr][a[i]], ++ s[cr][now], a[i] = now, -- sum[now];
while (now < 26 && ! sum[now])
++ now;
}
return ;
}
int main () {
ios::sync_with_stdio (0);
cin.tie (0);
cout.tie (0);
cin >> n >> m >> str;
for (int i = 0; i < n; ++ i)
a[i + 1] = str[i] >= 'a' ? str[i] - 'a' : str[i] - 'A';
init ();
while (m --) {
cin >> op >> l >> r;
if (op < 2)
cin >> c, cout << find (l, r, c >= 'a' ? c - 'a' : c - 'A') << '\n';
else if (op < 3)
cin >> c, change (l, r, c >= 'a' ? c - 'a' : c - 'A');
else
sort (l, r);
}
I AK IOI
}