# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
1092556 | Sunbae | Growing Trees (BOI11_grow) | C++17 | 89 ms | 3364 KiB |
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 z exit(0)
using namespace std;
const int N = 1e5 + 5;
int bit[N], a[N], n;
void upd(int i, int v){ for(; i<=n; i+=i&-i) bit[i] += v;}
int qry(int i){ int r = 0; for(; i; i-=i&-i) r += bit[i]; return r;}
void UPD(int l, int r){ upd(l, +1); upd(r+1, -1);}
signed main(){
int q; scanf("%d %d", &n, &q);
for(int i = 1; i<=n; ++i) scanf("%d", a+i);
//1 2 2 3 5
sort(a+1, a+1+n);
for(int i = 0; i<q; ++i){
char op; scanf(" %c", &op);
if(op == 'F'){
int c, h; scanf("%d %d", &c, &h);
int low = 1, high = n, st = -1;
while(low <= high){
int mid = low + ((high-low)>>1);
if(a[mid] + qry(mid) >= h) st = mid, high = mid-1;
else low = mid+1;
}
if(st < 0) continue;
int j = min(n, st+c-1), val = a[j] + qry(j), idx[2] = {-1, -1};
low = st; high = n;
while(low <= high){
int mid = low + ((high-low)>>1);
if(a[mid] + qry(mid) >= val) idx[0] = mid, high = mid-1;
else low = mid+1;
}
low = idx[0]; high = n;
while(low <= high){
int mid = low + ((high-low)>>1);
if(a[mid] + qry(mid) <= val) idx[1] = mid, low = mid+1;
else high = mid-1;
}
if(max(idx[0], idx[1]) < 0 || a[idx[0]] + qry(idx[0]) != val) continue;
if(st <= idx[0]-1) UPD(st, idx[0]-1);
int rem = c-(idx[0]-st);
if(idx > 0 && rem > 0 && idx[1]-rem+1 <= idx[1]) UPD(idx[1]-rem+1, idx[1]);
}else{
int l, r; scanf("%d %d", &l, &r);
int low = 1, high = n, L = n+1, R = -1;
while(low <= high){
int mid = low + ((high-low)>>1);
if(a[mid] + qry(mid) >= l) L = mid, high = mid-1;
else low = mid+1;
}
low = 1; high = n;
while(low <= high){
int mid = low + ((high-low)>>1);
if(a[mid] + qry(mid) <= r) R = mid, low = mid+1;
else high = mid-1;
}
if(L <= R) printf("%d\n", R-L+1);
else puts("0");
}
}
}
Compilation message (stderr)
# | 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... |