本文主要是介绍CodeForces 1555E : Boring Segments 双指针 + 线段树,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
传送门
题意
分析
首先可以确定这个问题是单调的,也就是说我们如果确定了一最大值,那么存在一个 m i d mid mid,最小值大于 m i d mid mid时不合法,小于 m i d mid mid的时候合法
所以,我们可以用双指针求左右边界,线段树去 c h e c k check check是否合法
代码
#pragma GCC optimize(3)
#include <bits/stdc++.h>
#define debug(x) cout<<#x<<":"<<x<<endl;
#define dl(x) printf("%lld\n",x);
#define di(x) printf("%d\n",x);
#define _CRT_SECURE_NO_WARNINGS
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
#define SZ(x) ((int)(x).size())
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> PII;
typedef vector<int> VI;
const int INF = 0x3f3f3f3f;
const int N = 1e6 + 10;
const ll mod = 1000000007;
const double eps = 1e-9;
const double PI = acos(-1);
template<typename T>inline void read(T &a) {char c = getchar(); T x = 0, f = 1; while (!isdigit(c)) {if (c == '-')f = -1; c = getchar();}while (isdigit(c)) {x = (x << 1) + (x << 3) + c - '0'; c = getchar();} a = f * x;
}
int gcd(int a, int b) {return (b > 0) ? gcd(b, a % b) : a;}
struct P {int l, r, w;
} a[N];
struct Node {int l, r;int x, add;
} tr[N * 4];int n, m;bool cmp(P A, P B) {return A.w < B.w;
}void push(int u) {tr[u].x = min(tr[u << 1].x, tr[u << 1 | 1].x);
}void down(int u) {if (tr[u].add != 0) {int &k = tr[u].add;tr[u << 1].x += k;tr[u << 1 | 1].x += k;tr[u << 1].add += k;tr[u << 1 | 1].add += k;k = 0;}
}void build(int u, int l, int r) {tr[u] = {l, r, 0, 0};if (l == r) return;int mid = (l + r) >> 1;build(u << 1, l, mid), build(u << 1 | 1, mid + 1, r);
}void modify(int u, int l, int r, int k) {if (tr[u].l >= l && tr[u].r <= r) {tr[u].x += k, tr[u].add += k;return;}down(u);int mid = (tr[u].l + tr[u].r) >> 1;if (l <= mid) modify(u << 1, l, r, k);if (r > mid) modify(u << 1 | 1, l, r, k);push(u);
}int main() {read(n), read(m);m--;build(1, 1, m);for (int i = 1; i <= n; i++) read(a[i].l), read(a[i].r), read(a[i].w);sort(a + 1, a + 1 + n, cmp);int l = 0, r = 0, ans = INF;while (r <= n) {while (r + 1 <= n && tr[1].x == 0) r++, modify(1, a[r].l, a[r].r - 1, 1);if (!tr[1].x) break;while (l + 1 <= r && tr[1].x > 0) l++, modify(1, a[l].l, a[l].r - 1, -1);ans = min(ans, a[r].w - a[l].w);}di(ans);return 0;
}
这篇关于CodeForces 1555E : Boring Segments 双指针 + 线段树的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!