This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
const int mxN = 1e5;
int segtree[4*mxN+1];
void upd(int l, int r, int idx, int tl, int tr){
if (tl > r || tr < l){
return;
}
if (l >= tl && r <= tr){
segtree[idx] += 1;
return;
}
int m = (l+r)/2;
upd(l,m,2*idx+1,tl,tr);
upd(m+1,r,2*idx+2,tl,tr);
}
int query(int l, int r, int idx, int t){
if (t > r || l > t){
return 0;
}
int res = segtree[idx];
if (l == r){
return res;
}
int m = (l+r)/2;
return res + query(l,m,2*idx+1,t) + query(m+1,r,2*idx+2,t);
}
int glowest(vector<int> &vals, int t){
int n = vals.size();
int l = -1;
int r = n;
int curr;
while (r - l > 1){
curr = (l+r)/2;
if (query(0,n-1,0,curr) + vals[curr] >= t){
r = curr;
} else {
l = curr;
}
}
return r;
}
int ghigh(vector<int> &vals, int tl){
int n = vals.size();
int l = tl;
int r = n;
int tar = query(0,n-1,0,l) + vals[l];
int curr;
while (r - l > 1){
curr = (l+r)/2;
if (query(0,n-1,0,curr) + vals[curr] > tar){
r = curr;
} else {
l = curr;
}
}
return l;
}
int main()
{
memset(segtree,0,sizeof(segtree));
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n,m;
cin >> n >> m;
vector<int> vals(n);
for (int i =0; i < n; i++){
cin >> vals[i];
}
sort(vals.begin(),vals.end());
char w;
int c,h;
int l,r;
int res;
/* for (int i =0; i < 3*1e8; i++){
continue;
}
for (int i =0; i < n; i++){
cout << vals[i] << " ";
}
cout << '\n';*/
for (int i =0; i < m; i++){
cin >> w;
// cout << w << ": ";
if (w == 'F'){
cin >> c >> h;
// cout << h << "\n";
l = glowest(vals,h);
while (l < n){
r = ghigh(vals,l);
// cout << c << ", " << l << "," << r << '\n';
if (r - l+1 >= c){
upd(0,n-1,0,r-c+1,r);
break;
}
upd(0,n-1,0,l,r);
c -= (r-l+1);
l = r+1;
}
// r = ghigh(vals,l);
// cout << l << "," << r << '\n';
/* if (r - l+1 >= c){
upd(0,n-1,0,r-c+1,r);
} else {
upd(0,n-1,0,l,l+c-1);
}*/
} else {
cin >> c >> h;
// cout << c << " , " << h << " ";
l = glowest(vals,c);
r = glowest(vals,h);
/* cout << l << "," << r << '\n';
for (int j = 0; j < n; j++){
cout << vals[j] + query(0,n-1,0,j) << " ";
}
cout << '\n';*/
res = r -l+1;
if ((r == n) || (vals[r] + query(0,n-1,0,r) > h)){
res -= 1;
}
cout << res << '\n';
}
}
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |