CPLibrary

This documentation is automatically generated by competitive-verifier/competitive-verifier

View the Project on GitHub o06660o/CPLibrary

:heavy_check_mark: test/ds/dynsgt.test.cpp

Depends on

Code

// competitive-verifier: PROBLEM https://judge.yosupo.jp/problem/point_set_range_composite
#include <bits/stdc++.h>
#define PUSHB push_back
using namespace std;
using ll = long long;
using ull = unsigned long long;

#include "../../src/ds/dynsgt.hpp"
#include "../../src/math/modint.hpp"
#include "../../src/misc/read.hpp"

struct Info_ {
  mint a = 1, b = 0;
  friend Info_ operator+(const Info_& lhs, const Info_& rhs) {
    return {lhs.a * rhs.a, lhs.b * rhs.a + rhs.b};
  }
};

int main() {
  cin.tie(nullptr)->sync_with_stdio(false);
  int n = read(), q = read();
  SegTree<Info_> sgt(n);
  for (int i = 0; i < n; i++) {
    int a = read(), b = read();
    sgt.set(i, {a, b});
  }
  while (q--) {
    int op = read();
    if (op == 0) {
      int pos = read(), c = read(), d = read();
      sgt.set(pos, {c, d});
    } else {
      int l = read(), r = read(), x = read();
      auto [A, B] = sgt.sum(l, r);
      cout << (A * x + B).data << "\n";
    }
  }
  return 0;
}
#line 1 "test/ds/dynsgt.test.cpp"
// competitive-verifier: PROBLEM https://judge.yosupo.jp/problem/point_set_range_composite
#include <bits/stdc++.h>
#define PUSHB push_back
using namespace std;
using ll = long long;
using ull = unsigned long long;

#line 1 "src/ds/dynsgt.hpp"
template <typename Info>
struct SegTree {
  struct Node {
    Info info = {};
    int ls = -1, rs = -1;
  };
  int n, _pos, _l, _r, root;
  Info _v;
  vector<Node> tree;
  SegTree(int n) : n(n) { root = alloc({}); }
  void set(int pos, Info v) { _pos = pos, _v = v, set(root, 0, n); }
  Info sum(int l, int r) { return _l = l, _r = r, sum(root, 0, n); }

 private:
  int alloc(Node node) { return tree.PUSHB(move(node)), int(tree.size()) - 1; }
  void set(int p, int pl, int pr) {
    if (pr - pl == 1) return tree[p].info = _v, void();
    int ls = tree[p].ls, rs = tree[p].rs, mid = pl + (pr - pl) / 2;
    if (ls == -1) ls = tree[p].ls = alloc({});
    if (rs == -1) rs = tree[p].rs = alloc({});
    _pos < mid ? set(ls, pl, mid) : set(rs, mid, pr);
    tree[p].info = tree[ls].info + tree[rs].info;
  }
  Info sum(int p, int pl, int pr) {
    if (_r <= pl || pr <= _l || p == -1) return {};
    if (_l <= pl && pr <= _r) return tree[p].info;
    int ls = tree[p].ls, rs = tree[p].rs, mid = pl + (pr - pl) / 2;
    return sum(ls, pl, mid) + sum(rs, mid, pr);
  }
};
struct Info {
  int data = 2e9;
  friend Info operator+(const Info& lhs, const Info& rhs) {
    return {min(lhs.data, rhs.data)};
  }
};
#line 1 "src/math/modint.hpp"
template <unsigned MOD>
struct ModInt {
  unsigned data;
  ModInt(ll v = 0) : data(norm(v % MOD)) {}
  ModInt operator-() const { return MOD - data; }
  ModInt& operator+=(ModInt rhs) { return data = norm(data + rhs.data), *this; }
  ModInt& operator-=(ModInt rhs) { return data = norm(data - rhs.data), *this; }
  ModInt& operator*=(ModInt rhs) {
    return data = ull(data) * rhs.data % MOD, *this;
  }
  ModInt& operator/=(ModInt rhs) {
    return data = ull(data) * rhs.inv() % MOD, *this;
  }
  friend ModInt operator+(ModInt lhs, ModInt rhs) { return lhs += rhs; }
  friend ModInt operator-(ModInt lhs, ModInt rhs) { return lhs -= rhs; }
  friend ModInt operator*(ModInt lhs, ModInt rhs) { return lhs *= rhs; }
  friend ModInt operator/(ModInt lhs, ModInt rhs) { return lhs /= rhs; }
  unsigned inv() const {
    ll x, y;  // Inverse does not exist if gcd(data, MOD) != 1.
    assert(exgcd(data, MOD, x, y) == 1);
    return norm(x);
  }
  ModInt pow(ull n) const { return pow_mod(data, n, MOD); }
  static ll exgcd(ll a, ll b, ll& x, ll& y) {
    x = 1, y = 0;
    ll x1 = 0, y1 = 1;
    while (b) {
      ll q = a / b;
      swap(a -= q * b, b);
      swap(x -= q * x1, x1);
      swap(y -= q * y1, y1);
    }
    return a;
  }
  static unsigned pow_mod(unsigned a, ull n, unsigned p) {
    unsigned ret = 1;
    for (; n; n /= 2) {
      if (n & 1) ret = ull(ret) * a % p;
      a = ull(a) * a % p;
    }
    return ret;
  }

