1. 程式人生 > >codeforces 558 E A Simple Task

codeforces 558 E A Simple Task

include ack ddc sts number ica alt ans div

題目大意就是給一個字符串,然後多個操作。每次操作能夠把每一段區間的字符進行升序或者降序排序,問終於的字符串是如何的。



做法的話就是用線段樹維護區間和


  一開始僅僅考慮字符串中字符‘a‘的情況。如果操作區間[L,R]中有x個‘a‘,那麽一次操作後,這x個‘a‘要麽去最左(升序)。要麽去最右(降序),我們能夠建立一顆線段樹來維護這種操作,字符‘a‘出現的位置值為1,否則為0,那麽q次操作後,最後值為1的地方填的就是‘a‘了。




  然後,在考慮字符‘a‘和‘b‘的情況,操作的情況和上面類似,字符‘a‘和‘b‘出現的位置值為1。否則為0,q次操作後,假設一個位置的值為1。而且該位置沒有填寫‘a‘。那麽這個位置就填寫‘b‘。


接下來的情況以此類推,考慮abc。abcd,......,abcd...z。


時間復雜度O(26*(n+q)logn)。

#include<map>
#include<string>
#include<cstring>
#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<queue>
#include<vector>
#include<iostream>
#include<algorithm>
#include<bitset>
#include<climits>
#include<list>
#include<iomanip>
#include<stack>
#include<set>
using namespace std;
struct Tree
{
	int l,r,sum;
}tree[int(4e5)];
void create(int l,int r,int k)
{
	tree[k].l=l;
	tree[k].r=r;
	tree[k].sum=0;
	if(l==r)
		return;
	int m=l+r>>1;
	create(l,m,k<<1);
	create(m+1,r,k<<1|1);
}
void lazy(int k)
{
	if(tree[k].l!=tree[k].r&&tree[k].sum!=tree[k<<1].sum+tree[k<<1|1].sum)
	{
		if(tree[k].sum==0)
			tree[k<<1].sum=tree[k<<1|1].sum=0;
		else
		{
			tree[k<<1].sum=tree[k<<1].r-tree[k<<1].l+1;
			tree[k<<1|1].sum=tree[k<<1|1].r-tree[k<<1|1].l+1;
		}
	}
}
void update(int l,int r,bool flag,int k)
{
	lazy(k);
	if(l==tree[k].l&&r==tree[k].r)
	{
		tree[k].sum=flag?r-l+1:0;
		return;
	}
	int m=tree[k].l+tree[k].r>>1;
	if(r<=m)
		update(l,r,flag,k<<1);
	else if(l>m)
		update(l,r,flag,k<<1|1);
	else
	{
		update(l,m,flag,k<<1);
		update(m+1,r,flag,k<<1|1);
	}
	tree[k].sum=tree[k<<1].sum+tree[k<<1|1].sum;
}
int seek(int l,int r,int k)
{
	lazy(k);
	if(l==tree[k].l&&r==tree[k].r)
		return tree[k].sum;
	int m=tree[k].l+tree[k].r>>1;
	if(r<=m)
		return seek(l,r,k<<1);
	if(l>m)
		return seek(l,r,k<<1|1);
	return seek(l,m,k<<1)+seek(m+1,r,k<<1|1);
}
void update(int l,int r,bool flag)
{
	int t=seek(l,r,1);
	if(flag)
	{
		if(l<=l+t-1)
			update(l,l+t-1,1,1);
		if(l+t<=r)
			update(l+t,r,0,1);
	}
	else
	{
		if(r-t+1<=r)
			update(r-t+1,r,1,1);
		if(l<=r-t)
			update(l,r-t,0,1);
	}
}
struct Box
{
	int l,r;
	bool flag;
}box[int(1e5)+10];
int ans[int(1e5)+10];
int main()
{
	int n,q;
	cin>>n>>q;
	create(1,n,1);
	string s;
	cin>>s;
	for(int i=0;i<q;i++)
		cin>>box[i].l>>box[i].r>>box[i].flag;
	for(int i=0;i<26;i++)
	{
		update(1,n,0,1);
		for(int j=0;j<n;j++)
			if(s[j]<=i+‘a‘)
				update(j+1,j+1,1,1);
		for(int j=0;j<q;j++)
			update(box[j].l,box[j].r,box[j].flag);
		for(int j=0;j<n;j++)
			if(ans[j]==0&&seek(j+1,j+1,1)==1)
				ans[j]=i+‘a‘;
	}
	for(int i=0;i<n;i++)
		putchar(ans[i]);
}


time limit per test 5 seconds memory limit per test 512 megabytes input standard input output standard output

This task is very simple. Given a string S of length n and q queries each query is on the format i j k which means sort the substring consisting of the characters from i

to j in non-decreasing order if k?=?1 or in non-increasing order if k?=?0.

Output the final string after applying the queries.

Input

The first line will contain two integers n,?q (1?≤?n?≤?105, 0?≤?q?≤?50?000), the length of the string and the number of queries respectively.

Next line contains a string S itself. It contains only lowercase English letters.

Next q lines will contain three integers each i,?j,?k (1?≤?i?≤?j?≤?n, 技術分享).

Output

Output one line, the string S after applying the queries.

Sample test(s) input
10 5
abacdabcda
7 10 0
5 8 1
1 4 0
3 6 0
7 10 1
output
cbcaaaabdd
input
10 1
agjucbvdfk
1 10 1
output
abcdfgjkuv
Note

First sample test explanation:

技術分享

技術分享

技術分享

技術分享

技術分享





codeforces 558 E A Simple Task