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块饼干,在这个节点上的每块饼干 ...
随机推荐
- 2-JRE System Libraty [eclipse-mars](unbound)
- bzoj5392 [Lydsy1806月赛]路径统计
传送门 分析 我们设sum[x]为小于等于x的点现在有多少联通 于是一个序列合法当且只当sum[R]-sum[L-1]=len且所有点度数不大于2 我们知道如果对于序列[L,R]满足条件则[L+1,R ...
- Java 集合框架必记框架图
- (转)Web API 入门指南 - 闲话安全
原文地址:http://www.cnblogs.com/developersupport/p/WebAPI-Security.html Web API入门指南有些朋友回复问了些安全方面的问题,安全方面 ...
- 关于在jeecms中css,图片,html,模板是如何组装成——part2
这是index.html折叠后的代码可以看出4部分:header+div+footer+right-fixed 好,先解决自己的第一个疑问,home.css是如何让一个巨丑无比的老汉子,变为年少的小欧 ...
- C#隐式FTPS (Implicit FTPS)
實現的方式是通過第三方程式庫實現的,當然最主要的是開源且免費,已測試過沒有問題! 目前還沒有直接取得目錄FileInfo list的方法,不過還好可以用GetDirectoryList來取得類似的結果 ...
- Dynamically loading unmanaged OCX in C#
You'll have to perform a number of steps that are normally taken of automatically when you use the t ...
- VSCODE 针对调试C语言时一闪而过解决办法
针对调试C语言时一闪而过解决办法 前提: 已经按照 C/C++ 已经安装 MINGW(并配置完成) 原因: 主要是因为tasks的配置没有写对 解决办法: tasks.json { // See h ...
- sklearn的train_test_split函数
train_test_split函数用于将矩阵随机划分为训练子集和测试子集,并返回划分好的训练集测试集样本和训练集测试集标签. from sklearn.model_selection import ...
- vi—终端中的编辑器
*:first-child { margin-top: 0 !important; } .markdown-body>*:last-child { margin-bottom: 0 !impor ...