题意:给一个字符串(<=1000000)和n个操作(<2000),每个操作可以在某个位置插入一个字符,或者查询该位置的字符。问查询结果。

思路:块状数组。

如果将原来的字符串都存在一起,每次插入肯定会超时。

而操作数比较少,考虑使用分块法。假设原字符串长度为L,则取每块长度l=sqrt(L)。这样每次插入,我们需要用sqrt(L)的时间找到对应的块,再用sqrt(L)在该块进行插入。查询同样需要sqrt(L)找到该块,如果用数组实现可以O(1)找到目标元素。(我尝试用stl链表来做,结果超时了)

这样最坏的情况下,一个块最多有2000个字符,当然操作不会超时,但是数组要开得足够大。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <list>
#define ll long long
using namespace std;
int Maxn, N ;
];
struct BlockList
{
    int size;
    ];
    int at(int pos)
    {
        return dat[pos];
    }
    void insert(int pos,char c)
    {
        ];
        dat[pos]=c;
    }
    void push_back(char c)
    {
        dat[++size]=c;
    }
};
BlockList block[];
char query(int s,int p)
{
    return block[s].at(p);
}
void insert(int s,int p,char c)
{
    p=min(p,block[s].size+);
    block[s].insert(p,c);
}
void maintain()
{
    ; i<=Maxn; ++i)
        sum[i]=sum[i-]+block[i].size;
}
void MyInsert(int pos,char c)
{
    ,sum++Maxn,pos)-(sum);
    insert(p,pos-sum[p-],c);
    maintain();
}
int MyQuery(int pos)
{
    ,sum++Maxn,pos)-(sum);
    ]);
}
];
void init()
{
    int len=strlen(str)+N;
    Maxn=sqrt(len*;
    ; str[i]; ++i)
        block[i/Maxn+].push_back(str[i]);
    maintain();
}
int main()
{
    gets(str);
    int pos;
    ],s[];
    scanf("%d",&N);
    init();
    while(N--)
    {
        scanf("%s",p);
        ]=='I')
        {
            scanf("%s%d",s,&pos);
            MyInsert(pos,s[]);
        }
        else
        {
            scanf("%d",&pos);
            printf("%c\n",MyQuery(pos));
        }
    }
    ;
}

POJ 2887 Big String (块状数组)的更多相关文章

  1. Poj 2887 Big String(块状数组)

    Big String Time Limit: 1000MS Memory Limit: 131072K Description You are given a string and supposed ...

  2. POJ 2887 Big String(块状链表)

    题目大意 给一个字符串,长度不超过 106,有两种操作: 1. 在第 i 个字符的前面添加一个字符 ch 2. 查询第 k 个位置是什么字符 操作的总数不超过 2000 做法分析 好多不同的做法都可以 ...

  3. poj 2887 Big String

    题目连接 http://poj.org/problem?id=2887 Big String Description You are given a string and supposed to do ...

  4. Big String 块状数组(或者说平方分割)

    Big String 给一个字符串,长度不超过 106,有两种操作: 1. 在第 i 个字符的前面添加一个字符 ch 2. 查询第 k 个位置是什么字符 操作的总数不超过 2000 如果直接模拟的话, ...

  5. hdu 3553 Just a String (后缀数组)

    hdu 3553 Just a String (后缀数组) 题意:很简单,问一个字符串的第k大的子串是谁. 解题思路:后缀数组.先预处理一遍,把能算的都算出来.将后缀按sa排序,假如我们知道答案在那个 ...

  6. codefroce D. Powerful array[初识块状数组]

    codefroce D. Powerful array[初识块状数组] 由于是初始所以,仅仅能先用别人的分析.囧... 题目: 给定一个数列:A1, A2,--,An,定义Ks为区间(l,r)中s出现 ...

  7. 范围运算符和索引的最终运算符 ^ 在string 和数组中的应用

    //范围运算符在string 和数组中的应用 static void Main(string[] args) { string examplestring = "123456789" ...

  8. POJ 2887:Big String(分块)

    http://poj.org/problem?id=2887 题意:给出一个字符串,还有n个询问,第一种询问是给出一个位置p和字符c,要在位置p的前面插入c(如果p超过字符串长度,自动插在最后),第二 ...

  9. Big String(poj 2887)

    题意: 给你一个不超过1e6的字符串,和不超过2000次的操作 操作分为两种: 1.将一个字符插入到某个位置的前面 2.询问当前位置的字符 /* 块状链表模板水题(我的智商也就能做这种题了). 观察题 ...

随机推荐

  1. 十问 Linux 虚拟内存管理 (glibc) (二)

    版权声明:本文由陈福荣原创文章,转载请注明出处: 文章原文链接:https://www.qcloud.com/community/article/184 来源:腾云阁 https://www.qclo ...

  2. 日期操作类--Calendar类

    Calendar-API Calendar类 通过Date和DateFormat能够格式化并创建一个日期对象了,但是我们如何才能设置和获取日期数据的特定部分呢,比如说小时,日,或者分钟? 我们又如何在 ...

  3. jq实现楼层切换效果

    <!DOCTYPE html> <html> <head> <style> * { margin: 0; padding: 0; box-sizing: ...

  4. selenium+python笔记4

    #!/usr/bin/env python # -*- coding: utf-8 -*- """ @desc: 使用unittest组织用例 ""& ...

  5. [Tomcat] Tomcat远程调试

    如何用eclispe远程调试tomcat 关键步骤: 1)修改启动脚本startup.bat 复制startup.bat为startup-debug.bat,然后打开startup-debug.bat ...

  6. ThreadLocal 那点事儿

    原文出处: 黄勇 ThreadLocal,直译为“线程本地”或“本地线程”,如果你真的这么认为,那就错了!其实,它就是一个容器,用于存放线程的局部变量,我认为应该叫做 ThreadLocalVaria ...

  7. struts2 mybatis spring hibernate 框架 pom.xml配置 下载地址

    访问以下地址:搜索需要框架的配置  选择需要的版本 http://mvnrepository.com

  8. Eclipse启动tomcat 报“ A child container failed during start”

    org.apache.catalina.LifecycleException: Failed to start component [StandardServer[8005]]   at org.ap ...

  9. 《JavaScript权威指南》读书笔记(四)

    日期:2015-12-06 事件传播:1.捕捉阶段2.运行阶段3.起泡阶段cookie和客户端持久性::HTML5引入了web应用缓存.LocalStorage.SessionStorage:使用XM ...

  10. Java script基础

    Java script基础 Js的每个语句后面都要有分号. <script  type="text/java script">所有JS内容</script> ...