zoukankan      html  css  js  c++  java
  • poj2887

    线段树

    这种每次在第几个位置插入元素的题,都可以用线段树,从最后一次插入开始来查找每次插入的元素的最终位置。

    View Code
    #include <iostream>
    #include <cstdlib>
    #include <cstring>
    #include <cstdio>
    using namespace std;

    #define maxl 2000005
    #define maxn 2005

    struct Elem
    {
    bool cmd;
    int p;
    char ch;
    int pos;
    } elem[maxn];

    struct Node
    {
    Node *pl;
    Node *pr;
    int lnum, rnum, l, r;
    } tree[maxl * 3];

    char st1[maxl], st[maxl];
    int n, len, ncount;

    void input()
    {
    scanf("%s%d", st1, &n);
    len = strlen(st1);
    for (int i = 0; i < n; i++)
    {
    char st[5];
    scanf("%s", st);
    if (st[0] == 'I')
    {
    elem[i].cmd = true;
    scanf("%s%d", st, &elem[i].p);
    len++;
    if (elem[i].p > len)
    elem[i].p = len;
    elem[i].ch = st[0];
    }
    else
    {
    elem[i].cmd = false;
    scanf("%d", &elem[i].p);
    }
    }
    }

    void build_tree(Node *proot, int l, int r)
    {
    int mid = (l + r) / 2;
    proot->lnum = mid - l + 1;
    proot->rnum = r - mid;
    proot->l = l;
    proot->r = r;
    if (l == r)
    return;
    ncount++;
    proot->pl = tree + ncount;
    ncount++;
    proot->pr = tree + ncount;
    build_tree(proot->pl, l, mid);
    build_tree(proot->pr, mid + 1, r);
    }

    int query(Node *proot, int a)
    {
    if (proot->l == proot->r)
    return proot->l;
    if (a > proot->lnum)
    return query(proot->pr, a - proot->lnum);
    return query(proot->pl, a);
    }

    void update(Node *proot, int pos, int a)
    {
    if (proot->l == proot->r)
    return;
    int mid = (proot->l + proot->r) / 2;
    if (pos > mid)
    {
    proot->rnum += a;
    update(proot->pr, pos, a);
    }
    else
    {
    proot->lnum += a;
    update(proot->pl, pos, a);
    }
    }

    void work()
    {
    memset(st, 0, sizeof(st));
    for (int i = n - 1; i >= 0; i--)
    if (elem[i].cmd)
    {
    elem[i].pos = query(tree, elem[i].p);
    st[elem[i].pos] = elem[i].ch;
    update(tree, elem[i].pos, -1);
    }
    int j = 0;
    for (int i = 1; i <= len; i++)
    if (!st[i])
    st[i] = st1[j++];
    for (int i = 0; i < n; i++)
    if (elem[i].cmd)
    update(tree, elem[i].pos, 1);
    else
    printf("%c\n",st[query(tree, elem[i].p)]);
    }

    int main()
    {
    //freopen("t.txt", "r", stdin);
    input();
    ncount = 0;
    build_tree(tree, 1, len);
    work();
    return 0;
    }

  • 相关阅读:
    使用python对mysql主从进行监控,并调用钉钉发送报警信息
    CentOS7下安装gitlab
    nginx日志自动切割
    Mysql定时备份数据脚本
    Linux下搭建FTP服务
    linux系统盘使用率达到100%的问题查找和解决方法
    CentOS6.5+nginx+mysql+php(laravel)服务器环境搭建
    RHEL6和RHEL7恢复root用户密码
    在Dell R720服务器上安装ESXI5.5时会出现卡在LSI_MR3.V00的解决方法
    /23 /24 /26/28 /29 /30或10.0.0.1/29这样怎么算服务器IP数是多少?
  • 原文地址:https://www.cnblogs.com/rainydays/p/2199936.html
Copyright © 2011-2022 走看看