uva12538 Version Controlled IDE(可持续化Treap * 模板, STL ext/rope(块状链表))

本文介绍了一种名为可持续化Treap的数据结构及其应用,并提供了详细的代码实现。此外,还介绍了另一种高效的数据结构——STL绳子(rope),并给出了具体的使用案例。

uva12538 Version Controlled IDE(可持续化Treap * 模板, STL ext/rope(块状链表))

学习:http://www.2cto.com/kf/201401/273863.html

本题是可持续化Treap 的基本题目,虽然可持续化的Treap基本操作不难,但是还没什么深研究,留作模板

可持续化数据结构还是用不好,待深入学习:范浩强谈数据结构

, CLJ的可持久化数据结构研究???

写下注意事项和理解:

(1)指针的使用,在使用之前必须先new()

(2)实现可持续化是Copy()中new()后在复制,若不是先可持续化在可直接复制

(3)注意:Split和Merge的使用


持续更新。。。

本题代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <cmath>
#include <cstdlib>
#include <fstream>
#include <vector>
#include <set>

using namespace std;
const double eps = 1e-10;
const int maxn = 1100000;
const int M = 51000;

struct node {
    node* ch[2];
    char c;
    int sz, r;
    void up()
    {
        sz = ch[0]->sz + ch[1]->sz + 1;
    }
}*null;
//node t[maxn];
node *root[M];

char s[1100];
int op, pos, num, version;
int tot;
int m, subt;
int step;

void newnode(node* &x, char cc = 0)
{
    x = new node();
    x->c = cc;
    x->sz = 1;
    x->r = rand();
    x->ch[0] = x->ch[1] = null;
}

void copy(node* &x, node* y)
{
    if (y == null) x = y;
    else newnode(x), *x = *y;///可持久化时,修改操作经过的路径要新建节点
}

void merge(node* &o, node* a, node* b)
{
    if (a == null) copy(o, b);
    else if (b == null) copy(o, a);
    else if (a->r < b->r)
    {
        copy(o, a);
        merge(o->ch[1], a->ch[1], b);
        o->up();
    }
    else
    {
        copy(o, b);
        merge(o->ch[0], a, b->ch[0]);
        o->up();
    }
}
void split(node* o, node* &a, node* &b, int k)
{
    if (!k) {
        copy(b, o);
        a = null;
    }
    else if (o->sz <= k){
       copy(a, o);
       b = null;
    }
    else if (o->ch[0]->sz >= k)
    {
        copy(b, o);
        split(o->ch[0], a, b->ch[0], k);
        b->up();
    }
    else
    {
        copy(a, o);
        split(o->ch[1], a->ch[1], b, k - o->ch[0]->sz - 1);
        a->up();
    }
}

void build(node* &o, int l, int r)
{
    if (l > r) return ;
    int m = (l + r) >> 1;
    newnode(o, s[m - 1]);
    build(o->ch[0], l, m - 1);
    build(o->ch[1], m + 1, r);
    o->up();
}


void out(node *o)
{
    if (o->ch[0] != null)
        out(o->ch[0]);
    printf("%c", o->c);
    if (o->c == 'c') subt++;
    if (o->ch[1] != null)
        out(o->ch[1]);
}

void solve_insert(node* &o, int pos, char s[])
{
    int n = strlen(s);
    node *a, *b, *c, *d;
    build(a, 1, n);
    split(root[step - 1], b, c, pos);
    merge(d, b, a);
    merge(o, d, c);

}
void solve_delet(node* &o, int pos, int num)
{
    node *a, *b, *c, *d;
    split(root[step - 1], a, b, pos - 1);
    split(b, b, c, num);///可同时使用b b???
    merge(o, a, c);
}

void solve_query(int version, int pos, int num)
{
    node *a, *b, *c, *d;

    split(root[version], a, b, pos - 1);
    split(b, c, d, num);
    out(c);
    puts("");
}


