Codeforces 551E GukiZ and GukiZiana 分块

本文介绍了一种基于分块思想的数据处理方法,适用于处理包括区间加权及查询特定数值最远距离的操作。通过将数据集分为固定大小的块,并对每个块进行独立处理,实现了高效的查询与更新性能。

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

题目链接

题意:给定一个序列,有两种操作

操作1、区间加权

操作2、询问序列中 a[i] == y 的数的最远距离

即:

for(int i = 0; i < n; i++)if(a[i] == y) L = i, break;

for(int i = n-1; i >= 0; i--)if(a[i]==y)R=i, break;

put(R-L);

思路:分块。

介绍一下分块思想

把区间分成 x 块,那么每块长度都为 n/x (如果n/x不能整除则最后一块长度是 n%x) 

设 y 为区间长度,即y=n/x;

我们保证每一块的区间都是有序的,即所有修改后都要排个序。

再对每块区间设置一个整体偏移量 long long b;

操作1:

查询与[l,r]相交的所有块:

if( 这一块被[l,r]完整覆盖 )

b+=val;

else 

{

暴力修改:

暴力枚举这块中的所有元素,若这个元素属于[l,r] 则 这个元素+=val

对这块排个序

(显然只有边界的2块是不会被完整覆盖的)

}

操作2:

查询每一块,因为块是有序的,所以二分一下就好了。


计算复杂度:

对于操作1:

最多查询块的数量是 x, 暴力修改的复杂度是 O( y*log(y) )

所以操作1的复杂度是 O(x + y*log(y))

对于操作2:

查询块的数量是 x, 查找块内元素是二分 : log(y)

所以操作2的复杂度是 O(x * log(y))


