# [2017-2018 ACM-ICPC Latin American Regional Programming Contest] D. Daunting device

### Statement

Given an array of length $1 \leq L \leq 10^5$, initially filled with 1. Define cnt[i] as the number of elements in the array that are equal to i. You should perform no more than $10^5$ operations. Each operation is four integers $P, X, A, B$, and let $M_1 = (A + S^2) \bmod L, M_2 = (A + (S + B)^2) \bmod L$, where S = cnt[P], then assign all elements with indices in $[min(M_1, M_2), max(M_1, M_2)]$ with value X. After all operations performed, output the maximum cnt[i] over all possible values of i.

### Solution

The main idea of the solution is just to simulate the operations in a reasonable time complexity. In fact, various possible solutions exist, including segment tree and square root decomposition. However, the fastest solution (at least theoretically) I know is employing balanced binary search trees.

We use the nodes of the balanced binary search tree to represent contiguous intervals of the array with same values. Additionally, we maintain an array “cnt”, which has the meaning as described in problem statement.

When updating an interval [l, r) to value X, we delete all nodes representing the interval [l, r), updating the array “cnt”. Note that this may split the nodes that across the boundaries of given interval. Finally, we inserting a new node which represents [l, r) with value X, and updating “cnt”.

Each operation can be done in amortized $O(\log L)$ time, since in each operation, only constant number of nodes are inserted, though it is possible that a great number of nodes are deleted. Hence the total time complexity is $O(n \log L)$.

### Source Code

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace std;
using namespace __gnu_pbds;

#ifdef __LOCAL_DEBUG__
# define _debug(fmt, ...) fprintf(stderr, "\033[94m%s: " fmt "\n\033[0m", \
__func__, ##__VA_ARGS__)
#else
# define _debug(...) ((void) 0)
#endif
#define rep(i, n) for (int i=0; i<(n); i++)
#define Rep(i, n) for (int i=1; i<=(n); i++)
#define range(x) (x).begin(), (x).end()
typedef long long LL;
typedef unsigned long long ULL;

int l, c, n;
int cnt;

typedef tree<int, pair<int, int>, less<int>, rb_tree_tag> rbtree;

#define tree shuorgsrh
rbtree tree;

void update(int l, int r, int c) {
rbtree mpart, rpart;

tree.split(l - 1, mpart);
int dif;
if (tree.size() &&
(dif = tree.rbegin()->first + tree.rbegin()->second.first - l) > 0) {
tree.rbegin()->second.first -= dif;
mpart.insert(make_pair(l, make_pair(dif, tree.rbegin()->second.second)));
}
mpart.split(r, rpart);

if (mpart.size() &&
(dif = mpart.rbegin()->first + mpart.rbegin()->second.first - r) > 0) {
mpart.rbegin()->second.first -= dif;
rpart.insert(make_pair(r, make_pair(dif, mpart.rbegin()->second.second)));
}
for (auto& p : mpart) {
cnt[p.second.second] -= p.second.first;
}
cnt[c] += r - l;
tree.insert(make_pair(l, make_pair(r - l, c)));
tree.join(rpart);
}

int main() {
scanf("%d%d%d", &l, &c, &n);
cnt = l;
tree.insert(make_pair(0, make_pair(l, 1)));
rep (i, n) {
int p, x, a, b;
scanf("%d%d%d%d", &p, &x, &a, &b);
int m1 = (a + 1ll * cnt[p] * cnt[p]) % l;
int m2 = (a + 1ll * (cnt[p] + b) * (cnt[p] + b)) % l;
if (m1 > m2) swap(m1, m2);
m2++;
update(m1, m2, x);
}
cout << *max_element(cnt+1, cnt+c+1) << endl;
return 0;
}