QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#322074 | #5014. 复读程度 | hos_lyric | 6 | 685ms | 83016kb | C++14 | 31.0kb | 2024-02-06 10:05:27 | 2024-02-06 10:05:27 |
Judging History
answer
#include <cassert>
#include <cmath>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <functional>
#include <iostream>
#include <limits>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
using namespace std;
using Int = long long;
template <class T1, class T2> ostream &operator<<(ostream &os, const pair<T1, T2> &a) { return os << "(" << a.first << ", " << a.second << ")"; };
template <class T> ostream &operator<<(ostream &os, const vector<T> &as) { const int sz = as.size(); os << "["; for (int i = 0; i < sz; ++i) { if (i >= 256) { os << ", ..."; break; } if (i > 0) { os << ", "; } os << as[i]; } return os << "]"; }
template <class T> void pv(T a, T b) { for (T i = a; i != b; ++i) cerr << *i << " "; cerr << endl; }
template <class T> bool chmin(T &t, const T &f) { if (t > f) { t = f; return true; } return false; }
template <class T> bool chmax(T &t, const T &f) { if (t < f) { t = f; return true; } return false; }
#define COLOR(s) ("\x1b[" s "m")
////////////////////////////////////////////////////////////////////////////////
// SA-IS
// String: string, vector<int>, vector<long long>
// if sigma <= n, O(n) time, O(n) space
// if sigma > n, O(n log n) time, O(n) space
template <class String> vector<int> suffixArrayRec(const String &as) {
const int n = as.size();
if (n == 0) return {};
const auto minmaxA = minmax_element(as.begin(), as.end());
const auto minA = *minmaxA.first, maxA = *minmaxA.second;
if (static_cast<unsigned long long>(maxA) - minA >=
static_cast<unsigned long long>(n)) {
vector<int> us(n);
for (int u = 0; u < n; ++u) us[u] = u;
std::sort(us.begin(), us.end(), [&](int u, int v) -> bool {
return (as[u] < as[v]);
});
int b = 0;
vector<int> bs(n, 0);
for (int i = 1; i < n; ++i) {
if (as[us[i - 1]] != as[us[i]]) ++b;
bs[us[i]] = b;
}
return suffixArrayRec(bs);
}
const int sigma = maxA - minA + 1;
vector<int> pt(sigma + 1, 0), poss(sigma);
for (int u = 0; u < n; ++u) ++pt[as[u] - minA + 1];
for (int a = 0; a < sigma; ++a) pt[a + 1] += pt[a];
// cmp[u] := (as[u, n) < as[u + 1, n))
vector<bool> cmp(n);
cmp[n - 1] = false;
for (int u = n - 1; --u >= 0; ) {
cmp[u] = (as[u] != as[u + 1]) ? (as[u] < as[u + 1]) : cmp[u + 1];
}
// ><, nn - id (0 <= id < n)
int nn = 0;
vector<int> ids(n, 0);
int last = n;
vector<int> nxt(n, 0);
// put ><, from the tail of each bucket
vector<int> us(n, 0);
memcpy(poss.data(), pt.data() + 1, sigma * sizeof(int));
for (int u = n - 1; --u >= 1; ) if (!cmp[u - 1] && cmp[u]) {
ids[u] = ++nn;
nxt[u] = last;
last = u;
us[--poss[as[u] - minA]] = u;
}
// follow > backwards, from the head of each bucket
memcpy(poss.data(), pt.data(), sigma * sizeof(int));
us[poss[as[n - 1] - minA]++] = n - 1;
for (int i = 0; i < n; ++i) {
const int u = us[i];
if (u && !cmp[u - 1]) us[poss[as[u - 1] - minA]++] = u - 1;
}
// follow < backwards, from the tail of each bucket
memcpy(poss.data(), pt.data() + 1, sigma * sizeof(int));
for (int i = n; --i >= 0; ) {
const int u = us[i];
if (u && cmp[u - 1]) us[--poss[as[u - 1] - minA]] = u - 1;
}
if (nn) {
int vsLen = 0;
vector<int> vs(nn);
for (const int u : us) if (ids[u]) vs[vsLen++] = u;
int b = 0;
vector<int> bs(nn, 0);
for (int j = 1; j < nn; ++j) {
// as[v1, w1] (< or =) as[v0, w0]
int v1 = vs[j - 1], v0 = vs[j];
const int w1 = nxt[v1], w0 = nxt[v0];
if (w1 - v1 != w0 - v0) {
++b;
} else {
for (; ; ++v1, ++v0) {
if (v1 == n) { ++b; break; }
if (as[v1] != as[v0]) { ++b; break; }
if (v1 == w1) break;
}
}
bs[nn - ids[vs[j]]] = b;
}
for (int u = 0; u < n; ++u) if (ids[u]) vs[nn - ids[u]] = u;
const auto sub = suffixArrayRec(bs);
// put ><, from the tail of each bucket
memset(us.data(), 0, n * sizeof(int));
memcpy(poss.data(), pt.data() + 1, sigma * sizeof(int));
for (int j = nn; --j >= 0; ) {
const int u = vs[sub[j]];
us[--poss[as[u] - minA]] = u;
}
// follow > backwards, from the head of each bucket
memcpy(poss.data(), pt.data(), sigma * sizeof(int));
us[poss[as[n - 1] - minA]++] = n - 1;
for (int i = 0; i < n; ++i) {
const int u = us[i];
if (u && !cmp[u - 1]) us[poss[as[u - 1] - minA]++] = u - 1;
}
// follow < backwards, from the tail of each bucket
memcpy(poss.data(), pt.data() + 1, sigma * sizeof(int));
for (int i = n; --i >= 0; ) {
const int u = us[i];
if (u && cmp[u - 1]) us[--poss[as[u - 1] - minA]] = u - 1;
}
}
return us;
}
// us[i]: i-th suffix
// su[u]: index of as[u, n)
// hs[i]: longest common prefix of as[us[i - 1], n) and as[us[i], n)
struct SuffixArray {
int n;
bool rmq;
vector<int> us, su, hs;
SuffixArray() : n(0), rmq(false) {}
SuffixArray(const string &as, bool rmq_) : rmq(rmq_) { build(as); }
SuffixArray(const vector<int> &as, bool rmq_) : rmq(rmq_) { build(as); }
SuffixArray(const vector<long long> &as, bool rmq_) : rmq(rmq_) { build(as); }
template <class String> void build(const String &as) {
n = as.size();
us = suffixArrayRec(as);
su.resize(n + 1);
for (int i = 0; i < n; ++i) su[us[i]] = i;
su[n] = -1;
hs.assign(n, 0);
for (int h = 0, u = 0; u < n; ++u) if (su[u]) {
for (int v = us[su[u] - 1]; v + h < n && as[v + h] == as[u + h]; ++h) {}
hs[su[u]] = h;
if (h) --h;
}
if (rmq) {
const int logN = n ? (31 - __builtin_clz(n)) : 0;
hs.resize((logN + 1) * n - (1 << logN) + 1);
for (int e = 0; e < logN; ++e) {
int *hes = hs.data() + e * n;
for (int i = 0; i <= n - (1 << (e + 1)); ++i) {
hes[n + i] = min(hes[i], hes[i + (1 << e)]);
}
}
}
}
// Returns longest common prefix of as[u, n) and as[v, n).
// 0 <= u, v <= n
// Assumes rmq.
inline int lcp(int u, int v) const {
if (u == v) return n - u;
int i = su[u], j = su[v];
if (i > j) swap(i, j);
const int e = 31 - __builtin_clz(j - i);
return min(hs[e * n + i + 1], hs[e * n + j + 1 - (1 << e)]);
}
};
////////////////////////////////////////////////////////////////////////////////
// before HLD:
// 0 <= u <= n: suffix [u, n) (n: root, empty string)
// n < u < m: LCA needed
// after HLD:
// DFS-order
// 0: root, empty string
// perm[u]: suffix[u, n) (0 <= u <= n)
struct SuffixTree {
int n, m;
SuffixArray sa;
struct Node {
int par, len, occ;
inline int l() const { return occ; }
inline int r() const { return occ + len; }
};
vector<Node> nodes;
vector<int> perm;
SuffixTree() {}
SuffixTree(const string &str, bool lastOcc) { build(str, lastOcc); }
SuffixTree(const vector<int> &str, bool lastOcc) { build(str, lastOcc); }
SuffixTree(const vector<long long> &str, bool lastOcc) { build(str, lastOcc); }
template <class String> void build(const String &str, bool lastOcc) {
n = str.size();
m = n + 1;
sa = SuffixArray(str, /*rmq=*/false);
nodes.resize(2 * n + 1);
nodes[n] = Node{/*par=*/-1, /*len=*/0, /*occ=*/lastOcc ? n : 0};
int top = 0;
vector<int> stack(n + 1);
stack[0] = n;
for (int i = 0; i < n; ++i) {
const int u = sa.us[i];
int v = -1;
for (; nodes[stack[top]].len > sa.hs[i]; --top) {
v = stack[top];
nodes[nodes[v].par].occ = lastOcc ? max(nodes[nodes[v].par].occ, nodes[v].occ) : min(nodes[nodes[v].par].occ, nodes[v].occ);
}
if (nodes[stack[top]].len < sa.hs[i]) {
const int w = m++;
nodes[w] = Node{/*par=*/nodes[v].par, /*len=*/sa.hs[i], /*occ=*/nodes[v].occ};
stack[++top] = nodes[v].par = w;
}
nodes[u] = Node{/*par=*/stack[top], /*len=*/n - u, /*occ=*/u};
stack[++top] = u;
}
for (; top; --top) {
const int v = stack[top];
nodes[nodes[v].par].occ = lastOcc ? max(nodes[nodes[v].par].occ, nodes[v].occ) : min(nodes[nodes[v].par].occ, nodes[v].occ);
}
nodes.resize(m);
runHld();
}
inline const Node &operator[](int u) const {
return nodes[u];
}
inline int size(int u) const {
return (~nodes[u].par) ? (nodes[u].len - nodes[nodes[u].par].len) : 1;
}
// Reindexes nodes by DFS-order.
// Ignores character order.
// Subtrees at the same "color" are isomorphic, should have the same HLD.
// old u -> new perm[u]
vector<int> pt, g, head;
void runHld() {
pt.assign(m + 1, 0);
for (int u = 0; u < m; ++u) if (u != n) ++pt[nodes[u].par];
for (int u = 0; u < m; ++u) pt[u + 1] += pt[u];
g.resize(pt[m]);
for (int u = m; --u >= 0; ) if (u != n) g[--pt[nodes[u].par]] = u;
vector<int> sz(m, 1);
dfsSz(n, sz);
int zeit = 0;
perm.resize(m);
head.resize(m);
head[n] = 0;
dfsHld(n, zeit, sz);
assert(zeit == m);
vector<Node> nodesReindexed(m);
for (int u = 0; u < m; ++u) {
Node &node = nodesReindexed[perm[u]] = nodes[u];
if (~node.par) node.par = perm[node.par];
}
nodes.swap(nodesReindexed);
for (int u = 0; u <= m; ++u) pt[u] = 0;
for (int u = 1; u < m; ++u) ++pt[nodes[u].par];
for (int u = 1; u < m; ++u) pt[u + 1] += pt[u];
g.resize(pt[m]);
for (int u = m; --u >= 1; ) g[--pt[nodes[u].par]] = u;
}
void dfsSz(int u, vector<int> &sz) {
for (int i = pt[u]; i < pt[u + 1]; ++i) {
dfsSz(g[i], sz);
sz[u] += sz[g[i]];
}
}
void dfsHld(int u, int &zeit, vector<int> &sz) {
perm[u] = zeit++;
if (pt[u] < pt[u + 1]) {
int im = pt[u];
for (int i = pt[u] + 1; i < pt[u + 1]; ++i) if (sz[g[im]] < sz[g[i]]) im = i;
swap(g[pt[u]], g[im]);
head[zeit] = head[zeit - 1];
dfsHld(g[pt[u]], zeit, sz);
for (int i = pt[u] + 1; i < pt[u + 1]; ++i) {
head[zeit] = zeit;
dfsHld(g[i], zeit, sz);
}
}
}
// Returns the shallowest node representing [l, r') for r <= r'.
int locate(int l, int r) const {
assert(0 <= l); assert(l <= r); assert(r <= n);
for (int u = perm[l]; ; ) {
const int h = head[u];
const int p = nodes[h].par;
if (!~p || nodes[p].len < r - l) {
int lo = h - 1, hi = u;
for (; lo + 1 < hi; ) {
const int mid = (lo + hi) / 2;
((nodes[mid].len < r - l) ? lo : hi) = mid;
}
return hi;
}
u = p;
}
}
};
// block i contains [ls[i] + x, rs[i] - y) s.t.
// 0 <= x < sizeL(i), 0 <= y < sizeR(i, x)
// 0 <= y < sizeR(i), 0 <= x < sizeL(i, y)
struct Substring {
// |str|
int n;
// stRev: occ is modified to represent the first occurrence in str
SuffixTree st, stRev;
// # of colors
int size;
// tree node -> block id
vector<int> is, isRev;
// [ls[i], rs[i]): representative of block i, i.e. [min l, max r)
vector<int> ls, rs;
// tree nodes for block i: us[js[i], js[i] + sizeL(i)), usRev[jsRev[i], jsRev[i] + sizeR(i))
vector<int> js, jsRev, us, usRev;
Substring() {}
Substring(const string &str) { build(str); }
Substring(const vector<int> &str) { build(str); }
Substring(const vector<long long> &str) { build(str); }
// O(n) time
template <class String> void build(const String &str) {
n = str.size();
st = SuffixTree(str, /*lastOcc=*/false);
String strRev = str;
std::reverse(strRev.begin(), strRev.end());
stRev = SuffixTree(strRev, /*lastOcc=*/true);
for (int u = 0; u < stRev.m; ++u) stRev.nodes[u].occ = n - stRev.nodes[u].r();
size = 0;
is.assign(st.m, -1);
isRev.assign(stRev.m, -1);
js = jsRev = {1};
us.assign(st.m, 0);
usRev.assign(stRev.m, 0);
{
// radix sort: incr len, incr occ
const int seqLen = (st.m - 1) + (stRev.m - 1);
vector<int> ptLen(n + 1, 0), ptOcc(n + 1, 0);
for (int u = 1; u < st.m; ++u) { ++ptLen[st[u].len]; ++ptOcc[st[u].occ]; }
for (int u = 1; u < stRev.m; ++u) { ++ptLen[stRev[u].len]; ++ptOcc[stRev[u].occ]; }
for (int len = 0; len < n; ++len) ptLen[len + 1] += ptLen[len];
for (int occ = 0; occ < n; ++occ) ptOcc[occ + 1] += ptOcc[occ];
vector<int> work(seqLen);
for (int u = stRev.m; --u >= 1; ) work[--ptOcc[stRev[u].occ]] = ~u;
for (int u = st.m; --u >= 1; ) work[--ptOcc[st[u].occ]] = u;
vector<int> seq(seqLen);
for (int k = seqLen; --k >= 0; ) seq[--ptLen[(work[k] >= 0) ? st[work[k]].len : stRev[~work[k]].len]] = work[k];
for (int k = 0; k < seqLen - 1; ++k) if (seq[k] >= 0 && seq[k + 1] < 0 && st[seq[k]].len == stRev[~seq[k + 1]].len && st[seq[k]].occ == stRev[~seq[k + 1]].occ) {
ls.push_back(st[seq[k]].l());
rs.push_back(st[seq[k]].r());
js.push_back(js.back() + stRev.size(~seq[k + 1]));
jsRev.push_back(jsRev.back() + st.size(seq[k]));
is[seq[k]] = isRev[~seq[k + 1]] = size++;
}
}
{
// radix sort: incr r, incr l
const int seqLen = st.m - 1;
vector<int> ptL(n + 1, 0), ptR(n + 1, 0);
for (int u = 1; u < st.m; ++u) { ++ptL[st[u].l()]; ++ptR[st[u].r()]; }
for (int l = 0; l < n; ++l) ptL[l + 1] += ptL[l];
for (int r = 0; r < n; ++r) ptR[r + 1] += ptR[r];
vector<int> work(seqLen);
for (int u = st.m; --u >= 1; ) work[--ptL[st[u].l()]] = u;
vector<int> seq(seqLen);
for (int k = seqLen; --k >= 0; ) seq[--ptR[st[work[k]].r()]] = work[k];
int i = -1, j = 0;
for (int k = 0; k < seqLen; ++k) {
if (~is[seq[k]]) j = js[i = is[seq[k]]];
is[us[j++] = seq[k]] = i;
}
}
{
// radix sort: decr l, decr r
const int seqLen = stRev.m - 1;
vector<int> ptL(n + 1, 0), ptR(n + 1, 0);
for (int u = 1; u < stRev.m; ++u) { ++ptL[stRev[u].l()]; ++ptR[stRev[u].r()]; }
for (int l = n; l > 0; --l) ptL[l - 1] += ptL[l];
for (int r = n; r > 0; --r) ptR[r - 1] += ptR[r];
vector<int> work(seqLen);
for (int u = stRev.m; --u >= 1; ) work[--ptR[stRev[u].r()]] = u;
vector<int> seq(seqLen);
for (int k = seqLen; --k >= 0; ) seq[--ptL[stRev[work[k]].l()]] = work[k];
int i = -1, j = 0;
for (int k = 0; k < seqLen; ++k) {
if (~isRev[seq[k]]) j = jsRev[i = isRev[seq[k]]];
isRev[usRev[j++] = seq[k]] = i;
}
}
}
// block id at representative position
// st node id
// stRev node id
friend ostream &operator<<(ostream &os, const Substring &sub) {
const int width = max(static_cast<int>(std::to_string(max(sub.st.m, sub.stRev.m) - 1).size()) + 1, 3);
for (int phase = 0; phase < 3; ++phase) {
for (int r = sub.n; r > 0; --r) {
for (int l = 0; l < r; ++l) {
const Location loc = sub.locate(l, r);
string s;
switch (phase) {
case 0: {
if (sub.ls[loc.i] == l && sub.rs[loc.i] == r) s = std::to_string(loc.i);
} break;
case 1: {
if (sub.st[loc.u].len == r - l) s = std::to_string(loc.u);
} break;
case 2: {
if (sub.stRev[loc.v].len == r - l) s = std::to_string(loc.v);
} break;
}
os << "\x1b[" << (41 + loc.i % 6) << "m";
os << string(width - static_cast<int>(s.size()), ' ') << s;
os << "\x1b[m";
}
os << '\n';
}
os << '\n';
}
return os;
}
inline int id(int i, int x = 0) const {
return us[js[i] + x];
}
inline int idRev(int i, int y = 0) const {
return usRev[jsRev[i] + y];
}
inline int sizeR(int i, int x = 0) const {
return st.size(id(i, x));
}
inline int sizeL(int i, int y = 0) const {
return stRev.size(idRev(i, y));
}
// i: block id
// x, y: coordinate within block i, [ls[i] + x, rs[i] - y)
// u = st.locate(l, r) : shallowest node of st for [l, r') s.t. r <= r'
// v = stRev.locate(n - r, n - l): shallowest node of stRev for [l', r) s.t. l' <= l
// O(log(n)) time
struct Location {
int i, x, y, u, v;
};
Location locate(int l, int r) const {
assert(0 <= l); assert(l <= r); assert(r <= n);
if (l == r) return Location{-1, 0, 0, 0, 0};
Location loc;
loc.u = st.locate(l, r);
loc.i = is[loc.u];
loc.x = st[loc.u].l() - ls[loc.i];
loc.y = ((l - loc.x) + (rs[loc.i] - ls[loc.i])) - r;
loc.v = idRev(loc.i, loc.y);
return loc;
}
// pattern ([l, r), t): (weight of str[l, r)) += t
// l < r
// T: commutative group
// query [l, r): \sum[l<=l'<r'<=r] (weight of str[l', r'))
// O(n + (|patterns| + |queries|) log(n)) time
template <class T>
vector<T> countOffline(const vector<pair<pair<int, int>, T>> &patterns,
const vector<pair<int, int>> &queries) const {
const int patternsLen = patterns.size();
const int queriesLen = queries.size();
// x -> ((y, p or ~q))
vector<vector<pair<int, int>>> eventss(st.m);
// tree DP (path to root)
vector<T> dp(st.m), dpRev(stRev.m);
for (int p = 0; p < patternsLen; ++p) {
const int l = patterns[p].first.first, r = patterns[p].first.second;
assert(0 <= l); assert(l < r); assert(r <= n);
const Location loc = locate(l, r);
eventss[js[loc.i] + loc.x].emplace_back(loc.y, p);
dp[loc.u] += patterns[p].second;
dpRev[loc.v] += patterns[p].second;
}
for (int u = 1; u < st.m; ++u) dp[u] += dp[st[u].par];
for (int u = 1; u < stRev.m; ++u) dpRev[u] += dpRev[stRev[u].par];
// query [ls[i], rs[i])
vector<T> corner(size);
for (int i = 0; i < size; ++i) {
for (int x = 0; x < sizeL(i); ++x) corner[i] += dp[id(i, x)];
const int ii = isRev[stRev[idRev(i)].par];
if (~ii) corner[i] += corner[ii];
}
// query [ls[i], rs[i] - y)
vector<T> edge(stRev.m);
for (int i = 0; i < size; ++i) {
const int ii = is[st[id(i)].par];
T sum = (~ii) ? corner[ii] : T();
for (int y = sizeR(i); --y >= 0; ) edge[jsRev[i] + y] = sum += dpRev[idRev(i, y)];
}
// suffix sum of dp[st[id(i, x)].par]
// can use segment tree if subtraction is unavailable
vector<T> sumPar(st.m);
for (int i = 0; i < size; ++i) {
T sum = T();
for (int x = sizeL(i); --x >= 0; ) sumPar[js[i] + x] = sum += dp[st[id(i, x)].par];
}
// query [l, r)
vector<T> ans(queriesLen);
vector<int> hasQuery(size, 0);
for (int q = 0; q < queriesLen; ++q) {
const int l = queries[q].first, r = queries[q].second;
assert(0 <= l); assert(l <= r); assert(r <= n);
if (l < r) {
const Location loc = locate(queries[q].first, queries[q].second);
if (loc.x == 0) {
if (loc.y == 0) {
ans[q] += corner[loc.i];
} else {
ans[q] += edge[jsRev[loc.i] + loc.y];
}
} else {
hasQuery[loc.i] = 1;
eventss[js[loc.i] + loc.x].emplace_back(loc.y, ~q);
ans[q] += sumPar[js[loc.i] + loc.x];
if (sizeL(loc.i, loc.y) < sizeL(loc.i)) ans[q] -= sumPar[js[loc.i] + sizeL(loc.i, loc.y)];
const int vv = stRev[loc.v].par;
const int ii = isRev[vv];
if (~ii) ans[q] += edge[jsRev[ii] + (rs[ii] - stRev[vv].r())];
}
}
}
// offline 2D
vector<T> bit(n + 1);
for (int i = 0; i < size; ++i) if (hasQuery[i]) {
for (int y = 1; y <= sizeR(i); ++y) bit[y] = T();
for (int x = sizeL(i); --x >= 0; ) for (const auto &event : eventss[js[i] + x]) {
if (event.second >= 0) {
const T t = patterns[event.second].second;
for (int y = sizeR(i) - event.first; y <= sizeR(i); y += y & -y) bit[y] += t;
} else {
T sum = T();
for (int y = sizeR(i) - event.first; y > 0; y &= y - 1) sum += bit[y];
ans[~event.second] += sum;
}
}
}
return ans;
}
};
////////////////////////////////////////////////////////////////////////////////
template <class X, class Y, class T> struct StaticPointAddRectSum {
struct Rect {
X x0, x1;
Y y0, y1;
};
vector<pair<X, Y>> as;
vector<Rect> bs;
vector<T> vals, anss;
// Adds val to (x, y).
void add(X x, Y y, const T &val) {
as.emplace_back(x, y);
vals.push_back(val);
}
// Gets sum of [x0, x1) * [y0, y1).
// ~~> Gets (x*, y*)
void get(X x0, X x1, Y y0, Y y1) {
assert(x0 <= x1); assert(y0 <= y1);
bs.push_back(Rect{x0, x1, y0, y1});
}
void run() {
const int asLen = as.size(), bsLen = bs.size();
// same x ==> get then add
vector<pair<X, int>> events((bsLen << 1) + asLen);
for (int i = 0; i < asLen; ++i) {
events[(bsLen << 1) + i] = std::make_pair(as[i].first, (bsLen << 1) + i);
}
for (int j = 0; j < bsLen; ++j) {
events[j << 1 ] = std::make_pair(bs[j].x0, j << 1 );
events[j << 1 | 1] = std::make_pair(bs[j].x1, j << 1 | 1);
}
std::sort(events.begin(), events.end());
vector<Y> ys(asLen);
for (int i = 0; i < asLen; ++i) {
ys[i] = as[i].second;
}
std::sort(ys.begin(), ys.end());
ys.erase(std::unique(ys.begin(), ys.end()), ys.end());
const int ysLen = ys.size();
vector<T> bit(ysLen, 0);
anss.assign(bsLen, 0);
for (const auto &event : events) {
if (event.second >= bsLen << 1) {
const int i = event.second - (bsLen << 1);
for (int l = std::lower_bound(ys.begin(), ys.end(), as[i].second) - ys.begin(); l < ysLen; l |= l + 1) {
bit[l] += vals[i];
}
} else {
const int j = event.second >> 1;
T sum = 0;
for (int l = std::lower_bound(ys.begin(), ys.end(), bs[j].y0) - ys.begin(); l > 0; l &= l - 1) {
sum -= bit[l - 1];
}
for (int l = std::lower_bound(ys.begin(), ys.end(), bs[j].y1) - ys.begin(); l > 0; l &= l - 1) {
sum += bit[l - 1];
}
(event.second & 1) ? (anss[j] += sum) : (anss[j] -= sum);
}
}
}
};
////////////////////////////////////////////////////////////////////////////////
// add val to a: O(sqrt(n))
// get sum of [a, b): O(1)
template <class T> struct PointAddRangeSum {
int n, l, e;
vector<T> giant, baby;
PointAddRangeSum() {}
PointAddRangeSum(int n_) : n(n_) {
for (e = 0; 1 << (2 * e) < n; ++e) {}
l = n >> e;
giant.assign(l + 1, T());
baby.assign(n + 1, T());
}
PointAddRangeSum(const vector<T> &ts) {
n = ts.size();
for (e = 0; 1 << (2 * e) < n; ++e) {}
l = n >> e;
giant.assign(l + 1, T());
baby.assign(n + 1, T());
T sumsum = T();
for (int x = 0; x <= l; ++x) {
giant[x] = sumsum;
const int limY = min((x + 1) << e, n + 1);
T sum = T();
for (int y = x << e; y < limY; ++y) {
baby[y] = sum;
if (y < n) sum += ts[y];
}
sumsum += sum;
}
}
void add(int a, T val) {
assert(0 <= a); assert(a < n);
for (int x = (a >> e) + 1; x <= l; ++x) giant[x] += val;
const int limY = min(((a >> e) + 1) << e, n + 1);
for (int y = a + 1; y < limY; ++y) baby[y] += val;
}
inline T get(int a) const {
return giant[a >> e] + baby[a];
}
inline T get(int a, int b) const {
return get(b) - get(a);
}
};
////////////////////////////////////////////////////////////////////////////////
using U = unsigned long long;
int blockSize;
struct Mo {
int u, v;
int q;
int sig;
};
bool operator<(const Mo &mo0, const Mo &mo1) {
const int x0 = mo0.u / blockSize;
const int x1 = mo1.u / blockSize;
return (x0 != x1) ? (x0 < x1) : (x0 & 1) ? (mo0.v > mo1.v) : (mo0.v < mo1.v);
}
struct Offline {
int s, t;
int k;
};
int N, Q;
char S[100'010];
vector<U> WL, WR;
vector<int> L0, R0, L1, R1;
int main() {
for (; ~scanf("%d%d", &N, &Q); ) {
scanf("%s", S);
WL.assign(N + 1, 0); for (int l = 0; l < N; ++l) scanf("%llu", &WL[l]);
WR.assign(N + 1, 0); for (int r = 1; r <= N; ++r) scanf("%llu", &WR[r]);
L0.resize(Q);
R0.resize(Q);
L1.resize(Q);
R1.resize(Q);
for (int q = 0; q < Q; ++q) {
scanf("%d%d%d%d", &L0[q], &R0[q], &L1[q], &R1[q]);
--L0[q];
--L1[q];
}
const Substring sub(S);
#ifdef LOCAL
cerr<<sub<<flush;
#endif
vector<int> freqNode(sub.st.m, 0);
vector<int> fin(sub.st.m, 0), finRev(sub.stRev.m, 0);
vector<U> VL(sub.st.m, 0), VR(sub.stRev.m, 0);
for (int l = 0; l <= N; ++l) ++freqNode[sub.st.perm[l]];
for (int u = 0; u < sub.st.m; ++u) fin[u] = u + 1;
for (int v = 0; v < sub.stRev.m; ++v) finRev[v] = v + 1;
for (int l = 0; l <= N; ++l) VL[sub.st.perm[l]] += WL[l];
for (int r = 0; r <= N; ++r) VR[sub.stRev.perm[N - r]] += WR[r];
for (int u = sub.st.m; --u >= 1; ) {
const int p = sub.st[u].par;
freqNode[p] += freqNode[u];
chmax(fin[p], fin[u]);
VL[p] += VL[u];
}
for (int v = sub.stRev.m; --v >= 1; ) {
const int p = sub.stRev[v].par;
chmax(finRev[p], finRev[v]);
VR[p] += VR[v];
}
vector<int> freq(sub.size, 0);
for (int i = 0; i < sub.size; ++i) freq[i] = freqNode[sub.id(i)];
#ifdef LOCAL
cerr<<"freq = "<<freq<<endl;
cerr<<"VL = "<<VL<<endl;
cerr<<"VR = "<<VR<<endl;
#endif
vector<U> ans(Q, 0);
vector<U> bad0Coef(Q), bad1Coef(Q);
StaticPointAddRectSum<int, int, U> bad0, bad1;
for (int j = 0; j < sub.stRev.m; ++j) bad0.add(j, sub.usRev[j], VR[sub.usRev[j]]);
for (int j = 0; j < sub.st.m; ++j) bad1.add(j, sub.us[j], VL[sub.us[j]]);
blockSize = max<int>(N / sqrt(max(Q, 1)), 1);
vector<Mo> mos(Q * 4);
for (int q = 0; q < Q; ++q) {
/*
subtree(loc0.u) in st
subtree(loc1.v) in stRev
bad:
- loc0.u, but len < R0[q] - L0[q]
- loc1.v, but len < R1[q] - L1[q]
*/
const auto loc0 = sub.locate(L0[q], R0[q]);
const auto loc1 = sub.locate(L1[q], R1[q]);
// bad {}
{
/*
U brt = 0;
for (int u = loc0.u; u < fin[loc0.u]; ++u) for (int v = loc1.v; v < finRev[loc1.v]; ++v) {
const int i = sub.is[u];
if (i == sub.isRev[v]) {
const int x = sub.st[u].l() - sub.ls[i];
const int y = sub.rs[i] - sub.stRev[v].r();
if (y < sub.sizeR(i, x)) {
// cerr<<__LINE__<<" q = "<<q<<", i = "<<i<<", u = "<<u<<", v = "<<v<<"; "<<freq[i]<<" * "<<VL[u]<<" * "<<VR[v]<<endl;
brt += freq[i] * VL[u] * VR[v];
}
}
}
ans[q] += brt;
*/
mos[q * 4 + 0] = Mo{ loc0.u , loc1.v , q, +1};
mos[q * 4 + 1] = Mo{ loc0.u , finRev[loc1.v], q, -1};
mos[q * 4 + 2] = Mo{fin[loc0.u], loc1.v , q, -1};
mos[q * 4 + 3] = Mo{fin[loc0.u], finRev[loc1.v], q, +1};
}
// bad {0}
{
const int i = loc0.i, x = loc0.x, u = loc0.u;
/*
U brt = 0;
for (int y = loc0.y + 1; y < sub.sizeR(i, x); ++y) {
const int v = sub.idRev(i, y);
if (loc1.v <= v && v < finRev[loc1.v]) {
cerr<<__LINE__<<" q = "<<q<<", i = "<<i<<", u = "<<u<<", v = "<<v<<"; "<<freq[i]<<" * "<<VL[u]<<" * "<<VR[v]<<endl;
brt += freq[i] * VL[u] * VR[v];
}
}
// ans[q] -= brt;
//*/
bad0Coef[q] = freq[i] * VL[u];
bad0.get(sub.jsRev[i] + loc0.y + 1, sub.jsRev[i] + sub.sizeR(i, x), loc1.v, finRev[loc1.v]);
}
// bad {1}
{
const int i = loc1.i, y = loc1.y, v = loc1.v;
/*
U brt = 0;
for (int x = loc1.x + 1; x < sub.sizeL(i, y); ++x) {
const int u = sub.id(i, x);
if (loc0.u <= u && u < fin[loc0.u]) {
cerr<<__LINE__<<" q = "<<q<<", i = "<<i<<", u = "<<u<<", v = "<<v<<"; "<<freq[i]<<" * "<<VL[u]<<" * "<<VR[v]<<endl;
brt += freq[i] * VL[u] * VR[v];
}
}
// ans[q] -= brt;
//*/
bad1Coef[q] = freq[i] * VR[v];
bad1.get(sub.js[i] + loc1.x + 1, sub.js[i] + sub.sizeL(i, y), loc0.u, fin[loc0.u]);
}
// bad {0, 1}
{
U brt = 0;
const int i = loc0.i;
if (i == loc1.i) {
const int u = loc0.u, x = loc0.x;
const int v = loc1.v, y = loc1.y;
if (y < sub.sizeR(i, x) && loc1.x < x && loc0.y < y) {
// cerr<<__LINE__<<" q = "<<q<<", i = "<<i<<", u = "<<u<<", v = "<<v<<"; "<<freq[i]<<" * "<<VL[u]<<" * "<<VR[v]<<endl;
brt += freq[i] * VL[u] * VR[v];
}
}
ans[q] += brt;
}
}
bad0.run();
bad1.run();
// cerr<<"bad0.anss = "<<bad0.anss<<endl;
// cerr<<"bad1.anss = "<<bad1.anss<<endl;
for (int q = 0; q < Q; ++q) {
ans[q] -= bad0Coef[q] * bad0.anss[q];
ans[q] -= bad1Coef[q] * bad1.anss[q];
}
sort(mos.begin(), mos.end());
vector<vector<Offline>> byU(sub.st.m + 1), byV(sub.stRev.m + 1);
for (int u = 1, v = 1, k = 0; k < Q * 4; ++k) {
byV[v].push_back(Offline{u, mos[k].u, k});
byU[u].push_back(Offline{v, mos[k].v, k});
u = mos[k].u;
v = mos[k].v;
}
vector<U> sums(Q * 4, 0);
{
PointAddRangeSum<U> f(sub.st.m);
auto get = [&](int v) -> U {
const int i = sub.isRev[v];
const int y = sub.rs[i] - sub.stRev[v].r();
return VR[v] * f.get(sub.js[i], sub.js[i] + sub.sizeL(i, y));
};
for (int u = 1; u <= sub.st.m; ++u) {
for (const auto &o : byU[u]) {
for (int v = o.s; v < o.t; ++v) sums[o.k] += get(v);
for (int v = o.s; --v >= o.t; ) sums[o.k] -= get(v);
}
if (u < sub.st.m) {
const int i = sub.is[u];
const int x = sub.st[u].l() - sub.ls[i];
f.add(sub.js[i] + x, freq[i] * VL[u]);
}
}
}
{
PointAddRangeSum<U> f(sub.stRev.m);
auto get = [&](int u) -> U {
const int i = sub.is[u];
const int x = sub.st[u].l() - sub.ls[i];
return VL[u] * f.get(sub.jsRev[i], sub.jsRev[i] + sub.sizeR(i, x));
};
for (int v = 1; v <= sub.stRev.m; ++v) {
for (const auto &o : byV[v]) {
for (int u = o.s; u < o.t; ++u) sums[o.k] += get(u);
for (int u = o.s; --u >= o.t; ) sums[o.k] -= get(u);
}
if (v < sub.stRev.m) {
const int i = sub.isRev[v];
const int y = sub.rs[i] - sub.stRev[v].r();
f.add(sub.jsRev[i] + y, freq[i] * VR[v]);
}
}
}
for (int k = 1; k < Q * 4; ++k) sums[k] += sums[k - 1];
for (int k = 0; k < Q * 4; ++k) ans[mos[k].q] += mos[k].sig * sums[k];
for (int q = 0; q < Q; ++q) {
printf("%llu\n", ans[q]);
}
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Subtask #1:
score: 0
Wrong Answer
Test #1:
score: 0
Wrong Answer
time: 0ms
memory: 4484kb
input:
500 500 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaabaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa...
output:
3180651086039962165 10767836730172512565 6527989020105025200 17437471869283549704 15403001870930361680 14959249889225951349 14923244255636218707 6445174895984787043 8773655205846916524 9337841098886235174 18340898993708903492 10836138585075870926 4151967209804417471 13142314545999179754 166254881074...
result:
wrong answer 1st lines differ - expected: '15720454042420499810', found: '3180651086039962165'
Subtask #2:
score: 0
Skipped
Dependency #1:
0%
Subtask #3:
score: 0
Skipped
Dependency #2:
0%
Subtask #4:
score: 6
Accepted
Test #22:
score: 6
Accepted
time: 341ms
memory: 74688kb
input:
100000 100000 zbbabaabbaababbabaababbaabbabaabbaababbaabbabaababbabaabbaababbabaababbaabbabaababbabaabbaababbaabbabaabbaababbabaababbaabbabaabbaababbaabbabaababbabaabbaababbaabbabaabbaababbabaababbaabbabaababbabaabbaababbabaababbaabbabaabbaababbaabbabaababbabaabbaababbabaababbaabbabaababbabaabbaabab...
output:
16102224067619618967 2409962914769200003 427496158535942638 17668679206267169316 9612725428377010375 16283030984784184667 14966758574838045581 8108029333542434517 5821899279772898061 7354415533246368927 15016230232022193055 9072126619623269970 5490256818353051548 432088324301719512 13681741566473101...
result:
ok 100000 lines
Test #23:
score: 0
Accepted
time: 326ms
memory: 70004kb
input:
100000 100000 zsyysysyysyysysyysysyysyysysyysyysysyysysyysyysysyysysyysyysysyysyysysyysysyysyysysyysyysysyysysyysyysysyysysyysyysysyysyysysyysysyysyysysyysysyysyysysyysyysysyysysyysyysysyysyysysyysysyysyysysyysysyysyysysyysyysysyysysyysyysysyysyysysyysysyysyysysyysysyysyysysyysyysysyysysyysyysysyysy...
output:
4559870725517531243 7294838067589907718 11611591353940805930 6570123277626119382 7422995984298182834 5907659314847245750 16910510485299347733 4264602948914788684 13190214217087942183 6600183534290302450 18342681242733610030 11565497126186922166 17128453730139037795 1670830382187028213 18164994643596...
result:
ok 100000 lines
Test #24:
score: 0
Accepted
time: 354ms
memory: 73680kb
input:
100000 100000 zoooooooollexlwockjmmpcsmrmxbcsxiopbhrsgmuffubpextcneqsmtouhuovwmosufyvtciwaiqfgxdjgebcnwbeyyyascjixpskyeyoecigpydkqrssvcwcuirkwyxxbcfgjdorrrgdghdooooooooofnkxriqwewxjgitnhfrykdhcrpbgmcnqujvlugcougvywjyjknbcfqdohyxidpswedsqodaqavibkmrykeiqfmoyavdcctpjvqomwmhjysbynqskjvprebydvglxmnqsvxy...
output:
812998614822431625 1250302312590066903 0 17068288240276554944 8822011249064016718 5154878686056167322 16634251694703169315 7627132526351165031 17489820411768677459 1612901206518396247 9557606214238964493 8125053178366415794 6923591044772654970 16010694286126551160 0 11810757301219826743 180907391938...
result:
ok 100000 lines
Subtask #5:
score: 0
Wrong Answer
Dependency #4:
100%
Accepted
Test #25:
score: 16
Accepted
time: 216ms
memory: 83016kb
input:
100000 100000 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa...
output:
15893638524428831028 10131593916133042820 10131593916133042820 1813611689029665142 15893638524428831028 10131593916133042820 1813611689029665142 10131593916133042820 9834492063345021236 9834492063345021236 15893638524428831028 9834492063345021236 9834492063345021236 10131593916133042820 158936385244...
result:
ok 100000 lines
Test #26:
score: -16
Wrong Answer
time: 685ms
memory: 62368kb
input:
100000 100000 zszzszszzszzszszzszszzszzszszzszzszszzszszzszzszszzszszzszzszszzszzszszzszszzszzszszzszzszszzszszzszzszszzszszzszzszszzszzszszzszszzszzszszzszszzszzszszzszzszszzszszzszzszszzszzszszzszszzszzszszzszszzszzszszzszzszszzszszzszzszszzszzszszzszszzszzszszzszszzszzszszzszzszszzszszzszzszszzsz...
output:
14439422629921813482 7264444986505301195 8399425172162457504 17048555139745491862 16735123042057153971 12928309510252812093 16135471956121714472 17826672489979253119 16258051235616677222 8446479705050496657 1152657636945638057 1971470682186795478 8333561403870758533 863416755415237407 70175023211500...
result:
wrong answer 11th lines differ - expected: '7223602662161126632', found: '1152657636945638057'
Subtask #6:
score: 0
Skipped
Dependency #2:
0%
Subtask #7:
score: 0
Skipped
Dependency #3:
0%