#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e5+11;
typedef long long ll; char str[maxn];
int a[26][maxn];
struct ST{
#define lc o<<1
#define rc o<<1|1
int num[maxn<<2],lazy[maxn<<2],lazy2[maxn<<2];
void pu(int o){
num[o]=num[lc]+num[rc];
}
void pd(int o,int l,int r){
if(~lazy[o]){
int m = l+r>>1;
lazy[lc]=lazy[rc]=lazy[o];
num[lc]=lazy[o]*(m-l+1);
num[rc]=lazy[o]*(r-m);
lazy[o]=-1;
}
}
void build(int o,int l,int r,int i){
lazy[o]=-1;
if(l==r){
num[o]=a[i][l];
return;
}
int m = l+r>>1;
build(lc,l,m,i);
build(rc,m+1,r,i);
pu(o);
}
void update(int o,int l,int r,int L,int R,int v){
if(L<=l&&r<=R){
lazy[o]=v;
num[o]=lazy[o]*(r-l+1);
return;
}
pd(o,l,r);
int m=l+r>>1;
if(L<=m) update(lc,l,m,L,R,v);
if(R>m) update(rc,m+1,r,L,R,v);
pu(o);
}
void pd2(int o,int l,int r,int L,int R){
lazy[o]=num[lc]=num[rc]=lazy[lc]=lazy[rc]=0;
}
inline void clean(int o,int l,int r,int L,int R){
// if(L<=l&&r<=R){
// lazy2[o]=0;
// num[o]=0;
// return;
// }
// pd(o,l,r);
// int m = l+r>>1;
// if(L<=m)clean(lc,l,m,L,R);
// if(R>m)clean(rc,m+1,r,L,R);
// pu(o);
update(o,l,r,L,R,0);
}
ll query(int o,int l,int r,int L,int R){
if(L<=l&&r<=R){
return num[o];
}
pd(o,l,r);
int m = l+r>>1;
ll ans = 0;
if(L<=m) ans+=query(lc,l,m,L,R);
if(R>m) ans+=query(rc,m+1,r,L,R);
return ans;
}
}st[26];
int n,m,li,ri,pos;
int t[26],odd,even;
int main(){
freopen("input.txt","r",stdin);
freopen("output.txt","w",stdout);
while(scanf("%d%d",&n,&m)!=EOF){
// for(int i = 0; i < 26; i++)st[i].init();
memset(a,0,sizeof a);
scanf("%s",str+1);
for(int i = 1; i <= n; i++){
a[str[i]-'a'][i]++;
}
for(int i = 0; i < 26; i++){
st[i].build(1,1,n,i);
}
for(int i = 1; i <= m; i++){
scanf("%d%d",&li,&ri);
int shift=0;
odd=even=pos=0;pos--;
for(int j = 0; j < 26; j++){
t[j]=st[j].query(1,1,n,li,ri);
if(t[j]&1){odd++;pos=j;}
else even++;
}
//
if(odd>1)continue;
else{
int lii=li,rii=ri;
if(~pos)t[pos]--;
for(int j = 0; j < 26; j++){
if(pos==j)st[j].clean(1,1,n,lii,rii);
if(0==t[j])continue;
st[j].clean(1,1,n,lii,rii);
st[j].update(1,1,n,li,li+(t[j]/2)-1,1);
li+=(t[j]/2);
}
for(int j = 0; j < 26; j++){
if(t[j]==0)continue;
st[j].update(1,1,n,ri-(t[j]/2)+1,ri,1);
ri-=(t[j]/2);
}
if(~pos){
if(t[pos]!=-1){
st[pos].update(1,1,n,li,ri,1);
}
}
}
}
//
for(int i = 1; i <= n; i++){
int tmp=-1;
for(int j = 0; j < 26; j++){
if(st[j].query(1,1,n,i,i)){tmp=j;break;}
}
char opt=tmp+'a';
if(i==n) printf("%c\n",opt);
else printf("%c",opt);
}
}
return 0;
}