我们要使得 O( max(x + ylog(y), xlog(y)) 尽可能小

根据基本不等式得出 当x==y时最小

=>  n = x*y; x=y=sqrt(n);

即把序列分成sqrt(n) 块, 每块的大小为sqrt(n)

#include <iostream>
#include <string>
#include <vector>
#include <cstring>
#include <cstdio>
#include <map>
#include <queue>
#include <algorithm>
#include <stack>
#include <cstring>
#include <cmath>
#include <set>
#include <vector>
using namespace std;
template <class T>
inline bool rd(T &ret) {
	char c; int sgn;
	if (c = getchar(), c == EOF) return 0;
	while (c != '-' && (c<'0' || c>'9')) c = getchar();
	sgn = (c == '-') ? -1 : 1;
	ret = (c == '-') ? 0 : (c - '0');
	while (c = getchar(), c >= '0'&&c <= '9') ret = ret * 10 + (c - '0');
	ret *= sgn;
	return 1;
}
template <class T>
inline void pt(T x) {
	if (x < 0) {
		putchar('-');
		x = -x;
	}
	if (x > 9) pt(x / 10);
	putchar(x % 10 + '0');
}
typedef long long ll;
typedef pair<ll, int> pii;
const int inf = 1e9;
const int N = 5e5 + 10;
const int Block = 850;//700
int n, q;
int ansl, ansr;
ll a[N];
set<pii>::iterator it;
struct Node {
	set<pii>x;
	ll b;
	inline void add(int l, int r, int val) {
		if (r - l + 1 == (int)x.size())b += val;
		else {
			for (int i = l; i <= r; i++)
			{
				x.erase({ a[i], i });
				a[i] += val;
				x.insert({ a[i], i });
			}
		}
	}
	inline void find(ll y) {
		y -= b;
		it = x.lower_bound({ y, -inf });
		if (it == x.end() || (*it).first != y)return;
		ansl = min(ansl, (*it).second);
		it = x.upper_bound({ y, inf }); it--;
		ansr = max(ansr, (*it).second);
	}
}y[Block];
int block;
int main() {
	rd(n); rd(q);
	for (int i = 0; i < n; i++)rd(a[i]);
	block = 0;
	for (int i = 0; i < n; i += Block, block++)
		for (int j = 0; i + j < n && j < Block; j++)
		{
			y[block].x.insert({ a[i + j],i + j });
		}

	int op, l, r, val;
	while (q--) {
		rd(op);
		if (op == 1)
		{
			rd(l); rd(r); rd(val);
			l--;r--;
			for (int i = l / Block; i <= r / Block; i++)
				y[i].add(max(l, i*Block), min(r, i*Block + Block - 1), val);
		}
		else {
			rd(val);
			ansl = inf, ansr = -inf;
			for (int i = 0; i < block; i++)
				y[i].find(val);
			if (ansr - ansl < 0)puts("-1");
			else pt(ansr - ansl), putchar('\n');
		}
	}
	return 0;
}
把set改掉。。:

#include <iostream>
#include <string>
#include <vector>
#include <cstring>
#include <cstdio>
#include <map>
#include <queue>
#include <algorithm>
#include <stack>
#include <cstring>
#include <cmath>
#include <set>
#include <vector>
using namespace std;
template <class T>
inline bool rd(T &ret) {
	char c; int sgn;
	if (c = getchar(), c == EOF) return 0;
	while (c != '-' && (c<'0' || c>'9')) c = getchar();
	sgn = (c == '-') ? -1 : 1;
	ret = (c == '-') ? 0 : (c - '0');
	while (c = getchar(), c >= '0'&&c <= '9') ret = ret * 10 + (c - '0');
	ret *= sgn;
	return 1;
}
template <class T>
inline void pt(T x) {
	if (x < 0) {
		putchar('-');
		x = -x;
	}
	if (x > 9) pt(x / 10);
	putchar(x % 10 + '0');
}
typedef long long ll;
typedef pair<int, int> pii;
const int inf = 1e9;
const int N = 5e5 + 10;
const int Block = 850;//700
int n, q;
int ansl, ansr;
struct Node {
	struct Point {
		ll a; int id;
		Point(ll _a = 0, ll _id = 0) :a(_a), id(_id) {}
		bool operator<(const Point&x)const {
			if (x.a != a)return a < x.a;
			return id < x.id;
		}
	}x[Block], tmp;
	ll b;
	int top;
	inline void Sort() {
		sort(x, x + top);
	}
	inline void add(int l, int r, int val) {
		if (r - l + 1 == top)b += val;
		else {
			for (int i = 0; i < top; i++)
				if (l <= x[i].id && x[i].id <= r)
				x[i].a += val;
			Sort();
		}
	}
	inline void find(ll y) {
		y -= b;
		if (y < x[0].a || x[top - 1].a < y)return;
		tmp = { y, -inf };
		int l = lower_bound(x, x + top, tmp) - x;
		if (l == top || x[l].a != y)return;
		tmp.id = inf;
		int r = lower_bound(x, x + top, tmp) - x - 1;
		ansl = min(ansl, x[l].id);
		ansr = max(ansr, x[r].id);
	}
}y[Block];
int a[N], block;
int main() {
	rd(n); rd(q);
	for (int i = 0; i < n; i++)rd(a[i]);
	block = 0;
	for (int i = 0; i < n; i += Block, block++) {
		for (int j = 0; i + j < n && j < Block; j++)
		{
			y[block].x[j].a = a[i + j];
			y[block].x[j].id = i + j;
			y[block].top++;
		}
		y[block].Sort();
	}
	int op, l, r, val;
	while (q--) {
		rd(op);
		if (op == 1)
		{
			rd(l); rd(r); rd(val);
			l--;r--;
			for (int i = l / Block; i <= r / Block; i++)
				y[i].add(max(l, i*Block), min(r, i*Block + Block - 1), val);
		}
		else {
			rd(val);
			ansl = inf, ansr = -inf;
			for (int i = 0; i < block; i++)
				y[i].find(val);
			if (ansr - ansl < 0)puts("-1");
			else pt(ansr - ansl), putchar('\n');
		}
	}
	return 0;
}



### 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?
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值