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块饼干,在这个节点上的每块饼干 ...
随机推荐
- 通明讲JDBC(一)–认识JDBC
本章记录了jdbc的简单使用方式! 0,jdbc的作用 1,jdbc入门准备工作 2,jdbc注册驱动 3,使用jdbc对数据库CRUD 0,jdbc的作用 与数据库建立连接.发送操作数据库的语句并处 ...
- Ubuntu 查看软件版本
Boost dpkg -S /usr/include/boost/version.hpp OpenCV pkg-config --modversion opencv
- sencha:日期选择组件datepicker
来源于<sencha touch权威指南> ------------------------------- 除app.js外,其它内容都与上一篇博客里的内容相同.app.js代码如下: E ...
- C# Code 非常好的学习博客
https://ardalis.com/how-to-become-master-writing-c-code
- Codeforces Round #272 (Div. 1) A. Dreamoon and Sums(数论)
题目链接 Dreamoon loves summing up something for no reason. One day he obtains two integers a and b occa ...
- .net Reflection(反射)- 二
反射 Reflection 中访问方法 新建一个ClassLibrary类库: public class Student { public string Name { get; set; } publ ...
- 变量声明和定义的关系------c++ primer
为了允许把程序分成多个逻辑部分来编写,c++语言支持分离式编译机制 为了支持分离式编译,c++语言把声明和定义区分开来.声明(declaration)使得名字为程序所知,一个文件如果想使用别处定义的名 ...
- Duration Assertion(持续时间)
Duration Assertion用来测试每一个响应的时间是否小于给定的值,任何超过给定毫秒数的响应都会标记为失败. Duration in milliseconds:响应的持续时间是否在给定的值范 ...
- ZED 常用资料汇总
Calibration file Location: /usr/local/zed/settings/SN10027507.conf I suggest the calibration file sh ...
- PTA数据结构之 List Leaves
Given a tree, you are supposed to list all the leaves in the order of top down, and left to right. I ...