QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#342541 | #21403. 文艺平衡树 | Ishy# | ML | 0ms | 3960kb | C++14 | 3.4kb | 2024-03-01 12:29:24 | 2024-03-01 12:29:24 |
Judging History
answer
// Sea, You & Me
#include<bits/stdc++.h>
#define LL long long
#define DB double
#define MOD 1000000007
#define ls(x) (x << 1)
#define rs(x) (x << 1 | 1)
#define lowbit(x) ((-x) & x)
#define MP make_pair
#define MT make_tuple
#define VI vector<int>
#define VL vector<LL>
#define VII VI::iterator
#define VLI VL::iterator
#define all(x) x.begin(), x.end()
#define EB emplace_back
#define PII pair<int, int>
#define SI set<int>
#define SII SI::iterator
#define fi first
#define se second
using namespace std;
template<typename T> void chkmn(T &a, const T b) { (a > b) && (a = b); }
template<typename T> void chkmx(T &a, const T b) { (a < b) && (a = b); }
void Inc(int &a, const int &b) { ((a += b) >= MOD) && (a -= MOD); }
void Dec(int &a, const int &b) { ((a -= b) < 0) && (a += MOD); }
void Mul(int &a, const int &b) { a = 1LL * a * b % MOD; }
void Sqr(int &a) { a = 1LL * a * a % MOD; }
int inc(const int &a, const int &b) { return (a + b >= MOD) ? a + b - MOD : a + b; }
int dec(const int &a, const int &b) { return (a - b < 0) ? a - b + MOD : a - b; }
int mul(const int &a, const int &b) { return 1LL * a * b % MOD; }
int sqr(const int &a) { return 1LL * a * a % MOD; }
int qwqmi(int x, int k = MOD - 2)
{
int res = 1;
while(k)
{
if(k & 1) Mul(res, x);
k >>= 1, Sqr(x);
}
return res;
}
template<typename T> void read(T &x)
{
x = 0;
int f = 1;
char ch = getchar();
while(!isdigit(ch))
{
if(ch == '-')
f = -1;
ch = getchar();
}
while(isdigit(ch))
{
x = (x << 1) + (x << 3) + (ch ^ 48);
ch = getchar();
}
x = x * f;
}
mt19937 mtrand(time(0));
const int N = 1e5 + 5;
int n, Q;
struct Akagi
{
int root = 0;
int rootl, rootr, rootm;
int tot = 0;
struct FHQ
{
int son[2];
int v;
int sz;
int dat;
int rev;
}tr[N];
void pushup(int p)
{
tr[p].sz = tr[tr[p].son[0]].sz + tr[tr[p].son[1]].sz + 1;
}
int build(int l, int r)
{
if(l > r) return 0;
int mid = l + (r - l) / 2;
tr[mid].v = mid;
tr[mid].sz = 1;
tr[mid].dat = mtrand();
tr[mid].son[0] = build(l, mid - 1);
tr[mid].son[1] = build(mid + 1, r);
pushup(mid);
return mid;
}
void pushdown(int p)
{
if(tr[p].rev)
{
swap(tr[p].son[0], tr[p].son[1]);
tr[tr[p].son[0]].rev ^= 1;
tr[tr[p].son[1]].rev ^= 1;
tr[p].rev = 0;
}
}
void split(int u, int &l, int &r, int x)
{
if(!u) return l = r = 0, void();
pushdown(u);
if(tr[tr[u].son[0]].sz + 1 <= x)
{
l = u;
split(tr[u].son[1], tr[l].son[1], r, x - tr[tr[u].son[0]].sz - 1);
return pushup(l);
}
else
{
r = u;
split(tr[u].son[0], l, tr[r].son[0], x);
return pushup(r);
}
}
int merge(int x, int y)
{
if(!x || !y) return x | y;
pushdown(x), pushdown(y);
if(tr[x].dat <= tr[y].dat)
{
tr[x].son[1] = merge(tr[x].son[1], y);
return pushup(x), x;
}
else
{
tr[y].son[0] = merge(x, tr[y].son[0]);
return pushup(y), y;
}
}
void reverse(int l, int r)
{
split(root, rootl, rootr, r);
split(root, rootl, rootm, l - 1);
tr[rootm].rev ^= 1;
root = merge(rootl, merge(rootm, rootr));
}
void print(int u)
{
if(!u) return;
pushdown(u);
print(tr[u].son[0]);
printf("%d ", tr[u].v);
print(tr[u].son[1]);
}
}akg;
int main()
{
read(n), read(Q);
akg.root = akg.build(1, n);
for(int cas = 1; cas <= Q; ++cas)
{
int l, r;
read(l), read(r);
akg.reverse(l, r);
}
akg.print(akg.root);
return 0;
}
详细
Test #1:
score: 100
Accepted
time: 0ms
memory: 3960kb
input:
30 5 7 26 7 18 5 9 4 15 3 15
output:
1 2 4 17 16 15 6 5 18 19 20 21 22 23 3 24 25 26 14 13 12 11 10 9 8 7 27 28 29 30
result:
ok single line: '1 2 4 17 16 15 6 5 18 19 20 21... 13 12 11 10 9 8 7 27 28 29 30 '
Test #2:
score: -100
Memory Limit Exceeded
input:
100 10 9 29 10 68 7 72 15 73 57 79 4 39 11 69 45 61 1 42 15 21