E. A Simple Task
problem ' s link:http://codeforces.com/problemset/problem/558/e
Mean:
Given a string, there is a Q operation, each operation will (L,R) in the word Fu Shen or descending order, output the string after the Q operation.
Analyse:
The basic idea is to count the sort.
The so-called counting sort is an algorithm for sorting a digital cluster with a more concentrated distribution of elements, with a time complexity of O (n), but with harsh conditions. First sweep the number of N, map out the number of occurrences of each number, and then O (n) sweep over the processing: for the number of AI, how many numbers in front of the AI. With this information, we can determine the position of the AI at the time of the Order in O (1).
Problem Solving Ideas:
For each query, we first count the number of occurrences of each letter in the (l,r) interval, and then sort (non-ascending or non-descending). This update is equivalent to the following:
for (int j=x; j<=y; j + +) ' a '] ++0; for (int j=x; j<=y; j + +) { while0) ind+ +; ' a ' ; Cnt[ind]--;}
But every time the complexity of the statistic is O (n), the time complexity of (10^5) * (5*10^4) is bound to time out. So we need a data structure---segment tree that is objective in interval updating and statistical time complexity.
We open 26 line segment tree, the first tree segment tree maintenance is: 26 letters in the number of letters in each interval.
In this way, we can fit a string s perfectly into the tree of the 26 line segments, and the updates and lookups change from O (n) above to O (Logn). Where the pass-through update needs to be tagged with lazy.
Time Complexity:o (Q*LOGN*SZ)
Source Code:
/** This code was made by crazyacking* verdict:accepted* submission date:2015-07-15-21.40* time:0ms* memory:137kb*/#include<queue>#include<cstdio>#include<Set>#include<string>#include<stack>#include<cmath>#include<climits>#include<map>#include<cstdlib>#include<iostream>#include<vector>#include<algorithm>#include<cstring>#defineLL Long Long#defineULL unsigned long Longusing namespacestd;#defineMX 100007#defineLFT (idx<<1)#defineRGT (lft|1)#defineMid ((l+r) >>1)#defineRep (i,x,y) for (int i=x;i<=y;++i)inttree[ -][4*MX];intlazy[ -][4*MX];CharS[MX];voidBuild (intIdxintLintR) { if(L = =r) {intid = s[l]-'a'+1; TREE[ID][IDX]=1; return; } Build (Lft,l,mid); Build (Rgt,mid+1, R); Rep (I,1, -) Tree[i][idx] = Tree[i][lft] + TREE[I][RGT];//Backtracking Pushup}voidPushup (intIdintIdxintLintRintv) {Lazy[id][idx]=v; TREE[ID][IDX]= (r-l+1) * (v%2);}voidUpdate (intIdintIdxintLintRintSintEintv) { if(L==s && r==e) {pushup (id,idx,l,r,v); return; } if(Lazy[id][idx]) {pushup (id,lft,l,mid,lazy[id][idx]); Pushup (Id,rgt,mid+1, R,lazy[id][idx]); LAZY[ID][IDX]=0; } if(e <=mid) {Update (id,lft,l,mid,s,e,v);} Else if(S > Mid) {Update (id,rgt,mid+1, r,s,e,v); } Else{Update (ID,LFT,L,MID,S,MID,V), update (id,rgt,mid+1, r,mid+1, e,v); } Tree[id][idx]= Tree[id][lft] +TREE[ID][RGT];}intQuery (intIdintIdxintLintRintSintE//query S~e How many letters I have on this paragraph{ if(L = = s && r = = e) {returnTree[id][idx];} if(Lazy[id][idx]) {pushup (id,lft,l,mid,lazy[id][idx]); Pushup (Id,rgt,mid+1, R,lazy[id][idx]); LAZY[ID][IDX]=0; } if(e <= mid) {returnQuery (id,lft,l,mid,s,e);} Else if(S > Mid) {returnQuery (id,rgt,mid+1, r,s,e); } Else{returnQuery (ID,LFT,L,MID,S,MID) + query (id,rgt,mid+1, r,mid+1, E); }}intMain () {intn,m; scanf ("%d%d",&n,&m); scanf ("%s", s+1); Build (1,1, N); while(m--) { ints,e,k; scanf (" %d%d%d",&s,&e,&k); intcnt[ -] = {0}; Rep (I,1, -) {Cnt[i]= Query (I,1,1, n,s,e); Update (i,1,1, N,s,e,2); } if(k)/**< non-decreasing*/ { intL =s; Rep (I,1, -) { intSt =l; inted = st+cnt[i]-1; if(St <= ed) {Update (I,1,1, N,st,ed,1); }//set the St to Ed of the string to IL = ed+1; } } Else/**< non-increasing*/ { intL =s; for(intI= -; i>=1; --i) {intSt =l; inted = st+cnt[i]-1; if(St <= ed) {Update (I,1,1, N,st,ed,1); } l= ed+1; }}} rep (I,1, N) {Rep (J,1, -) { intQQ = Query (J,1,1, N,i,i); if(QQ) {Putchar ('a'+j-1); Break;} }} puts (""); return 0;}
Count sort + Segment Tree Optimization---codeforces 558e:a simple Task