Submission #1482068


Source Code Expand

#include <bits/stdc++.h>
using namespace std;
using ll = long long;

ll id = 0;
ll id2 = 0;
ll merge(ll l, ll r) {
	return l + r;
}
ll merge2(ll l, ll r) {
	return l + r;
}

enum COL { BLACK, RED };

template <typename T>
struct node;
template <typename T>
node<T> *update(node<T> *t);

template <typename T>
struct node {
	T val, all, lazy;
	node *ch[2];
	COL color;
	int level;
	int size;
	node() {}
	void init(T v) {
		val = v;
		all = v;
		lazy = id2;
		color = BLACK;
		level = 0;
		size = 1;
		ch[0] = ch[1] = nullptr;
	}
	void init(node *l, node *r, COL c) {
		val = id;
		lazy = id2;
		color = c;
		ch[0] = l;
		ch[1] = r;
		update(this);
	}
};

int M;

const int pmax = 3e7;

int it = 0;
node<ll> pool[pmax];

template <typename T>
int ranks(node<T> *t) { return t == nullptr ? 0 : t->level; }
template <typename T>
int count(node<T> *t) { return t == nullptr ? 0 : t->size; }
template <typename T>
T que(node<T> *t) { return t == nullptr ? id : t->all + t->lazy * t->size; }
template <typename T>
node<T> *update(node<T> *t) {
	t->level = ranks(t->ch[0]) + (t->ch[0]->color == BLACK);
	t->size = count(t->ch[0]) + count(t->ch[1]);
	t->all = merge(que(t->ch[0]), merge(t->val, que(t->ch[1])));
	return t;
}
template <typename T>
node<T> *new_leaf(T v) {
	if (it == pmax) exit(EXIT_SUCCESS);
	pool[it].init(v);
	return &pool[it++];
}
template <typename T>
node<T> *new_node(node<T> *l, node<T> *r, COL c) {
	if (it == pmax) exit(EXIT_SUCCESS);
	pool[it].init(l, r, c);
	return &pool[it++];
}
template <typename T>
node<T> *fix(node<T> *t) {
	if (t == nullptr) return t;
	if (it >= pmax) exit(EXIT_SUCCESS);
	pool[it] = *t;
	return &pool[it++];
}
template <typename T>
node<T> *push(node<T> *t) {
	if (t == nullptr) return t;
	t = fix(t);
	if (t->ch[0] != nullptr) {
		t->ch[0] = fix(t->ch[0]);
		t->ch[0]->lazy = merge2(t->ch[0]->lazy, t->lazy);
	}
	if (t->ch[1] != nullptr) {
		t->ch[1] = fix(t->ch[1]);
		t->ch[1]->lazy = merge2(t->ch[1]->lazy, t->lazy);
	}
	t->val = merge2(t->val, t->lazy);
	t->all = merge(que(t->ch[0]), merge(t->val, que(t->ch[1])));
	t->lazy = id2;
	return t;
}
template <typename T>
node<T> *submerge(node<T> *l, node<T> *r) {
	if (l->level < r->level) {
		r = push(r);
		node<T> *c = submerge(l, r->ch[0]);
		if (r->color == BLACK && c->color == RED && c->ch[0]->color == RED) {
			if (r->ch[1]->color == BLACK) return new_node(c->ch[0], new_node(c->ch[1], r->ch[1], RED), BLACK);
			else return new_node(new_node(c->ch[0], c->ch[1], BLACK), new_node(r->ch[1]->ch[0], r->ch[1]->ch[1], BLACK), RED);
		}
		else return new_node(c, r->ch[1], r->color);
	}
	else if (l->level > r->level) {
		l = push(l);
		node<T> *c = submerge(l->ch[1], r);
		if (l->color == BLACK && c->color == RED && c->ch[1]->color == RED) {
			if (l->ch[0]->color == BLACK) return new_node(new_node(l->ch[0], c->ch[0], RED), c->ch[1], BLACK);
			else return new_node(new_node(l->ch[0]->ch[0], l->ch[0]->ch[1], BLACK), new_node(c->ch[0], c->ch[1], BLACK), RED);
		}
		else return new_node(l->ch[0], c, l->color);
	}
	else {
		return new_node(l, r, RED);
	}
}
template <typename T>
node<T> *merge(node<T> *l, node<T> *r) {
	if (l == nullptr || r == nullptr) return l == nullptr ? r : l;
	node<T> *c = submerge(l, r);
	if (c->color == RED) {
		return new_node(c->ch[0], c->ch[1], BLACK);
	}
	return c;
}
template <typename T>
pair<node<T>*, node<T>*> split(node<T> *t, int k) {
	if (k == 0) return make_pair(nullptr, t);
	if (k >= count(t)) return make_pair(t, nullptr);
	t = push(t);
	int c = count(t->ch[0]);
	if (k < c) {
		pair<node<T>*, node<T>*> p = split(t->ch[0], k);
		return make_pair(p.first, merge(p.second, t->ch[1]));
	}
	else if (k > c) {
		pair<node<T>*, node<T>*> p = split(t->ch[1], k - c);
		return make_pair(merge(t->ch[0], p.first), p.second);
	}
	else {
		return make_pair(t->ch[0], t->ch[1]);
	}
}
template <typename T>
node<T> *insert(node<T> *t, int k, int v) {
	pair<node<T>*, node<T>*> s = split(t, k);
	node<T> *r = merge(s.first, new_leaf(v));
	r = merge(r, s.second);
	return r;
}
template <typename T>
node<T> *erase(node<T> *t, int k) {
	pair<node<T>*, node<T>*> s1 = split(t, k), s2 = split(s1.second, 1);
	return merge(s1.first, s2.second);
}
template <typename T>
node<T>* find(node<T> *t, int l, int r, T& res) {
	auto t1 = split(t, l);
	auto t2 = split(t1.second, r - l);
	res = que(t2.first);
	return t;
}
template <typename T>
node<T> *find(node<T> *t, int k) {
	if (t == nullptr) return t;
	int c = count(t->ch[0]);
	return k < c ? find(t->ch[0], k) : k == c ? t : find(t->ch[1], k - (c + 1));
}
template <typename T>
int cnt(node<T> *t, int v) {
	if (t == nullptr) return 0;
	if (t->val < v) return count(t->ch[0]) + 1 + cnt(t->ch[1], v);
	if (t->val == v) return count(t->ch[0]);
	return cnt(t->ch[0], v);
}
template <typename T>
node<T> *update(node<T> *t, int l, int r, T v) {
	auto t1 = split(t, l);
	auto t2 = split(t1.second, r - l);
	t2.first->lazy = merge2(t2.first->lazy, v);
	return merge(t1.first, merge(t2.first, t2.second));
}
template<typename T>
node<T> *copy(node<T> *t, int a, int b, int c, int d) {
	auto t1 = split(t, a);
	auto t2 = split(t1.second, b - a);
	auto t3 = split(t, c);
	auto t4 = split(t3.second, d - c);
	return merge(t1.first, merge(t4.first, t2.second));
}
int xit;
ll x[200000];
int N;

