#include "bits/stdc++.h" #include #include using namespace __gnu_pbds; using namespace std; // clang-format off #ifdef LOCAL #include "debug.h" #else #define debug(...) #endif using ll = long long; using str = string; using AR2 = array; template using oset = tree, rb_tree_tag, tree_order_statistics_node_update>; template using vec = vector; template using vvec = vec>; template using vvvec = vec>; template using vac = vec>; template using vvac = vec>; // template using priority_queue_min = priority_queue, greater>; #define sz(x) int((x).size()) #define all(x) begin(x), end(x) #define rall(x) x.rbegin(), x.rend() #define sor(x) sort(all(x)) #define pb push_back #define F_OR(i, a, b, s) for (int i=(a); (s)>0?i<(int)(b):i>(int)(b); i+=(s)) #define F_OR1(e) F_OR(i, 0, e, 1) #define F_OR2(i, e) F_OR(i, 0, e, 1) #define F_OR3(i, b, e) F_OR(i, b, e, 1) #define F_OR4(i, b, e, s) F_OR(i, b, e, s) #define GET5(a, b, c, d, e, ...) e #define F_ORC(...) GET5(__VA_ARGS__, F_OR4, F_OR3, F_OR2, F_OR1) #define FOR(...) F_ORC(__VA_ARGS__)(__VA_ARGS__) #define E_ACH2(x, a) for (auto& x: a) #define E_ACH3(x, y, a) for (auto& [x, y]: a) #define E_ACH4(x, y, z, a) for (auto& [x, y, z]: a) #define E_ACHC(...) GET5(__VA_ARGS__, E_ACH4, E_ACH3, E_ACH2) #define EACH(...) E_ACHC(__VA_ARGS__)(__VA_ARGS__) #define SUBMASKS(t, s) for (ll t = (s); t >= 0; t = (t == 0 ? -1 : (t - 1) & (s))) #define MIN(v) *min_element(all(v)) #define MAX(v) *max_element(all(v)) constexpr int popcount(int x) { return __builtin_popcount(x); } constexpr int bitlength(int x) { return x == 0 ? 0 : 31 - __builtin_clz(x); } ll cdiv(ll a, ll b) { return a/b + ((a^b) > 0 && a % b); }; ll fdiv(ll a, ll b) { return a/b - ((a^b) < 0 && a % b); }; template T pop(vec &v) { T x = v.back(); v.pop_back(); return x; } template bool bounds(T a, T lo, T hi) { return lo <= a && a <= hi; } template T truemod(T x, T M) { return (x % M + M) % M; } template bool umin(T &a, const T &b) { return b < a ? a = b, 1 : 0; } template bool umax(T &a, const T &b) { return a < b ? a = b, 1 : 0; } template int lwb(vec &a, T &b) { return int(lower_bound(all(a), b) - begin(a)); } template int upb(vec &a, T &b) { return int(upper_bound(all(a), b) - begin(a)); } template void removeDupes(vec &v) { sort(all(v)); v.erase(unique(all(v)), end(v)); } template void eraseOne(T &t, U &u) { auto it = t.find(u); assert(it != end(t)); t.erase(it); } template T firstTrue(T lo, T hi, U f) { ++hi; assert(lo <= hi); while (lo < hi) { T mi = lo + (hi-lo) / 2; f(mi) ? hi = mi : lo = mi + 1; } return lo; } template T lastTrue(T lo, T hi, U f) { --lo; assert(lo <= hi); while (lo < hi) { T mi = lo + (hi-lo+1) / 2; f(mi) ? lo = mi : hi = mi - 1; } return lo; } template istream &operator>>(istream &s, array& v) { FOR(sz(v)) s >> v[i]; return s; } template istream &operator>>(istream &s, vec& v) { FOR(sz(v)) s >> v[i]; return s; } template ostream &operator<<(ostream &s, vec& v) { FOR(sz(v)) s << (i?" ":"") << v[i]; return s; } template ostream &operator<<(ostream &s, const vec& v) { FOR(sz(v)) s << (i?" ":"") << v[i]; return s; } template void write(A x) { cout << x; } template void write(const H& h, const T&... t) { write(h); write(t...); } void print() { write("\n"); } template void print(const H& h, const T&... t) { write(h); if (sizeof...(t)) write(" "); print(t...); } void decrement() {} template void decrement(vec> &v) { EACH(row, v) EACH(x, row) --x; } template void decrement(vec> &v) { EACH(row, v) EACH(x, row) --x; } template void decrement(vec &v) { EACH(x, v) --x; } template void decrement(T &t, U &...u) { --t; decrement(u...); } template void read(T& x) { cin >> x; } template void read(T &t, U &...u) { read(t); read(u...); } #define ints(...) int __VA_ARGS__; read(__VA_ARGS__); #define int1(...) ints(__VA_ARGS__); decrement(__VA_ARGS__); #define vint(n, a) int n; cin >> n; vec a(n); cin >> a; #define vin(n, a) vec a((n)); cin >> a; #define vvin(n, m, a) vec> a((n), vec((m))); cin >> a; #define vain(n, m, a) vec> a((n)); cin >> a; #define graphin(n, m, adj) vvec adj(n); FOR(m) {int1(u, v); adj[u].pb(v); adj[v].pb(u); } #define lgraphin(n, m, adj) vvac adj(n); FOR(i, m) {int1(u, v); adj[u].pb({v,i}); adj[v].pb({u,i}); } #define wgraphin(n, m, adj) vvac adj(n); FOR(m) {int1(u, v); ints(w); adj[u].pb({v,w}); adj[v].pb({u,w}); } #define dgraphin(n, m, adj) vvec adj(n); FOR(m) {int1(u, v); adj[u].pb(v);} #define dwgraphin(n, m, adj) vvac adj(n); FOR(m) {int1(u, v, w); adj[u].pb({v, w+1});} // clang-format on void solve() { ints(N); str S; cin >> S; vac segs; function count = [&](int l, int r) { return (ll)(r - l + 1) * (r - l) / 2; }; int i = 0; while (i < N) { int j = i + 1; while (j < N && S[j] >= S[j - 1]) j++; segs.push_back({i, j}); i = j; } vec P(1, 0); EACH(l, r, segs) { P.push_back(P.back() + count(l, r)); } ints(Q); FOR(qi, Q) { int1(L, R); array arleft = {L, N + 1}, arright = {R + 1, 0}; int i = upb(segs, arleft); int j = lwb(segs, arright); i = max(i - 1, 0); j = max(j - 1, i); ll ans = P[j + 1] - P[i]; ans -= count(segs[i][0], segs[i][1]); ans += count(L, min(segs[i][1], R + 1)); if (i != j) { ans -= count(segs[j][0], segs[j][1]); ans += count(max(L, segs[j][0]), R + 1); } print(ans); } } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); const int MAGIC = 500; ints(N, M); vain(N, 4, segs); vac A(M); FOR(i, M) { cin >> A[i][0]; A[i][1] = i; } sor(A); vec> events; FOR(i, N) { events.push_back({segs[i][0], i}); events.push_back({segs[i][1] + 1, -i - 1}); } sor(events); multiset> blender; vec mask(100001); vec ans(sz(A)); int i = 0, j = 0; FOR(t, 100001) { while (i < sz(A) && A[i][0] < t) i++; while (j < sz(events) && events[j][0] <= t) { int ix = events[j][1]; if (ix >= 0) { auto [l, r, x, y] = segs[ix]; if (x <= MAGIC) { blender.insert({x, y}); } else { FOR(y0, y, 100001, x) mask[y0]++; } } else { auto [l, r, x, y] = segs[-ix - 1]; if (x <= MAGIC) { array pair = {x, y}; eraseOne(blender, pair); } else { FOR(y0, y, 100001, x) mask[y0]--; } } j++; } int bns = -1; while (i < sz(A) && A[i][0] == t) { if (bns == -1) { bns = mask[t]; EACH(x, y, blender) if (t % x == y) bns += 1; } ans[A[i][1]] = bns; i++; } } EACH(x, ans) print(x); return 0; }