#include <bits/stdc++.h>
using namespace std;
#define sim template < class c
#define ris return * this
#define dor > debug & operator <<
#define eni(x) sim > typename \
enable_if<sizeof dud<c>(0) x 1, debug&>::type operator<<(c i) {
sim > struct rge { c b, e; };
sim > rge<c> range(c i, c j) { return {i, j}; }
sim > auto dud(c* x) -> decltype(cerr << *x, 0);
sim > char dud(...);
struct debug {
#ifdef LOCAL
~debug() { cerr << endl; }
eni(!=) cerr << boolalpha << i; ris; }
eni(==) ris << range(begin(i), end(i)); }
sim, class b dor(pair < b, c > d) {
ris << "(" << d.first << ", " << d.second << ")";
}
sim dor(rge<c> d) {
*this << "[";
for (c it = d.b; it != d.e; ++it)
*this << ", " + 2 * (it == d.b) << *it;
ris << "]";
}
#else
sim dor(const c&) { ris; }
#endif
};
#define imie(x...) " [" #x ": " << (x) << "] "
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
template <typename A, typename B>
using unordered_map2 = __gnu_pbds::gp_hash_table<A, B>;
using namespace __gnu_pbds;
template <typename T> using ordered_set =
__gnu_pbds::tree<T, __gnu_pbds::null_type, less<T>, __gnu_pbds::rb_tree_tag,
__gnu_pbds::tree_order_statistics_node_update>;
// ordered_set<int> s; s.insert(1); s.insert(2);
// s.order_of_key(1); // Out: 0.
// *s.find_by_order(1); // Out: 2.
using ld = long double;
using ll = long long;
constexpr int mod = 1000 * 1000 * 1000 + 7;
constexpr int odw2 = (mod + 1) / 2;
void OdejmijOd(int& a, int b) { a -= b; if (a < 0) a += mod; }
int Odejmij(int a, int b) { OdejmijOd(a, b); return a; }
void DodajDo(int& a, int b) { a += b; if (a >= mod) a -= mod; }
int Dodaj(int a, int b) { DodajDo(a, b); return a; }
int Mnoz(int a, int b) { return (ll) a * b % mod; }
void MnozDo(int& a, int b) { a = Mnoz(a, b); }
int Pot(int a, ll b) { int res = 1; while (b) { if (b % 2 == 1) MnozDo(res, a); a = Mnoz(a, a); b /= 2; } return res; }
int Odw(int a) { return Pot(a, mod - 2); }
void PodzielDo(int& a, int b) { MnozDo(a, Odw(b)); }
int Podziel(int a, int b) { return Mnoz(a, Odw(b)); }
int Moduluj(ll x) { x %= mod; if (x < 0) x += mod; return x; }
template <typename T> T Maxi(T& a, T b) { return a = max(a, b); }
template <typename T> T Mini(T& a, T b) { return a = min(a, b); }
class Number {
public:
Number() : x_(0), is_big_(false) {}
explicit Number(int x) : x_(x), is_big_(false) {
assert(0 <= x and x < mod);
}
bool is_zero() const {
return x_ == 0 and !is_big_;
}
bool is_big() const {
return is_big_;
}
int raw_value() const {
return x_;
}
int to_numerical() const {
if (is_big_) {
return mod;
}
return x_;
}
bool is_less_than(const Number& n) {
if (is_big_) {
if (n.is_big_) {
// All big numbers are considered equal.
return false;
} else {
return false;
}
} else {
if (n.is_big_) {
return true;
} else {
// Two small numbers.
return x_ < n.x_;
}
}
}
Number& operator+=(const Number& n) {
x_ += n.x_;
if (x_ >= mod) {
x_ -= mod;
is_big_ = true;
}
if (n.is_big_) {
is_big_ = true;
}
return *this;
}
Number operator+(const Number& n) const {
return Number(*this) += n;
}
Number& operator*=(const Number& n) {
if (is_zero() or n.is_zero()) {
x_ = 0;
is_big_ = false;
return *this;
}
const ll tmp = static_cast<ll>(x_) * n.x_;
if (tmp >= mod or n.is_big_) {
is_big_ = true;
}
x_ = tmp % mod;
return *this;
}
Number& operator*(const Number& n) const {
return Number(*this) *= n;
}
private:
int x_;
bool is_big_;
};
debug& operator<<(debug& deb, const Number& n) {
#ifdef LOCAL
deb << n.raw_value();
if (n.is_big()) {
deb << "*";
}
#endif
return deb;
}
class Splay {
public:
Splay(Number n) : ptr{nullptr, nullptr, nullptr}, value(n), mul(1), sum(0) {}
static Splay* join(Splay* left, Splay* right) {
assert(left == nullptr or left->ptr[2] == nullptr);
assert(right == nullptr or right->ptr[2] == nullptr);
if (left == nullptr) {
return right;
}
if (right == nullptr) {
return left;
}
left->touch();
while (left->ptr[1] != nullptr) {
left = left->ptr[1];
left->touch();
}
left->splay();
assert(left->ptr[1] == nullptr);
left->ptr[1] = right;
right->ptr[2] = left;
return left;
}
static pair<Splay*, Splay*> split_before_number(Splay* tree, Number n) {
if (tree == nullptr) {
return {nullptr, nullptr};
}
assert(tree->ptr[2] == nullptr);
while (true) {
tree->touch();
if (tree->value.is_less_than(n)) {
// Go right.
if (tree->ptr[1] == nullptr) {
tree->splay();
Splay* right = tree->ptr[1];
tree->ptr[1] = nullptr;
if (right != nullptr) {
right->ptr[2] = nullptr;
}
return {tree, right};
}
tree = tree->ptr[1];
} else {
// Go left.
if (tree->ptr[0] == nullptr) {
tree->splay();
Splay* left = tree->ptr[0];
tree->ptr[0] = nullptr;
if (left != nullptr) {
left->ptr[2] = nullptr;
}
return {left, tree};
}
tree = tree->ptr[0];
}
}
}
void bring_to_top() {
touch_from_bottom();
splay();
}
Splay* remove_root() {
touch();
assert(ptr[2] == nullptr);
Splay* left = ptr[0];
Splay* right = ptr[1];
if (left != nullptr) {
left->ptr[2] = nullptr;
}
if (right != nullptr) {
right->ptr[2] = nullptr;
}
ptr[0] = nullptr;
ptr[1] = nullptr;
return join(left, right);
}
void apply_sum(Number a) {
assert(ptr[2] == nullptr);
touch();
sum += a;
}
void apply_mul(Number b) {
assert(ptr[2] == nullptr);
touch();
mul *= b;
}
Number get_value() {
touch();
return value;
}
static void print(debug& deb, Splay* tree) {
#ifdef LOCAL
deb << "Tree {\n";
if (tree != nullptr) {
assert(tree->ptr[2] == nullptr);
tree->print(deb, 1);
}
deb << "}\n";
#endif
}
private:
void print(debug& deb, int indent) {
#ifdef LOCAL
if (ptr[0] != nullptr) ptr[0]->print(deb, indent + 1);
for (int i = 0; i < indent; i++) {
deb << " ";
}
deb << "(" << value << " * " << mul << " + " << sum << ")\n";
if (ptr[1] != nullptr) ptr[1]->print(deb, indent + 1);
#endif
}
int my_side() {
// 0 - left
// 1 - right
// 2 - root
Splay* p = ptr[2];
if (p == nullptr) return 2;
if (p->ptr[0] == this) return 0;
if (p->ptr[1] == this) return 1;
assert(false);
}
void rotate() {
const int ms = my_side();
Splay* p = ptr[2];
const int ps = p->my_side();
Splay* q = p->ptr[2];
Splay* mid = ptr[ms ^ 1];
ptr[2] = q;
if (q != nullptr) {
q->ptr[ps] = this;
}
p->ptr[ms] = mid;
if (mid != nullptr) {
mid->ptr[2] = p;
}
p->ptr[2] = this;
ptr[ms ^ 1] = p;
}
void touch() {
value *= mul;
value += sum;
for (int i = 0; i < 2; i++) {
if (ptr[i] != nullptr) {
ptr[i]->mul *= mul;
ptr[i]->sum *= mul;
ptr[i]->sum += sum;
}
}
mul = Number(1);
sum = Number(0);
}
void touch_from_bottom() {
static vector<Splay*> stack;
stack.push_back(this);
while (stack.back()->ptr[2] != nullptr) {
stack.push_back(stack.back()->ptr[2]);
}
while (!stack.empty()) {
stack.back()->touch();
stack.pop_back();
}
}
void splay() {
while (ptr[2] != nullptr) {
Splay* p = ptr[2];
Splay* q = p->ptr[2];
const int ms = my_side();
const int ps = p->my_side();
if (q == nullptr) {
rotate();
} else if (ms == ps) {
p->rotate();
rotate();
} else {
rotate();
rotate();
}
}
}
// Left, right, parent.
Splay* ptr[3];
// Value in this node.
Number value;
// All values in the subtree should be changed from x to x*mul+sum.
Number mul;
Number sum;
};
unordered_map<int, Splay*> splays;
class DataStructure {
public:
DataStructure() : root(nullptr) {}
void Add(int id, Number x) {
debug() << "Add(" imie(id) imie(x) ")";
Splay* node = new Splay(x);
splays[id] = node;
auto [left, right] = Splay::split_before_number(root, x);
Splay::print(debug() << "left ", left);
Splay::print(debug() << "right ", right);
Splay* mid = Splay::join(node, right);
Splay::print(debug() << "mid ", mid);
root = Splay::join(left, mid);
Splay::print(debug() << "root", root);
}
Number GetAndRemove(int id) {
debug() << "GetAndRemove(" imie(id) ")";
auto it = splays.find(id);
assert(it != splays.end());
assert(it->first == id);
Splay* node = it->second;
splays.erase(it);
node->bring_to_top();
root = node->remove_root();
const Number result = node->get_value();
delete node;
debug() << imie(result);
return result;
}
DataStructure Split(int x) {
debug() << "Split(" imie(x) ")";
auto [left, right] = Splay::split_before_number(root, Number(x));
root = left;
DataStructure other;
other.root = right;
return other;
}
void Join(DataStructure& right) {
debug() << "Join()";
root = Splay::join(root, right.root);
right.root = nullptr;
}
void ApplySum(Number a) {
debug() << "ApplySum(" imie(a) ")";
if (root != nullptr) {
root->apply_sum(a);
}
}
void ApplyMul(Number b) {
debug() << "ApplyMul(" imie(b) ")";
if (root != nullptr) {
root->apply_mul(b);
}
}
friend debug& operator<<(debug& deb, const DataStructure& ds) {
#ifdef LOCAL
Splay::print(deb, ds.root);
#endif
return deb;
}
private:
Splay* root;
};
struct Event {
int a, b;
};
struct Query {
Number x;
int l, r;
Number answer;
};
class Problem {
public:
void Write() {
for (const Query& q : queries_) {
cout << q.answer.raw_value() << "\n";
}
}
void Read() {
int n, q;
cin >> n >> q;
assert(1 <= n);
assert(1 <= q);
events_.resize(n);
queries_.resize(q);
for (Event& event : events_) {
cin >> event.a >> event.b;
assert(0 <= event.a and event.a < mod);
assert(1 <= event.b and event.b < mod);
}
for (Query& query : queries_) {
int x;
cin >> x >> query.l >> query.r;
query.x = Number(x);
assert(0 <= x and x < mod);
assert(0 <= query.l and query.l < query.r and query.r <= n);
}
}
void Solve() {
const int n = (int) events_.size();
const int q = (int) queries_.size();
vector<vector<int>> begs(n);
vector<vector<int>> ends(n);
for (int i = 0; i < q; i++) {
begs[queries_[i].l].push_back(i);
ends[queries_[i].r - 1].push_back(i);
}
DataStructure data_structure;
for (int i = 0; i < n; i++) {
debug() << imie(i) imie(data_structure);
for (int id : begs[i]) {
data_structure.Add(id, queries_[id].x);
}
debug() << "AFTER ADD: " imie(i) imie(data_structure);
const int a = events_[i].a;
const int b = events_[i].b;
assert(1 <= b);
if (b == 1) {
data_structure.ApplySum(Number(a));
} else {
assert(b >= 2);
const int alpha = (a + b - 2) / (b - 1);
auto right = data_structure.Split(alpha);
debug() << "AFTER SPLIT:" imie(data_structure) imie(right);
data_structure.ApplySum(Number(a));
right.ApplyMul(Number(b));
data_structure.Join(right);
}
debug() << "AFTER MID: " imie(i) imie(data_structure);
for (int id : ends[i]) {
queries_[id].answer = data_structure.GetAndRemove(id);
}
debug() << "AFTER END: " imie(i) imie(data_structure);
}
}
private:
vector<Event> events_;
vector<Query> queries_;
};
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
Problem problem;
problem.Read();
problem.Solve();
problem.Write();
return 0;
}
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 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 | #include <bits/stdc++.h> using namespace std; #define sim template < class c #define ris return * this #define dor > debug & operator << #define eni(x) sim > typename \ enable_if<sizeof dud<c>(0) x 1, debug&>::type operator<<(c i) { sim > struct rge { c b, e; }; sim > rge<c> range(c i, c j) { return {i, j}; } sim > auto dud(c* x) -> decltype(cerr << *x, 0); sim > char dud(...); struct debug { #ifdef LOCAL ~debug() { cerr << endl; } eni(!=) cerr << boolalpha << i; ris; } eni(==) ris << range(begin(i), end(i)); } sim, class b dor(pair < b, c > d) { ris << "(" << d.first << ", " << d.second << ")"; } sim dor(rge<c> d) { *this << "["; for (c it = d.b; it != d.e; ++it) *this << ", " + 2 * (it == d.b) << *it; ris << "]"; } #else sim dor(const c&) { ris; } #endif }; #define imie(x...) " [" #x ": " << (x) << "] " #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> template <typename A, typename B> using unordered_map2 = __gnu_pbds::gp_hash_table<A, B>; using namespace __gnu_pbds; template <typename T> using ordered_set = __gnu_pbds::tree<T, __gnu_pbds::null_type, less<T>, __gnu_pbds::rb_tree_tag, __gnu_pbds::tree_order_statistics_node_update>; // ordered_set<int> s; s.insert(1); s.insert(2); // s.order_of_key(1); // Out: 0. // *s.find_by_order(1); // Out: 2. using ld = long double; using ll = long long; constexpr int mod = 1000 * 1000 * 1000 + 7; constexpr int odw2 = (mod + 1) / 2; void OdejmijOd(int& a, int b) { a -= b; if (a < 0) a += mod; } int Odejmij(int a, int b) { OdejmijOd(a, b); return a; } void DodajDo(int& a, int b) { a += b; if (a >= mod) a -= mod; } int Dodaj(int a, int b) { DodajDo(a, b); return a; } int Mnoz(int a, int b) { return (ll) a * b % mod; } void MnozDo(int& a, int b) { a = Mnoz(a, b); } int Pot(int a, ll b) { int res = 1; while (b) { if (b % 2 == 1) MnozDo(res, a); a = Mnoz(a, a); b /= 2; } return res; } int Odw(int a) { return Pot(a, mod - 2); } void PodzielDo(int& a, int b) { MnozDo(a, Odw(b)); } int Podziel(int a, int b) { return Mnoz(a, Odw(b)); } int Moduluj(ll x) { x %= mod; if (x < 0) x += mod; return x; } template <typename T> T Maxi(T& a, T b) { return a = max(a, b); } template <typename T> T Mini(T& a, T b) { return a = min(a, b); } class Number { public: Number() : x_(0), is_big_(false) {} explicit Number(int x) : x_(x), is_big_(false) { assert(0 <= x and x < mod); } bool is_zero() const { return x_ == 0 and !is_big_; } bool is_big() const { return is_big_; } int raw_value() const { return x_; } int to_numerical() const { if (is_big_) { return mod; } return x_; } bool is_less_than(const Number& n) { if (is_big_) { if (n.is_big_) { // All big numbers are considered equal. return false; } else { return false; } } else { if (n.is_big_) { return true; } else { // Two small numbers. return x_ < n.x_; } } } Number& operator+=(const Number& n) { x_ += n.x_; if (x_ >= mod) { x_ -= mod; is_big_ = true; } if (n.is_big_) { is_big_ = true; } return *this; } Number operator+(const Number& n) const { return Number(*this) += n; } Number& operator*=(const Number& n) { if (is_zero() or n.is_zero()) { x_ = 0; is_big_ = false; return *this; } const ll tmp = static_cast<ll>(x_) * n.x_; if (tmp >= mod or n.is_big_) { is_big_ = true; } x_ = tmp % mod; return *this; } Number& operator*(const Number& n) const { return Number(*this) *= n; } private: int x_; bool is_big_; }; debug& operator<<(debug& deb, const Number& n) { #ifdef LOCAL deb << n.raw_value(); if (n.is_big()) { deb << "*"; } #endif return deb; } class Splay { public: Splay(Number n) : ptr{nullptr, nullptr, nullptr}, value(n), mul(1), sum(0) {} static Splay* join(Splay* left, Splay* right) { assert(left == nullptr or left->ptr[2] == nullptr); assert(right == nullptr or right->ptr[2] == nullptr); if (left == nullptr) { return right; } if (right == nullptr) { return left; } left->touch(); while (left->ptr[1] != nullptr) { left = left->ptr[1]; left->touch(); } left->splay(); assert(left->ptr[1] == nullptr); left->ptr[1] = right; right->ptr[2] = left; return left; } static pair<Splay*, Splay*> split_before_number(Splay* tree, Number n) { if (tree == nullptr) { return {nullptr, nullptr}; } assert(tree->ptr[2] == nullptr); while (true) { tree->touch(); if (tree->value.is_less_than(n)) { // Go right. if (tree->ptr[1] == nullptr) { tree->splay(); Splay* right = tree->ptr[1]; tree->ptr[1] = nullptr; if (right != nullptr) { right->ptr[2] = nullptr; } return {tree, right}; } tree = tree->ptr[1]; } else { // Go left. if (tree->ptr[0] == nullptr) { tree->splay(); Splay* left = tree->ptr[0]; tree->ptr[0] = nullptr; if (left != nullptr) { left->ptr[2] = nullptr; } return {left, tree}; } tree = tree->ptr[0]; } } } void bring_to_top() { touch_from_bottom(); splay(); } Splay* remove_root() { touch(); assert(ptr[2] == nullptr); Splay* left = ptr[0]; Splay* right = ptr[1]; if (left != nullptr) { left->ptr[2] = nullptr; } if (right != nullptr) { right->ptr[2] = nullptr; } ptr[0] = nullptr; ptr[1] = nullptr; return join(left, right); } void apply_sum(Number a) { assert(ptr[2] == nullptr); touch(); sum += a; } void apply_mul(Number b) { assert(ptr[2] == nullptr); touch(); mul *= b; } Number get_value() { touch(); return value; } static void print(debug& deb, Splay* tree) { #ifdef LOCAL deb << "Tree {\n"; if (tree != nullptr) { assert(tree->ptr[2] == nullptr); tree->print(deb, 1); } deb << "}\n"; #endif } private: void print(debug& deb, int indent) { #ifdef LOCAL if (ptr[0] != nullptr) ptr[0]->print(deb, indent + 1); for (int i = 0; i < indent; i++) { deb << " "; } deb << "(" << value << " * " << mul << " + " << sum << ")\n"; if (ptr[1] != nullptr) ptr[1]->print(deb, indent + 1); #endif } int my_side() { // 0 - left // 1 - right // 2 - root Splay* p = ptr[2]; if (p == nullptr) return 2; if (p->ptr[0] == this) return 0; if (p->ptr[1] == this) return 1; assert(false); } void rotate() { const int ms = my_side(); Splay* p = ptr[2]; const int ps = p->my_side(); Splay* q = p->ptr[2]; Splay* mid = ptr[ms ^ 1]; ptr[2] = q; if (q != nullptr) { q->ptr[ps] = this; } p->ptr[ms] = mid; if (mid != nullptr) { mid->ptr[2] = p; } p->ptr[2] = this; ptr[ms ^ 1] = p; } void touch() { value *= mul; value += sum; for (int i = 0; i < 2; i++) { if (ptr[i] != nullptr) { ptr[i]->mul *= mul; ptr[i]->sum *= mul; ptr[i]->sum += sum; } } mul = Number(1); sum = Number(0); } void touch_from_bottom() { static vector<Splay*> stack; stack.push_back(this); while (stack.back()->ptr[2] != nullptr) { stack.push_back(stack.back()->ptr[2]); } while (!stack.empty()) { stack.back()->touch(); stack.pop_back(); } } void splay() { while (ptr[2] != nullptr) { Splay* p = ptr[2]; Splay* q = p->ptr[2]; const int ms = my_side(); const int ps = p->my_side(); if (q == nullptr) { rotate(); } else if (ms == ps) { p->rotate(); rotate(); } else { rotate(); rotate(); } } } // Left, right, parent. Splay* ptr[3]; // Value in this node. Number value; // All values in the subtree should be changed from x to x*mul+sum. Number mul; Number sum; }; unordered_map<int, Splay*> splays; class DataStructure { public: DataStructure() : root(nullptr) {} void Add(int id, Number x) { debug() << "Add(" imie(id) imie(x) ")"; Splay* node = new Splay(x); splays[id] = node; auto [left, right] = Splay::split_before_number(root, x); Splay::print(debug() << "left ", left); Splay::print(debug() << "right ", right); Splay* mid = Splay::join(node, right); Splay::print(debug() << "mid ", mid); root = Splay::join(left, mid); Splay::print(debug() << "root", root); } Number GetAndRemove(int id) { debug() << "GetAndRemove(" imie(id) ")"; auto it = splays.find(id); assert(it != splays.end()); assert(it->first == id); Splay* node = it->second; splays.erase(it); node->bring_to_top(); root = node->remove_root(); const Number result = node->get_value(); delete node; debug() << imie(result); return result; } DataStructure Split(int x) { debug() << "Split(" imie(x) ")"; auto [left, right] = Splay::split_before_number(root, Number(x)); root = left; DataStructure other; other.root = right; return other; } void Join(DataStructure& right) { debug() << "Join()"; root = Splay::join(root, right.root); right.root = nullptr; } void ApplySum(Number a) { debug() << "ApplySum(" imie(a) ")"; if (root != nullptr) { root->apply_sum(a); } } void ApplyMul(Number b) { debug() << "ApplyMul(" imie(b) ")"; if (root != nullptr) { root->apply_mul(b); } } friend debug& operator<<(debug& deb, const DataStructure& ds) { #ifdef LOCAL Splay::print(deb, ds.root); #endif return deb; } private: Splay* root; }; struct Event { int a, b; }; struct Query { Number x; int l, r; Number answer; }; class Problem { public: void Write() { for (const Query& q : queries_) { cout << q.answer.raw_value() << "\n"; } } void Read() { int n, q; cin >> n >> q; assert(1 <= n); assert(1 <= q); events_.resize(n); queries_.resize(q); for (Event& event : events_) { cin >> event.a >> event.b; assert(0 <= event.a and event.a < mod); assert(1 <= event.b and event.b < mod); } for (Query& query : queries_) { int x; cin >> x >> query.l >> query.r; query.x = Number(x); assert(0 <= x and x < mod); assert(0 <= query.l and query.l < query.r and query.r <= n); } } void Solve() { const int n = (int) events_.size(); const int q = (int) queries_.size(); vector<vector<int>> begs(n); vector<vector<int>> ends(n); for (int i = 0; i < q; i++) { begs[queries_[i].l].push_back(i); ends[queries_[i].r - 1].push_back(i); } DataStructure data_structure; for (int i = 0; i < n; i++) { debug() << imie(i) imie(data_structure); for (int id : begs[i]) { data_structure.Add(id, queries_[id].x); } debug() << "AFTER ADD: " imie(i) imie(data_structure); const int a = events_[i].a; const int b = events_[i].b; assert(1 <= b); if (b == 1) { data_structure.ApplySum(Number(a)); } else { assert(b >= 2); const int alpha = (a + b - 2) / (b - 1); auto right = data_structure.Split(alpha); debug() << "AFTER SPLIT:" imie(data_structure) imie(right); data_structure.ApplySum(Number(a)); right.ApplyMul(Number(b)); data_structure.Join(right); } debug() << "AFTER MID: " imie(i) imie(data_structure); for (int id : ends[i]) { queries_[id].answer = data_structure.GetAndRemove(id); } debug() << "AFTER END: " imie(i) imie(data_structure); } } private: vector<Event> events_; vector<Query> queries_; }; int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); Problem problem; problem.Read(); problem.Solve(); problem.Write(); return 0; } |
English