#include<iostream>
#include<stdio.h>
#include<stdint.h>
#include<vector>
#include<algorithm>
#include<utility>
#include<set>
#include<map>
#include<queue>
#include<stack>
#include<deque>
#include<string>
#include<assert.h>
#include<math.h>
#include<chrono>
#include<random>
#include<bitset>
#include<time.h>
#include<iomanip>
using namespace std;
const int64_t INF = (int64_t) 5 * 1e5 + 5;
const int64_t mINF = (int64_t) 1e9 + 5;
const int64_t MOD = (int64_t) 1e9 + 9;
const int nbit = 30;
const int ndig = 10;
const int nchar = 26;
const int p1 = 31;
const int p2 = 53;
const int D = 4;
int dr[D] = {0, 1, 0, -1};
int dc[D] = {1, 0, -1, 0};
// 0 right // 1 down // 2 left // 3 up
struct Solution
{
int n;
vector<int> a;
vector<int> bit;
vector<int> tree;
Solution() {}
void solve()
{
cin >> n;
a.resize(n, 0);
bit.resize(n + 1, 0);
for(int i = 0; i < n; i++)
{
int val;
cin >> val;
update(i + 1, val);
}
while(__builtin_popcount(n) != 1) n++;
tree.resize(2 * n, 0);
for(int i = 0; i < (int) a.size(); i++)
{
tree[n + i] = a[i];
}
for(int i = n - 1; i >= 1; i--)
{
tree[i] = max(tree[i * 2], tree[i * 2 + 1]);
}
int q;
cin >> q;
for(int i = 0; i < q; i++)
{
int t;
cin >> t;
if(t == 1)
{
int id;
int x;
cin >> id >> x;
update(id, x);
updateTree(id - 1);
}
else
{
int l;
int r;
cin >> l >> r;
l--; r--;
int ans = 0;
for(int j = l; j <= r; j++)
{
if(check(j, l, r)) ans++;
}
cout << ans << "\n";
}
}
}
bool check(int id, int left, int right)
{
int x = id;
int y = id;
int tmp = a[id];
while(x != left || y != right)
{
int l = left;
int r = x;
int i = x;
while(l <= r)
{
int mid = l + (r - l) / 2;
int max_ = get(1, 0, n - 1, mid, x);
if(max_ <= tmp)
{
i = mid;
r = mid - 1;
}
else l = mid + 1;
}
tmp += sum(x) - sum(i);
l = y;
r = right;
int j = y;
while(l <= r)
{
int mid = l + (r - l) / 2;
int max_ = get(1, 0, n - 1, y, mid);
if(max_ <= tmp)
{
j = mid;
l = mid + 1;
}
else r = mid - 1;
}
tmp += sum(j + 1) - sum(y + 1);
if(x == i && y == j) break;
x = i; y = j;
}
return (x == left && y == right);
}
int sum(int i)
{
int ans = 0;
while(i > 0)
{
ans += bit[i];
i -= (i & (-i));
}
return ans;
}
int get(int node, int nleft, int nright, int left, int right)
{
if(nleft > right || nright < left) return 0;
if(left <= nleft && nright <= right) return tree[node];
int mid = nleft + (nright - nleft) / 2;
return max(get(node * 2, nleft, mid, left, right),
get(node * 2 + 1, mid + 1, nright, left, right));
}
void updateTree(int i)
{
tree[n + i] = a[i];
for(int j = (n + i) / 2; j >= 1; j /= 2)
{
tree[j] = max(tree[j * 2], tree[j * 2 + 1]);
}
}
void update(int i, int val)
{
int tmp = val - a[i - 1];
a[i - 1] = val;
while(i < (int) bit.size())
{
bit[i] += tmp;
i += (i & (-i));
}
}
int modsub(int t1, int t2)
{
int res = t1 - t2;
if(res < 0) res += MOD;
return res;
}
int modadd(int t1, int t2)
{
int res = t1 + t2;
if(res >= MOD) res -= MOD;
return res;
}
int modmul(int t1, int t2)
{
int64_t res = 1LL * t1 * t2;
return (res % MOD);
}
bool BIT(int mask, int j)
{
return (mask & MASK(j));
}
int64_t MASK(int j)
{
return (1LL << j);
}
int64_t Abs(int64_t t1)
{
if(t1 < 0) return -t1;
return t1;
}
};
void __startup()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL); cout.tie(NULL);
// freopen("in.txt", "r", stdin);
// freopen("out.txt", "w", stdout);
}
int main()
{
__startup();
int t = 1;
// cin >> t;
for(int i = 1; i <= t; i++)
{
Solution().solve();
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
320 KB |
Output is correct |
3 |
Correct |
1 ms |
320 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Incorrect |
90 ms |
320 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Incorrect |
839 ms |
2708 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
320 KB |
Output is correct |
3 |
Correct |
1 ms |
320 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Incorrect |
90 ms |
320 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Incorrect |
839 ms |
2708 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Incorrect |
839 ms |
2708 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
320 KB |
Output is correct |
3 |
Correct |
1 ms |
320 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Incorrect |
90 ms |
320 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |