Codeforces 525B. Pasha and String【线段树 区间更新 单点查询】
来源:互联网 发布:星际争霸2数据库 编辑:程序博客网 时间:2024/05/18 18:01
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.
abcdef12
aedcbf
vwxyz22 2
vwxyz
abcdef31 2 3
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;}
- Codeforces 525B. Pasha and String【线段树 区间更新 单点查询】
- codeforces 315 B.Sereja and Array(线段树区间更新+单点更新+单点询问)
- codeforces-525B Pasha and String
- codeforces 525B Pasha and String
- codeforces 525b Pasha and String
- CodeForces 525B Pasha and String 水题
- Codeforces 525B Pasha and String 【思维】
- codeforces B. Pasha and String
- Codeforces Round #343 (Div. 2) B. Far Relative’s Problem (线段树+区间更新+单点查询)
- zoj (单点更新区间查询:线段树)
- 线段树单点更新和区间查询
- hdu1166(线段树单点更新区间查询)
- Codeforces--525B--Pasha and String(思维)
- Codeforces Round#297 B. Pasha and String
- codeforces 438D The Child and Sequence(线段树:单点更新+区间取模+区间和)
- CodeForces 315B(线段树+区间更新)
- hdu5316 Magician (线段树+单点更新+区间查询+区间合并)
- B. Pasha and String
- Angular JS 基础
- 内存分区分配算法
- C#—异常类
- Eclipse取得路径的方法
- bzoj 2310: ParkII
- Codeforces 525B. Pasha and String【线段树 区间更新 单点查询】
- Python学习记录
- 2015级C++第9周项目 动态存储分配及深复制
- ZOJ 1243 URLs
- hdoj-2519-新生晚会
- struts2使用redirectAction,运行后地址没有改变,result中没有配置该result
- 进度条的使用
- ViewHolder模式超简洁写法
- AIDL API级别详细解释