結果
| 問題 |
No.1441 MErGe
|
| コンテスト | |
| ユーザー |
nok0
|
| 提出日時 | 2021-03-27 10:56:54 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
TLE
(最新)
AC
(最初)
|
| 実行時間 | - |
| コード長 | 4,166 bytes |
| コンパイル時間 | 3,356 ms |
| コンパイル使用メモリ | 206,452 KB |
| 最終ジャッジ日時 | 2025-01-19 23:59:37 |
|
ジャッジサーバーID (参考情報) |
judge2 / judge4 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 2 |
| other | AC * 16 TLE * 12 |
ソースコード
#include <bits/stdc++.h>
struct fast_io {
fast_io() {
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
std::cout << std::fixed << std::setprecision(15);
}
} fast_io_;
template <class S,
S (*op)(S, S),
S (*e)()>
struct implicit_treap {
public:
implicit_treap() : implicit_treap(0) {}
implicit_treap(const int n) : implicit_treap(std::vector(n, e())) {}
implicit_treap(const int n, S x) : implicit_treap(std::vector(n, x)) {}
implicit_treap(const std::vector<S> &v) {
for(int i = v.size() - 1; i >= 0; i--) insert(0, v[i]);
}
void insert(const int p, const S x) {
assert(0 <= p and p <= size());
insert(root, p, std::make_shared<node>(x, rnd()));
}
void erase(const int l, const int r) {
assert(0 <= l and l <= r and r <= size());
erase(root, l, r);
}
void erase(const int p) {
erase(root, p, p + 1);
}
void set(const int p, const S x) {
assert(0 <= p and p < size());
set(root, p, x);
}
S get(const int p) {
assert(0 <= p and p < size());
return prod(p, p + 1);
}
S prod(const int l, const int r) {
assert(0 <= l and l <= r and r <= size());
return prod(root, l, r);
}
S all_prod() { return root != nullptr ? root->acc : e(); }
S front() { return get(0); }
S back() { return get(size() - 1); }
void push_front(const S x) { insert(0, x); }
void push_back(const S x) { insert(size(), x); }
void pop_front() {
assert(size());
erase(0);
}
void pop_back() {
assert(size());
erase(size() - 1);
}
int size() const noexcept { return cnt(root); }
bool empty() const noexcept { return cnt(root) == 0; }
private:
struct xorshift {
uint32_t x = 123456789, y = 362436069, z = 521288629, w = 88675123;
xorshift(uint32_t seed = 0) { z ^= seed; }
uint32_t operator()() {
uint32_t t = x ^ (x << 11);
x = y;
y = z;
z = w;
return w = (w ^ (w >> 19)) ^ (t ^ (t >> 8));
}
};
xorshift rnd;
struct node;
using node_ptr = std::shared_ptr<node>;
struct node {
S val, acc;
uint32_t pri;
int cnt;
node_ptr l, r;
node(S val, uint32_t pri) : val(val), acc(e()), pri(pri), cnt(1), l(nullptr), r(nullptr) {}
};
node_ptr root = nullptr;
int cnt(const node_ptr &t) const { return t != nullptr ? t->cnt : 0; }
S acc(const node_ptr &t) const { return t != nullptr ? t->acc : e(); }
void update(node_ptr &t) {
if(t != nullptr) {
t->cnt = cnt(t->l) + 1 + cnt(t->r);
t->acc = op(acc(t->l), op(t->val, acc(t->r)));
}
}
void split(node_ptr t, int key, node_ptr &l, node_ptr &r) {
if(t == nullptr) {
l = r = nullptr;
return;
}
int implicit_key = cnt(t->l) + 1;
if(key < implicit_key)
split(t->l, key, l, t->l), r = t;
else
split(t->r, key - implicit_key, t->r, r), l = t;
update(t);
}
void merge(node_ptr &t, node_ptr l, node_ptr r) {
if(l == nullptr or r == nullptr)
t = (l != nullptr ? l : r);
else if(l->pri > r->pri)
merge(l->r, l->r, r), t = l;
else
merge(r->l, l, r->l), t = r;
update(t);
}
void insert(node_ptr &t, int key, node_ptr x) {
node_ptr t1(nullptr), t2(nullptr);
split(t, key, t1, t2);
merge(t1, t1, x);
merge(t, t1, t2);
}
void erase(node_ptr &t, int l, int r) {
node_ptr t1(nullptr), t2(nullptr), t3(nullptr);
split(t, r, t1, t2);
split(t1, l, t1, t3);
merge(t, t1, t2);
}
S prod(node_ptr &t, int l, int r) {
node_ptr t1 = nullptr, t2 = nullptr, t3 = nullptr;
split(t, l, t1, t2);
split(t2, r - l, t2, t3);
S ret = t2->acc;
merge(t2, t2, t3);
merge(t, t1, t2);
return ret;
}
void set(node_ptr &t, const int p, const S x) {
node_ptr t1 = nullptr, t2 = nullptr, t3 = nullptr;
split(t, p, t1, t2);
split(t2, 1, t2, t3);
t2->val = x;
merge(t2, t2, t3);
merge(t, t1, t2);
}
};
using S = long long;
S op(S x, S y) { return x + y; }
S e() { return 0ll; }
int main() {
int n, q;
std::cin >> n >> q;
std::vector a(n, 0ll);
for(auto &v : a) std::cin >> v;
implicit_treap<S, op, e> seg(a);
while(q--) {
int t, l, r;
std::cin >> t >> l >> r;
--l;
if(t == 1) {
long long sum = seg.prod(l, r);
seg.erase(l, r);
seg.insert(l, sum);
} else
std::cout << seg.prod(l, r) << '\n';
}
return 0;
}
nok0