Codeforces Round #312 (Div. 2) E. A Simple Task line segment tree latency tag,
E. A Simple Task
Time limit per test5 seconds
Memory limit per test512 megabytes
Inputstandard input
Outputstandard 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 limit = Limit 1 or in non-increasing order if k Limit = Limit 0.
Output the final string after applying the queries.
Input
The first line will contain two integers n, Qaq (1 ≤ 0000n ≤ 0000105, 0 ≤ 0000q ≤ 000050 0000000 ), 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, comment j, comment k (1 comment ≤ I comment ≤ comment j comment ≤ limit 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:
Give a string, and then sort the strings between I and j from small to large, or sort the strings from large to small, maintain the line tree of 26, I, j modify, you only need to query the number of a-z, sort by count, and update the line segment tree. The total complexity is o (26 * q * log (n )); the speed is a little slow. It should be faster to stretch the tree. Because it is a segment update, we need to use the latency tag, note-1 does not update, 0 all set 0, 1 All set 1, only this tips!
#define N 100005#define M 100005#define maxn 205#define SZ 26#define MOD 1000000000000000007#define lson (now<<1)#define rson (now<<1|1)int n,q,ss,ee,k,num[SZ];char str[N];struct node{ int c,sum,l,r;};node tree[SZ][N*4];void pushDown(int tn,int now){ if(tree[tn][now].c != -1){ tree[tn][lson].c = tree[tn][rson].c = tree[tn][now].c; tree[tn][lson].sum = tree[tn][lson].c*(tree[tn][lson].r-tree[tn][lson].l + 1); tree[tn][rson].sum = tree[tn][rson].c*(tree[tn][rson].r-tree[tn][rson].l + 1); tree[tn][now].c = -1; }}void pushUp(int tn,int now){ tree[tn][now].sum = tree[tn][lson].sum + tree[tn][rson].sum ;}void buildTree(int l,int r,int now){ FI(SZ) tree[i][now].c = -1,tree[i][now].l = l,tree[i][now].r = r,tree[i][now].sum = 0; if(l >= r){ return ; } int mid = (l+r)>>1; buildTree(l,mid,lson); buildTree(mid+1,r,rson);}void updateTree(int l,int r,int now,int s,int e,int tn,int c){ if(s <= l && e>= r){ tree[tn][now].c = c; tree[tn][now].sum = tree[tn][now].c*(tree[tn][now].r - tree[tn][now].l + 1); return ; } pushDown(tn,now); int mid = (l+r)>>1; if(s <= mid) updateTree(l,mid,lson,s,e,tn,c); if(e > mid) updateTree(mid+1,r,rson,s,e,tn,c); pushUp(tn,now);}int queryTree(int l,int r,int now,int s,int e,int tn){ if(s <= l && e>= r){ return tree[tn][now].sum; } pushDown(tn,now); int mid = (l+r)>>1; int ans = 0; if(s <= mid) ans += queryTree(l,mid,lson,s,e,tn); if(e > mid) ans += queryTree(mid+1,r,rson,s,e,tn); pushUp(tn,now); return ans;}void outputStr(){ FI(n){ FJ(SZ){ if(queryTree(1,n,1,i+1,i+1,j)){ printf("%c",j+'a'); break; } } } printf("\n");}int main(){ while(S2(n,q)!=EOF) { SS(str); buildTree(1,n,1); FI(n){ updateTree(1,n,1,i+1,i+1,str[i] - 'a',1); } FI(q){ S2(ss,ee);S(k); FJ(SZ) num[j] = queryTree(1,n,1,ss,ee,j); FJ(SZ) updateTree(1,n,1,ss,ee,j,0); if(k){ int sum = 0; FJ(SZ){ if(num[j]) updateTree(1,n,1,ss + sum,ss + sum + num[j] - 1,j,1); sum += num[j]; } } else { int sum = 0; for(int j = SZ - 1;j>=0;j--){ if(num[j]) updateTree(1,n,1,ss + sum,ss + sum + num[j] - 1,j,1); sum += num[j]; } } } outputStr(); } return 0;}
Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.