因为 A,B 是随机的,所以分块维护一下颜色即可,当然用其他方法也可以,验题人表示还可以用一个 set/map 存下所有的相同的段,每次把[l,r]这一段分离出来,然后把这一段里面的相同段修改之后合并成
#include<cassert>
#include<cctype>
#include<cerrno>
#include<cfloat>
#include<ciso646>
#include<climits>
#include<clocale>
#include<cmath>
#include<csetjmp>
#include<csignal>
#include<cstdarg>
#include<cstddef>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<ctime>
#include<bitset>
#include<complex>
#include<deque>
#include<exception>
#include<fstream>
#include<functional>
#include<iomanip>
#include<ios>
#include<iosfwd>
#include<iostream>
#include<istream>
#include<iterator>
#include<limits>
#include<list>
#include<locale>
#include<map>
#include<memory>
#include<new>
#include<numeric>
#include<ostream>
#include<queue>
#include<set>
#include<sstream>
#include<stack>
#include<stdexcept>
#include<streambuf>
#include<string>
#include<typeinfo>
#include<utility>
#include<valarray>
#include<vector>
using namespace std;
#define LL long long
const int maxn = 100000 + 100;
int n, c, q;
LL x, y, a, b, p;
int num[maxn << 2], cnt[maxn];
void push_up(int rt) {
if(num[rt << 1] == num[rt << 1 | 1]) {
num[rt] = num[rt << 1];
return ;
}
num[rt] = 0;
}
void push_down(int rt) {
if(num[rt] != 0) {
num[rt << 1] = num[rt << 1 | 1] = num[rt];
}
}
void build(int l, int r, int rt) {
if(l == r) {
cnt[l] = 0;
num[rt] = 1;
return ;
}
int m = (l + r) >> 1;
build(l, m, rt << 1);
build(m + 1, r, rt << 1 | 1);
push_up(rt);
}
void dfs(int tmp, int l, int r, int rt) {
if(num[rt] != 0) {
cnt[num[rt]] -= (r - l + 1);
num[rt] = tmp;
cnt[tmp] += (r - l + 1);
return ;
}
int m = (l + r) >> 1;
dfs(tmp, l, m, rt << 1);
dfs(tmp, m + 1, r, rt << 1 | 1);
push_up(rt);
}
void update(int L, int R, int tmp, int l, int r, int rt) {
if(L <= l && r <= R) {
if(num[rt] == 0) {
dfs(tmp, l, r, rt);
} else {
cnt[num[rt]] -= (r - l + 1);
num[rt] = tmp;
cnt[tmp] += (r - l + 1);
}
return ;
}
push_down(rt);
int m = (l + r) >> 1;
if(L <= m) {
update(L, R, tmp, l, m, rt << 1);
}
if(R > m) {
update(L, R, tmp, m + 1, r, rt << 1 | 1);
}
push_up(rt);
}
int main() {
ios::sync_with_stdio(false);
while(scanf("%d%d%d", &n, &c, &q) != EOF) {
build(1, n, 1);
cnt[1] = n;
for(int i = 0; i < q; ++i) {
scanf("%lld%lld%lld%lld", &x, &y, &a, &b);
p = cnt[x];
LL l = (a + (p + b) * (p + b)) % n;
LL r = (a + (p * b % n) * (p * b % n) % n) % n;
LL ll = min(l, r) + 1;
LL rr = max(l, r) + 1;
update(ll, rr, y, 1, n, 1);
}
int ans = 0;
for(int i = 1; i <= c; ++i) {
ans = max(ans, cnt[i]);
}
printf("%d\n", ans);
}
return 0;
}