 private:
  static unsigned norm(unsigned x) {
    if ((x >> (8 * sizeof(unsigned) - 1)) & 1) x += MOD;
    return x >= MOD ? x -= MOD : x;
  }
};
constexpr unsigned MOD = 998244353;
using mint = ModInt<MOD>;
#line 1 "src/misc/read.hpp"
#define GC ch = getchar_unlocked()
ll read() {
  ll x = 0, f = 1, GC;
  while (ch < '0' || ch > '9') ch == '-' ? f = -1, GC : GC;
  while (ch >= '0' && ch <= '9') x = x * 10 + ch - '0', GC;
  return x * f;
}
#undef GC
#line 11 "test/ds/dynsgt.test.cpp"

struct Info_ {
  mint a = 1, b = 0;
  friend Info_ operator+(const Info_& lhs, const Info_& rhs) {
    return {lhs.a * rhs.a, lhs.b * rhs.a + rhs.b};
  }
};

int main() {
  cin.tie(nullptr)->sync_with_stdio(false);
  int n = read(), q = read();
  SegTree<Info_> sgt(n);
  for (int i = 0; i < n; i++) {
    int a = read(), b = read();
    sgt.set(i, {a, b});
  }
  while (q--) {
    int op = read();
    if (op == 0) {
      int pos = read(), c = read(), d = read();
      sgt.set(pos, {c, d});
    } else {
      int l = read(), r = read(), x = read();
      auto [A, B] = sgt.sum(l, r);
      cout << (A * x + B).data << "\n";
    }
  }
  return 0;
}

Test cases

Env Name Status Elapsed Memory
g++ example_00 :heavy_check_mark: AC 5 ms 4 MB
g++ max_random_00 :heavy_check_mark: AC 419 ms 20 MB
g++ max_random_01 :heavy_check_mark: AC 429 ms 20 MB
g++ max_random_02 :heavy_check_mark: AC 422 ms 20 MB
g++ max_random_03 :heavy_check_mark: AC 427 ms 21 MB
g++ max_random_04 :heavy_check_mark: AC 454 ms 21 MB
g++ random_00 :heavy_check_mark: AC 356 ms 20 MB
g++ random_01 :heavy_check_mark: AC 378 ms 21 MB
g++ random_02 :heavy_check_mark: AC 160 ms 5 MB
g++ random_03 :heavy_check_mark: AC 122 ms 21 MB
g++ random_04 :heavy_check_mark: AC 116 ms 20 MB
g++ small_00 :heavy_check_mark: AC 5 ms 3 MB
g++ small_01 :heavy_check_mark: AC 4 ms 4 MB
g++ small_02 :heavy_check_mark: AC 5 ms 4 MB
g++ small_03 :heavy_check_mark: AC 5 ms 4 MB
g++ small_04 :heavy_check_mark: AC 5 ms 4 MB
Back to top page