void Init()
{
    tot = 0;
    newnode(null);
    null->sz = 0;

    for (int i = 0; i <= m; i++)
        root[i] = null;
}

int main()
{
    scanf("%d", &m);
    Init();
    subt = 0;
    step = 0;
    while (m--)
    {
        scanf("%d", &op);
        if (op == 1)
        {
            scanf("%d", &pos);
            scanf("%s", s);
            solve_insert(root[++step], pos - subt, s);
        }
        else if (op == 2)
        {
            scanf("%d%d", &pos, &num);
            solve_delet(root[++step], pos - subt, num - subt);
        }
        else if (op == 3)
        {
            scanf("%d%d%d", &version, &pos, &num);
            solve_query(version - subt, pos - subt, num - subt);
        }

    }

}


还有一种STL的解法:

#include <ext/rope>

using namespace __gnu_cxx;

crope t, trp[12], tmp;

具体使用见代码。。。

此STL实现的是块状链表,效率和上面的几乎一样,具体是n*(n^0.5)参考来源:http://www.cnblogs.com/zhsl/archive/2013/05/06/3062000.html

STL。。。

//#pragma warning (disable: 4786)
//#pragma comment (linker, "/STACK:16777216")
//HEAD
#include <cstdio>
#include <ctime>
#include <cstdlib>
#include <cstring>
#include <queue>
#include <string>
#include <set>
#include <stack>
#include <map>
#include <cmath>
#include <vector>
#include <iostream>
#include <algorithm>

#include <ext/rope>///!!
using namespace __gnu_cxx;///!!
using namespace std;


typedef long long LL;
crope t, trp[50005], tmp;///!!
char s[205];
int n, nowv, subt, version;
int op, pos, num;

int main ()
{
    scanf("%d", &n);
    subt = 0;
    nowv = 0;
    while (n--)
    {
        scanf("%d", &op);
        if (op == 1) {
            scanf("%d%s", &pos, s);
            pos -= subt;
            t.insert(pos, s);///在pos后插入串s
            trp[++nowv] = t;
        }
        else if (op == 2) {
            scanf("%d%d", &pos, &num);
            pos -= subt; num -= subt;
            t.erase(pos - 1, num);///删除pos-1后面num个字母
            trp[++nowv] = t;
        }
        else {
            scanf("%d%d%d", &version, &pos, &num);
            version -= subt; pos -= subt; num -= subt;
            tmp = trp[version].substr(pos - 1, num);///取出pos - 1后面的num个字母
            subt += count(tmp.begin(), tmp.end(), 'c');///统计c的个数
            cout << tmp << endl;///输出tmp
        }
    }
    return 0;
}



