QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#792374 | #21403. 文艺平衡树 | oyzr | ML | 0ms | 0kb | C++23 | 2.5kb | 2024-11-29 09:37:30 | 2024-11-29 09:37:31 |
answer
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1e5 + 5;
int read(){
int res = 0, flag = 1;
char c = getchar();
for (; c < '0' || c > '9'; c = getchar())
if (c == '-')
flag = -1;
for (; c >= '0' && c <= '9'; c = getchar())
res = (res << 1) + (res << 3) + (c ^ 48);
return res * flag;
}
class Treap{
private:
#define ls(x) t[x].lson
#define rs(x) t[x].rson
struct Node{
int lson, rson;
bool rev;
int rnd, val, size;
}t[MAXN];
int tot = 0;
void pushDown(int id){
if (t[id].rev){
reverse(ls(id));
reverse(rs(id));
}
t[id].rev = false;
}
void update(int id){
t[id].size = t[ls(id)].size + t[rs(id)].size + 1;
}
public:
int newNode(int val){
++tot;
t[tot].lson = t[tot].rson = 0;
t[tot].rev = false;
t[tot].rnd = rand();
t[tot].val = val;
t[tot].size = 1;
return tot;
}
void reverse(int id){
if (!id)
return;
t[id].rev ^= 1;
swap(ls(id), rs(id));
}
void split(int id, int size, int &x, int &y){
if (!id){
x = y = 0;
return;
}
pushDown(id);
if (t[ls(id)].size + 1 <= size){
x = id;
split(rs(id), size - (t[ls(id)].size + 1), rs(x), y);
}else{
y = id;
split(ls(id), size, x, ls(y));
}
update(id);
}
int merge(int x, int y){
if (!x || !y)
return x + y;
pushDown(x); pushDown(y);
if (t[x].rnd < t[y].rnd){
rs(x) = merge(rs(x), y);
update(x);
return x;
}else{
ls(y) = merge(x, ls(y));
update(y);
return y;
}
}
void output(int id){
if (!id)
return;
pushDown(id);
output(ls(id));
cout << t[id].val << ' ';
output(rs(id));
}
}treap;
int main(){
int n = read(), m = read();
int rt = 0;
for (int i = 1; i <= n; i++)
rt = treap.merge(rt, treap.newNode(i));
while (m--){
int l = read(), r = read();
int rt1, rt2, rt3;
treap.split(rt, r, rt2, rt3);
treap.split(rt, l - 1, rt1, rt2);
treap.reverse(rt2);
rt = treap.merge(treap.merge(rt1, rt2), rt3);
}
treap.output(rt);
return 0;
}
詳細信息
Test #1:
score: 0
Memory Limit Exceeded
input:
30 5 7 26 7 18 5 9 4 15 3 15