typedef long long ll; typedef long double ld; #include using namespace std; #define int long long // Segment Tree template struct SegTree { using FuncMonoid = function< Monoid(Monoid, Monoid) >; using FuncAction = function< void(Monoid&, Action) >; using FuncComposition = function< void(Action&, Action) >; FuncMonoid FM; FuncAction FA; FuncComposition FC; Monoid IDENTITY_MONOID; Action IDENTITY_LAZY; int SIZE, HEIGHT; vector dat; vector lazy; SegTree() {} SegTree(int n, const FuncMonoid fm, const FuncAction fa, const FuncComposition fc, const Monoid &identity_monoid, const Action &identity_lazy) : FM(fm), FA(fa), FC(fc), IDENTITY_MONOID(identity_monoid), IDENTITY_LAZY(identity_lazy) { SIZE = 1, HEIGHT = 0; while (SIZE < n) SIZE <<= 1, ++HEIGHT; dat.assign(SIZE * 2, IDENTITY_MONOID); lazy.assign(SIZE * 2, IDENTITY_LAZY); } void init(int n, const FuncMonoid fm, const FuncAction fa, const FuncComposition fc, const Monoid &identity_monoid, const Action &identity_lazy) { FM = fm, FA = fa, FC = fc; IDENTITY_MONOID = identity_monoid, IDENTITY_LAZY = identity_lazy; SIZE = 1, HEIGHT = 0; while (SIZE < n) SIZE <<= 1, ++HEIGHT; dat.assign(SIZE * 2, IDENTITY_MONOID); lazy.assign(SIZE * 2, IDENTITY_LAZY); } // set, a is 0-indexed void set(int a, const Monoid &v) { dat[a + SIZE] = v; } void build() { for (int k = SIZE - 1; k > 0; --k) dat[k] = FM(dat[k*2], dat[k*2+1]); } // update [a, b) inline void evaluate(int k) { if (lazy[k] == IDENTITY_LAZY) return; if (k < SIZE) FC(lazy[k*2], lazy[k]), FC(lazy[k*2+1], lazy[k]); FA(dat[k], lazy[k]); lazy[k] = IDENTITY_LAZY; } inline void update(int a, int b, const Action &v, int k, int l, int r) { evaluate(k); if (a <= l && r <= b) FC(lazy[k], v), evaluate(k); else if (a < r && l < b) { update(a, b, v, k*2, l, (l+r)>>1); update(a, b, v, k*2+1, (l+r)>>1, r); dat[k] = FM(dat[k*2], dat[k*2+1]); } } inline void update(int a, int b, const Action &v) { update(a, b, v, 1, 0, SIZE); } // get [a, b) inline Monoid get(int a, int b, int k, int l, int r) { evaluate(k); if (a <= l && r <= b) return dat[k]; else if (a < r && l < b) return FM(get(a, b, k*2, l, (l+r)>>1), get(a, b, k*2+1, (l+r)>>1, r)); else return IDENTITY_MONOID; } inline Monoid get(int a, int b) { return get(a, b, 1, 0, SIZE); } inline Monoid operator [] (int a) { return get(a, a + 1); } // debug void print() { for (int i = 0; i < SIZE; ++i) { if (i) cout << ","; cout << get(i, i+1); } cout << endl; } }; signed main(){ ll n,b,q; std::cin >> n>>b>>q; vector> memo(q+1); vector done(q+1); memo[0] = {1%b,1%b,1%b}; done[0] = true; function(ll)> dfs = [&](ll cnt){ if(done[cnt]){ return memo[cnt]; } done[cnt]=true; auto [x,y,z] = dfs(cnt-1); tuple tmp = {(x+1)%b, (3*y+2*(x+1)*z)%b, 3*z%b}; return memo[cnt] = tmp; }; dfs(q); // define segtree auto fm = [&](ll a, ll b) { return a+b; }; auto fa = [&](ll &a, int d) { if (d == 0) return; a += d; }; auto fc = [&](int &d, int e) { d += e; }; SegTree seg(n+1, fm, fa, fc, 0, 0); for (int i = 0; i < q; i++) { ll l,m,r; std::cin >> l>>m>>r; seg.update(l,r+1, 1); ll val = seg.get(m, m+1); auto [x,y,z] = memo[val]; std::cout << x<<" "<