LOJ #2551. 「JSOI2018」列队

题意

i i i 个人有一个所在坐标 a i a_i ai,每次将 i ∈ [ l , r ] i \in [l,r] i[l,r] 一一对应地分配到区间 [ K , K + r − l ] [K, K + r - l] [K,K+rl] 中,第 i i i 个人分配到坐标 x x x 的花费是 ∣ a i − x ∣ |a_i - x| aix。求最小总花费。

Soulution

根据贪心的思想,易证最优方案是将 a l ∼ a r a_l \sim a_r alar 按坐标进行排序,然后在区间中从小到大进行分配。
考虑计算答案。便于计算,我们思考如何将花费中的绝对值去掉,发现有一部分人是往左走,贡献为 a i − x a_i - x aix,有一部分人往右走,贡献为 x − a i x - a_i xai
由于答案具有单调性,我们可以对 a l ∼ a r a_l \sim a_r alar 进行二分。
用主席树维护权值和,在主席树上二分答案。
由于一段连续区间是等差数列,所以我们可以直接计算答案。

Code

//Dlove's template
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 

#define R register
#define ll long long
#define ull unsigned long long
#define db double
#define ld long double
#define sqr(_x) ((_x) * (_x))
#define Cmax(_a, _b) ((_a) < (_b) ? (_a) = (_b), 1 : 0)
#define Cmin(_a, _b) ((_a) > (_b) ? (_a) = (_b), 1 : 0)
#define Max(_a, _b) ((_a) > (_b) ? (_a) : (_b))
#define Min(_a, _b) ((_a) < (_b) ? (_a) : (_b))
#define Abs(_x) (_x < 0 ? (-(_x)) : (_x))

using namespace std;

namespace Dntcry
{
	//char Bs[1 << 22], *Ss = Bs, *Ts = Bs;
	//#define getchar() (Ss == Ts && (Ts = (Ss = Bs) + fread(Bs, 1, 1 << 22, stdin), Ss == Ts) ? EOF : *Ss++) 
	inline int read()
	{
		R int a = 0, b = 1; R char c = getchar();
		for(; c < '0' || c > '9'; c = getchar()) (c == '-') ? b = -1 : 0;
		for(; c >= '0' && c <= '9'; c = getchar()) a = (a << 1) + (a << 3) + c - '0';
		return a * b;
	}
	inline ll lread()
	{
		R ll a = 0, b = 1; R char c = getchar();
		for(; c < '0' || c > '9'; c = getchar()) (c == '-') ? b = -1 : 0;
		for(; c >= '0' && c <= '9'; c = getchar()) a = (a << 1) + (a << 3) + c - '0';
		return a * b;
	}
	const int Maxn = 500010;
	int n, m, cnt;
	int A[Maxn], B[Maxn];
	ll Sum[Maxn], Ans;
	struct node
	{
		int siz;
		ll v;
		node *ls, *rs;
	}t[Maxn << 5], *rt[Maxn], *tot = t, *Null;
	node *make(R node *last)
	{
		*++tot = *last;
		return tot;	
	}
	void Insert(R node *root, R int nowl, R int nowr, R int part)
	{
		root->siz++;
		root->v += part;
		if(nowl == nowr) return ;
		R int mid = nowl + nowr >> 1;
		if(part <= mid) Insert(root->ls = make(root->ls), nowl, mid, part);
		else Insert(root->rs = make(root->rs), mid + 1, nowr, part);
		return ;
	}
	int Query(R node *left, R node *right, R int nowl, R int nowr, R int K)
	{
		if(nowl == nowr) return nowl;
		R int mid = nowl + nowr >> 1;
		if(K + right->ls->siz - left->ls->siz <= mid) return Query(left->ls, right->ls, nowl, mid, K);
		return Query(left->rs, right->rs, mid + 1, nowr, K + right->ls->siz - left->ls->siz);
	}
	bool Check(R int x, R int usel, R int user, R int K)
	{
		R int tmp = Query(rt[usel - 1], rt[user], 1, cnt, x - usel + 1);
		return B[tmp] - (K + x - usel) >= 0;
	}
	ll Solve(R node *left, R node *right, R int nowl, R int nowr, R int usel, R int user)
	{
		if(usel > user || nowl > user || nowr < usel) return 0;
		if(usel <= nowl && nowr <= user) return right->v - left->v;
		R int mid = nowl + nowr >> 1;
		return Solve(left->ls, right->ls, nowl, mid, usel, user) + Solve(left->rs, right->rs, mid + 1, nowr, usel, user);
	}
	int Main()
	{
		n = read(), m = read();
		for(R int i = 1; i <= n; i++) A[i] = read();
		cnt = 2e6;
		Null = rt[0] = t, rt[0]->ls = rt[0]->rs = Null;
		for(R int i = 1; i <= n; i++) Insert(rt[i] = make(rt[i - 1]), 1, cnt, A[i]);
		while(m--)
		{
			R int usel = read(), user = read(), K = read();
			R int part = Query(rt[usel - 1], rt[user], 1, cnt, K - 1);
			if(K == 1) part = 0;
			Ans = (1ll * (K + part) * (part - K + 1) - 1ll * (part + K + user - usel + 1) * (K + user - usel - part)) / 2;
			Ans += Solve(rt[usel - 1], rt[user], 1, cnt, part + 1, cnt);
			Ans -= Solve(rt[usel - 1], rt[user], 1, cnt, 1, part);
			printf("%lld\n", Ans);
		}
		return 0;
	}
}
int main()
{
	return Dntcry :: Main();
}

你可能感兴趣的:(数据结构)