日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

BZOJ 3223: Tyvj 1729 文艺平衡树(splay)

發(fā)布時間:2025/5/22 编程问答 20 豆豆
生活随笔 收集整理的這篇文章主要介紹了 BZOJ 3223: Tyvj 1729 文艺平衡树(splay) 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

速度居然進前十了...第八...

?

splay, 區(qū)間翻轉(zhuǎn),用一個類似線段樹的lazy標記表示是否翻轉(zhuǎn)

----------------------------------------------------------------------------------------

#include<cstdio>#include<algorithm>#include<cstring>#include<iostream>#define rep(i, n) for(int i = 0; i < n; ++i)#define clr(x, c) memset(x, c, sizeof(x))using namespace std;const int maxn = 100000 + 5;int n;struct node *null, *pt;struct node {? ? node* ch[2];? ? int v, s;? ? bool flip;? ? node(int _v = 0) : v(_v), s(1), flip(0) {? ? ? ? ch[0] = ch[1] = null;? ? }? ? inline int cmp(int k) const {? ? ? ? k -= ch[0]->s;? ? ? ? if(k == 1) return -1;? ? ? ? return k <= 0 ? 0 : 1;? ? }? ? inline void maintain() {? ? ? ? s = ch[0]->s + ch[1]->s + 1;? ? }? ? inline void pushdown() {? ? ? ? if(flip) {? ? ? ? ? ? flip = 0;? ? ? ? ? ? swap(ch[0], ch[1]);? ? ? ? ? ? ch[0]->flip ^= 1;? ? ? ? ? ? ch[1]->flip ^= 1;? ? ? ? }? ? }? ? void *operator new (size_t) { return pt++; }};node* root;node N[maxn], Null;void rotate(node* &o, int d) {? ? node* k = o->ch[d^1];? ? o->ch[d^1] = k->ch[d];? ? k->ch[d] = o;? ? o->maintain(); k->maintain();? ? o = k;}void splay(node* &o, int k) {? ? o->pushdown();? ? int d = o->cmp(k);? ? if(d == 1) k -= o->ch[0]->s + 1;? ? if(d != -1) {? ? ? ? node* p = o->ch[d];? ? ? ? p->pushdown();? ? ? ? int d2 = p->cmp(k);? ? ? ? int k2 = d2 ? k - p->ch[0]->s - 1 : k;? ? ? ? if(d2 != -1) {? ? ? ? ? ? splay(p->ch[d2], k2);? ? ? ? ? ? d == d2 ? rotate(o, d^1) : rotate(o->ch[d], d);? ? ? ? }? ? ? ? rotate(o, d^1);? ? }}node* build(int l, int r) {? ? if(l >= r) return null;? ? int m = (l + r) >> 1;? ? node* o = new node(m);? ? if(l < m) o->ch[0] = build(l, m);? ? if(m + 1 < r) o->ch[1] = build(m + 1, r);? ? o->maintain();? ? return o;}void init() { pt = N; null = &Null; null->s = 0; root = build(0, n + 2); }void dfs(node* o) {? ? if(o == null) return;? ? o->pushdown();? ? dfs(o->ch[0]);? ? if(o->v >= 1 && o->v <= n) printf("%d ",o->v);? ? dfs(o->ch[1]);}inline int read() {int ans = 0, f = 1;char c = getchar();while(!isdigit(c)) {if(c == '-') f = -1;c = getchar();}while(isdigit(c)) {ans = ans * 10 + c - '0';c = getchar();}return f * ans;}int main() {? ? n = read();? ? int m = read();? ? init();? ? while(m--) {int l = read(), r = read();? ? ? ? if(l == r) continue;? ? ? ? splay(root, l);? ? ? ? splay(root->ch[1], r + 1 - root->ch[0]->s);? ? ? ? root->ch[1]->ch[0]->flip ^= 1;? ? }? ? dfs(root);? ? return 0;}

??

----------------------------------------------------------------------------------------?

3223: Tyvj 1729 文藝平衡樹

Time Limit:?10 Sec??Memory Limit:?128 MB
Submit:?1675??Solved:?931
[Submit][Status][Discuss]

Description

您需要寫一種數(shù)據(jù)結(jié)構(gòu)(可參考題目標題),來維護一個有序數(shù)列,其中需要提供以下操作:翻轉(zhuǎn)一個區(qū)間,例如原有序序列是5?4?3?2?1,翻轉(zhuǎn)區(qū)間是[2,4]的話,結(jié)果是5?2?3?4?1?

Input

第一行為n,m?n表示初始序列有n個數(shù),這個序列依次是(1,2……n-1,n)??m表示翻轉(zhuǎn)操作次數(shù)
接下來m行每行兩個數(shù)[l,r]?數(shù)據(jù)保證?1<=l<=r<=n?

Output

?

輸出一行n個數(shù)字,表示原始序列經(jīng)過m次變換后的結(jié)果?

Sample Input

5 3

1 3

1 3

1 4

Sample Output

4 3 2 1 5

HINT



N,M<=100000

Source

平衡樹

?

轉(zhuǎn)載于:https://www.cnblogs.com/JSZX11556/p/4468791.html

總結(jié)

以上是生活随笔為你收集整理的BZOJ 3223: Tyvj 1729 文艺平衡树(splay)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。