Codeforces - 240F 是男人就上26棵线段树的更多相关文章

  1. Codeforces J. A Simple Task(多棵线段树)

    题目描述: Description This task is very simple. Given a string S of length n and q queries each query is ...

  2. Codeforces 558E A Simple Task(计数排序+线段树优化)

    http://codeforces.com/problemset/problem/558/E Examples input 1 abacdabcda output 1 cbcaaaabdd input ...

  3. CodeForces - 960F Pathwalks —— 主席树(n棵线段树)

    题目链接:https://vjudge.net/problem/CodeForces-960F You are given a directed graph with n nodes and m ed ...

  4. cf276E 两棵线段树分别维护dfs序和bfs序,好题回头再做

    搞了一晚上,错了,以后回头再来看 /* 对于每次更新,先处理其儿子方向,再处理其父亲方向 处理父亲方向时无法达到根,那么直接更新 如果能达到根,那么到兄弟链中去更新,使用bfs序 最后,查询结点v的结 ...

  5. UVA - 12424 Answering Queries on a Tree(十棵线段树的树链剖分)

    You are given a tree with N nodes. The tree nodes are numbered from 1 to N and have colors C1, C2,. ...

  6. Codeforces 558E A Simple Task(权值线段树)

    题目链接  A Simple Task 题意  给出一个小写字母序列和若干操作.每个操作为对给定区间进行升序排序或降序排序. 考虑权值线段树. 建立26棵权值线段树.每次操作的时候先把26棵线段树上的 ...

  7. [Codeforces 464E] The Classic Problem(可持久化线段树)

    [Codeforces 464E] The Classic Problem(可持久化线段树) 题面 给出一个带权无向图,每条边的边权是\(2^{x_i}(x_i<10^5)\),求s到t的最短路 ...

  8. CF240F (26颗线段树计数)

    题目链接:Topcoder----洛谷 题目大意: 给定一个长为n的由a到z组成的字符串,有m次操作,每次操作将[l,r]这些位置的字符进行重排,得到字典序最小的回文字符串,如果无法操作就不进行. 思 ...

  9. Codeforces Round #530 (Div. 2) F (树形dp+线段树)

    F. Cookies 链接:http://codeforces.com/contest/1099/problem/F 题意: 给你一棵树,树上有n个节点,每个节点上有ai块饼干,在这个节点上的每块饼干 ...

随机推荐

  1. Luogu 4251 [SCOI2015]小凸玩矩阵

    BZOJ 4443 二分答案 + 二分图匹配 外层二分一个最小值,然后检验是否能选出$n - k + 1$个不小于当前二分出的$mid$的数.对于每一个$a_{i, j} \geq mid$,从$i$ ...

  2. .net对Cookie的简单操作

    1 声明:HttpCookie MyCookie= new HttpCookie("test"); 2增加:MyCookie.Values.Add("key1" ...

  3. Mr_matcher的细节1

    1.NodeHandle类(或者NodeHandle句柄)的私有名称 1)句柄可以让你通过构造函数指定命名空间 ros::NodeHandle nh("my_namespace") ...

  4. WEB测试和APP测试区别

    Web测试和App测试从流程上来说,没有区别.都需要经历测试计划方案,用例设计,测试执行,缺陷管理,测试报告等相关活动.从技术上来说,WEB测试和APP测试其测试类型也基本相似,都需要进行功能测试.性 ...

  5. App测试从入门到精通之功能测试

    App的功能测试指的是针对软件需求以及用户要求针对APP功能进行测试.简单点理解就是保证App功能的正确性,不要系统出现Bug.让用户用户的舒服,用的爽!好了,我们看下关于App的功能测试要点有哪些? ...

  6. 编写高质量代码改善C#程序的157个建议——建议49:在Dispose模式中应提取一个受保护的虚方法

    建议49:在Dispose模式中应提取一个受保护的虚方法 在标准的Dispose模式中,真正的IDisposable接口的Dispose方法并没有做实际的清理工作,它其实是调用了下面的这个带bool参 ...

  7. 编写高质量代码改善C#程序的157个建议——建议6: 区别readonly和const的使用方法

    建议6: 区别readonly和const的使用方法 很多初学者分不清readonly和const的使用场合.在我看来,要使用const的理由只有一个,那就是效率.但是,在大部分应用情况下, “效率” ...

  8. LibreOJ 6004 圆桌聚餐 (最大流)

    题解:天啊,这道最大流真是水的一批……只需要每张桌子向每个单位建一条容量为1的边,源点向桌子建边,容量为桌子能坐的人数;单位向汇点建边,容量为单位人数即可,然后根据单位与桌子的连边值是否为一来了解每个 ...

  9. wcf文件上传时碰到的配置问题

    1.远程服务器返回了意外相应:(413) Request Entity Too Large 修改客户端配置maxReceivedMessageSize="2147483647" & ...

  10. cinder侧卸载卷流程分析

    cinder侧卸载卷分析,存储类型以lvm+iscsi的方式为分析基础在虚机卸载卷的过程中,主要涉及如下三个函数1)cinder.volume.api.begin_detaching 把volume的 ...