P4644 [USACO05DEC]Cleaning Shifts S(线段树优化dp) , dp14

https://www.luogu.com.cn/problem/P4644
#include<bits/stdc++.h>
#include<unordered_map>
#include<array>
#define ll long long
#define ull unsigned long long
#define all(a) a.begin(),a.end()
#define IOS ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
using namespace std;

const int inf = 0x3f3f3f3f;
const ll INF = 0x3f3f3f3f3f3f3f3f;
const double eps = 1e-8;
const ll mod = 998244353;
const int N = 1e5 + 5;

#define int ll
int n, M, E;
struct node {
	ll l, r, w;
	bool operator < (node a)
	{
		if (r != a.r)
			return r > a.r;
		return l < a.l;
	}
}a[N];

struct SegTree {
	struct node {
		int l, r, mx, lazy;
	}tree[N << 2];
	void build(int l, int r, int p)
	{
		tree[p].l = l, tree[p].r = r;
		tree[p].mx = INF;
		if (l == r)
			return;
		int mid = (l + r) / 2;
		build(l, mid, p << 1);
		build(mid + 1, r, p << 1 | 1);
	}
	void push_up(int p)
	{
		tree[p].mx = min(tree[p << 1].mx, tree[p << 1 | 1].mx);
	}
	void push_down(int p)
	{
		if (!tree[p].lazy)
			return;
		tree[p << 1].mx += tree[p].lazy;
		tree[p << 1 | 1].mx += tree[p].lazy;
		tree[p << 1].lazy += tree[p].lazy;
		tree[p << 1 | 1].lazy += tree[p].lazy;
		tree[p].lazy = 0;
	}
	void update(int ul, int ur, int val, int p)
	{
		int l = tree[p].l, r = tree[p].r;
		if (ul <= l && ur >= r)
		{
			tree[p].mx += val;
			tree[p].lazy += val;
			return;
		}
		push_down(p);
		int mid = (l + r) / 2;
		if (ul <= mid)
			update(ul, ur, val, p << 1);
		if (ur > mid)
			update(ul, ur, val, p << 1 | 1);
		push_up(p);
	}
	int query(int ql, int qr, int p)
	{
		int l = tree[p].l, r = tree[p].r;
		if (ql <= l && qr >= r)
			return tree[p].mx;
		push_down(p);
		int mid = (l + r) / 2;
		int ans = INF;
		if (ql <= mid)
			ans = min(ans, query(ql, qr, p << 1));
		if (qr > mid)
			ans = min(ans, query(ql, qr, p << 1 | 1));
		return ans;
	}
}sg;

void solve()//复杂度O(nlogn)
{
	cin >> n >> M >> E;
	M++;
	E++;
	for (int i = 1; i <= n; i++)
	{
		cin >> a[i].l >> a[i].r >> a[i].w;
		a[i].l++;
		a[i].r++;
	}
	sort(a + 1, a + 1 + n);

	ll r = a[1].r, l = a[1].l;
	for (int i = 2; i <= n; i++)
	{
		if (a[i].r < l - 1)
		{
			cout << -1 << '\n';
			return;
		}
		l = min(l, a[i].l);
	}
	if (r < M || l > M)
	{
		cout << -1 << '\n';
		return;
	}

	sg.build(M - 1, E, 1);
	sg.update(M - 1, M - 1, -INF, 1);
	for (int i = n; i >= 1; i--)
	{
		int temp = sg.query(a[i].l - 1, a[i].r, 1);
		int temp1 = sg.query(a[i].r, a[i].r, 1);
		if(temp + a[i].w < temp1)
			sg.update(a[i].r, a[i].r, temp + a[i].w - temp1, 1);
	}
	cout << sg.query(E, E, 1) << '\n';
}

signed main()
{
	IOS;
	int t = 1;
	//cin >> t;
	while (t--)
		solve();
	return 0;
}

O(n^2) 瞎搞

#include<bits/stdc++.h>
#include<unordered_map>
#include<array>
#define ll long long
#define ull unsigned long long
#define all(a) a.begin(),a.end()
#define IOS ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
using namespace std;

const int inf = 0x3f3f3f3f;
const ll INF = 0x3f3f3f3f3f3f3f3f;
const double eps = 1e-8;
const ll mod = 998244353;
const int N = 1e5 + 5;

ll n, M, E, dp[N];
struct node {
	ll l, r, w;
	bool operator < (node a)
	{
		if (r != a.r)
			return r > a.r;
		return l < a.l;
	}
}a[N];

void solve()//复杂度O(n ^ 2),根据转移方程可知只从 l - 1 进行转移,所以只要更新l-1的dp值即可
{
	cin >> n >> M >> E;
	M++;
	E++;
	for (int i = 1; i <= n; i++)
	{
		cin >> a[i].l >> a[i].r >> a[i].w;
		a[i].l++;
		a[i].r++;
	}
	sort(a + 1, a + 1 + n);

	ll r = a[1].r, l = a[1].l;
	for (int i = 2; i <= n; i++)
	{
		if (a[i].r < l - 1)
		{
			cout << -1 << '\n';
			return;
		}
		l = min(l, a[i].l);
	}
	if (r < M || l > M)
	{
		cout << -1 << '\n';
		return;
	}

	vector<int> v;
	for (int i = 1; i <= n; i++)
		v.push_back(a[i].l - 1);
	v.push_back(E);
	sort(all(v));
	v.erase(unique(all(v)), v.end());

	for (int i : v)
		dp[i] = inf;
	dp[M - 1] = 0;
	for (int i : v)
		for (int j = 1; j <= n; j++)
			if (a[j].r >= i && a[j].l <= i)
				dp[i] = min(dp[i], dp[a[j].l - 1] + a[j].w);
	cout << dp[E] << '\n';
}

signed main()
{
	IOS;
	int t = 1;
	//cin >> t;
	while (t--)
		solve();
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值