String Manipulation 1.0
This problem will be judged on CodeForces. Original ID: 159C
64-bit integer IO format: %I64d Java class name: (Any)
One popular website developed an unusual username editing procedure. One can change the username only by deleting some characters from it: to change the current name s, a user can pick number p and character c and delete the p-th occurrence of character c from the name. After the user changed his name, he can't undo the change.
For example, one can change name "arca" by removing the second occurrence of character "a" to get "arc".
Polycarpus learned that some user initially registered under nickname t, where t is a concatenation of k copies of string s. Also, Polycarpus knows the sequence of this user's name changes. Help Polycarpus figure out the user's final name.
Input
The first line contains an integer k (1 ≤ k ≤ 2000). The second line contains a non-empty string s, consisting of lowercase Latin letters, at most 100characters long. The third line contains an integer n (0 ≤ n ≤ 20000) — the number of username changes. Each of the next n lines contains the actual changes, one per line. The changes are written as "pi ci" (without the quotes), where pi (1 ≤ pi ≤ 200000) is the number of occurrences of letter ci, ci is a lowercase Latin letter. It is guaranteed that the operations are correct, that is, the letter to be deleted always exists, and after all operations not all letters are deleted from the name. The letters' occurrences are numbered starting from 1.
Output
Print a single string — the user's final name after all changes are applied to it.
Sample Input
2
bac
3
2 a
1 b
2 c
acb
1
abacaba
4
1 a
1 a
1 c
2 b
baa
Hint
Let's consider the first sample. Initially we have name "bacbac"; the first operation transforms it into "bacbc", the second one — to "acbc", and finally, the third one transforms it into "acb".
Source
#include <bits/stdc++.h>
using namespace std;
const int maxn = ;
int tree[][maxn<<],k,n,p;
bool isdel[maxn];
string str,s,c;
void pushup(int wd,int v) {
tree[wd][v] = tree[wd][v<<] + tree[wd][v<<|];
}
void update(int L,int R,int wd,int id,int v) {
if(L == R) {
tree[wd][v]++;
return;
}
int mid = (L + R)>>;
if(id <= mid) update(L,mid,wd,id,v<<);
if(id > mid) update(mid+,R,wd,id,v<<|);
pushup(wd,v);
}
void del(int L,int R,int wd,int v,int rk){
if(L == R) {
tree[wd][v]--;
isdel[L] = true;
return;
}
int mid = (L + R)>>;
if(tree[wd][v<<] >= rk) del(L,mid,wd,v<<,rk);
else del(mid+,R,wd,v<<|,rk-tree[wd][v<<]);
pushup(wd,v);
}
int main() {
cin.sync_with_stdio(false);
cin>>k>>s>>n;
str = "";
for(int i = ; i < k; ++i) str += s;
int len = str.length();
for(int i = ; i < len; ++i)
update(,len-,str[i]-'a',i,);
while(n--){
cin>>p>>c;
del(,len-,c[]-'a',,p);
}
for(int i = ; i < len; ++i)
if(!isdel[i]) cout<<str[i];
cout<<endl;
return ;
}