萌新分块重构写崩了,求调,悬关
  • 板块学术版
  • 楼主Womind
  • 当前回复0
  • 已保存回复0
  • 发布时间2023/10/31 17:27
  • 上次更新2023/11/2 10:32:34
查看原帖
萌新分块重构写崩了,求调,悬关
801322
Womind楼主2023/10/31 17:27

会有出不来的问题 另外还有一个问题,query 函数中我必须重新赋值一边,不然

return buc[i][pos - now - 1] 会返回乱码,QAQ,为什么啊

/*
    Name:
    Copyright:
    Author: 不要和 std 命名空间冲突!
    Date: 10/31/2023 4:11:41 PM
    Description:
*/
#include<bits/stdc++.h>
using namespace std;
#define rep(i,a,b)  for (int i = (a); i <= (b); i++)
#define repe(i,a,b) for (int i = (a); i >= (b); i--)
//#define int long long
inline int read() {
	int ans = 0, f = 1; char ch = getchar();
	for(; ch < '0' || ch > '9'; ch = getchar()) if (ch == '-') f = -f;
	for(; ch >= '0' && ch <= '9'; ch = getchar()) ans = (ans << 3) + (ans << 1) + ch - '0';
	return ans * f;
}
const int N = 2e5 + 1;  
vector<int> buc[500];
int n, m, sq, tot, siz[N], a[N];
void rebuild() {
	n = 0;
	rep(i,1,tot) {
//		for(auto j : buc[i]) a[++n] = j;
		rep(j,0,siz[i] - 1) a[++n] = buc[i][j];
		buc[i].clear(); 
	}
	sq = sqrt(n);
	rep(i,1,sq) {
		int l = (i - 1) * sq + 1, r = i * sq; siz[i] = sq;
		rep(j,l,r) buc[i].push_back(a[j]);
	}
	tot = sq;
	if(sq * sq < n) {
		int l = sq * sq + 1, r = n; tot ++; siz[tot] = r - l + 1;
		rep(j,l,r) buc[tot].push_back(a[j]);
	}
} 
void Insert(int l, int val) {
	int now = 0, i = 1;
	for(; now + siz[i] < l; now += siz[i++]); // 小于 要加 
	
//	rep(j,1,l - now) it++; vector 内是连续的 

	buc[i].insert(buc[i].begin() + l - now - 1, val); siz[i]++;
	if (siz[i] > sq * 20) rebuild();
}
int query(int pos) {
	int now = 0, i = 1;
	for(; now + siz[i] < pos; now += siz[i++]) // 小于 要加 
//	cout << "fdsfs  " <<i << " " << pos - now - 1 << endl;
	i = i; int cc = pos - now - 1;
	return buc[i][pos - now - 1]; // 从零开始 
}
void test() {
	cout << "test" << endl;
	rep(i,1,tot) {
		rep(j,0,siz[i] - 1)  cout << buc[i][j] << " ";
		cout << endl;
	}
	cout << "test" << endl;
}
int main() {
	freopen("a10.in", "r", stdin);
	freopen("a.out", "w", stdout);
	n = read();
	rep(i,1,n) a[i] = read();
	tot = sq = sqrt(n);
	rep(i,1,sq) {
		int l = (i - 1) * sq + 1, r = i * sq; siz[i] = sq;
		rep(j,l,r) buc[i].push_back(a[j]);
	}
	
	if(sq * sq < n) {
		tot = sq + 1;
		rep(j,sq * sq + 1, n) buc[tot].push_back(a[j]);
		siz[tot] = n - sq * sq;
	}
	rep(i,1,n) {
//		test();
		int op = read(), l = read(), r = read(), c = read();
		if(op == 0) {
			Insert(l, r);
		}
		else cout << query(r) << "\n";
	}
}
2023/10/31 17:27
加载中...