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 2·ai ≤ |s|.
You face the following task: determine what Pasha's string will look like after m days.
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 ≤ ai; 2·ai ≤ |s|) — the position from which Pasha started transforming the string on the i-th day.
In the first line of the output print what Pasha's string s will look like after m days.
abcdef 1 2
aedcbf
vwxyz 2 2 2
vwxyz
abcdef 3 1 2 3
fbdcea
恩,题意是字符串,长度为len,给出一系列数a 意思是将字符串从 a 到 len-a+1的字符反转一下,线段树,记录每个区间的状态,反转两次等于没有反转,所以状态就两种,区间更新,单点查询,查询时就顺便反转了。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#define maxn 100020
using 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;
}