代码求调
查看原帖
代码求调
491607
Reisael楼主2022/7/20 21:27

本人今天学习了线段树的基本思想,但是并没有完全听懂。

但是!我自己发明了一种类似线段树的数据结构!!!

这种数据结构借鉴了线段树的基本思想,凝聚了我的智慧与汗水,我相信它在学术界是有极大的价值的

然而!它!只!有!70!分

我用尽毕生所学加了各种常数优化。我的卡常功底是非常深厚的,我的快读快写效率也相当高。

然而2它还是只有70分,这是为什么呢?我无法接受我的优秀算法只获得了70分的事实,有人能帮我调一下吗

#include <cstdio>
#include <iostream>
#include <string>
#include <sstream>
#define int long

inline int kuaidu() {
	std::string str;
	std::stringstream x;
	int val;
	std::cin >> str;
	x << str;
	x >> val;
	return val;
}
inline int kuaixie(int val) {
	std::string str;
	std::stringstream x;
	x << val;
	x >> str;
	std::cout << str << std::endl;
}
struct node {
	int l, r, v,tag;
} tree[400005];
int n;
inline void build(register int p, register int l, register int r) {
	if(r>n)return;
	tree[p].l=l,tree[p].r=r;
	build(p+1,l+1,r+1);
}
void pushdown(int p) {
	tree[p<<1].v+=tree[p].tag*(tree[p<<1].r-tree[p<<1].l+1),tree[p<<1].tag+=tree[p].tag;
	tree[p<<1|1].v+=tree[p].tag*(tree[p<<1|1].r-tree[p<<1|1].l+1),tree[p<<1|1].tag+=tree[p].tag;
	tree[p].tag = 0;
}
void update(int p, int l, int r, int v) {
	if(!tree[p].r)return;
	if (l <= tree[p].l && tree[p].r <= r) tree[p].v += v;
	int mid = tree[p].l + tree[p].r + 2 >> 1;
	pushdown(p);
	update(p + 1, l, r, v);
}
int query(int p, int l, int r) {
	if(!tree[p].r)return 0;
	int ans = 0;
	if (l <= tree[p].l && tree[p].r <= r) ans += tree[p].v;
	pushdown(p);
	int mid = tree[p].l + tree[p].r + 2 >> 1;
	ans+=query(p+1,l,r);
	return ans;
}
signed main() {
	n = kuaidu();
	int q = kuaidu();
	build(1,1,1);
	for (int i = 1; i <= n; ++ i) {
		int x = kuaidu();
		update(1,i,i,x);
	}
	while(q--){
		int op=kuaidu();
		if(op==1){
			int x=kuaidu(),y=kuaidu(),k=kuaidu();
			update(1,x,y,k);
		}else{
			int x=kuaidu(),y=kuaidu();
			kuaixie(query(1,x,y));
		}
	}
	return 0;
}
2022/7/20 21:27
加载中...