#include <iostream>
#include <vector>
#include <algorithm>
#include <random>
#define pii pair<int, int>
#define For(i, l, r) for (int i=l;(l<=r?i<=r:i>=r);(l<=r?i++:i--))
//#define DEBUG
#ifdef DEBUG
auto operator<<(auto &o,auto p)->decltype(p.first,o){return o<<'('<<p.first<<", "<<p.second<<')';}
auto operator<<(auto &o,auto x)->decltype(x.end(),o){o<<'{';int i=2;for(auto &e:x)o<<(", ")+i<<e,i=0;return o<<'}';}
#define LOG(X...)cerr<<"["#X"]: ",[](auto...$){((cerr<<$<<"; "),...)<<endl;}(X);
#else
#define LOG(x...)(void)0
#endif
using namespace std;
const int mod = 1'000'000'007;
mt19937 rng(42);
typedef struct Node * Tree;
struct Node {
int val, weight, mul_lazy, add_lazy;
double val_real, mul_real, add_real;
Tree p, l, r;
Node(int v) : val(v), weight(rng()), mul_lazy(1), add_lazy(0), val_real(v), mul_real(1), add_real(0), p(NULL), l(NULL), r(NULL) {}
static void update_lazy(Tree t, long long mul, long long add, double rmul, double radd){
if (!t)
return;
t->add_lazy = (mul * t->add_lazy) % mod;
t->mul_lazy = (mul * t->mul_lazy) % mod;
t->add_lazy = (add + t->add_lazy) % mod;
t->add_real *= rmul;
t->mul_real *= rmul;
t->add_real += radd;
}
static void update(Tree t){
if (!t)
return;
if (t->l)
t->l->p = t;
if (t->r)
t->r->p = t;
}
static void push(Tree t){
if (!t)
return;
long long new_val = (long long)t->val * t->mul_lazy + t->add_lazy;
t->val = new_val % mod;
t->val_real = t->val_real * t->mul_real + t->add_real;
update_lazy(t->l, t->mul_lazy, t->add_lazy, t->mul_real, t->add_real);
update_lazy(t->r, t->mul_lazy, t->add_lazy, t->mul_real, t->add_real);
t->mul_real = t->mul_lazy = 1;
t->add_real = t->add_lazy = 0;
}
static void sync(Tree t){
if (!t)
return;
if (t->p)
sync(t->p);
push(t);
}
static void split(Tree t, int key, Tree &l, Tree &r){
if (!t){
l = r = NULL;
return;
}
push(t);
if (t->val_real >= mod || t->val > key)
split(t->l, key, l, t->l), r = t;
else
split(t->r, key, t->r, r), l = t;
update(t);
}
static void insert(Tree & t, Tree it){
push(t);
if(!t)
t = it;
else if (!it)
return;
else if (it->weight > t->weight)
split(t, it->val, it->l, it->r), t = it;
else
insert((t->val_real < mod && t->val <= it->val? t->r: t->l), it);
update(t);
}
static void merge(Tree &t, Tree l, Tree r){
if (!l){
t = r;
return;
}
else if (!r){
t = l;
return;
}
push(l);
push(r);
if (l->weight > r->weight)
merge(l->r, l->r, r), t = l;
else
merge(r->l, l, r->l), t = r;
update(t);
}
static void step(Tree &t, int a, int b){
if (b == 1){
update_lazy(t, 1, a, 1, a);
return;
}
int split_key = a / (b - 1);
Tree l, r;
split(t, split_key, l, r);
update_lazy(l, 1, a, 1, a);
update_lazy(r, b, 0, b, 0);
merge(t, l, r);
}
static int find(Tree ptr){
sync(ptr);
return ptr->val;
}
};
void solve(){
int n, q;
cin >> n >> q;
vector <int> a(n), b(n), x(q), l(q), r(q), res(q);
vector<pair<pair<int, bool>, pair<int, int>>> v(2 * q);
for (int i = 0; i < n; i++)
cin >> a[i] >> b[i];
for (int i = 0; i < q; i++){
cin >> x[i] >> l[i] >> r[i];
v[2 * i] = {{l[i] - 1, 1}, {x[i], i}};
v[2 * i + 1] = {{r[i] - 1, 0}, {x[i], i}};
}
sort(v.begin(), v.end());
Tree root = NULL;
int id = 0;
vector<Tree> ptrs(q);
while (id < 2 * q && v[id].first.first < 0){
auto el = v[id++];
ptrs[el.second.second] = new Node(el.second.first);
Node::insert(root, ptrs[el.second.second]);
}
for (int i = 0; i < n; i++){
Node::step(root, a[i], b[i]);
while (id < 2 * q && v[id].first.first == i){
auto el = v[id++];
if (el.first.second){
ptrs[el.second.second] = new Node(el.second.first);
Node::insert(root, ptrs[el.second.second]);
}
else {
res[el.second.second] = Node::find(ptrs[el.second.second]);
}
}
}
for (int i = 0; i < q; i++)
delete ptrs[i];
for (int i = 0; i < q; i++)
cout << res[i] << '\n';
}
signed main(){
cin.tie(0)->sync_with_stdio(0);
solve();
}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 | #include <iostream> #include <vector> #include <algorithm> #include <random> #define pii pair<int, int> #define For(i, l, r) for (int i=l;(l<=r?i<=r:i>=r);(l<=r?i++:i--)) //#define DEBUG #ifdef DEBUG auto operator<<(auto &o,auto p)->decltype(p.first,o){return o<<'('<<p.first<<", "<<p.second<<')';} auto operator<<(auto &o,auto x)->decltype(x.end(),o){o<<'{';int i=2;for(auto &e:x)o<<(", ")+i<<e,i=0;return o<<'}';} #define LOG(X...)cerr<<"["#X"]: ",[](auto...$){((cerr<<$<<"; "),...)<<endl;}(X); #else #define LOG(x...)(void)0 #endif using namespace std; const int mod = 1'000'000'007; mt19937 rng(42); typedef struct Node * Tree; struct Node { int val, weight, mul_lazy, add_lazy; double val_real, mul_real, add_real; Tree p, l, r; Node(int v) : val(v), weight(rng()), mul_lazy(1), add_lazy(0), val_real(v), mul_real(1), add_real(0), p(NULL), l(NULL), r(NULL) {} static void update_lazy(Tree t, long long mul, long long add, double rmul, double radd){ if (!t) return; t->add_lazy = (mul * t->add_lazy) % mod; t->mul_lazy = (mul * t->mul_lazy) % mod; t->add_lazy = (add + t->add_lazy) % mod; t->add_real *= rmul; t->mul_real *= rmul; t->add_real += radd; } static void update(Tree t){ if (!t) return; if (t->l) t->l->p = t; if (t->r) t->r->p = t; } static void push(Tree t){ if (!t) return; long long new_val = (long long)t->val * t->mul_lazy + t->add_lazy; t->val = new_val % mod; t->val_real = t->val_real * t->mul_real + t->add_real; update_lazy(t->l, t->mul_lazy, t->add_lazy, t->mul_real, t->add_real); update_lazy(t->r, t->mul_lazy, t->add_lazy, t->mul_real, t->add_real); t->mul_real = t->mul_lazy = 1; t->add_real = t->add_lazy = 0; } static void sync(Tree t){ if (!t) return; if (t->p) sync(t->p); push(t); } static void split(Tree t, int key, Tree &l, Tree &r){ if (!t){ l = r = NULL; return; } push(t); if (t->val_real >= mod || t->val > key) split(t->l, key, l, t->l), r = t; else split(t->r, key, t->r, r), l = t; update(t); } static void insert(Tree & t, Tree it){ push(t); if(!t) t = it; else if (!it) return; else if (it->weight > t->weight) split(t, it->val, it->l, it->r), t = it; else insert((t->val_real < mod && t->val <= it->val? t->r: t->l), it); update(t); } static void merge(Tree &t, Tree l, Tree r){ if (!l){ t = r; return; } else if (!r){ t = l; return; } push(l); push(r); if (l->weight > r->weight) merge(l->r, l->r, r), t = l; else merge(r->l, l, r->l), t = r; update(t); } static void step(Tree &t, int a, int b){ if (b == 1){ update_lazy(t, 1, a, 1, a); return; } int split_key = a / (b - 1); Tree l, r; split(t, split_key, l, r); update_lazy(l, 1, a, 1, a); update_lazy(r, b, 0, b, 0); merge(t, l, r); } static int find(Tree ptr){ sync(ptr); return ptr->val; } }; void solve(){ int n, q; cin >> n >> q; vector <int> a(n), b(n), x(q), l(q), r(q), res(q); vector<pair<pair<int, bool>, pair<int, int>>> v(2 * q); for (int i = 0; i < n; i++) cin >> a[i] >> b[i]; for (int i = 0; i < q; i++){ cin >> x[i] >> l[i] >> r[i]; v[2 * i] = {{l[i] - 1, 1}, {x[i], i}}; v[2 * i + 1] = {{r[i] - 1, 0}, {x[i], i}}; } sort(v.begin(), v.end()); Tree root = NULL; int id = 0; vector<Tree> ptrs(q); while (id < 2 * q && v[id].first.first < 0){ auto el = v[id++]; ptrs[el.second.second] = new Node(el.second.first); Node::insert(root, ptrs[el.second.second]); } for (int i = 0; i < n; i++){ Node::step(root, a[i], b[i]); while (id < 2 * q && v[id].first.first == i){ auto el = v[id++]; if (el.first.second){ ptrs[el.second.second] = new Node(el.second.first); Node::insert(root, ptrs[el.second.second]); } else { res[el.second.second] = Node::find(ptrs[el.second.second]); } } } for (int i = 0; i < q; i++) delete ptrs[i]; for (int i = 0; i < q; i++) cout << res[i] << '\n'; } signed main(){ cin.tie(0)->sync_with_stdio(0); solve(); } |
English