#include <cassert> #include <cstdio> #include <climits> #include <algorithm> #include <vector> const int MAXN = 100000; const int MAXM = 100000; struct Mission { int val; bool isDelete; Mission() {} Mission(int val, bool isDelete) : val(val), isDelete(isDelete) {} }; std::vector<Mission> a[MAXM + 50]; struct PresentTreeByGoldYeInInternationalOlympidInInformaticsHellc { struct Node { int l, r; Node *lc, *rc; int size, sum; Node() {} Node(int l, int r, Node *lc, Node *rc) : l(l), r(r), lc(lc), rc(rc), size(lc->size + rc->size), sum(lc->sum + rc->sum) {} Node(int l, int r, Node *lc, Node *rc, int size, int sum) : l(l), r(r), lc(lc), rc(rc), size(size), sum(sum) {} } *roots[MAXN + 50], *null, _pool[MAXN * 200], *_curr; int l, r; PresentTreeByGoldYeInInternationalOlympidInInformaticsHellc() { null = new Node(-1, -1, NULL, NULL, 0, 0); null->lc = null, null->rc = null; } Node *insert(Node *v, int l, int r, int x, bool isDelete) { if (!isDelete) { if (x < l || x > r) return v; else if (l == r) return new (_curr++) Node(l, r, null, null, v->size + 1, v->sum + x); else { int mid = l + (r - l) / 2; return new (_curr++) Node(l, r, insert(v->lc, l, mid, x, isDelete), insert(v->rc, mid + 1, r, x, isDelete)); } } else { if (x < l || x > r) return v; else if (l == r) return new (_curr++) Node(l, r, null, null, v->size - 1, v->sum - x); else { int mid = l + (r - l) / 2; return new (_curr++) Node(l, r, insert(v->lc, l, mid, x, isDelete), insert(v->rc, mid + 1, r, x, isDelete)); } } } void update(Node *v, int x, bool isDelete) { if (!isDelete) { v->size++, v->sum += x; if (v->l == v->r) return; else { int mid = v->l + (v->r - v->l) / 2; if (x <= mid) update(v->lc, x, isDelete); else update(v->rc, x, isDelete); } } else { v->size--, v->sum -= x; if (v->l == v->r) return; else { int mid = v->l + (v->r - v->l) / 2; if (x <= mid) update(v->lc, x, isDelete); else update(v->rc, x, isDelete); } } } */ void build(std::vector<Mission> *a, int n, int l, int r) { _curr = _pool; roots[0] = null; for (int i = 1; i <= n; i++) { std::vector<Mission>::iterator it = a[i].begin(); if (it == a[i].end()) { roots[i] = roots[i - 1]; continue; } else { roots[i] = insert(roots[i - 1], l, r, it->val, it->isDelete); it++; for (; it != a[i].end(); it++) { assert(it != a[i].end()); roots[i] = insert(roots[i], l, r, it->val, it->isDelete); } } } this->l = l, this->r = r; } long long query(int qr, int k) { long long ans = 0, lsize; Node *v = roots[qr]; if (k >= v->size) return v->sum; lsize = v->lc->size; while (v->l != v->r) { if (k <= lsize) { v = v->lc; lsize = v->lc->size; } else { k -= lsize; ans += v->lc->sum; v = v->rc; lsize = v->lc->size; } } if (k) ans += v->l * k; return ans; } } presentTreeByGoldYeInInternationalOlympidInInformaticsHellc; int main() { int m, n; scanf("%d %d", &m, &n); int max = INT_MIN, min = INT_MAX; for (int i = 1; i <= m; i++) { int s, t, val; scanf("%d %d %d", &s, &t, &val); max = std::max(max, val); min = std::min(min, val); a[s].push_back(Mission(val, false)); a[t + 1].push_back(Mission(val, true)); } presentTreeByGoldYeInInternationalOlympidInInformaticsHellc.build(a, n, min, max); long long pre = 1; for (int i = 1; i <= n; i++) { int x, k; long long a, b, c; scanf("%d %lld %lld %lld", &x, &a, &b, &c); k = 1 + (a * pre + b) % c; pre = presentTreeByGoldYeInInternationalOlympidInInformaticsHellc.query(x, k); printf("%lld\n", pre); } return 0; }
|