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

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
abcdef
1
2
output
aedcbf
input
vwxyz
2
2 2
output
vwxyz
input
abcdef
3
1 2 3
output
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;
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值