Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

O(M*log(N)) DP数组加线段树 32ms

Posted by 3272621977 at 2022-09-30 22:11:03 on Problem 3616
#include <iostream>
#include <algorithm>
using namespace std;

int N, M, R;
const int maxn = 1000007;
int ans=0;
struct segment_tree
{
	struct node
	{
		int l, r, num;
	} tr[maxn * 4];
	void build(int p, int l, int r)
	{
		tr[p].l=l;
		tr[p].r=r;
		tr[p].num=0;
		if (tr[p].l == tr[p].r)
		{
			tr[p].num = 0;
			return;
		}
		int mid = (l + r) / 2;
		build(p << 1, l, mid);
		build(p << 1 | 1, mid + 1, r);
	}
	void modify(int p, int l, int r, int k)
	{
		if (tr[p].l >= l && tr[p].r <= r)
		{
			tr[p].num += k;
			return;
		}
		int mid = (tr[p].l + tr[p].r) / 2;
		if (l <= mid)
		{
			modify(p << 1, l, r, k);
		}
		if (r > mid)
		{
			modify(p << 1 | 1, l, r, k);
		}
	}
	void query(int p, int x)
	{
		ans += tr[p].num;
		if (tr[p].l == tr[p].r)
		{
			return;
		}
		int mid = (tr[p].l + tr[p].r) / 2;
		if (x <= mid)
		{
			query(p << 1, x);
		}
		else
		{
			query(p << 1 | 1, x);
		}
	}
} ST;
struct Node
{
	int start;
	int end;
	int value;
} node[1007];
bool compareNode(Node node1, Node node2)
{
	return node1.end < node2.end;
}
int dp[1000007];
int main()
{
	scanf("%d%d%d", &N, &M, &R);
	for (int i = 1; i <= M; i++)
	{
		scanf("%d%d%d", &node[i].start, &node[i].end, &node[i].value);
	}
	sort(node + 1, node + 1 + M, compareNode);
	ST.build(1, 1, N);
	ST.modify(1, node[1].end, N, node[1].value);
	for (int i = 2; i <= M; i++)
	{
		ans = 0;
		ST.query(1, N);
		int dp_N = ans;
		ans = 0;
		ST.query(1, node[i].start);
		int dp_nodei_start = ans;
		if ((node[i].start == 0 || node[i].start <= R || dp_nodei_start == 0) && node[i].value > dp_N)
		{
			ST.modify(1, node[i].end, N, node[i].value - dp_N);
		}
		else
		{
			ans = 0;
			ST.query(1, node[i].start - R);
			int dp_nodei_before_r = ans;
			if (node[i].value + dp_nodei_before_r > dp_N)
			{
				ST.modify(1, node[i].end, N, node[i].value + dp_nodei_before_r - dp_N);
			}
		}
	}
	ans=0;
	ST.query(1, N);
	printf("%d\n", ans);
	return 0;
}

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator