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 int long long
#define pi pair<int,int>
#define pb push_back
#define V vector
#define vi V<int>
#define mi map<int,int>
#define MOD 1000000007
#define MOD2 998244353
#define mp make_pair
#define ins insert
#define qi queue<int>
#define pqi priority_queue<int>
#define si set<int>
#define v2i V<vi>
#define v3i V<v2i>
#define v4i V<v3i>
#define v5i V<v4i>
#define INF 1e18
#define all(x) begin(x), end(x)
#define sz(x) (int) (x).size()
#define fastio ios_base::sync_with_stdio(false); cin.tie(nullptr);
#define endl "\n"
#define lb lower_bound
#define ub upper_bound
#define print(x) cout << x<<" ";
#define vpi V<pi>
#define Y cout<<"YES"<<endl;
#define NO cout<<"NO"<<endl;
#define msi multiset<int>
#define F first
#define S second
#define ld long double
#define RE return;
#define IMP cout<<-1<<endl;return;
using namespace std;
template <typename T>
std::istream& operator>>(std::istream& in, std::vector<T>& vec) {
for (T& x : vec) {
in >> x;
}
return in;
}
vi arr; vi heights;
void build(int node, int l, int r){
if(l==r){
arr[node] = (l==0 ? heights[l] : heights[l]-heights[l-1]);
return;
}
int mid = (l+r)/2;
build(2*node,l,mid); build(2*node+1, mid+1, r);
arr[node] = arr[2*node]+arr[2*node+1];
}
void update(int node, int l, int r, int i, int val){
if(i == sz(heights)) return;
if(l==r){
arr[node] += val;
return;
}
int mid = (l+r)/2;
if(mid<i) update(2*node+1, mid+1, r, i, val);
else update(2*node, l, mid, i, val);
arr[node] = arr[2*node]+arr[2*node+1];
}
int query(int node, int l, int r, int tl, int tr){
if(tl>tr) return 0;
if(l==tl && r==tr) return arr[node];
int mid = (l+r)/2;
return query(2*node, l, mid, tl, min(mid, tr)) + query(2*node+1, mid+1, r, max(tl, mid+1), tr);
}
int findl(int val){
int l=0; int r = sz(heights)-1; int best = -1; int rand=0;
while(l<=r && rand<=2){
int mid = (l+r+1)/2;
if(query(1, 0, sz(heights)-1, 0, mid)<=val){
best = mid;
l = mid;
}else r = mid-1;
if(l==r) rand++;
}
return best;
}
signed main(){
int n,q; cin >> n>>q; heights.resize(n); arr.resize(4*n); cin >> heights; sort(all(heights));
build(1, 0, n-1);
while(q--){
char c; cin >> c;
if(c=='C'){
int l,r; cin >>l>>r;
cout<<findl(r) - findl(l-1)<<endl;
}else{
int cnt,h; cin>>cnt>>h;
int fst = findl(h-1)+1;
int lst = fst + cnt-1;
if(fst==n) continue;
if(fst + cnt>=n){
update(1, 0, n-1, fst, 1);
continue;
}
int jhxt = query(1, 0, n-1, 0, lst);
int aft = findl(jhxt); int bef = findl(jhxt-1);
if(bef>=fst){
update(1, 0, n-1, fst, 1);
update(1, 0, n-1, bef+1, -1);
update(1, 0, n-1, aft+1, -1);
update(1, 0, n-1, aft - (lst - bef)+1, 1);
}else{
update(1, 0, n-1, aft+1,-1);
update(1, 0, n-1, aft-cnt+1, 1);
}
}
}
}
# | 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... |