結果
問題 |
No.3239 Omnibus
|
ユーザー |
![]() |
提出日時 | 2025-08-15 22:10:24 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 3,244 ms / 10,000 ms |
コード長 | 17,583 bytes |
コンパイル時間 | 3,032 ms |
コンパイル使用メモリ | 261,248 KB |
実行使用メモリ | 75,036 KB |
最終ジャッジ日時 | 2025-08-15 22:12:02 |
合計ジャッジ時間 | 74,429 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge1 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 1 |
other | AC * 33 |
ソースコード
#line 1 "library/Template/template.hpp" #include <bits/stdc++.h> using namespace std; #define rep(i, a, b) for (int i = (int)(a); i < (int)(b); i++) #define rrep(i, a, b) for (int i = (int)(b)-1; i >= (int)(a); i--) #define ALL(v) (v).begin(), (v).end() #define UNIQUE(v) sort(ALL(v)), (v).erase(unique(ALL(v)), (v).end()) #define SZ(v) (int)v.size() #define MIN(v) *min_element(ALL(v)) #define MAX(v) *max_element(ALL(v)) #define LB(v, x) int(lower_bound(ALL(v), (x)) - (v).begin()) #define UB(v, x) int(upper_bound(ALL(v), (x)) - (v).begin()) using uint = unsigned int; using ll = long long int; using ull = unsigned long long; using i128 = __int128_t; using u128 = __uint128_t; const int inf = 0x3fffffff; const ll INF = 0x1fffffffffffffff; template <typename T, typename S = T> S SUM(const vector<T> &a) { return accumulate(ALL(a), S(0)); } template <typename S, typename T = S> S POW(S a, T b) { S ret = 1, base = a; for (;;) { if (b & 1) ret *= base; b >>= 1; if (b == 0) break; base *= base; } return ret; } template <typename T> inline bool chmax(T &a, T b) { if (a < b) { a = b; return 1; } return 0; } template <typename T> inline bool chmin(T &a, T b) { if (a > b) { a = b; return 1; } return 0; } template <typename T, typename U> T ceil(T x, U y) { assert(y != 0); if (y < 0) x = -x, y = -y; return (x > 0 ? (x + y - 1) / y : x / y); } template <typename T, typename U> T floor(T x, U y) { assert(y != 0); if (y < 0) x = -x, y = -y; return (x > 0 ? x / y : (x - y + 1) / y); } template <typename T> int popcnt(T x) { return __builtin_popcountll(x); } template <typename T> int topbit(T x) { return (x == 0 ? -1 : 63 - __builtin_clzll(x)); } template <typename T> int lowbit(T x) { return (x == 0 ? -1 : __builtin_ctzll(x)); } template <class T, class U> ostream &operator<<(ostream &os, const pair<T, U> &p) { os << "P(" << p.first << ", " << p.second << ")"; return os; } template <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) { os << "{"; for (int i = 0; i < vec.size(); i++) { os << vec[i] << (i + 1 == vec.size() ? "" : ", "); } os << "}"; return os; } template <typename T, typename U> ostream &operator<<(ostream &os, const map<T, U> &map_var) { os << "{"; for (auto itr = map_var.begin(); itr != map_var.end(); itr++) { os << "(" << itr->first << ", " << itr->second << ")"; itr++; if (itr != map_var.end()) os << ", "; itr--; } os << "}"; return os; } template <typename T> ostream &operator<<(ostream &os, const set<T> &set_var) { os << "{"; for (auto itr = set_var.begin(); itr != set_var.end(); itr++) { os << *itr; ++itr; if (itr != set_var.end()) os << ", "; itr--; } os << "}"; return os; } #ifdef LOCAL #define debug 1 #define show(...) _show(0, #__VA_ARGS__, __VA_ARGS__) #else #define debug 0 #define show(...) true #endif template <typename T> void _show(int i, T name) { cerr << '\n'; } template <typename T1, typename T2, typename... T3> void _show(int i, const T1 &a, const T2 &b, const T3 &...c) { for (; a[i] != ',' && a[i] != '\0'; i++) cerr << a[i]; cerr << ":" << b << " "; _show(i + 1, a, c...); } #line 2 "library/Utility/fastio.hpp" #include <unistd.h> namespace fastio { static constexpr uint32_t SZ = 1 << 17; char ibuf[SZ]; char obuf[SZ]; char out[100]; // pointer of ibuf, obuf uint32_t pil = 0, pir = 0, por = 0; struct Pre { char num[10000][4]; constexpr Pre() : num() { for (int i = 0; i < 10000; i++) { int n = i; for (int j = 3; j >= 0; j--) { num[i][j] = n % 10 | '0'; n /= 10; } } } } constexpr pre; inline void load() { memmove(ibuf, ibuf + pil, pir - pil); pir = pir - pil + fread(ibuf + pir - pil, 1, SZ - pir + pil, stdin); pil = 0; if (pir < SZ) ibuf[pir++] = '\n'; } inline void flush() { fwrite(obuf, 1, por, stdout); por = 0; } void rd(char &c) { do { if (pil + 1 > pir) load(); c = ibuf[pil++]; } while (isspace(c)); } void rd(string &x) { x.clear(); char c; do { if (pil + 1 > pir) load(); c = ibuf[pil++]; } while (isspace(c)); do { x += c; if (pil == pir) load(); c = ibuf[pil++]; } while (!isspace(c)); } template <typename T> void rd_real(T &x) { string s; rd(s); x = stod(s); } template <typename T> void rd_integer(T &x) { if (pil + 100 > pir) load(); char c; do c = ibuf[pil++]; while (c < '-'); bool minus = 0; if constexpr (is_signed<T>::value || is_same_v<T, i128>) { if (c == '-') { minus = 1, c = ibuf[pil++]; } } x = 0; while ('0' <= c) { x = x * 10 + (c & 15), c = ibuf[pil++]; } if constexpr (is_signed<T>::value || is_same_v<T, i128>) { if (minus) x = -x; } } void rd(int &x) { rd_integer(x); } void rd(ll &x) { rd_integer(x); } void rd(i128 &x) { rd_integer(x); } void rd(uint &x) { rd_integer(x); } void rd(ull &x) { rd_integer(x); } void rd(u128 &x) { rd_integer(x); } void rd(double &x) { rd_real(x); } void rd(long double &x) { rd_real(x); } template <class T, class U> void rd(pair<T, U> &p) { return rd(p.first), rd(p.second); } template <size_t N = 0, typename T> void rd_tuple(T &t) { if constexpr (N < std::tuple_size<T>::value) { auto &x = std::get<N>(t); rd(x); rd_tuple<N + 1>(t); } } template <class... T> void rd(tuple<T...> &tpl) { rd_tuple(tpl); } template <size_t N = 0, typename T> void rd(array<T, N> &x) { for (auto &d : x) rd(d); } template <class T> void rd(vector<T> &x) { for (auto &d : x) rd(d); } void read() {} template <class H, class... T> void read(H &h, T &...t) { rd(h), read(t...); } void wt(const char c) { if (por == SZ) flush(); obuf[por++] = c; } void wt(const string s) { for (char c : s) wt(c); } void wt(const char *s) { size_t len = strlen(s); for (size_t i = 0; i < len; i++) wt(s[i]); } template <typename T> void wt_integer(T x) { if (por > SZ - 100) flush(); if (x < 0) { obuf[por++] = '-', x = -x; } int outi; for (outi = 96; x >= 10000; outi -= 4) { memcpy(out + outi, pre.num[x % 10000], 4); x /= 10000; } if (x >= 1000) { memcpy(obuf + por, pre.num[x], 4); por += 4; } else if (x >= 100) { memcpy(obuf + por, pre.num[x] + 1, 3); por += 3; } else if (x >= 10) { int q = (x * 103) >> 10; obuf[por] = q | '0'; obuf[por + 1] = (x - q * 10) | '0'; por += 2; } else obuf[por++] = x | '0'; memcpy(obuf + por, out + outi + 4, 96 - outi); por += 96 - outi; } template <typename T> void wt_real(T x) { ostringstream oss; oss << fixed << setprecision(15) << double(x); string s = oss.str(); wt(s); } void wt(int x) { wt_integer(x); } void wt(ll x) { wt_integer(x); } void wt(i128 x) { wt_integer(x); } void wt(uint x) { wt_integer(x); } void wt(ull x) { wt_integer(x); } void wt(u128 x) { wt_integer(x); } void wt(double x) { wt_real(x); } void wt(long double x) { wt_real(x); } template <class T, class U> void wt(const pair<T, U> val) { wt(val.first); wt(' '); wt(val.second); } template <size_t N = 0, typename T> void wt_tuple(const T t) { if constexpr (N < std::tuple_size<T>::value) { if constexpr (N > 0) { wt(' '); } const auto x = std::get<N>(t); wt(x); wt_tuple<N + 1>(t); } } template <class... T> void wt(tuple<T...> tpl) { wt_tuple(tpl); } template <class T, size_t S> void wt(const array<T, S> val) { auto n = val.size(); for (size_t i = 0; i < n; i++) { if (i) wt(' '); wt(val[i]); } } template <class T> void wt(const vector<T> val) { auto n = val.size(); for (size_t i = 0; i < n; i++) { if (i) wt(' '); wt(val[i]); } } void print() { wt('\n'); } template <class Head, class... Tail> void print(Head &&head, Tail &&...tail) { wt(head); if (sizeof...(Tail)) wt(' '); print(forward<Tail>(tail)...); } void __attribute__((destructor)) _d() { flush(); } } // namespace fastio using fastio::flush; using fastio::print; using fastio::read; inline void first(bool i = true) { print(i ? "first" : "second"); } inline void Alice(bool i = true) { print(i ? "Alice" : "Bob"); } inline void Takahashi(bool i = true) { print(i ? "Takahashi" : "Aoki"); } inline void yes(bool i = true) { print(i ? "yes" : "no"); } inline void Yes(bool i = true) { print(i ? "Yes" : "No"); } inline void No() { print("No"); } inline void YES(bool i = true) { print(i ? "YES" : "NO"); } inline void NO() { print("NO"); } inline void Yay(bool i = true) { print(i ? "Yay!" : ":("); } inline void Possible(bool i = true) { print(i ? "Possible" : "Impossible"); } inline void POSSIBLE(bool i = true) { print(i ? "POSSIBLE" : "IMPOSSIBLE"); } /** * @brief Fast IO */ #line 2 "library/Utility/random.hpp" namespace Random { mt19937_64 randgen(chrono::steady_clock::now().time_since_epoch().count()); using u64 = unsigned long long; u64 get() { return randgen(); } template <typename T> T get(T L) { // [0,L] return get() % (L + 1); } template <typename T> T get(T L, T R) { // [L,R] return get(R - L) + L; } double uniform() { return double(get(1000000000)) / 1000000000; } string str(int n) { string ret; rep(i, 0, n) ret += get('a', 'z'); return ret; } template <typename Iter> void shuffle(Iter first, Iter last) { if (first == last) return; int len = 1; for (auto it = first + 1; it != last; it++) { len++; int j = get(0, len - 1); if (j != len - 1) iter_swap(it, first + j); } } template <typename T> vector<T> select(int n, T L, T R) { // [L,R] if (n * 2 >= R - L + 1) { vector<T> ret(R - L + 1); iota(ALL(ret), L); shuffle(ALL(ret)); ret.resize(n); return ret; } else { unordered_set<T> used; vector<T> ret; while (SZ(used) < n) { T x = get(L, R); if (!used.count(x)) { used.insert(x); ret.push_back(x); } } return ret; } } void relabel(int n, vector<pair<int, int>> &es) { shuffle(ALL(es)); vector<int> ord(n); iota(ALL(ord), 0); shuffle(ALL(ord)); for (auto &[u, v] : es) u = ord[u], v = ord[v]; } template <bool directed, bool multi, bool self> vector<pair<int, int>> genGraph(int n, int m) { vector<pair<int, int>> cand, es; rep(u, 0, n) rep(v, 0, n) { if (!self and u == v) continue; if (!directed and u > v) continue; cand.push_back({u, v}); } if (m == -1) m = get(SZ(cand)); // chmin(m, SZ(cand)); vector<int> ord; if (multi) rep(_, 0, m) ord.push_back(get(SZ(cand) - 1)); else { ord = select(m, 0, SZ(cand) - 1); } for (auto &i : ord) es.push_back(cand[i]); relabel(n, es); return es; } vector<pair<int, int>> genTree(int n) { vector<pair<int, int>> es; rep(i, 1, n) es.push_back({get(i - 1), i}); relabel(n, es); return es; } }; // namespace Random /** * @brief Random */ #line 4 "sol.cpp" #line 2 "library/DataStructure/bit.hpp" template <typename T> struct BIT { int n; T all = 0; vector<T> val; BIT(int _n = 0) : n(_n), val(_n + 10) {} void clear() { val.assign(n + 10, 0); all = T(); } void add(int i, T x) { for (i++; i <= n; i += (i & -i)) val[i] = val[i] + x; all += x; } T sum(int i) { i = clamp(i, 0, n); T res = 0; for (; i; i -= (i & -i)) res += val[i]; return res; } // [L,R) T sum(int L, int R) { if (L > R) return T(0); return sum(R) - sum(L); } int lower_bound(T x) { int ret = 0, len = 1; while (2 * len <= n) len <<= 1; for (; len >= 1; len >>= 1) { if (ret + len <= n and val[ret + len] < x) { ret += len; x -= val[ret]; } } return ret; } }; /** * @brief Binary Indexed Tree */ #line 3 "library/DataStructure/staticrectsum.hpp" template <typename XY, typename T> struct StaticRectangleSum { struct P { XY x, y; T w; }; struct Rect { XY l, d, r, u; }; struct Q { XY x, d, u; int id, inv; }; vector<P> plus; vector<Rect> que; StaticRectangleSum() {} void add(XY x, XY y, T w) { plus.push_back({x, y, w}); } void query(XY l, XY d, XY r, XY u) { que.push_back({l, d, r, u}); } vector<T> run() { ll n = plus.size(), q = que.size(); sort(ALL(plus), [](P &p, P &q) { return p.y < q.y; }); vector<XY> ys; rep(i, 0, n) ys.push_back(plus[i].y); ys.erase(unique(ALL(ys)), ys.end()); rep(i, 0, n) plus[i].y = lower_bound(ALL(ys), plus[i].y) - ys.begin(); vector<Q> qs; rep(i, 0, q) { auto &[l, d, r, u] = que[i]; d = lower_bound(ALL(ys), d) - ys.begin(); u = lower_bound(ALL(ys), u) - ys.begin(); qs.push_back({l, d, u, i, -1}); qs.push_back({r, d, u, i, 1}); } sort(ALL(plus), [](P &p, P &q) { return p.x < q.x; }); sort(ALL(qs), [](Q &p, Q &q) { return p.x < q.x; }); vector<T> res(q); int k = 0; BIT<T> bit(ys.size()); for (auto &q : qs) { while (k < n and plus[k].x < q.x) { bit.add(plus[k].y, plus[k].w); k++; } res[q.id] += bit.sum(q.d, q.u) * q.inv; } return res; } }; /** * @brief Static Rectangle Sum */ #line 3 "library/DataStructure/dynamicrectsum.hpp" template <typename XY, typename T> struct DynamicPointAddRectangleSum { using Point = tuple<XY, XY, T>; using Query = tuple<XY, XY, XY, XY>; vector<variant<Point, Query>> que; DynamicPointAddRectangleSum() {} void add(XY x, XY y, T w) { que.push_back(Point{x, y, w}); } void query(XY L, XY D, XY R, XY U) { que.push_back(Query{L, D, R, U}); } vector<T> run() { int Q = SZ(que); vector<int> isq(Q); vector<int> pos(Q); int ptr = 0; rep(i, 0, Q) if (holds_alternative<Query>(que[i])) { isq[i] = 1; pos[i] = ptr++; } vector<T> ret(ptr); auto rec = [&](auto &rec, int L, int R) -> void { if (L + 1 >= R) return; int mid = (L + R) >> 1; rec(rec, L, mid); rec(rec, mid, R); StaticRectangleSum<XY, T> buf; rep(k, L, mid) if (!isq[k]) { XY x, y; T w; tie(x, y, w) = get<Point>(que[k]); buf.add(x, y, w); } rep(k, mid, R) if (isq[k]) { XY L, D, R, U; tie(L, D, R, U) = get<Query>(que[k]); buf.query(L, D, R, U); } auto sub = buf.run(); ptr = 0; rep(k, mid, R) if (isq[k]) { ret[pos[k]] += sub[ptr++]; } }; rec(rec, 0, Q); return ret; } }; /** * @brief Dynamic Point Add Rectangle Sum */ #line 6 "sol.cpp" int main() { int n, Q; string s; read(n, Q, s); vector<int> pos(n - 2, -1); DynamicPointAddRectangleSum<int, ll> buf1, buf2; auto upd = [&](int i) -> void { string t = s.substr(i, 3); int key = 0; rep(j, 0, 3) { key *= 26; key += t[j] - 'a'; } if (pos[i] != -1) { buf1.add(i, pos[i], -1); buf2.add(i, pos[i], -(i + 1)); } pos[i] = key; buf1.add(i, pos[i], 1); show(i, pos[i], i + 1); buf2.add(i, pos[i], i + 1); }; rep(i, 0, n - 2) { upd(i); } vector<ll> Ls; rep(i, 0, Q) { int ty; read(ty); if (ty == 1) { int k; char c; read(k, c); k--; s[k] = c; rep(j, max(0, k - 2), k + 1) upd(j); } else { int L, R; string a; read(L, R, a); L--; int key = 0; rep(j, 0, 3) { key *= 26; key += a[j] - 'a'; } buf1.query(L, key, R - 2, key + 1); show(L, R - 2, key); buf2.query(L, key, R - 2, key + 1); Ls.push_back(L); } } auto cnt = buf1.run(); auto sum = buf2.run(); show(cnt, sum); rep(i, 0, SZ(Ls)) { ll ret = sum[i] - cnt[i] * Ls[i]; print(ret); } return 0; }