动态开点怎么调呜呜,#2和#12TLE了QAQ
查看原帖
动态开点怎么调呜呜,#2和#12TLE了QAQ
791327
LONERANGERhcm楼主2023/5/6 13:54
#include <bits/stdc++.h>
using namespace std;

#define MAXN 1000001


int N, M;

class Node {
public:
	Node *left, *right;
	long long val;
	Node() : left(nullptr), right(nullptr), val(0) {}
	Node(const Node &node) {
		this->left = node.left;
		this->right = node.right;
		this->val = node.val;
	}
};

Node* tree[MAXN];

class Solution {
public:
	Solution() {}
	
	void pushUp(Node *node) {
		node->val = node->left->val + node->right->val;
		return;
	}		
	
	void updateZero(Node *node, int start, int end, int l, int r, int val) {
		if (l <= start && r >= end) {
			node->val = (end - start + 1) * val;
			return;
		}
		int mid = (start + end) >> 1;
		if (!node->left) node->left = new Node();
		if (!node->right) node->right = new Node();
		if (l <= mid) updateZero(node->left, start, mid, l, r, val);
		if (r > mid) updateZero(node->right, mid + 1, end, l, r, val);
		pushUp(node);
		return;
	}
	
	Node* updateMore(Node *node, int start, int end, int l, int r, int val) {
		if (l <= start && r >= end) {
			Node *cloneNode = new Node(*node);
			cloneNode->val = (end - start + 1) * val;
			return cloneNode;
		}
		Node *cloneNode = new Node(*node);
		int mid = (start + end) >> 1;
		if (l <= mid) cloneNode->left = updateMore(node->left, start, mid, l, r, val);
		if (r > mid) cloneNode->right = updateMore(node->right, mid + 1, end, l, r, val);
		pushUp(cloneNode);
		return cloneNode;
	}

	long long query(Node *node, int start, int end, int l, int r) {
		if (l <= start && r >= end) return node->val;
		int mid = (start + end) >> 1;
		long long ans(0);
		if (l <= mid) ans += query(node->left, start, mid, l, r);
		if (r > mid) ans += query(node->right, mid + 1, end, l, r);
		return ans;
	} 
};


int read() {
	int x = 0, f = 1;
	char ch = getchar();
	while (ch < '0' || ch > '9') {
		if (ch == '-') f = -1;
		ch = getchar();
	}
	while (ch >= '0' && ch <= '9') {
		x = x * 10 + ch - '0';
		ch = getchar();
	}
	return x * f;
}

void print(long long x) {
	if (x < 0) {
		putchar('-');
		x = -x;
	}
	if (x > 9) {
		print(x / 10);
	}
	putchar(x % 10 + '0');
}

int main(void) {
	cin.tie(NULL); cout.tie(NULL);
	ios::sync_with_stdio(false);
	N = read(), M = read();
	Node *root = new Node();
	Solution *solution = new Solution();
	for (int i = 1; i <= N; ++i) {
		int num = read();
		solution->updateZero(root, 1, N, i, i, num);
	}
	tree[0] = root;
	for (int i = 1; i <= M; ++i) {
		int vi = read(), command = read();
		if (command == 1) {	
			int loci = read(), valuei = read();
			tree[i] = solution->updateMore(tree[vi], 1, N, loci, loci, valuei);
		}
		else if (command == 2) {
			tree[i] = new Node(*tree[vi]);
			int loci = read();
			print(solution->query(tree[vi], 1, N, loci, loci));
			printf("\n");
		}
	}
	delete root;
	delete solution;
	return 0;
}
2023/5/6 13:54
加载中...