-
Notifications
You must be signed in to change notification settings - Fork 0
/
Easy Task
132 lines (121 loc) · 3.7 KB
/
Easy Task
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
//{ Driver Code Starts
//Initial Template for Java
import java.io.*;
import java.util.*;
class GFG{
public static void main (String[] args) throws Exception{
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
PrintWriter out=new PrintWriter(System.out);
int t=Integer.parseInt(in.readLine());
while(t-- > 0){
int n=Integer.parseInt(in.readLine().trim());
String s=in.readLine().trim();
int q=Integer.parseInt(in.readLine().trim());
query a[]=new query[q];
for(int i=0;i<q;i++){
String str=in.readLine().trim();
String st[]=str.split(" ");
if(st[0].trim().equals("1")){
a[i]=new query("1",st[1],st[2],"");
}else{
a[i]=new query("2",st[1],st[2],st[3]);
}
}
Solution ob=new Solution();
ArrayList<Character> ans=ob.easyTask(n,s,q,a);
for(char ch:ans){
out.print(ch+" ");
}out.println();
}
out.close();
}
}
// } Driver Code Ends
//User function Template for Java
class Solution {
static int segment[][];
static void bnado(int left,int right,int index,char s[]){
if(left==right){
segment[index][s[right]-'a']=1;
return;
}
int mid = (left+right)/2;
bnado(left,mid,2*index+1,s);
bnado(mid+1,right,2*index+2,s);
for(int i=0;i<26;i++){
segment[index][i]=segment[2*index+1][i]+segment[2*index+2][i];
}
}
static void update(int left,int right,int index,int Uindex,char s[],char c){
if(Uindex<left || Uindex>right)return;
segment[index][s[Uindex]-'a']--;
segment[index][c-'a']++;
if(left==right)return;
int mid = (left+right)/2;
update(left,mid,2*index+1,Uindex,s,c);
update(mid+1,right,2*index+2,Uindex,s,c);
}
static void nikaldo(int left,int right,int index,int s,int e,int temp[]){
if(s>right || e<left)return;
if(left>=s && right<=e){
for(int i=0;i<26;i++){
temp[i]+=segment[index][i];
}
return;
}
int mid = (left+right)/2;
nikaldo(left,mid,2*index+1,s,e,temp);
nikaldo(mid+1,right,2*index+2,s,e,temp);
}
public static ArrayList<Character> easyTask(int n,String s,int q,query queries[])
{
ArrayList<Character> ans = new ArrayList<>();
segment = new int[4*n][26];
char cc[]=s.toCharArray();
bnado(0,n-1,0,cc);
for(int i=0;i<q;i++){
if(queries[i].type=="1"){
int Uindex=Integer.parseInt(queries[i].a);
char c=queries[i].b.charAt(0);
update(0,n-1,0,Uindex,cc,c);
cc[Uindex]=c;
}
else{
int temp[] = new int[26];
int l=Integer.parseInt(queries[i].a);
int r=Integer.parseInt(queries[i].b);
int k=Integer.parseInt(queries[i].c);
nikaldo(0,n-1,0,l,r,temp);
int j=0;
for(j=25;j>=0;j--){
k-=temp[j];
if(k<=0)break;
}
ans.add((char)('a'+j));
}
}
return ans;
}
}
/*In case the query is of type 1.
type=1
c=null
*/
/*In case the query is of type 2.
type=2
c=k
*/
class query
{
String type;
String a;
String b;
String c;
public query(String type,String a,String b,String c)
{
this.type=type;
this.a=a;
this.b=b;
this.c=c;
}
}