#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
using vi = vector<int>;
using ll = long long;
using vll = vector<ll>;
const int mx = 100'000;
const int Z = (1<<17);
vll delta(1+mx);
int N, M;
struct segtree
{
vi l = vi(1+Z);
vi r = vi(1+Z);
vll sm = vll(1+Z);
vll mxs = vll(1+Z);
segtree()
{
;
}
void build(int i, int L, int R)
{
l[i] = L;
r[i] = R;
if(l[i] == r[i])
{
sm[i] = delta[l[i]];
mxs[i] = sm[i];
}
else
{
build(2*i, L, (L+R)/2);
build(2*i+1, (L+R)/2+1, R);
sm[i] = sm[2*i] + sm[2*i+1];
mxs[i] = max(mxs[2*i], sm[2*i] + mxs[2*i+1]);
}
}
int first_geq(int i, ll s)
{
// cerr << i << ' ' << l[i] << ' ' << r[i] << " : " << sm[i] << ' ' << s << '\n';
if(mxs[i] < s) return N+1;
else if(l[i] == r[i]) return l[i];
else if(mxs[i<<1] >= s) return first_geq(i<<1, s);
else return first_geq((i<<1) + 1, s - sm[i<<1]);
}
int first_geq(ll s) {return first_geq(1, s);}
void add(int i, int I, ll V)
{
if(l[i] != r[i])
{
if(I <= (l[i] + r[i])/2) add(2*i, I, V);
else add(2*i+1, I, V);
sm[i] = sm[2*i] + sm[2*i+1];
mxs[i] = max(mxs[2*i], sm[2*i] + mxs[2*i+1]);
}
else
{
sm[i] += V;
mxs[i] += V;
}
}
void add(int I, int V) {if(1 <= I && I <= N) add(1, I, V);}
int get_sum(int i, int I)
{
if(l[i] == r[i]) return sm[i];
else if(I <= (l[i] + r[i])/2) return get_sum(i<<1, I);
else return sm[(i<<1)] + get_sum((i<<1) + 1, I);
}
// void dfs(int i)
// {
// // cerr << l[i] << ' ' << r[i] << " : " << sm[i] << '\n';
// if(l[i] != r[i]) dfs(2*i), dfs(2*i+1);
// }
};
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> N >> M;
vll init_h(1+N);
for(int i = 1; i <= N; i++) cin >> init_h[i];
init_h[0] = 0;
sort(init_h.begin(), init_h.end());
for(int i = 1; i <= N; i++) delta[i] = init_h[i] - init_h[i-1];
segtree S;
S.build(1, 1, N+1);
// cerr << "\n\n\n";
// S.dfs(1);
for(int q = 1; q <= M; q++)
{
// cerr << "query = " << q << '\n';
char t;
cin >> t;
if(t == 'F')
{
ll c, h;
cin >> c >> h;
int stp = S.first_geq(h);
// cerr << "stp = " << stp << '\n';
if(stp >= N+1) continue;
int ep = min(stp + c - 1, (ll)N);
// cerr << "ep = " << ep << '\n';
ll ep_val = S.get_sum(1, ep);
// cerr << "ep_val = " << ep_val << '\n';
int last_start = S.first_geq(1, ep_val);
int last_end = S.first_geq(1, ep_val+1) - 1;
// cerr << "ls, le = " << last_start << ' ' << last_end << '\n';
int last_count = ep - last_start + 1;
// cerr << "lc = " << last_count << '\n';
S.add(last_end - last_count + 1, +1);
// cerr << "done\n";
S.add(last_end+1, -1);
// cerr << last_end - last_count + 1 << " [) " << last_end+1 << '\n';
S.add(stp, +1);
S.add(last_start, -1);
// cerr << stp << " : " << last_start << '\n';
// cerr << S.first_geq(2) << '\n';
}
else
{
ll mn, mx;
cin >> mn >> mx;
cout << S.first_geq(1, mx+1) - S.first_geq(1, mn) << '\n';
}
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
32 ms |
4940 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4172 KB |
Output is correct |
2 |
Correct |
3 ms |
4172 KB |
Output is correct |
3 |
Correct |
4 ms |
4172 KB |
Output is correct |
4 |
Correct |
3 ms |
4172 KB |
Output is correct |
5 |
Correct |
33 ms |
4368 KB |
Output is correct |
6 |
Correct |
41 ms |
4548 KB |
Output is correct |
7 |
Correct |
7 ms |
4172 KB |
Output is correct |
8 |
Correct |
23 ms |
4436 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
38 ms |
4580 KB |
Output is correct |
2 |
Correct |
50 ms |
4588 KB |
Output is correct |
3 |
Correct |
3 ms |
4172 KB |
Output is correct |
4 |
Correct |
27 ms |
4428 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
36 ms |
4632 KB |
Output is correct |
2 |
Correct |
43 ms |
4556 KB |
Output is correct |
3 |
Correct |
13 ms |
4172 KB |
Output is correct |
4 |
Correct |
54 ms |
4676 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
81 ms |
4888 KB |
Output is correct |
2 |
Incorrect |
28 ms |
5024 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
28 ms |
4824 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
25 ms |
4812 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
33 ms |
4940 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
29 ms |
4896 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
38 ms |
5060 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |