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块饼干,在这个节点上的每块饼干 ...
随机推荐
- java就业指南 zookeeper分布式系统 zookeeper实现分布式锁 有用
目前几乎很多大型网站及应用都是分布式部署的,分布式场景中的数据一致性问题一直是一个比较重要的话题.分布式的CAP理论告诉我们“任何一个 分布式系统都无法同时满足一致性(Consistency).可用性 ...
- Luogu 4949 最短距离
这就是个水题. 一开始想把整个环找出来断开当一条链,然后其他部分正常链剖,两个点之间的路径如果经过环就考虑一下走哪边更快. 但是这样子还是太麻烦了. 我们可以直接断开环上的一条边,然后正常链剖,只要在 ...
- hdu 4678 Mine
HDU 4678 把点开空地时会打开的一大片区域看成一块,题目中说到,在一盘游戏 中,一个格子不可能被翻开两次,说明任意两块空地不会包含相同的格子. 那么就可以看成一个组合游戏. 当空地旁边没连任何数 ...
- css总结19:HTML5 Canvas(画布)
1 <canvas> 标签定义图形,比如图表和其他图像. 例1:简单使用: <canvas id="Canva" width="200" h ...
- (转)【推荐】使用Jquery+EasyUI进行框架项目开发案例讲解之一---员工管理源码分享
原文地址:http://www.cnblogs.com/huyong/p/3334848.html 在开始讲解之前,我们先来看一下什么是Jquery EasyUI?jQuery EasyUI是一组基于 ...
- 解决Tomcat错误信息:No 'Access-Control-Allow-Origin' header is present on the requested resource | Solving Tomcat Error: No 'Access-Control-Allow-Origin' header is present on the requested resource
最近在使用GeoServer调用Vector Tile服务时,经常会显示不出来结果.打开浏览器调试台,发现报No 'Access-Control-Allow-Origin' header is pre ...
- Thumbnail 图片帮助
public class Thumbnail { private Image srcImage; private string srcFileName; /// <summary> /// ...
- 将“100px” 转换为100
parseInt("100px") //结果是100
- C# return、continue、break
return 终止当前进程 可用循环判断,验证,等功能 if (ew == v) { PublicControlLib.Class.PublicProperties.ShowSuccess(); re ...
- js 封装常用方法
1. 获取数据类型 function getType(params) { , -) } 2. 深拷贝 function deepCopy(params) { var obj; if (typeof p ...