你确定?linuxHCI 到底是什么 通俗易懂的讲 我给你看一下linuxHCI 文件夹下面的东西 #include <stdio.h> #include <string.h> #include <stdlib.h> #include "LinuxSPPLE.h" /* Main Application Prototypes and Constants. */ #include "SS1BTPS.h" /* Includes for the SS1 Bluetooth Protocol Stack. */ #include "SS1BTDBG.h" /* Includes/Constants for Bluetooth Debugging. */ #include "SS1BTGAT.h" /* Includes for the SS1 GATT Profile. */ #include "SS1BTGAP.h" /* Includes for the SS1 GAP Service. */ #include "SS1BTDIS.h" /* Inlcudes for Device Information Service. */ #define printErr(fmt, args...) printf("\033[1m[ %s ] %03d: "fmt"\033[0m", __FUNCTION__, __LINE__, ##args) #define printWar(fmt, args...) printf("\033[4m[ %s ] %03d: "fmt"\033[0m", __FUNCTION__, __LINE__, ##args) #define NUM_EXPECTED_PARAMETERS_USB (2) /* Denotes the number*/ /* of command line */ /* parameters */ /* accepted at Run */ /* Time when running */ /* in USB Mode. */ #define NUM_EXPECTED_PARAMETERS_UART (4) /* Denotes the */ /* number of command */ /* line parameters */ /* accepted at Run */ /* Time when running */ /* in UART Mode. */ #define USB_PARAMETER_VALUE (0) /* Denotes the value */ /* passed in on the */ /* command line for */ /* running with the */ /* transport set to */ /* USB. */ #define UART_PARAMETER_VALUE (1) /* Denotes the value */ /* passed in on the */ /* command line for */ /* running with the */ /* transport set to */ /* UART. */ #define BCSP_PARAMETER_VALUE (2) /* Denotes the value */ /* passed in on the */ /* command line for */ /* running with the */ /* transport set to */ /* BCSP. */ #define MAX_SUPPORTED_COMMANDS (36) /* Denotes the */ /* maximum number of */ /* User Commands that*/ /* are supported by */ /* this application. */ #define MAX_COMMAND_LENGTH (64) /* Denotes the max */ /* buffer size used */ /* for user commands */ /* input via the */ /* User Interface. */ #define MAX_NUM_OF_PARAMETERS (5) /* Denotes the max */ /* number of */ /* parameters a */ /* command can have. */ #define DEFAULT_IO_CAPABILITY (licNoInputNoOutput) /* Denotes the */ /* default I/O */ /* Capability that is*/ /* used with Pairing.*/ #define DEFAULT_MITM_PROTECTION (TRUE) /* Denotes the */ /* default value used*/ /* for Man in the */ /* Middle (MITM) */ /* protection used */ /* with Secure Simple*/ /* Pairing. */ #define DEFAULT_SECURE_CONNECTIONS (TRUE) /* Denotes the */ /* default value used*/ /* for Secure */ /* Connections. */ #define SPPLE_DATA_BUFFER_LENGTH (BTPS_CONFIGURATION_GATT_DEFAULT_MAXIMUM_SUPPORTED_MTU_SIZE) /* Defines the length*/ /* of a SPPLE Data */ /* Buffer. */ #define SPPLE_DATA_CREDITS (SPPLE_DATA_BUFFER_LENGTH*3) /* Defines the */ /* number of credits */ /* in an SPPLE Buffer*/ #define LED_TOGGLE_RATE_SUCCESS (500) /* The LED Toggle */ /* rate when the demo*/ /* successfully */ /* starts up. */ #define CONSOLE_MONITOR_RATE (10) /* The rate at which */ /* the console will */ /* be tested for */ /* input. */ #define NO_COMMAND_ERROR (-1) /* Denotes that no */ /* command was */ /* specified to the */ /* parser. */ #define INVALID_COMMAND_ERROR (-2) /* Denotes that the */ /* Command does not */ /* exist for */ /* processing. */ #define EXIT_CODE (-3) /* Denotes that the */ /* Command specified */ /* was the Exit */ /* Command. */ #define FUNCTION_ERROR (-4) /* Denotes that an */ /* error occurred in */ /* execution of the */ /* Command Function. */ #define TO_MANY_PARAMS (-5) /* Denotes that there*/ /* are more */ /* parameters then */ /* will fit in the */ /* UserCommand. */ #define INVALID_PARAMETERS_ERROR (-6) /* Denotes that an */ /* error occurred due*/ /* to the fact that */ /* one or more of the*/ /* required */ /* parameters were */ /* invalid. */ #define UNABLE_TO_INITIALIZE_STACK (-7) /* Denotes that an */ /* error occurred */ /* while Initializing*/ /* the Bluetooth */ /* Protocol Stack. */ #define INVALID_STACK_ID_ERROR (-8) /* Denotes that an */ /* occurred due to */ /* attempted */ /* execution of a */ /* Command when a */ /* Bluetooth Protocol*/ /* Stack has not been*/ /* opened. */ #define UNABLE_TO_REGISTER_SERVER (-9) /* Denotes that an */ /* error occurred */ /* when trying to */ /* create a Serial */ /* Port Server. */ #define EXIT_MODE (-10) /* Flags exit from */ /* any Mode. */
最新发布
11-27
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值