[Codeforces558E]A Simple Task 线段树
题意:给定一个长度不超过 \(10^5\) 的字符串(小写英文字母),和不超过5000个操作。
每个操作 L R K 表示给区间[L,R]的字符串排序,K=1为升序,K=0为降序。
最后输出最终的字符串。
题解
发现只有小写英文字母,26个,直接建26棵线段树,排序的时候,直接统计每个字母的个数暴力安排即可
好像见过值域 \(10^9\) 的,不过不知道在哪了,先留个坑,等看到了或者记起来了再补
#include<bits/stdc++.h>
#define REP(i,a,b) for(int i(a);i<=(b);++i)
using namespace std;
typedef long long ll;
inline int read(){char c;int w;
while(!isdigit(c=getchar()));w=c&15;
while(isdigit(c=getchar()))w=w*10+(c&15);return w;
}
inline char smax(int&x,const int&y){return x<y?x=y,1:0;}
inline char smin(int&x,const int&y){return x>y?x=y,1:0;}
const int N=1e5+5;
struct node{
int ls,rs,w,ch;
}t[N*120];
int rt[26],cnt;
inline void add(int&o,int w,int len){if(!o)o=++cnt;t[o].ch=w,t[o].w=len*w;}
inline void pushdown(int&o,int len){
if(~t[o].ch){
add(t[o].ls,t[o].ch,len-(len>>1));
add(t[o].rs,t[o].ch,len>>1);
t[o].ch=-1;
}
}
inline void pushup(int o){t[o].w=t[t[o].ls].w+t[t[o].rs].w;}
inline void update(int&o,int l,int r,int x,int y){
pushdown(o,r-l+1);
if(x<=l&&r<=y)return add(o,1,r-l+1);
if(!o)t[o=++cnt].ch=-1;int mid=l+r>>1;
if(x<=mid)update(t[o].ls,l,mid,x,y);if(y>mid)update(t[o].rs,mid+1,r,x,y);
pushup(o);
}
inline void ins(int&o,int l,int r,int x){
if(!o)t[o=++cnt].ch=-1;++t[o].w;
if(l==r)return;int mid=l+r>>1;
x<=mid?ins(t[o].ls,l,mid,x):ins(t[o].rs,mid+1,r,x);
}
inline int query(int&o,int l,int r,int x,int y){
if(!o||x>r||y<l)return 0;pushdown(o,r-l+1);
if(x<=l&&r<=y){int ans=t[o].w;return t[o].w=t[o].ch=0,ans;}
int mid=l+r>>1,ans=query(t[o].ls,l,mid,x,y)+query(t[o].rs,mid+1,r,x,y);
return pushup(o),ans;
}
char s[N];
int c[26];
int main(){
int n=read(),q=read();
scanf("%s",s+1);
REP(i,1,n)ins(rt[s[i]-'a'],1,n,i);
while(q--){
int l=read(),r=read(),op=read();
REP(i,0,25)c[i]=query(rt[i],1,n,l,r);
if(op){
int p=l;
REP(i,0,25)if(c[i])update(rt[i],1,n,p,p+c[i]-1),p+=c[i];
}
else{
int p=r;
REP(i,0,25)if(c[i])update(rt[i],1,n,p-c[i]+1,p),p-=c[i];
}
}
REP(i,1,n)REP(j,0,25)if(query(rt[j],1,n,i,i)){putchar(j+'a');break;}
return 0;
}
[Codeforces558E]A Simple Task 线段树的更多相关文章
- Codeforces Round #312 (Div. 2) E. A Simple Task 线段树
E. A Simple Task 题目连接: http://www.codeforces.com/contest/558/problem/E Description This task is very ...
- Codeforces Round #312 (Div. 2) E. A Simple Task 线段树+计数排序
题目链接: http://codeforces.com/problemset/problem/558/E E. A Simple Task time limit per test5 secondsme ...
- CodeForces 588E A Simple Task(线段树)
This task is very simple. Given a string S of length n and q queries each query is on the format i j ...
- Codeforces Round #312 (Div. 2) E. A Simple Task 线段树 延时标记
E. A Simple Task time limit per test5 seconds memory limit per test512 megabytes inputstandard input ...
- Codeforces 588E. A Simple Task (线段树+计数排序思想)
题目链接:http://codeforces.com/contest/558/problem/E 题意:有一串字符串,有两个操作:1操作是将l到r的字符串升序排序,0操作是降序排序. 题解:建立26棵 ...
- CF #312 E. A Simple Task 线段树
题目链接:http://codeforces.com/problemset/problem/558/E 给一个字符串,每次对一个区间内的子串进行升序或者降序的排列,问最后字符串什么样子. 对于字符串排 ...
- CF558E A simple task 线段树
这道题好猥琐啊啊啊啊啊啊 写了一个上午啊啊啊啊 没有在update里写pushup啊啊啊啊 题目大意: 给你一个字符串s,有q个操作 l r 1 :把sl..rsl..r按升序排序 l r 0 :把s ...
- codeforces 558E A Simple Task 线段树
题目链接 题意较为简单. 思路: 由于仅仅有26个字母,所以用26棵线段树维护就好了,比較easy. #include <iostream> #include <string> ...
- Codeforces558E A Simple Task(线段树)
题目 Source http://codeforces.com/problemset/problem/558/E Description This task is very simple. Given ...
随机推荐
- [JZOJ4274] [NOIP2015模拟10.28B组] 终章-剑之魂 解题报告(二进制)
Description [背景介绍]古堡,暗鸦,斜阳,和深渊……等了三年,我独自一人,终于来到了这里……“终焉的试炼吗?就在这里吗?”我自言自语道.“终焉的试炼啊!就在这里啊!”我再一次自言自语道.“ ...
- <Sicily>Inversion Number(线段树求逆序数)
一.题目描述 There is a permutation P with n integers from 1 to n. You have to calculate its inversion num ...
- python的模块导入机制
在python中用import或者from...import来导入相应的模块. 模块(Module)其实就是一些函数和类的集合文件,它能实现一些相应的功能,当我们需要使用这些功能的时候,直接把相应的模 ...
- 前端之CSS选择器
基本选择器 元素选择器 p {color: "red";} ID选择器 #i1 { background-color: red; } 类选择器 .c1 { font-size: 1 ...
- memset函数的用法
重点需要注意一个细节:memset是以byte来填充的,也就是一个字节(8bit)一个单位来填充. 因此:如果int array[10]: memset(array, 1, 10):然后再把array ...
- 洛谷 P1273 有线电视网 && caioj 1109 树形动态规划(TreeDP)4:比赛转播(树上分组背包总结)
从这篇博客往前到二叉苹果树都可以用分组背包做 这依赖性的问题,都可以用于这道题类似的方法来做 表示以i为根的树中取j个节点所能得的最大价值 那么每一个子树可以看成一个组,每个组里面取一个节点,两个节点 ...
- Unity 编辑器学习(四)之 静态游戏物体
一.Static GameObjects 关于静态对象的信息往往可以预先在编辑器里计算,不需要实时计算,进而优化性能,明显降低DrawCall. 各种Static说明: Lightmapping: 用 ...
- Ubuntu 经常使用软件安装
Chromium 14.04+ FlashPlayer 原理 就是安装一个flash插件而已啊,网上都没有详解. 我们首先在浏览器地址栏输入chrome://plugins, 全部插件都会展示出来了. ...
- uva 473(dp)
题意:按创作时间给出n首歌每首歌的时间ti,然后按创作时间装到m个光盘内,给出光盘最大分钟数t,问m个光盘最多总共放多少首歌. 题解:对于每首歌都能够选或者不选,假设选择了这首歌,是否把这首歌当做第j ...
- POJ 3014:Asteroids(二分匹配,匈牙利算法)
id=3041">Asteroids Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 14399 Acce ...