-
Notifications
You must be signed in to change notification settings - Fork 10
/
racetime2.cpp
123 lines (98 loc) · 3.08 KB
/
racetime2.cpp
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
#include<bits/stdc++.h>
using namespace std;
#define mp make_pair
#define FOR(i,n) for(int i=0;i<n;i++)
#define REP(i, a, b) for(int i=a;i<=b;i++)
#define FORb(i, n) for(int i=n-1; i>=0; i--)
#define lli long long int
#define ulli unsigned long long int
#define dout if(debug)cout<<" "
int debug=0;
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int N, Q;
//Just scanning the number of elements in array and number of queries.
cin >> N >> Q;
int arr[N];
//Scanning of array elements
for(int i = 0; i < N; i++){
cin >> arr[i];
}
//Finding the square root of N. So, that I can decompose the whole array into small but equal parts.
int root = sqrt(N);
int i = 0;
vector<vector<int> >sqrtdec;
vector<int>vect;
//This while loop basically decompose the whole array into small parts. These small parts contain elements in sorted order.
while(i < N){
if(i%root==0 && i!=0){
sort(vect.begin(), vect.end());
sqrtdec.push_back(vect);
vect.clear();
dout<<" pushing in another vector now "<< arr[i] << endl;
vect.push_back(arr[i]);
}
else{
dout<<"pushing in vect "<< arr[i] <<endl;
vect.push_back(arr[i]);
}
i++;
}
//Putting the last group of elements into the vector
sort(vect.begin(), vect.end());
sqrtdec.push_back(vect);
vect.clear();
char type;
//Below I am going through each query
for(int i = 0; i < Q; i++){
cin >> type;
int s, e, val;
if(type == 'C'){
cin >> s >> e >> val;
int flag=0;
int count=0;
int j=s-1;
while( j % root !=0){
if(j == e-1)
{
if(arr[j] <= val)
count++;
flag=1;
break;
}
if(arr[j] <= val){
count++;
}
j++;
}
if( flag != 1 ){
while(j+root-1 <= e-1){
int vectIndex = j/root;
int arrIndex = upper_bound(sqrtdec[vectIndex].begin(), sqrtdec[vectIndex].end(), val) - sqrtdec[vectIndex].begin();
count+=arrIndex;
j = j+root;
}
for(int k = j; k <=e-1; k++)
{
if(arr[k] <= val)
{
count++;
}
}
}
cout<<count<<endl;
}
else{
int pos, val;
cin>>pos>>val;
int changeVal=arr[pos-1];
arr[pos-1]=val;
int vectIndex = (pos-1)/root;
int arrIndex = upper_bound(sqrtdec[vectIndex].begin(), sqrtdec[vectIndex].end(), changeVal)-sqrtdec[vectIndex].begin();
sqrtdec[vectIndex][arrIndex-1]=val;
sort(sqrtdec[vectIndex].begin(), sqrtdec[vectIndex].end());
}
}
return 0;
}