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>
#define DEBUG 0
using namespace std;
const int N = 1e5 + 10;
int n;
int h[N];
int fenwick[N];
void update(int idx, int val) {
for(; idx < N; idx += (idx & -idx)) {
fenwick[idx] += val;
}
}
int query(int idx) {
int res = 0;
for(; idx > 0; idx -= (idx & -idx)) {
res += fenwick[idx];
}
return res;
}
int lowerbound(int x) {
int l = 1, r = n, low = n + 1;
while(l <= r) {
int mid = (l + r) / 2;
if(h[mid] + query(mid) < x) {
l = mid + 1;
}
else {
r = mid - 1;
low = mid;
}
}
return low;
}
int upperbound(int x) {
int l = 1, r = n, up = 0;
while(l <= r) {
int mid = (l + r) / 2;
if(h[mid] + query(mid) > x) {
r = mid - 1;
}
else {
l = mid + 1;
up = mid;
}
}
return up;
}
int main() {
ios_base::sync_with_stdio(0); cin.tie(0);
int m;
cin >> n >> m;
for(int i = 1; i <= n; i++) {
cin >> h[i];
}
sort(h + 1, h + n + 1);
while(m--) {
char t;
int a, b;
cin >> t >> a >> b;
if(t == 'F') {
int idx = lowerbound(b);
if(idx == n + 1) {
continue;
}
update(idx, 1);
if(idx + a - 1 >= n) {
update(n + 1, -1);
continue;
}
int last1 = h[idx + a - 1] + query(idx + a - 1);
int last2 = h[idx + a] + query(idx + a);
if(last1 == last2) {
int idx2 = lowerbound(last1);
int idx3 = upperbound(last1);
update(idx2, -1);
a -= idx2 - idx;
update(idx3 - a + 1, 1);
update(idx3 + 1, -1);
}
else {
update(idx + a, -1);
}
}
else {
cout << max(0, upperbound(b) - lowerbound(a) + 1) << '\n';
}
if(DEBUG) {
cout << "Debugging : \n";
for(int i = 1; i <= n; i++) {
cout << h[i] + query(i) << " ";
}
cout << '\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... |