This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#pragma GCC target ("avx2")
#pragma GCC optimize ("O3")
#pragma GCC optimize ("unroll-loops")
#include<bits/stdc++.h>
#include<math.h>
using namespace std;
typedef long long int ll;
typedef long double ld;
typedef pair<ll, ll> pl;
#define K first
#define V second
#define G(x) ll x; cin >> x;
#define GD(x) ld x; cin >> x;
#define GS(s) string s; cin >> s;
#define EX(x) { cout << x << '\n'; exit(0); }
#define A(a) (a).begin(), (a).end()
#define F(i, l, r) for (ll i = (l); i < r; ++i)
#define NN 100010
namespace seg {
typedef ll T;
T id=0;
T f(T a, T b) {return a+b;}
T t[2 * NN];
ll n=NN; // array size
void _modify(ll p, T value) { // set value at position p
for (p+=n, t[p] += value; p /= 2;) t[p] = f(t[2*p], t[2*p+1]);
}
T _query(ll l, ll r) { // fold f on interval [l, r)
T resl=id, resr=id;
for (l += n, r += n; l < r; l /= 2, r /= 2) {
if (l&1) resl = f(resl, t[l++]);
if (r&1) resr = f(t[--r], resr);
}
return f(resl, resr);
}
void modify(ll l, ll r, ll T) { // already inclusive exclusive we smart about it
if (l>=r) return; // nothing should happen
_modify(l, T);
_modify(r, -T);
}
T query(ll i) {
return _query(0, i+1);
}
}
ll n;
pl get(ll lo, ll hi) {
pl res;
{
ll l = -1, r = n;
while (l+1<r){
ll m =(l+r)/2;
if (seg::query(m) < lo) l = m;
else r = m;
}
res.K = r;
}
{
ll l = -1, r = n;
while (l+1<r){
ll m =(l+r)/2;
if (seg::query(m) <= hi) l = m;
else r = m;
}
res.V = r;
}
return res;
}
int main(){
// freopen("a.in", "r", stdin);
// freopen("a.out", "w", stdout);
ios_base::sync_with_stdio(false); cin.tie(0);
cout << fixed << setprecision(20);
cin >> n; G(q)
vector<ll> v;
F(i, 0, n) {
G(x) v.push_back(x);
}
sort(A(v));
F(i, 0, n) {
seg::modify(i, i+1, v[i]);
}
while (q--) {
GS(s) if (s == "F") {
G(c) G(h)
assert(c>0);
auto [st, _] = get(h, 2e9);
if (st == n) continue;
// cout << "YA " << st << endl;
// cout << "?? " << n-1 << " " << st << " " << c-1 << endl;
ll want = seg::query(min(n-1, st + c - 1));
// cout << "HELLO? " << want << endl;
auto [tl, tr] = get(want, want);
// cout << "WTF " << tl << " " << tr << endl;
seg::modify(st, tl, 1); c -= tl - st;
seg::modify(max(tl, tr-c), tr, 1);
// cout << "Added 1 to " << st << ", " << tl-1 << " and " << max(tl, tr-c) << ", " << tr << endl;
} else {
G(a) G(b)
auto [l, r] = get(a, b);
cout << r-l << '\n';
}
// F(i, 0, n) cout << seg::query(i) << " "; cout << endl;
}
}
# | 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... |