Codeforces 525B. Pasha and String【线段树 区间更新 单点查询】

来源:互联网 发布:星际争霸2数据库 编辑:程序博客网 时间:2024/05/18 18:01

B. Pasha and String
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Pasha got a very beautiful string s for his birthday, the string consists of lowercase Latin letters. The letters in the string are numbered from 1 to |s| from left to right, where |s| is the length of the given string.

Pasha didn't like his present very much so he decided to change it. After his birthday Pasha spent m days performing the following transformations on his string — each day he chose integer ai and reversed a piece of string (a segment) from position ai to position|s| - ai + 1. It is guaranteed that ai ≤ |s|.

You face the following task: determine what Pasha's string will look like after m days.

Input

The first line of the input contains Pasha's string s of length from 2 to 2·105 characters, consisting of lowercase Latin letters.

The second line contains a single integer m (1 ≤ m ≤ 105) —  the number of days when Pasha changed his string.

The third line contains m space-separated elements ai (1 ≤ aiai ≤ |s|) — the position from which Pasha started transforming the string on the i-th day.

Output

In the first line of the output print what Pasha's string s will look like after m days.

Examples
input
abcdef12
output
aedcbf
input
vwxyz22 2
output
vwxyz
input
abcdef31 2 3
output
fbdcea

恩,题意是字符串,长度为len,给出一系列数a 意思是将字符串从 a 到 len-a+1的字符反转一下,线段树,记录每个区间的状态,反转两次等于没有反转,所以状态就两种,区间更新,单点查询,查询时就顺便反转了。


#include<iostream>#include<cstdio>#include<cstring>#include<cmath>#define maxn 100020using namespace std;char str[maxn<<1];int len;struct lnode{    int l,r,c,mark;}node[maxn<<2];void pushdown(int o){    if(node[o].mark)    {        node[o<<1].c^=1;        node[o<<1|1].c^=1;        node[o<<1].mark^=1;        node[o<<1|1].mark^=1;        node[o].mark=0;    }}void build(int o,int l,int r){    node[o].l=l;    node[o].r=r;    node[o].c=0;    node[o].mark=0;    if(l==r)        return ;    int mid=(l+r)>>1;    build(o<<1,l,mid);    build(o<<1|1,mid+1,r);}void update(int o,int l,int r){    if(node[o].l==l&&node[o].r==r)    {        node[o].c^=1;        node[o].mark^=1;        return ;    }    pushdown(o);    int mid=(node[o].l+node[o].r)>>1;    if(r<=mid)        update(o<<1,l,r);    else if(l>mid)        update(o<<1|1,l,r);    else    {        update(o<<1,l,mid);        update(o<<1|1,mid+1,r);    }}void query(int o,int aim){    if(node[o].l==node[o].r)    {        if(node[o].c)            swap(str[aim],str[len-aim-1]);        return ;    }    pushdown(o);    int mid=(node[o].l+node[o].r)>>1;    if(aim<=mid)        query(o<<1,aim);    else        query(o<<1|1,aim);}int main(){    int m,a,middle;    while(~scanf("%s",str))    {        len=strlen(str);        middle=len/2;        build(1,0,middle-1);        scanf("%d",&m);        while(m--)        {            scanf("%d",&a);            update(1,a-1,middle-1);        }        for(int i=0;i<middle;++i)            query(1,i);        printf("%s\n",str);    }    return 0;}


0 0
原创粉丝点击