#include #include #include using namespace std; using namespace atcoder; // --- LazySegTree 用の型定義 --- using S = long long; using F = long long; // 区間和 S op1(S a, S b) { return a + b; } S e() { return 0LL; } // LazySegTree の作用 S mapping1(F f, S x) { return f + x; } F composition1(F f, F g) { return f + g; } F id() { return 0LL; } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int n, m; cin >> n >> m; // LazySegTree と SegTree を初期化 lazy_segtree kinnzyo(m + 1); segtree rate(m + 1); // Python の iwai_dict を C++ では map> で持つ map> iwai_dict; for (int i = 0; i < n; i++) { int a, l, r; cin >> a >> l >> r; int idx = i + 1; rate.set(idx, a); kinnzyo.apply(l, r + 1, 1); iwai_dict[idx] = {idx, a, l, r}; } long long ans = 0; for (int i = 1; i <= n; i++) { auto [prev, a, l, r] = iwai_dict[i]; ans += (long long)(r - l + 1) * a - rate.prod(l, r + 1); } int q; cin >> q; while (q--) { int x, y, u, v; cin >> x >> y >> u >> v; auto [prev, a, l, r] = iwai_dict[x]; ans -= (long long)(r - l + 1) * a - rate.prod(l, r + 1); rate.set(prev, 0); kinnzyo.apply(l, r + 1, -1); ans += kinnzyo.prod(prev, prev + 1) * a; ans -= kinnzyo.prod(y, y + 1) * a; kinnzyo.apply(u, v + 1, 1); rate.set(y, a); iwai_dict[x] = {y, a, u, v}; ans += (long long)(v - u + 1) * a - rate.prod(u, v + 1); cout << ans << "\n"; } return 0; }