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;
    }

  • 相关阅读:
    论大型信息系统项目的范围管理
    算法分析与设计——回溯法实验报告
    算法分析与设计——贪心法实验报告
    算法分析与设计——动态规划法实验报告
    算法分析与设计——分治法实验报告
    机器学习与数据挖掘期末考试复习重点整理
    软件项目文档——Responsibility Assignment Matrix
    软件项目文档——Stakeholder Register
    在Eclipse中显示空格(space)和制表符(tab)
    Eclipse 每行 80 字符限制的提示线
  • 原文地址:https://www.cnblogs.com/rainydays/p/2199936.html
Copyright © 2011-2022 走看看