求助,我的线段树在#1和#11号点MLE(使用动态开点)
查看原帖
求助,我的线段树在#1和#11号点MLE(使用动态开点)
152651
xiyihan楼主2020/10/12 22:30

写了1晚上的线段树,交上去发现自己的程序有2个点MLE了...我线段树的写法是指针式动态开点线段树,如果谁能帮我改一下程序,我相当感谢(是不是因为64位系统使用64位指针的原因?)

#include <bits/stdc++.h>
using namespace std;
int n, m;
inline int read() noexcept
{
	char c = getchar();
	int ans = 0, sgn = 1;
	while (!isdigit(c))
	{
		if (c == '-')
		{
			sgn = -1;
		}
		c = getchar();
	}
	while (isdigit(c))
	{
		ans = ans * 10 + (c ^ 48);
		c = getchar();
	}
	return ans * sgn;
}
struct node
{
	int val = 0, l = 0, r = 0;
	node* lson = nullptr,* rson = nullptr;
	node() noexcept
	{

	}
	node(int v, int l, int r, node* ls, node* rs) noexcept
	{
		val = v;
		this->l = l;
		this->r = r;
		lson = ls;
		rson = rs;
	}
	node(const node& tmp) noexcept
	{
		*this = tmp;
	}
	int& operator()() noexcept
	{
		return val;
	}
};
class tree
{
public:
	tree(const int& size, const vector<int>& elem) noexcept;
	node& operator()(int hv, int loc, bool modify) noexcept;
	//void copypath(int hv, int loc);
private:
	vector<node*> roots;
	node* newnode(const int& l, const int& r, const vector<int>& elem) noexcept;

};
bool inrge(node* a1, int loc)
{
	return a1->l <= loc and a1->r >= loc;
}

int main()
{
	//cin >> n >> m;
	n = read(), m = read();
	vector<int> tmp(n + 1);
	for (int i = 1; i <= n; i++)
	{
		//cin >> tmp[i];
		tmp[i] = read();
	}
	tree T(n, tmp);
	for (int i = 1; i <= m; i++)
	{
		int ver, type, a1, a2;
		//cin >> ver >> type;
		ver = read(), type = read(), a1 = read();
		switch (type)
		{
		case 1:
			//cin >> a1 >> a2;
			a2 = read();
			T(ver, a1, 1)() = a2;
			break;
		case 2:
			//cin >> a1;
			//cout << T(ver, a1)() << endl;
			printf("%d\n", T(ver, a1, 0)());
		}
	}
}

tree::tree(const int& size, const vector<int>& elem) noexcept
{
	roots.push_back(newnode(0, size, elem));
}

node& tree::operator()(int hv, int loc, bool modify) noexcept
{
	node* now = new node(*roots[hv]);
	roots.push_back(now);
	while (now->l < now->r)
	{
		if (inrge(now->lson, loc))
		{
			if (modify)
			{
				now->lson = new node(*(now->lson));
			}
			
			now = now->lson;
		}
		else
		{
			if (modify)
			{
				now->rson = new node(*(now->rson));
			}
			
			now = now->rson;
		}
	}
	return *now;
}

node* tree::newnode(const int& l, const int& r, const vector<int>& elem) noexcept
{
	if (l == r)
	{
		return new node(elem[l], l, r, nullptr, nullptr);
	}
	else
	{
		int mid = l + r >> 1;
		return new node(0, l, r, newnode(l, mid, elem), newnode(mid + 1, r, elem));
	}
}



2020/10/12 22:30
加载中...