Live2D

Solution -「POI 2011」「洛谷 P3527」MET-Meteors

\(\mathcal{Description}\)

  Link.

  给定一个大小为 \(n\) 的环,每个结点有一个所属国家。\(k\) 次事件,每次对 \([l,r]\) 区间上的每个点点权加上一个值。对于每个国家,求操作多少次事件后其拥有的结点权值总和不小于给定值。

  \(n,k\le3\times10^5\)

\(\mathcal{Soltuion}\)

  新初二大佬切掉的题兔子都不会 qwq。

  于是补习了一下整体二分。

  考虑一个单点的情况,显然二分,不过每个点单独二分答案的复杂度是不能接受的。我们考虑“整体二分”——把一段点的答案一起二分。具体地,设当前处理询问(此后会打乱国家顺序,所以说“询问”会更准确)区间 \([q_l,q_r]\),已知答案区间 \([a_l,a_r]\),取出答案终点 \(mid=\lfloor\frac{a_l+a_r}2\rfloor\),在树状数组上作用 \([a_l,mid]\) 内的所有事件。把此时满足“不小于给定值”的询问放到左边,其余放到右边并将其要求的值减去当前这些事件的贡献,最后还原树状数组,递归处理两端询问区间即可。

  复杂度 \(\mathcal O(n\log n\log k)\)

\(\mathcal{Code}\)

#include <cstdio>
#include <vector>

typedef long long LL;

const int MAXN = 3e5;
int n, m, K, L[MAXN + 5], R[MAXN + 5], A[MAXN + 5], ans[MAXN + 5];
std::vector<int> station[MAXN + 5];
LL bit[MAXN * 2 + 5];

struct Country { int expt, id; } qry[MAXN + 5], tqry[MAXN * 2 + 5];

inline int rint () {
	int x = 0; char s = getchar ();
	for ( ; s < '0' || '9' < s; s = getchar () );
	for ( ; '0' <= s && s <= '9'; s = getchar () ) x = x * 10 + ( s ^ '0' );
	return x;
}

inline int lowbit ( const int x ) { return x & -x; }

inline void update ( int x, const int k ) { for ( ; x <= m << 1; x += lowbit ( x ) ) bit[x] += k; }

inline LL sum ( int x ) { LL ret = 0; for ( ; x; x -= lowbit ( x ) ) ret += bit[x]; return ret; }

inline void solve ( const int ql, const int qr, const int al, const int ar ) {
	if ( al == ar ) {
		for ( int i = ql; i <= qr; ++ i ) ans[qry[i].id] = al;
		return ;
	}
	int mid = al + ar >> 1, inL = 0, inR = n;
	for ( int i = al; i <= mid; ++ i ) update ( L[i], A[i] ), update ( R[i] + 1, -A[i] );
	for ( int i = ql; i <= qr; ++ i ) {
		LL curs = 0;
		for ( int st: station[qry[i].id] ) {
			curs += sum ( st ) + sum ( st + m );
			if ( curs >= qry[i].expt ) break;
		}
		if ( curs >= qry[i].expt ) tqry[++ inL] = qry[i];
		else qry[i].expt -= curs, tqry[++ inR] = qry[i];
	}
	for ( int i = al; i <= mid; ++ i ) update ( L[i], -A[i] ), update ( R[i] + 1, A[i] );
	for ( int i = 1; i <= inL; ++ i ) qry[ql + i - 1] = tqry[i];
	for ( int i = 1; i <= inR - n; ++ i ) qry[ql + inL + i - 1] = tqry[n + i];
	solve ( ql, ql + inL - 1, al, mid ), solve ( qr - inR + n + 1, qr, mid + 1, ar );
}

int main () {
	n = rint (), m = rint ();
	for ( int i = 1; i <= m; ++ i ) station[rint ()].push_back ( i );
	for ( int i = 1; i <= n; ++ i ) qry[i].expt = rint (), qry[i].id = i;
	K = rint ();
	for ( int i = 1; i <= K; ++ i ) {
		L[i] = rint (), R[i] = rint (), A[i] = rint ();
		if ( R[i] < L[i] ) R[i] += m;
	}
	solve ( 1, n, 1, K + 1 );
	for ( int i = 1; i <= n; ++ i ) {
		if ( ans[i] == K + 1 ) puts ( "NIE" );
		else printf ( "%d\n", ans[i] );
	}
	return 0;
}
posted @ 2020-07-30 09:51  Rainybunny  阅读(137)  评论(1编辑  收藏  举报