社区讨论

16PTS,除了AC全RE,求助

P3919【模板】可持久化线段树 1(可持久化数组)参与者 1已保存回复 0

讨论操作

快速查看讨论及其快照的属性,并进行相关操作。

当前回复
0 条
当前快照
1 份
快照标识符
@m0dr22qf
此快照首次捕获于
2024/08/28 19:05
2 年前
此快照最后确认于
2024/08/28 21:20
2 年前
查看原帖
CPP
#include <bits/stdc++.h>
using namespace std;

const int N = 3e7 + 5;
const int M = 3e7 + 5;

namespace IO 
{
	#define iL (1 << 20)
	char ibuf[iL], *iS = ibuf + iL, *iT = ibuf + iL;
	#define gc() ((iS == iT) ? (iT = (iS = ibuf) + fread(ibuf, 1, iL, stdin), iS == iT ? EOF : *iS ++) : *iS ++)
	template<class T> inline void read(T &x) {
	  	x = 0;int f = 0;char ch = gc();
	  	for (; !isdigit(ch); f |= ch == '-', ch = gc());
	  	for (; isdigit(ch); x = (x << 1) + (x << 3) + (ch ^ 48), ch = gc());
	  	x = (f == 1 ? ~ x + 1 : x);
	}
	template<class T, class... Args> inline void read(T &x, Args&... args) { read(x), read(args...); }
	template<class T> inline void readch(T &x) { char ch = gc(); for (; !isalpha(ch); ch = gc()); x = ch; }
	char Out[iL], *iter = Out;
	#define flush() fwrite(Out, 1, iter - Out, stdout), iter = Out
	template<class T> inline void write(T x, char ch = '\n') {
 	 	T l, c[35];
  		if (x < 0) *iter ++ = '-', x = ~ x + 1;
  		for (l = 0; !l || x; c[l] = x % 10, l++, x /= 10);
 		for (; l; -- l, *iter ++ = c[l] + '0');
		*iter ++ = ch;
  		flush();
	}
	template<class T, class... Args> inline void write(T x, Args... args) { write(x, ' '), write(args...); }
}
using namespace IO;

struct node
{
	node* ls;
	node* rs;
	int val;
	node() : ls(nullptr), rs(nullptr), val(0) {}
};

node* rt[M];
int a[N];

int main()
{
	int n, m;
	read(n, m);
	for (int i = 1; i <= n; ++ i)
	{
		read(a[i]);
	}
	auto build = [&](auto &rep, node*& pos, int l, int r)
	{
		pos = new node(); 
		if (l == r)  return pos -> val = a[l];
		int mid = l + r >> 1;
		return pos -> val = rep(rep, pos -> ls, l, mid) + rep(rep, pos -> rs, mid + 1, r);
	};
	build(build, rt[0], 1, n);
	for (int t = 1; t <= m; ++ t)
	{
		int v, op, loc, val;
		read(v, op, loc);
		if (op == 1)
		{
			read(val);
			auto update = [&](auto &rep, node* ori, node*& pos, int l, int r, int val, int loc)
			{
				pos = new node();
				*pos = *ori;
				if (l == r)
				{
					pos -> val = val;
					return;
				}
				int mid = l + r >> 1;	
				if (loc <= mid)  rep(rep, ori -> ls, pos -> ls, l, mid, val, loc);
				else rep(rep, ori -> rs, pos -> rs, mid + 1, r, val, loc);			
			};
			update(update, rt[v], rt[t], 1, n, val, loc);
		}
		else
		{
			auto query = [&](auto &rep, node* pos, int l, int r, int loc)
			{
				if (l == r)
				{
					return pos -> val;
				}
				int mid = l + r >> 1;
				if (loc <= mid) return rep(rep, pos -> ls, l, mid, loc);
				else rep(rep, pos -> rs, mid + 1, r, loc);
			};
			printf("%d\n", query(query, rt[v], 1, n, loc));
			rt[t] = rt[v];
		}
	}
	return 0;
}

回复

0 条回复,欢迎继续交流。

正在加载回复...