#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vb = vector<bool>;
using vvb = vector<vb>;
using vi = vector<int>;
using vvi = vector<vi>;
using vl = vector<ll>;
using vvl = vector<vl>;
using vc = vector<char>;
using vvc = vector<vc>;
const ll mod = 1e9 + 7,inf = 1e18;
#define pb push_back
#define fast ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define lc 2 * no
#define rc 2 * no + 1
int n = 1e5 + 5,q;
vi bit(n);
void update(int i,int inc)
{
for (;i<=n;i+=i&(-i))bit[i] += inc;
}
void update(int i,int j,int inc)
{
update(i,inc);
update(j + 1,-inc);
}
int sum(int i)
{
int ret = 0;
for (;i>0;i-=i&(-i))ret += bit[i];
return ret;
}
int lowerBound(int h)
{
int l = 1,r = n,ans = n;
while (l <= r){
int mid = (l + r)/2;
if (sum(mid) >= h){
ans = mid;
r = mid - 1;
}
else l = mid + 1;
}
return ans;
}
int Bound(int pos,int h)
{
int l = pos,r = n,ans = pos;
while (l <= r){
int mid = (l + r)/2;
if (sum(mid) == h){
ans = mid;
l = mid + 1;
}
else r = mid - 1;
}
return ans;
}
int Lower(int h)
{
int l = 1,r = n,ans = 0;
while (l <= r){
int mid = (l + r)/2;
if (sum(mid) < h){
ans = mid;
l = mid + 1;
}
else r = mid - 1;
}
return ans;
}
int Lower2(int h)
{
int l = 1,r = n,ans = -1;
while (l <= r){
int mid = (l + r)/2;
if (sum(mid) <= h){
ans = mid;
l = mid + 1;
}
else r = mid - 1;
}
return ans;
}
int main()
{
fast;
cin>>n>>q;
vi a(n + 1);
for (int i = 1;i<=n;i++)cin>>a[i];
sort(a.begin() + 1,a.end());
for (int i = 1;i<=n;i++)update(i,i,a[i]);
while (q--){
char x;
cin>>x;
if (x == 'F'){
int c,h;
cin>>c>>h;
int first = lowerBound(h);//first index to be updated
if (first == n && sum(first) < h)continue;
int Last = min(n,first + c - 1);//last index to be updated
int val = sum(Last);//height of the last index to be updated
int pos = Lower(val);//index before the height of the last index to be updated
if (pos == 0)pos = Last;
update(first,pos,1);
int rem = c - (pos - first + 1);
if (rem == 0)continue;
int actLast = Bound(Last,val);
update(actLast - rem + 1,actLast,1);
}
else{
int mn,mx;
cin>>mn>>mx;
int L1 = lowerBound(mn),L2 = Lower2(mx);//rectify
if (L1 == n && sum(n) < mn)cout<<0<<'\n';
else cout<<L2 - L1 + 1<<'\n';
}
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
66 ms |
2232 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
716 KB |
Output is correct |
2 |
Incorrect |
4 ms |
732 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
55 ms |
1820 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
55 ms |
2120 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
89 ms |
2020 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
82 ms |
2008 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
56 ms |
2084 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
124 ms |
2828 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
90 ms |
2544 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
128 ms |
3356 KB |
Output isn't correct |