int ans = f1 - f0; auto it = lower_bound(ilr[i0].begin(), ilr[i0].end(), array<int, 2>{f0 + 1, -1}); if (it != ilr[i0].begin()) f0 = max(f0,(*--it)[1]);
it = lower_bound(ilr[i1].begin(), ilr[i1].end(), array<int, 2>{f1 + 1, -1}); if (it != ilr[i1].begin()) { it--; if (f1 <= (*it)[1]) { f1 = (*it)[0]; } }
if (f0 >= f1) return ans + (i0 != i1); // 跳到一个右端点 保证f0 是右端点 ans++; int idx = lower_bound(segs.begin(), segs.end(), array<int, 2>{f0 + 1, -1}) - segs.begin(); // 未被覆盖到 if (!idx) return-1; idx--; if (f0 > segs[idx][1]) return-1; if (segs[idx][1] >= f1) return ans + 1; if (segs[jump[idx][lg]][1] < f1) return-1; per(k,0,lg+1){ if (segs[jump[idx][k]][1] >= f1) continue; idx = jump[idx][k]; ans += 1 << k; } idx = jump[idx][0]; return ans + 2; }
intmain(){ n = read(); m = read(); q = read(); rep(i,0,m){ int a, b, c; a = read() - 1; b = read(); c = read(); ilr[a].push_back({b, c}); } // 每组内部 排序 与 合并 rep(i,0,n){ sort(ilr[i].begin(), ilr[i].end()); vector<array<int, 2>> temp; for (auto [l, r] : ilr[i]) { if (!temp.empty() && l <= temp.back()[1]) { temp.back()[1] = max(temp.back()[1], r); } else { temp.push_back({l, r}); } } ilr[i] = temp; for (auto s : temp) segs.push_back(s); }
sort(segs.begin(), segs.end());
vector<array<int, 2>> temp; for (auto [l, r] : segs) { if (!temp.empty() && r <= temp.back()[1]) { // continue; } if (!temp.empty() && l == temp.back()[0]) { temp.pop_back(); } temp.push_back({l, r}); } segs = temp;
N = segs.size(); jump = vector<vector<int>>(N, vector<int>(lg + 1));
// jump[段id][pwr] = 段id for (int i = 0, j = 0; i < N; i++) { while (j + 1 < N && segs[j + 1][0] <= segs[i][1]) { j++; } jump[i][0] = j; } rep(j,0,lg){ rep(i,0,N){ jump[i][j + 1] = jump[jump[i][j]][j]; } }
// 跑前缀 <= 前缀 { int cur = 0; rep(i,l,r+1){ cur += p1[i]; if( cur > p[i] - p[l]+1) returnfalse; } } { int cur = 0; per(i,l,r+1){ cur += p0[i]; if( cur > s[i] - s[r]+1) returnfalse; } }
ll Pollard_Rho(ll N){ assert(N!=1); if (N == 4) return2; if (is_prime(N)) return N; if (is_prime_square(N)) returnprime_square(N); while(true){ ll c = randint(1, N - 1); auto f = [=](ll x) { return ((lll)x * x + c) % N; }; ll t = 0, r = 0, p = 1, q; do { for (int i = 0; i < 128; ++i) { // 令固定距离C=128 t = f(t), r = f(f(r)); if (t == r || (q = (lll)p * abs(t - r) % N) == 0) break; // 如果发现环,或者积即将为0,退出 p = q; } ll d = gcd(p, N); if (d > 1) return d; } while (t != r); } }