题解:CF1034D Intervals of Intervals
UKE_Automation · · 题解
CF1034D Intervals of Intervals
下文中大写字母
首先考虑如何求出一段区间
也就是说当我们插入一个线段
然后考虑原题,我们要求出前
先把珂朵莉的 vector 记录下每次修改的位置和值即可。然后去掉二分的
最后把树状数组的
最后要求出比第
那么我们的复杂度就可以降到
#include <bits/stdc++.h>
#define il inline
#define int long long
using namespace std;
const int Maxn = 3e5 + 5;
const int Inf = 1e9;
const int Mod = 1e9 + 7;
il int Add(int x, int y) {return x + y >= Mod ? x + y - Mod: x + y;} il void pls(int &x, int y) {x = Add(x, y);}
il int Del(int x, int y) {return x - y < 0 ? x - y + Mod : x - y;} il void sub(int &x, int y) {x = Del(x, y);}
il int qpow(int a, int b, int P = Mod) {int res = 1; for(; b; a = 1ll * a * a % P, b >>= 1) if(b & 1) res = 1ll * res * a % P; return res;}
il int Inv(int a) {return qpow(a, Mod - 2);}
template <typename T> il void chkmax(T &x, T y) {x = (x >= y ? x : y);}
template <typename T> il void chkmin(T &x, T y) {x = (x <= y ? x : y);}
template <typename T>
il void read(T &x) {
x = 0; char ch = getchar(); bool flg = 0;
for(; ch < '0' || ch > '9'; ch = getchar()) flg = (ch == '-');
for(; ch >= '0' && ch <= '9'; ch = getchar()) x = (x << 1) + (x << 3) + (ch ^ 48);
flg ? x = -x : 0;
}
template <typename T>
il void write(T x, bool typ = 1) {
static short Stk[50], Top = 0;
x < 0 ? putchar('-'), x = -x : 0;
do Stk[++Top] = x % 10, x /= 10; while(x);
while(Top) putchar(Stk[Top--] | 48);
typ ? putchar('\n') : putchar(' ');
}
il void IOS() {ios::sync_with_stdio(0); cin.tie(0), cout.tie(0);}
il void File() {freopen("in.txt", "r", stdin); freopen("out.txt", "w", stdout);}
bool Beg;
int n, k, l[Maxn], r[Maxn];
#define pii pair<int, int>
#define mk make_pair
#define fi first
#define se second
vector <pii> V[Maxn];
namespace ODT {
struct Seg {
int l, r, id;
bool operator < (const Seg &t) const {return l < t.l;}
};
set <Seg> s;
void init() {
s.insert({1, Inf, 0});
}
auto split(int x) {
auto it = s.lower_bound({x, 0, 0});
if(it -> l == x) return it;
it--;
int l = it -> l, r = it -> r, id = it -> id;
s.erase(it);
s.insert({l, x - 1, id});
return s.insert({x, r, id}).first;
}
void assign(int l, int r, int id) {
auto itr = split(r + 1), itl = split(l);
for(auto it = itl; it != itr; it++) {
int tl = it -> l, tr = it -> r, ti = it -> id;
if(ti == 0) continue;
V[id].push_back(mk(1, -(tr - tl + 1)));
V[id].push_back(mk(ti + 1, tr - tl + 1));
}
s.erase(itl, itr); s.insert({l, r, id});
V[id].push_back(mk(1, r - l + 1));
V[id].push_back(mk(id + 1, -(r - l + 1)));
}
}
int w[Maxn];
int check(int x) {
for(int i = 0; i <= n; i++) w[i] = 0;
int l = 1, r = 1, sum = 0, ans = 0;
for(; r <= n; r++) {
for(auto p : V[r]) {
if(p.fi <= l) sum += p.se;
else w[p.fi] += p.se;
}
while(sum + w[l + 1] >= x && l < r) l++, sum += w[l];
if(sum >= x) ans += l;
}
return ans >= k;
}
bool End;
il void Usd() {cerr << (&Beg - &End) / 1024.0 / 1024.0 << "MB " << (double)clock() * 1000.0 / CLOCKS_PER_SEC << "ms\n"; }
signed main() {
read(n), read(k);
for(int i = 1; i <= n; i++) read(l[i]), read(r[i]), r[i]--;
ODT::init();
for(int i = 1; i <= n; i++) ODT::assign(l[i], r[i], i);
int l = 0, r = 1e9, ans = 0;
while(l <= r) {
int mid = (l + r) >> 1;
if(check(mid)) ans = mid, l = mid + 1;
else r = mid - 1;
}
for(int i = 0; i <= n; i++) w[i] = 0;
l = 1, r = 1;
int sum = 0, res = 0, ret = 0, cnt = 0;
for(; r <= n; r++) {
for(auto p : V[r]) {
if(p.fi <= l) sum += p.se, ret += p.se * (l - p.fi + 1);
else w[p.fi] += p.se;
}
if(sum <= ans) continue;
while(sum + w[l + 1] > ans && l < r) l++, sum += w[l], ret += sum;
res += ret;
if(sum > ans) cnt += l;
}
res += (k - cnt) * ans;
write(res);
Usd();
return 0;
}