結果

問題 No.1649 Manhattan Square
ユーザー hashiryohashiryo
提出日時 2023-11-05 22:48:27
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 1,933 ms / 3,000 ms
コード長 15,292 bytes
コンパイル時間 2,753 ms
コンパイル使用メモリ 235,708 KB
実行使用メモリ 148,780 KB
最終ジャッジ日時 2024-09-25 22:51:46
合計ジャッジ時間 69,175 ms
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,812 KB
testcase_01 AC 2 ms
6,944 KB
testcase_02 AC 10 ms
6,940 KB
testcase_03 AC 10 ms
6,944 KB
testcase_04 AC 10 ms
6,940 KB
testcase_05 AC 10 ms
6,940 KB
testcase_06 AC 10 ms
6,940 KB
testcase_07 AC 1,925 ms
148,420 KB
testcase_08 AC 1,825 ms
148,004 KB
testcase_09 AC 1,839 ms
148,096 KB
testcase_10 AC 1,789 ms
148,152 KB
testcase_11 AC 1,808 ms
148,212 KB
testcase_12 AC 1,723 ms
139,520 KB
testcase_13 AC 1,819 ms
139,520 KB
testcase_14 AC 1,806 ms
139,640 KB
testcase_15 AC 1,737 ms
139,608 KB
testcase_16 AC 1,748 ms
139,564 KB
testcase_17 AC 1,854 ms
139,508 KB
testcase_18 AC 1,436 ms
124,032 KB
testcase_19 AC 1,738 ms
131,488 KB
testcase_20 AC 1,754 ms
139,648 KB
testcase_21 AC 1,802 ms
139,536 KB
testcase_22 AC 1,778 ms
148,660 KB
testcase_23 AC 1,822 ms
148,636 KB
testcase_24 AC 1,759 ms
148,660 KB
testcase_25 AC 1,933 ms
148,676 KB
testcase_26 AC 1,831 ms
148,680 KB
testcase_27 AC 1,839 ms
148,712 KB
testcase_28 AC 1,801 ms
148,592 KB
testcase_29 AC 1,814 ms
148,580 KB
testcase_30 AC 1,811 ms
148,616 KB
testcase_31 AC 1,872 ms
148,600 KB
testcase_32 AC 1,819 ms
148,572 KB
testcase_33 AC 1,922 ms
148,752 KB
testcase_34 AC 1,776 ms
148,772 KB
testcase_35 AC 1,805 ms
148,660 KB
testcase_36 AC 1,831 ms
148,696 KB
testcase_37 AC 1,793 ms
148,652 KB
testcase_38 AC 1,788 ms
148,780 KB
testcase_39 AC 1,907 ms
148,696 KB
testcase_40 AC 1,777 ms
148,776 KB
testcase_41 AC 1,855 ms
148,740 KB
testcase_42 AC 251 ms
24,956 KB
testcase_43 AC 2 ms
6,940 KB
testcase_44 AC 1 ms
6,940 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

