QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#793074 | #21403. 文艺平衡树 | GalenoJiao | WA | 0ms | 3584kb | C++14 | 2.5kb | 2024-11-29 16:31:58 | 2024-11-29 16:31:58 |
Judging History
answer
/*********************************************************************
Author: Galenojiao
Datetime: 2024/11/29 15:54:04
*********************************************************************/
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MAXN = 1e5+5;
int n, m;
struct Node{
int val, ch[2], fa, size;
bool rev;
}tree[MAXN];
int root = 0, ncnt = 0;
void pushup(int x){
if(x==0) return;
tree[x].size = tree[tree[x].ch[0]].size + tree[tree[x].ch[1]].size + 1;
}
void pushdown(int x){
if(x==0 || !tree[x].rev) return;
tree[tree[x].ch[0]].rev ^=1;
tree[tree[x].ch[1]].rev ^=1;
swap(tree[x].ch[0], tree[x].ch[1]);
tree[x].rev = false;
}
int get(int x){
return x==tree[tree[x].fa].ch[1];
}
void rotate(int x){
int y = tree[x].fa, z = tree[y].fa, ws = get(x);
pushdown(z); pushdown(y); pushdown(x);
tree[y].ch[ws] = tree[x].ch[ws^1];
if(tree[x].ch[ws^1]){
tree[ tree[x].ch[ws^1] ].fa = y;
}
tree[x].ch[ws^1] = y;
tree[y].fa = x;
if(z){
tree[z].ch[y==tree[z].ch[1]] = x;
}
tree[x].fa = z;
pushup(y);
pushup(x);
}
void splay(int x, int k){
int y, z;
while(tree[x].fa!=k){
y = tree[x].fa; z = tree[y].fa;
if(z!=k){
if(get(x)==get(y)){
rotate(y);
}else{
rotate(x);
}
}
rotate(x);
}
if(k==0){
root = x;
}
}
int kth(int k){
int cur = root;
while(cur){
pushdown(cur);
if(k<=tree[tree[cur].ch[0]].size){
cur = tree[cur].ch[0];
}else{
k-=tree[tree[cur].ch[0]].size;
if(k==1){
splay(cur, 0);
return cur;
}else{
k-=1;
cur = tree[cur].ch[1];
}
}
}
return -1;
}
void rev(int l, int r){
int pre = kth(l-1), succ = kth(r+1);
splay(pre, 0);
splay(succ, pre);
tree[tree[succ].ch[0]].rev ^=1;
}
int build(int l, int r, int fa){
int mid = (l+r)>>1;
int cur = ++ncnt;
tree[cur].val = mid;
tree[cur].size = 1;
tree[cur].fa = fa;
if(l<mid){
tree[cur].ch[0] = build(l, mid-1, cur);
}
if(mid<r){
tree[cur].ch[1] = build(mid+1, r, cur);
}
pushup(cur);
return cur;
}
void print(int x){
if(x==0) return;
print(tree[x].ch[0]);
if(tree[x].val >0 && tree[x].val<=n){
cout << tree[x].val << " ";
}
print(tree[x].ch[1]);
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0), cout.tie(0);
cin >> n >> m;
root = build(0, n+1, 0);
int l, r;
for(int i = 1; i<=m; ++i){
cin >> l >> r;
rev(l+1, r+1);
}
print(root);
return 0;
}
详细
Test #1:
score: 0
Wrong Answer
time: 0ms
memory: 3584kb
input:
30 5 7 26 7 18 5 9 4 15 3 15
output:
1 2 3 4 5 6 15 16 17 18 19 20 21 22 23 24 25 26 14 13 12 11 8 9 10 7 27 28 29 30
result:
wrong answer 1st lines differ - expected: '1 2 4 17 16 15 6 5 18 19 20 21...4 13 12 11 10 9 8 7 27 28 29 30', found: '1 2 3 4 5 6 15 16 17 18 19 20 ... 13 12 11 8 9 10 7 27 28 29 30 '