Codeforces 551E GukiZ and GukiZiana(分块思想)

本文介绍了一种使用分块技术解决特定数列操作问题的方法。面对支持两种操作(区间加值和查询特定值范围)的问题,文章通过将序列分割成若干块并维护每个块的状态来降低时间复杂度。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目链接 GukiZ and GukiZiana

题目大意:一个数列,支持两个操作。一种是对区间$[l, r]$中的数全部加上$k$,另一种是查询数列中值为$x$的下标的最大值减最小值。

$n <= 500000, q <= 50000$

我一开始的反应是线段树,然后发现自己完全想错了……

这道题时限$10$秒,但也很容易超时。我后来是用分块过的。

把序列分成$\sqrt{n}$个块,每个块的大小为$\sqrt{n}$(最后一个块可能因为不能整除的关系可能会小一些)

每个块维护一个值$delta[i]$,表示这块的每一个数值都要加上这个值。

第1种操作的时候,找到$l$和$r$所在的块。

这两个块之间(不包含$l$所在的块和$r$所在的块,如果没有就不修改)的所有块的$delta$都加上$x$

这样就降低了修改的时间复杂度

$l$所在的块中的元素依次遍历,若下标满足$l <= i <= r$,则值加$x$

$r$所在的块中的元素依次遍历,若下标满足$l <= i <= r$,则值加$x$

每个块内按照值升序排序(第二关键字为下标)

当一个块的整体大小顺序可能发生改变时,就对这个块内部$sort$一遍,当然没必要$sort$的时候不要$sort$

不然可能$TLE$

查询的时候对每个块二分查找,找到值为$x$的元素的下标,并实时更新答案。

时间复杂度$O(q\sqrt{n}log(\sqrt{n}))$

 

#include <bits/stdc++.h>

using namespace std;

#define rep(i, a, b)	for (int i(a); i <= (b); ++i)
#define dec(i, a, b)	for (int i(a); i >= (b); --i)
#define fi		first
#define se		second

typedef long long LL;

const int N = 500010;
const int Q = 810;

int block_size, block_num, n, q, c[N], cnt, et, op, l, r;
LL a[N], delta[N], x;
vector <pair<LL, int> > block[Q];


void update(int l, int r, LL x){


	rep(i, c[l] + 1, c[r] - 1)
		delta[i] += x;

	for (auto &node : block[c[l]])
		if (node.se >= l && node.se <= r)
			node.fi += x;

	sort(block[c[l]].begin(), block[c[l]].end());

	if (c[r] > c[l]){
		for (auto &node : block[c[r]])
			if (node.se >= l && node.se <= r)
				node.fi += x;

		sort(block[c[r]].begin(), block[c[r]].end());

	}

}

void query(LL x){
	int L = 1 << 30, R = -1;
	rep(i, 1, block_num){
		auto it = lower_bound(block[i].begin(), block[i].end(), make_pair(x - delta[i], 0));
		if (it != block[i].end() && it -> first == x - delta[i])
			L = min(L, it -> se);

		it = lower_bound(block[i].begin(), block[i].end(), make_pair(x - delta[i] + 1, 0));
		if (it != block[i].begin()){
			--it;
			if (it -> fi == x - delta[i])
				R = max(R, it -> se);
		}
	}

	if (~R) printf("%d\n", R - L);
	else puts("-1");
}

int main(){


	scanf("%d%d", &n, &q);
	rep(i, 1, n) scanf("%lld", a + i);
	block_size = sqrt(n + 0.5);

	block_num = n / block_size;
	if (n % block_size) ++block_num;

	cnt = 1;
	rep(i, 1, n){
		++et;
		c[i] = cnt;
		block[cnt].push_back({a[i], i});
		if (et == block_size){
			et = 0;
			++cnt;
		}


	}

	rep(i, 1, block_num) sort(block[i].begin(), block[i].end());

	for (; q--; ){
		scanf("%d", &op);
		if (op == 1){
			scanf("%d%d%lld", &l, &r, &x); 
			update(l, r, x);
		}

		else{
			scanf("%lld", &x);
			query(x);
		}
	}

	return 0;
}

 

转载于:https://www.cnblogs.com/cxhscst2/p/7226758.html

### Codeforces 887E Problem Solution and Discussion The problem **887E - The Great Game** on Codeforces involves a strategic game between two players who take turns to perform operations under specific rules. To tackle this challenge effectively, understanding both dynamic programming (DP) techniques and bitwise manipulation is crucial. #### Dynamic Programming Approach One effective method to approach this problem utilizes DP with memoization. By defining `dp[i][j]` as the optimal result when starting from state `(i,j)` where `i` represents current position and `j` indicates some status flag related to previous moves: ```cpp #include <bits/stdc++.h> using namespace std; const int MAXN = ...; // Define based on constraints int dp[MAXN][2]; // Function to calculate minimum steps using top-down DP int minSteps(int pos, bool prevMoveType) { if (pos >= N) return 0; if (dp[pos][prevMoveType] != -1) return dp[pos][prevMoveType]; int res = INT_MAX; // Try all possible next positions and update 'res' for (...) { /* Logic here */ } dp[pos][prevMoveType] = res; return res; } ``` This code snippet outlines how one might structure a solution involving recursive calls combined with caching results through an array named `dp`. #### Bitwise Operations Insight Another critical aspect lies within efficiently handling large integers via bitwise operators instead of arithmetic ones whenever applicable. This optimization can significantly reduce computation time especially given tight limits often found in competitive coding challenges like those hosted by platforms such as Codeforces[^1]. For detailed discussions about similar problems or more insights into solving strategies specifically tailored towards contest preparation, visiting forums dedicated to algorithmic contests would be beneficial. Websites associated directly with Codeforces offer rich resources including editorials written after each round which provide comprehensive explanations alongside alternative approaches taken by successful contestants during live events. --related questions-- 1. What are common pitfalls encountered while implementing dynamic programming solutions? 2. How does bit manipulation improve performance in algorithms dealing with integer values? 3. Can you recommend any online communities focused on discussing competitive programming tactics? 4. Are there particular patterns that frequently appear across different levels of difficulty within Codeforces contests?
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值