// #define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
// clang-format off
std::ostream&operator<<(std::ostream&os,std::int8_t x){return os<<(int)x;}
std::ostream&operator<<(std::ostream&os,std::uint8_t x){return os<<(int)x;}
std::ostream&operator<<(std::ostream&os,const __int128_t &v){if(!v)os<<"0";__int128_t tmp=v<0?(os<<"-",-v):v;std::string s;while(tmp)s+='0'+(tmp%10),tmp/=10;return std::reverse(s.begin(),s.end()),os<<s;}
std::ostream&operator<<(std::ostream&os,const __uint128_t &v){if(!v)os<<"0";__uint128_t tmp=v;std::string s;while(tmp)s+='0'+(tmp%10),tmp/=10;return std::reverse(s.begin(),s.end()),os<<s;}
#define checkpoint() (void(0))
#define debug(...) (void(0))
#define debugArray(x,n) (void(0))
#define debugMatrix(x,h,w) (void(0))
// clang-format on
#include <type_traits>
template <class T> static constexpr bool tuple_like_v= false;
template <class... Args> static constexpr bool tuple_like_v<std::tuple<Args...>> = true;
template <class T, class U> static constexpr bool tuple_like_v<std::pair<T, U>> = true;
template <class T, size_t K> static constexpr bool tuple_like_v<std::array<T, K>> = true;
template <class T> auto to_tuple(const T &t) {
 if constexpr (tuple_like_v<T>) return std::apply([](auto &&...x) { return std::make_tuple(x...); }, t);
}
template <class T> auto forward_tuple(const T &t) {
 if constexpr (tuple_like_v<T>) return std::apply([](auto &&...x) { return std::forward_as_tuple(x...); }, t);
}
template <class T> static constexpr bool array_like_v= false;
template <class T, size_t K> static constexpr bool array_like_v<std::array<T, K>> = true;
template <class T, class U> static constexpr bool array_like_v<std::pair<T, U>> = std::is_convertible_v<T, U>;
template <class T> static constexpr bool array_like_v<std::tuple<T>> = true;
template <class T, class U, class... Args> static constexpr bool array_like_v<std::tuple<T, U, Args...>> = array_like_v<std::tuple<T, Args...>> && std::is_convertible_v<U, T>;
template <class T> auto to_array(const T &t) {
 if constexpr (array_like_v<T>) return std::apply([](auto &&...x) { return std::array{x...}; }, t);
}
template <class T> using to_tuple_t= decltype(to_tuple(T()));
template <class T> using to_array_t= decltype(to_array(T()));
template <class pos_t, class M> class SegmentTree_2D {
 using T= typename M::T;
 using Pos= std::array<pos_t, 2>;
 std::vector<pos_t> xs;
 std::vector<Pos> yxs;
 std::vector<int> id, tol;
 std::vector<T> val;
 template <class P> using canbe_Pos= std::is_convertible<to_tuple_t<P>, std::tuple<pos_t, pos_t>>;
 template <class P> using canbe_PosV= std::is_convertible<to_tuple_t<P>, std::tuple<pos_t, pos_t, T>>;
 template <class P, class U> static constexpr bool canbe_Pos_and_T_v= std::conjunction_v<canbe_Pos<P>, std::is_convertible<U, T>>;
 int sz;
 inline int x2i(pos_t x) const { return std::lower_bound(xs.begin(), xs.end(), x) - xs.begin(); }
 inline int y2i(pos_t y) const {
  return std::lower_bound(yxs.begin(), yxs.end(), Pos{y, 0}, [](const Pos &a, const Pos &b) { return a[0] < b[0]; }) - yxs.begin();
 }
 inline int xy2i(pos_t x, pos_t y) const {
  Pos p{y, x};
  auto it= std::lower_bound(yxs.begin(), yxs.end(), p);
  return assert(p == *it), it - yxs.begin();
 }
 template <bool z, size_t k, class P> inline auto get_(const P &p) {
  if constexpr (z) return std::get<k>(p);
  else return std::get<k>(p.first);
 }
 template <bool z, class XYW> inline void build(const XYW *xyw, int n, const T &v= M::ti()) {
  xs.resize(n), yxs.resize(n);
  for (int i= n; i--;) xs[i]= get_<z, 0>(xyw[i]);
  std::sort(xs.begin(), xs.end()), xs.erase(std::unique(xs.begin(), xs.end()), xs.end()), id.resize((sz= 1 << (32 - __builtin_clz(xs.size()))) * 2 + 1);
  std::vector<int> ix(n), ord(n);
  for (int i= n; i--;) ix[i]= x2i(get_<z, 0>(xyw[i]));
  for (int i: ix)
   for (i+= sz; i; i>>= 1) ++id[i + 1];
  for (int i= 1, e= sz * 2; i < e; ++i) id[i + 1]+= id[i];
  val.assign(id.back() * 2, M::ti()), tol.resize(id[sz] + 1), std::iota(ord.begin(), ord.end(), 0), std::sort(ord.begin(), ord.end(), [&](int i, int j) { return get_<z, 1>(xyw[i]) == get_<z, 1>(xyw[j]) ? get_<z, 0>(xyw[i]) < get_<z, 0>(xyw[j]) : get_<z, 1>(xyw[i]) < get_<z, 1>(xyw[j]); });
  for (int i= n; i--;) yxs[i]= {get_<z, 1>(xyw[ord[i]]), get_<z, 0>(xyw[ord[i]])};
  std::vector<int> ptr= id;
  for (int r: ord)
   for (int i= ix[r] + sz, j= -1; i; j= i, i>>= 1) {
    int p= ptr[i]++;
    if constexpr (z) {
     if constexpr (std::tuple_size_v<XYW> == 3) val[id[i + 1] + p]= std::get<2>(xyw[r]);
     else val[id[i + 1] + p]= v;
    } else val[id[i + 1] + p]= xyw[r].second;
    if (j != -1) tol[p + 1]= !(j & 1);
   }
  for (int i= 1, e= id[sz]; i < e; ++i) tol[i + 1]+= tol[i];
  for (int i= 0, e= sz * 2; i < e; ++i) {
   auto dat= val.begin() + id[i] * 2;
   for (int j= id[i + 1] - id[i]; --j > 0;) dat[j]= M::op(dat[j * 2], dat[j * 2 + 1]);
  }
 }
 inline T fold(int i, int a, int b) const {
  int n= id[i + 1] - id[i];
  T ret= M::ti();
  auto dat= val.begin() + id[i] * 2;
  for (a+= n, b+= n; a < b; a>>= 1, b>>= 1) {
   if (a & 1) ret= M::op(ret, dat[a++]);
   if (b & 1) ret= M::op(dat[--b], ret);
  }
  return ret;
 }
 template <bool z> inline void seti(int i, int j, T v) {
  auto dat= val.begin() + id[i] * 2;
  j+= id[i + 1] - id[i];
  if constexpr (z) dat[j]= v;
  else dat[j]= M::op(dat[j], v);
  for (; j;) j>>= 1, dat[j]= M::op(dat[2 * j], dat[2 * j + 1]);
 }
 template <bool z> inline void set_(pos_t x, pos_t y, T v) {
  for (int i= 1, p= xy2i(x, y);;) {
   if (seti<z>(i, p - id[i], v); i >= sz) break;
   if (int lc= tol[p] - tol[id[i]], rc= (p - id[i]) - lc; tol[p + 1] - tol[p]) p= id[2 * i] + lc, i= 2 * i;
   else p= id[2 * i + 1] + rc, i= 2 * i + 1;
  }
 }
 template <class F> void dfs(const F &f, int i, int a, int b, int c, int d, int L, int R) const {
  if (c == d || R <= a || b <= L) return;
  if (L <= a && b <= R) return f(i, c, d);
  int m= (a + b) / 2, ac= tol[id[i] + c] - tol[id[i]], bc= c - ac, ad= tol[id[i] + d] - tol[id[i]], bd= d - ad;
  dfs(f, i * 2, a, m, ac, ad, L, R), dfs(f, i * 2 + 1, m, b, bc, bd, L, R);
 }
public:
 template <class P, typename= std::enable_if_t<std::disjunction_v<canbe_Pos<P>, canbe_PosV<P>>>> SegmentTree_2D(const P *p, size_t n) { build<1>(p, n); }
 template <class P, typename= std::enable_if_t<std::disjunction_v<canbe_Pos<P>, canbe_PosV<P>>>> SegmentTree_2D(const std::vector<P> &p): SegmentTree_2D(p.data(), p.size()) {}
 template <class P, typename= std::enable_if_t<canbe_Pos<P>::value>> SegmentTree_2D(const std::set<P> &p): SegmentTree_2D(std::vector(p.begin(), p.end())) {}
 template <class P, class U, typename= std::enable_if_t<canbe_Pos_and_T_v<P, U>>> SegmentTree_2D(const P *p, size_t n, const U &v) { build<1>(p, n, v); }
 template <class P, class U, typename= std::enable_if_t<canbe_Pos_and_T_v<P, U>>> SegmentTree_2D(const std::vector<P> &p, const U &v): SegmentTree_2D(p.data(), p.size(), v) {}
 template <class P, class U, typename= std::enable_if_t<canbe_Pos_and_T_v<P, U>>> SegmentTree_2D(const std::set<P> &p, const U &v): SegmentTree_2D(std::vector(p.begin(), p.end()), v) {}
 template <class P, class U, typename= std::enable_if_t<canbe_Pos_and_T_v<P, U>>> SegmentTree_2D(const std::pair<P, U> *p, size_t n) { build<0>(p, n); }
 template <class P, class U, typename= std::enable_if_t<canbe_Pos_and_T_v<P, U>>> SegmentTree_2D(const std::vector<std::pair<P, U>> &p): SegmentTree_2D(p.data(), p.size()) {}
 template <class P, class U, typename= std::enable_if_t<canbe_Pos_and_T_v<P, U>>> SegmentTree_2D(const std::map<P, U> &p): SegmentTree_2D(std::vector(p.begin(), p.end())) {}
 // [l,r) x [u,d)
 T fold(pos_t l, pos_t r, pos_t u, pos_t d) const {
  T ret= M::ti();
  auto f= [&](int i, int c, int d) { ret= M::op(ret, fold(i, c, d)); };
  return dfs(f, 1, 0, sz, y2i(u), y2i(d), x2i(l), x2i(r)), ret;
 }
 // [l,r) x [u,d)
 int count(pos_t l, pos_t r, pos_t u, pos_t d) const {
  int ret= 0;
  auto f= [&](int i, int c, int d) { ret+= d - c; };
  return dfs(f, 1, 0, sz, y2i(u), y2i(d), x2i(l), x2i(r)), ret;
 }
 void set(pos_t x, pos_t y, T v) { set_<1>(x, y, v); }
 void mul(pos_t x, pos_t y, T v) { set_<0>(x, y, v); }
 T get(pos_t x, pos_t y) const { return val[xy2i(x, y) + id[2]]; }
};
template <class Int> constexpr inline Int mod_inv(Int a, Int mod) {
 static_assert(std::is_signed_v<Int>);
 Int x= 1, y= 0, b= mod;
 for (Int q= 0, z= 0; b;) z= x, x= y, y= z - y * (q= a / b), z= a, a= b, b= z - b * q;
 return assert(a == 1), x < 0 ? mod - (-x) % mod : x % mod;
}
namespace math_internal {
using namespace std;
using u8= unsigned char;
using u32= unsigned;
using i64= long long;
using u64= unsigned long long;
using u128= __uint128_t;
#define CE constexpr
#define IL inline
#define NORM \
 if (n >= mod) n-= mod; \
 return n
#define PLUS(U, M) \
 CE IL U plus(U l, U r) const { \
  if (l+= r; l >= M) l-= M; \
  return l; \
 }
#define DIFF(U, C, M) \
 CE IL U diff(U l, U r) const { \
  if (l-= r; l >> C) l+= M; \
  return l; \
 }
#define SGN(U) \
 static CE IL U set(U n) { return n; } \
 static CE IL U get(U n) { return n; } \
 static CE IL U norm(U n) { return n; }
template <class u_t, class du_t, u8 B, u8 A> struct MP_Mo {
 u_t mod;
 CE MP_Mo(): mod(0), iv(0), r2(0) {}
 CE MP_Mo(u_t m): mod(m), iv(inv(m)), r2(-du_t(mod) % mod) {}
 CE IL u_t mul(u_t l, u_t r) const { return reduce(du_t(l) * r); }
 PLUS(u_t, mod << 1)
 DIFF(u_t, A, mod << 1)
 CE IL u_t set(u_t n) const { return mul(n, r2); }
 CE IL u_t get(u_t n) const {
  n= reduce(n);
  NORM;
 }
 CE IL u_t norm(u_t n) const { NORM; }
private:
 u_t iv, r2;
 static CE u_t inv(u_t n, int e= 6, u_t x= 1) { return e ? inv(n, e - 1, x * (2 - x * n)) : x; }
 CE IL u_t reduce(const du_t &w) const { return u_t(w >> B) + mod - ((du_t(u_t(w) * iv) * mod) >> B); }
};
struct MP_Na {
 u32 mod;
 CE MP_Na(): mod(0){};
 CE MP_Na(u32 m): mod(m) {}
 CE IL u32 mul(u32 l, u32 r) const { return u64(l) * r % mod; }
 PLUS(u32, mod) DIFF(u32, 31, mod) SGN(u32)
};
struct MP_Br {  // mod < 2^31
 u32 mod;
 CE MP_Br(): mod(0), s(0), x(0) {}
 CE MP_Br(u32 m): mod(m), s(95 - __builtin_clz(m - 1)), x(((u128(1) << s) + m - 1) / m) {}
 CE IL u32 mul(u32 l, u32 r) const { return rem(u64(l) * r); }
 PLUS(u32, mod) DIFF(u32, 31, mod) SGN(u32) private: u8 s;
 u64 x;
 CE IL u64 quo(u64 n) const { return (u128(x) * n) >> s; }
 CE IL u32 rem(u64 n) const { return n - quo(n) * mod; }
};
struct MP_Br2 {  // 2^20 < mod <= 2^41
 u64 mod;
 CE MP_Br2(): mod(0), x(0) {}
 CE MP_Br2(u64 m): mod(m), x((u128(1) << 84) / m) {}
 CE IL u64 mul(u64 l, u64 r) const { return rem(u128(l) * r); }
 PLUS(u64, mod << 1)
 DIFF(u64, 63, mod << 1)
 static CE IL u64 set(u64 n) { return n; }
 CE IL u64 get(u64 n) const { NORM; }
 CE IL u64 norm(u64 n) const { NORM; }
private:
 u64 x;
 CE IL u128 quo(const u128 &n) const { return (n * x) >> 84; }
 CE IL u64 rem(const u128 &n) const { return n - quo(n) * mod; }
};
struct MP_D2B1 {
 u8 s;
 u64 mod, d, v;
 CE MP_D2B1(): s(0), mod(0), d(0), v(0) {}
 CE MP_D2B1(u64 m): s(__builtin_clzll(m)), mod(m), d(m << s), v(u128(-1) / d) {}
 CE IL u64 mul(u64 l, u64 r) const { return rem((u128(l) * r) << s) >> s; }
 PLUS(u64, mod) DIFF(u64, 63, mod) SGN(u64) private: CE IL u64 rem(const u128 &u) const {
  u128 q= (u >> 64) * v + u;
  u64 r= u64(u) - (q >> 64) * d - d;
  if (r > u64(q)) r+= d;
  if (r >= d) r-= d;
  return r;
 }
};
template <class u_t, class MP> CE u_t pow(u_t x, u64 k, const MP &md) {
 for (u_t ret= md.set(1);; x= md.mul(x, x))
  if (k & 1 ? ret= md.mul(ret, x) : 0; !(k>>= 1)) return ret;
}
#undef NORM
#undef PLUS
#undef DIFF
#undef SGN
#undef CE
}
namespace math_internal {
struct m_b {};
struct s_b: m_b {};
}
template <class mod_t> constexpr bool is_modint_v= std::is_base_of_v<math_internal::m_b, mod_t>;
template <class mod_t> constexpr bool is_staticmodint_v= std::is_base_of_v<math_internal::s_b, mod_t>;
namespace math_internal {
#define CE constexpr
template <class MP, u64 MOD> struct SB: s_b {
protected:
 static CE MP md= MP(MOD);
};
template <class Int, class U, class B> struct MInt: public B {
 using Uint= U;
 static CE inline auto mod() { return B::md.mod; }
 CE MInt(): x(0) {}
 template <class T, enable_if_t<is_modint_v<T> && !is_same_v<T, MInt>, nullptr_t> = nullptr> CE MInt(T v): x(B::md.set(v.val() % B::md.mod)) {}
 CE MInt(__int128_t n): x(B::md.set((n < 0 ? ((n= (-n) % B::md.mod) ? B::md.mod - n : n) : n % B::md.mod))) {}
 CE MInt operator-() const { return MInt() - *this; }
#define FUNC(name, op) \
 CE MInt name const { \
  MInt ret; \
  ret.x= op; \
  return ret; \
 }
 FUNC(operator+(const MInt& r), B::md.plus(x, r.x))
 FUNC(operator-(const MInt& r), B::md.diff(x, r.x))
 FUNC(operator*(const MInt& r), B::md.mul(x, r.x))
 FUNC(pow(u64 k), math_internal::pow(x, k, B::md))
#undef FUNC
 CE MInt operator/(const MInt& r) const { return *this * r.inv(); }
 CE MInt& operator+=(const MInt& r) { return *this= *this + r; }
 CE MInt& operator-=(const MInt& r) { return *this= *this - r; }
 CE MInt& operator*=(const MInt& r) { return *this= *this * r; }
 CE MInt& operator/=(const MInt& r) { return *this= *this / r; }
 CE bool operator==(const MInt& r) const { return B::md.norm(x) == B::md.norm(r.x); }
 CE bool operator!=(const MInt& r) const { return !(*this == r); }
 CE bool operator<(const MInt& r) const { return B::md.norm(x) < B::md.norm(r.x); }
 CE inline MInt inv() const { return mod_inv<Int>(val(), B::md.mod); }
 CE inline Uint val() const { return B::md.get(x); }
 friend ostream& operator<<(ostream& os, const MInt& r) { return os << r.val(); }
 friend istream& operator>>(istream& is, MInt& r) {
  i64 v;
  return is >> v, r= MInt(v), is;
 }
private:
 Uint x;
};
template <u64 MOD> using ModInt= conditional_t < (MOD < (1 << 30)) & MOD, MInt<int, u32, SB<MP_Mo<u32, u64, 32, 31>, MOD>>, conditional_t < (MOD < (1ull << 62)) & MOD, MInt<i64, u64, SB<MP_Mo<u64, u128, 64, 63>, MOD>>, conditional_t<MOD<(1u << 31), MInt<int, u32, SB<MP_Na, MOD>>, conditional_t<MOD<(1ull << 32), MInt<i64, u32, SB<MP_Na, MOD>>, conditional_t<MOD <= (1ull << 41), MInt<i64, u64, SB<MP_Br2, MOD>>, MInt<i64, u64, SB<MP_D2B1, MOD>>>>>>>;
#undef CE
}
using math_internal::ModInt;
template <class mod_t, size_t LM> mod_t get_inv(int n) {
 static_assert(is_modint_v<mod_t>);
 static const auto m= mod_t::mod();
 static mod_t dat[LM];
 static int l= 1;
 if (l == 1) dat[l++]= 1;
 while (l <= n) dat[l++]= dat[m % l] * (m - m / l);
 return dat[n];
}
using namespace std;
using Mint= ModInt<998244353>;
struct RSQ {
 using T= array<Mint, 4>;
 static T ti() { return {0, 0, 0, 0}; }
 static T op(const T &l, const T &r) { return {l[0] + r[0], l[1] + r[1], l[2] + r[2], l[3] + r[3]}; }
};
signed main() {
 cin.tie(0);
 ios::sync_with_stdio(false);
 int N;
 cin >> N;
 vector<tuple<int, int, array<Mint, 4>>> v(N);
 for (auto &[x, y, c]: v) cin >> x >> y, c= {1, x, y, Mint(x) * y};
 SegmentTree_2D<int, RSQ> seg(v);
 Mint xs= 0, x2s= 0, ys= 0, y2s= 0;
 for (auto &[x, y, c]: v) xs+= x, x2s+= Mint(x) * x, ys+= y, y2s+= Mint(y) * y;
 Mint ans= (x2s + y2s) * N - xs * xs - ys * ys;
 for (auto &[x, y, c]: v) {
  auto [cnt, xs, ys, xys]= seg.fold(0, x, 0, y);
  ans+= (xys - xs * y - ys * x + cnt * x * y) * 2;
 }
 for (auto &[x, y, c]: v) {
  auto [cnt, xs, ys, xys]= seg.fold(x, 0x7fffffff, 0, y);
  ans-= (xys - xs * y - ys * x + cnt * x * y) * 2;
 }
 cout << ans << '\n';
 return 0;
}
0