結果
問題 |
No.3227 Matrix Query
|
ユーザー |
|
提出日時 | 2025-08-08 22:19:32 |
言語 | C++23 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 1,660 ms / 8,000 ms |
コード長 | 12,782 bytes |
コンパイル時間 | 3,672 ms |
コンパイル使用メモリ | 310,304 KB |
実行使用メモリ | 9,472 KB |
最終ジャッジ日時 | 2025-08-08 22:20:31 |
合計ジャッジ時間 | 30,314 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge4 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 1 |
other | AC * 28 |
ソースコード
#include <bits/stdc++.h> using namespace std; #define all(...) std::begin(__VA_ARGS__), std::end(__VA_ARGS__) #define rall(...) std::rbegin(__VA_ARGS__), std::rend(__VA_ARGS__) #define OVERLOAD_REP(_1, _2, _3, _4, name, ...) name #define REP1(n) for(ll i=0;i<(n);i++) #define REP2(i, n) for (ll i=0;i<(n);i++) #define REP3(i, a, n) for (ll i=a;i<(n);i++) #define REP4(i, a, b, n) for(ll i=a;i<(n);i+=b) #define rep(...) OVERLOAD_REP(__VA_ARGS__, REP4, REP3, REP2, REP1)(__VA_ARGS__) #define OVERLOAD_RREP(_1, _2, _3, _4, name, ...) name #define RREP1(n) for(ll i=(n)-1;i>=0;i--) #define RREP2(i, n) for(ll i=(n)-1;i>=0;i--) #define RREP3(i, a, n) for(ll i=(n)-1;i>=(a);i--) #define RREP4(i, a, b, n) for(ll i=(n)-1;i>=(a);i-=(b)) #define rrep(...) OVERLOAD_RREP(__VA_ARGS__, RREP4, RREP3, RREP2, RREP1)(__VA_ARGS__) #define uniq(a) sort(all(a));a.erase(unique(all(a)),end(a)) #define len(n) (long long)(n).size() using ll = long long; using ld = long double; using ull = unsigned long long; using vi = vector<int>; using vvi = vector<vi>; using vvvi = vector<vvi>; using vll = vector<ll>; using vvll = vector<vll>; using vvvll = vector<vvll>; using vs = vector<string>; using vvs = vector<vs>; using vvvs = vector<vvs>; using vld = vector<ld>; using vvld = vector<vld>; using vvvld = vector<vvld>; using vc = vector<char>; using vvc = vector<vc>; using vvvc = vector<vvc>; using pll = pair<ll,ll>; using vpll = vector<pll>; using vvpll = vector<vpll>; ll intpow(ll a,ll b){ ll ans = 1; while (b){ if (b & 1){ ans *= a; } a *= a; b /= 2; } return ans; } ll modpow(ll a,ll b,ll c){ ll ans = 1; while (b){ if (b & 1){ ans *= a; ans %= c; } a *= a; a %= c; b /= 2; } return ans; } template<class... T> void input(T&... a){ (cin >> ... >> a); } #define INT(...) int __VA_ARGS__; input(__VA_ARGS__) #define LL(...) ll __VA_ARGS__; input(__VA_ARGS__) #define ULL(...) ull __VA_ARGS__; input(__VA_ARGS__) #define LD(...) ld __VA_ARGS__; input(__VA_ARGS__) #define STR(...) string __VA_ARGS__; input(__VA_ARGS__) #define CHA(...) char __VA_ARGS__; input(__VA_ARGS__) #define VLL(name,length) vll name(length);rep(i,length){cin >> name[i];} #define VVLL(name,h,w) vvll name(h,vll(w));rep(i,h)rep(j,w){cin >> name[i][j];} #define VVVLL(name,a,b,c) vvvll name(a,vvll(b,vll(c)));rep(i,a)rep(j,b)rep(k,c){cin >> name[i][j][k];} #define VI(name,length) vi name(length);rep(i,length){cin >> name[i];} #define VVI(name,h,w) vvi name(h,vi(w));rep(i,h)rep(j,w){cin >> name[i][j];} #define VVVI(name,a,b,c) vvvi name(a,vvll(b,vi(c)));rep(i,a)rep(j,b)rep(k,c){cin >> name[i][j][k];} #define VLD(name,length) vld name(length);rep(i,length){cin >> name[i];} #define VVLD(name,h,w) vvld name(h,vld(w));rep(i,h)rep(j,w){cin >> name[i][j];} #define VVVLD(name,a,b,c) vvvld name(a,vvld(b,vld(c)));rep(i,a)rep(j,b)rep(k,c){cin >> name[i][j][k];} #define VC(name,length) vc name(length);rep(i,length){cin >> name[i];} #define VVC(name,h,w) vvc name(h,vc(w));rep(i,h)rep(j,w){cin >> name[i][j];} #define VVVC(name,a,b,c) vvvc name(a,vvc(b,vc(c)));rep(i,a)rep(j,b)rep(k,c){cin >> name[i][j][k];} #define VS(name,length) vs name(length);rep(i,length){cin >> name[i];} #define VVS(name,h,w) vvs name(h,vs(w));rep(i,h)rep(j,w){cin >> name[i][j];} #define VVVS(name,a,b,c) vvvs name(a,vvs(b,vs(c)));rep(i,a)rep(j,b)rep(k,c){cin >> name[i][j][k];} #define PLL(name) pll name;cin>>name.first>>name.second; #define VPLL(name,length) vpll name(length);rep(i,length){cin>>name[i].first>>name[i].second;} void print(){cout << "\n";} template <typename T1, typename T2> std::ostream& operator<<(std::ostream& os, const std::pair<T1, T2>& p) { os << "(" << p.first << ", " << p.second << ")"; return os; } template <typename T> std::ostream& operator<<(std::ostream& os, const std::vector<T>& vec) { os << "["; for (size_t i = 0; i < vec.size(); ++i) { os << vec[i]; if (i + 1 < vec.size()) os << ", "; } os << "]"; return os; } template <typename T1, typename T2> std::ostream& operator<<(std::ostream& os, const std::vector<std::pair<T1, T2>>& a) { os << "["; for (size_t j = 0; j < a.size(); ++j) { os << "(" << a[j].first << ", " << a[j].second << ")"; if (j + 1 < a.size()) os << ", "; } os << "]"; return os; } template <typename T1, typename T2> std::ostream& operator<<(std::ostream& os, const std::vector<std::vector<std::pair<T1, T2>>>& mat) { os << "["; for (size_t i = 0; i < mat.size(); ++i) { os << "["; for (size_t j = 0; j < mat[i].size(); ++j) { os << "(" << mat[i][j].first << ", " << mat[i][j].second << ")"; if (j + 1 < mat[i].size()) os << ", "; } os << "]"; if (i + 1 < mat.size()) os << ", "; } os << "]"; return os; } template <typename T> std::ostream& operator<<(std::ostream& os, const std::set<T>& s) { os << "{"; bool first = true; for (const auto& x : s) { if (!first) os << ", "; os << x; first = false; } os << "}"; return os; } template <typename K, typename V> std::ostream& operator<<(std::ostream& os, const std::map<K, V>& m) { os << "{"; bool first = true; for (const auto& [key, val] : m) { if (!first) os << ", "; os << key << ": " << val; first = false; } os << "}"; return os; } template<class T, class... Ts> void print(const T& a, const Ts&... b){cout << a;(cout << ... << (cout << ' ', b));cout << '\n';} #ifdef LOCAL void debug() { std::cerr << "\n"; } template<class T, class... Ts> void debug(const T& a, const Ts&... b) { std::cerr << a; (std::cerr << ... << (std::cerr << ' ', b)); std::cerr << '\n'; } #else #define debug(...) (void)0 #endif void write(){cout << "\n";} template<class T, class... Ts> void write(const T& a, const Ts&... b){cout << a;(cout << ... << (cout << ' ', b));cout << '\n';} void write(vll x){rep(i,len(x)){cout << x[i];if(i!=len(x)-1){cout << " ";}else{cout << '\n';}}} void write(vvll x){rep(i,len(x))rep(j,len(x[i])){cout << x[i][j];if(j!=len(x[i])-1){cout << " ";}else{cout << '\n';}}} void write(vi x){rep(i,len(x)){cout << x[i];if(i!=len(x)-1){cout << " ";}else{cout << '\n';}}} void write(vvi x){rep(i,len(x))rep(j,len(x[i])){cout << x[i][j];if(j!=len(x[i])-1){cout << " ";}else{cout << '\n';}}} void write(vvvi x){rep(i,len(x))rep(j,len(x[i]))rep(k,len(x[i][j])){cout << x[i][j][k];if(k!=len(x[i][j])-1){cout << " ";}else if(j!=len(x[i])-1){cout << " | ";}else{cout << '\n';}}} void write(vld x){rep(i,len(x)){cout << x[i];if(i!=len(x)-1){cout << " ";}else{cout << '\n';}}} void write(vvld x){rep(i,len(x))rep(j,len(x[i])){cout << x[i][j];if(j!=len(x[i])-1){cout << " ";}else{cout << '\n';}}} void write(vvvld x){rep(i,len(x))rep(j,len(x[i]))rep(k,len(x[i][j])){cout << x[i][j][k];if(k!=len(x[i][j])-1){cout << " ";}else if(j!=len(x[i])-1){cout << " | ";}else{cout << '\n';}}} void write(vc x){rep(i,len(x)){cout << x[i];if(i!=len(x)-1){cout << " ";}else{cout << '\n';}}} void write(vvc x){rep(i,len(x))rep(j,len(x[i])){cout << x[i][j];if(j!=len(x[i])-1){cout << " ";}else{cout << '\n';}}} void write(vvvc x){rep(i,len(x))rep(j,len(x[i]))rep(k,len(x[i][j])){cout << x[i][j][k];if(k!=len(x[i][j])-1){cout << " ";}else if(j!=len(x[i])-1){cout << " | ";}else{cout << '\n';}}} void write(vs x){rep(i,len(x)){cout << x[i];if(i!=len(x)-1){cout << " ";}else{cout << '\n';}}} void write(vvs x){rep(i,len(x))rep(j,len(x[i])){cout << x[i][j];if(j!=len(x[i])-1){cout << " ";}else{cout << '\n';}}} void write(vvvs x){rep(i,len(x))rep(j,len(x[i]))rep(k,len(x[i][j])){cout << x[i][j][k];if(k!=len(x[i][j])-1){cout << " ";}else if(j!=len(x[i])-1){cout << " | ";}else{cout << '\n';}}} void write(pll x){cout << x.first << ' ' << x.second << '\n';} void write(vpll x){rep(i,len(x)){cout << x[i].first << ' ' << x[i].second << '\n';}} void write(vvpll x){rep(i,len(x))rep(j,len(x[i])){cout << x[i][j].first << ' ' << x[i][j].second;if(j!=len(x[i])-1){cout << " ";}else{cout << '\n';}}} template <typename T> T sum(const std::vector<T>& v) { return std::accumulate(v.begin(), v.end(), T(0)); } template<class T> bool chmin(T& a, const T& b){ if(a > b){ a = b; return 1; } return 0; } template<class T> bool chmax(T& a, const T& b){ if(a < b){ a = b; return 1; } return 0; } template<class T, class U> bool chmin(T& a, const U& b){ if(a > T(b)){ a = b; return 1; } return 0; } template<class T, class U> bool chmax(T& a, const U& b){ if(a < T(b)){ a = b; return 1; } return 0; } //https://judge.yosupo.jp/submission/293165 template<typename T> vector<vector<T>> mat_mul(vector<vector<T>> a, vector<vector<T>> b){ int n = a.size(); int m = a[0].size(); int k = b[0].size(); vector<vector<T>> c(n,vector<T>(k,0)); for(int i=0;i<n;i++){ for(int j=0;j<m;j++){ T a_i_j = a[i][j]; for(int l=0;l<k;l++){ c[i][l] += b[j][l] * a_i_j; } } } return c; } //https://judge.yosupo.jp/submission/293169 template<typename T> vector<vector<T>> mat_pow(vector<vector<T>> t, long long k){ vector<vector<T>> u(t.size(),vector<T>(t.size(),0)); for(int i=0;i<t.size();i++){ u[i][i] = 1; } while(k){ if(k&1){ u = mat_mul(u,t); } t = mat_mul(t,t); k >>= 1; } return u; } #line 2 "matrix/inverse-matrix.hpp" #line 2 "matrix/gauss-elimination.hpp" #include <utility> #include <vector> using namespace std; // {rank, det(非正方行列の場合は未定義)} を返す // 型が double や Rational でも動くはず?(未検証) // // pivot 候補 : [0, pivot_end) template <typename T> std::pair<int, T> GaussElimination(vector<vector<T>> &a, int pivot_end = -1, bool diagonalize = false) { if (a.empty()) return {0, 1}; int H = a.size(), W = a[0].size(), rank = 0; if (pivot_end == -1) pivot_end = W; T det = 1; for (int j = 0; j < pivot_end; j++) { int idx = -1; for (int i = rank; i < H; i++) { if (a[i][j] != T(0)) { idx = i; break; } } if (idx == -1) { det = 0; continue; } if (rank != idx) det = -det, swap(a[rank], a[idx]); det *= a[rank][j]; if (diagonalize && a[rank][j] != T(1)) { T coeff = T(1) / a[rank][j]; for (int k = j; k < W; k++) a[rank][k] *= coeff; } int is = diagonalize ? 0 : rank + 1; for (int i = is; i < H; i++) { if (i == rank) continue; if (a[i][j] != T(0)) { T coeff = a[i][j] / a[rank][j]; for (int k = j; k < W; k++) a[i][k] -= a[rank][k] * coeff; } } rank++; } return make_pair(rank, det); } #line 4 "matrix/inverse-matrix.hpp" template <typename mint> vector<vector<mint>> inverse_matrix(const vector<vector<mint>>& a) { int N = a.size(); assert(N > 0); assert(N == (int)a[0].size()); vector<vector<mint>> m(N, vector<mint>(2 * N)); for (int i = 0; i < N; i++) { copy(begin(a[i]), end(a[i]), begin(m[i])); m[i][N + i] = 1; } auto [rank, det] = GaussElimination(m, N, true); if (rank != N) return {}; vector<vector<mint>> b(N); for (int i = 0; i < N; i++) { copy(begin(m[i]) + N, end(m[i]), back_inserter(b[i])); } return b; } // std::chronoを利用した時間計測用クラス class Timer{ chrono::system_clock::time_point start; public: Timer() : start(chrono::system_clock::now()) {} double count(){ chrono::duration<double> Time_ = chrono::system_clock::now() - start; return Time_.count(); } bool is_under(double x){ return (this -> count()) < x; } }; #include <atcoder/modint> #include <atcoder/segtree> using mint = atcoder::modint; using vm = vector<mint>; using vvm = vector<vm>; vvm S(vvm x,vvm y){ return mat_mul(x,y); } vvm e(){ return {{1,0},{0,1}}; } int main(){ ios::sync_with_stdio(false); std::cin.tie(nullptr); LL(k,n); mint::set_mod(k); atcoder::segtree<vvm,S,e> st(n); rep(i,n){ vvm temp(2,vm(2)); rep(j,2){ rep(k,2){ LL(x); temp[j][k] = x; } } st.set(i,temp); } LL(q); rep(i,q){ LL(idx,l,r); idx--; l--; vvm temp(2,vm(2)); rep(j,2){ rep(k,2){ LL(x); temp[j][k] = x; } } st.set(idx,temp); auto ans = st.prod(l,r); rep(j,2){ rep(k,2){ cout << ans[j][k].val() << ' '; } cout << '\n'; } } }