node<ll>* build(int l, int r) {
	if (l + 1 == r) {
		return new_leaf(x[l]);
	}
	return merge(build(l, (l + r) / 2), build((l + r) / 2, r));
}

void up(node<ll> *t) {
	if (t == nullptr) return;
	t = push(t);
	up(t->ch[0]);
	if (xit >= N) return;
	x[xit++] = t->val;
	up(t->ch[1]);
}

node<ll>* rebuild(node<ll>* t) {
	xit = 0;
	up(t);
	it = 0;
	return build(0, N);
}

int main()
{
	cin.sync_with_stdio(false);
	int Q, type, a, b, c, d;
	ll v;
	cin >> N >> Q;
	for (int i = 0; i < N; i++) {
		cin >> x[i];
	}
	node<ll> *root = build(0, N);
	while (Q--) {
		cin >> type >> a >> b; a--;
		if (type == 1) {
			cin >> v;
			root = update(root, a, b, v);
		}
		else if (type == 2) {
			cin >> c >> d; c--;
			root = copy(root, a, b, c, d);
		}
		else {
			root = find(root, a, b, v);
			printf("%lld\n", v);
		}
		if (it >= pmax / 2) {
			root = rebuild(root);
		}
	}
	return 0;
}

Submission Info

Submission Time
Task D - グラフではない
User kazuma
Language C++14 (GCC 5.4.1)
Score 0
Code Size 6397 Byte
Status WA
Exec Time 1454 ms
Memory 845440 KB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 0 / 100
Status
AC × 1
WA × 1
AC × 5
WA × 11
MLE × 6
Set Name Test Cases
Sample subtask0_sample_01.txt, subtask0_sample_02.txt
All subtask0_sample_01.txt, subtask0_sample_02.txt, subtask1_largerandom_t1_01.txt, subtask1_largerandom_t1_02.txt, subtask1_largerandom_t2_03.txt, subtask1_largerandom_t2_04.txt, subtask1_largespecial01.txt, subtask1_largespecial02.txt, subtask1_largespecial03.txt, subtask1_largespecial04.txt, subtask1_largespecial05.txt, subtask1_largespecial06.txt, subtask1_smallrandom_01.txt, subtask1_smallrandom_02.txt, subtask1_smallrandom_03.txt, subtask1_smallrandom_04.txt, subtask1_smallrandom_05.txt, subtask1_smallrandom_06.txt, subtask1_smallrandom_07.txt, subtask1_smallrandom_08.txt, subtask1_smallrandom_09.txt, subtask1_smallrandom_10.txt
Case Name Status Exec Time Memory
subtask0_sample_01.txt AC 1 ms 256 KB
subtask0_sample_02.txt WA 1 ms 384 KB
subtask1_largerandom_t1_01.txt MLE 1397 ms 844928 KB
subtask1_largerandom_t1_02.txt MLE 1423 ms 844928 KB
subtask1_largerandom_t2_03.txt MLE 1238 ms 845312 KB
subtask1_largerandom_t2_04.txt MLE 1240 ms 845184 KB
subtask1_largespecial01.txt MLE 1030 ms 845440 KB
subtask1_largespecial02.txt MLE 1454 ms 845056 KB
subtask1_largespecial03.txt AC 100 ms 48384 KB
subtask1_largespecial04.txt AC 100 ms 49152 KB
subtask1_largespecial05.txt AC 98 ms 49024 KB
subtask1_largespecial06.txt AC 58 ms 1408 KB
subtask1_smallrandom_01.txt WA 2 ms 2816 KB
subtask1_smallrandom_02.txt WA 2 ms 2816 KB
subtask1_smallrandom_03.txt WA 2 ms 2816 KB
subtask1_smallrandom_04.txt WA 2 ms 2816 KB
subtask1_smallrandom_05.txt WA 2 ms 2816 KB
subtask1_smallrandom_06.txt WA 2 ms 2816 KB
subtask1_smallrandom_07.txt WA 2 ms 2816 KB
subtask1_smallrandom_08.txt WA 2 ms 2816 KB
subtask1_smallrandom_09.txt WA 2 ms 2816 KB
subtask1_smallrandom_10.txt WA 2 ms 2816 KB