Codeforces - 240F 是男人就上26棵线段树
#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棵线段树的更多相关文章
- Codeforces J. A Simple Task(多棵线段树)
题目描述: Description This task is very simple. Given a string S of length n and q queries each query is ...
- Codeforces 558E A Simple Task(计数排序+线段树优化)
http://codeforces.com/problemset/problem/558/E Examples input 1 abacdabcda output 1 cbcaaaabdd input ...
- CodeForces - 960F Pathwalks —— 主席树(n棵线段树)
题目链接:https://vjudge.net/problem/CodeForces-960F You are given a directed graph with n nodes and m ed ...
- cf276E 两棵线段树分别维护dfs序和bfs序,好题回头再做
搞了一晚上,错了,以后回头再来看 /* 对于每次更新,先处理其儿子方向,再处理其父亲方向 处理父亲方向时无法达到根,那么直接更新 如果能达到根,那么到兄弟链中去更新,使用bfs序 最后,查询结点v的结 ...
- 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,. ...
- Codeforces 558E A Simple Task(权值线段树)
题目链接 A Simple Task 题意 给出一个小写字母序列和若干操作.每个操作为对给定区间进行升序排序或降序排序. 考虑权值线段树. 建立26棵权值线段树.每次操作的时候先把26棵线段树上的 ...
- [Codeforces 464E] The Classic Problem(可持久化线段树)
[Codeforces 464E] The Classic Problem(可持久化线段树) 题面 给出一个带权无向图,每条边的边权是\(2^{x_i}(x_i<10^5)\),求s到t的最短路 ...
- CF240F (26颗线段树计数)
题目链接:Topcoder----洛谷 题目大意: 给定一个长为n的由a到z组成的字符串,有m次操作,每次操作将[l,r]这些位置的字符进行重排,得到字典序最小的回文字符串,如果无法操作就不进行. 思 ...
- Codeforces Round #530 (Div. 2) F (树形dp+线段树)
F. Cookies 链接:http://codeforces.com/contest/1099/problem/F 题意: 给你一棵树,树上有n个节点,每个节点上有ai块饼干,在这个节点上的每块饼干 ...
随机推荐
- 690. Employee Importance员工权限重要性
[抄题]: You are given a data structure of employee information, which includes the employee's unique i ...
- xgboost 调参参考
XGBoost的参数 XGBoost的作者把所有的参数分成了三类: 1.通用参数:宏观函数控制. 2.Booster参数:控制每一步的booster(tree/regression). 3.学习目标参 ...
- 第八课 ROS的空间描述和变换
1.tf的实际应用 1)在机器人的配置中 从上面可以看出激光雷达中心距离机器人底座的中心有20cm,激光雷达的中心距机器人底座中心有10cm,如果激光雷达在障碍物前面0.3米,那么机器人底座离障碍物多 ...
- IE6支持兼容max-height、min-height CSS样式
1.IE6支持max-height解决方法 - TOP IE6支持最大高度解决CSS代码: .yangshi{max-height:1000px;_height:expression((doc ...
- C# 世界坐标 页面坐标 PageUnit PageScale
PageScale: 获取或设置此 Graphics 的世界单位和页单位之间的比例.PageUnit: 获取或设置用于此 Graphics 中的页坐标的度量单位. 话不多说,上代码: privat ...
- Robot Framework - 常用断言讲解
RobotFramework带有丰富的系统关键,使用时无需导入,直接使用,为写自动化用例带来了极大的方便:不能停留在知道或者是会得程度,只有熟练使用各关键字,才能提升自动化用例的写作效率. 下面将逐个 ...
- HandleErrorAttribute只能处理httpStatusCode为500的异常(服务器异常)
HandleErrorAttribute源代码: [AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, Inherited ...
- ubuntu 下python安装及hello world
//@desn:ubuntu 下python安装及hello world //@desn:码字不宜,转载请注明出处 //@author:张慧源 <turing_zhy@163.com> ...
- 【转】微信中MMAlert(半透明底部弹出菜单)的使用介绍
原文地址:http://blog.csdn.net/singwhatiwanna/article/details/8892930 果大家时常用过微信或者用过iphone,就会发现有种从底部弹出的半透明 ...
- SQLServer备份恢复助手(太强大了!)
下载地址: http://download.csdn.net/detail/gguozhenqian/8105779