#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 10;
struct SegTree
{
int l, r, mid;
SegTree *lc, *rc;
int val = 0;
SegTree(int l_, int r_) : l(l_), r(r_)
{
mid = (l + r) / 2;
if (l == r - 1)
{
return;
}
lc = new SegTree(l, mid);
rc = new SegTree(mid, r);
}
void add(int ll, int rr, int u)
{
if (ll <= l && rr >= r)
{
val += u;
return;
}
if (ll < mid)
{
lc->add(ll, rr, u);
}
if (mid < rr)
{
rc->add(ll, rr, u);
}
}
int query(int i)
{
if (l == r - 1)
{
return val;
}
int ret = val;
if (i < mid)
{
ret += lc->query(i);
}
else
{
ret += rc->query(i);
}
return ret;
}
};
int main()
{
cin.tie(0)->sync_with_stdio(0);
int n, q;
cin >> n >> q;
vector<int> a(n);
for (int i = 0; i < n; i++)
{
cin >> a[i];
}
SegTree st(0, N);
auto add = [&](int l, int r, int u)
{
if (l > r)
{
swap(l, r);
}
st.add(l, r, u);
};
for (int i = 1; i < n; i++)
{
add(a[i - 1], a[i], 1);
}
while (q--)
{
int op;
cin >> op;
if (op == 1)
{
int i, x;
cin >> i >> x;
i--;
if (i > 0)
{
add(a[i - 1], a[i], -1);
}
if (i + 1 < n)
{
add(a[i], a[i + 1], -1);
}
a[i] = x;
if (i > 0)
{
add(a[i - 1], a[i], 1);
}
if (i + 1 < n)
{
add(a[i], a[i + 1], 1);
}
}
else
{
int x;
cin >> x;
int ans = st.query(x);
cout << ans << '\n';
}
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
61 ms |
94284 KB |
Output is correct |
2 |
Correct |
64 ms |
94288 KB |
Output is correct |
3 |
Correct |
66 ms |
94128 KB |
Output is correct |
4 |
Correct |
63 ms |
94292 KB |
Output is correct |
5 |
Correct |
63 ms |
94376 KB |
Output is correct |
6 |
Correct |
73 ms |
94288 KB |
Output is correct |
7 |
Correct |
62 ms |
94328 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
61 ms |
94284 KB |
Output is correct |
2 |
Correct |
64 ms |
94288 KB |
Output is correct |
3 |
Correct |
66 ms |
94128 KB |
Output is correct |
4 |
Correct |
63 ms |
94292 KB |
Output is correct |
5 |
Correct |
63 ms |
94376 KB |
Output is correct |
6 |
Correct |
73 ms |
94288 KB |
Output is correct |
7 |
Correct |
62 ms |
94328 KB |
Output is correct |
8 |
Correct |
114 ms |
95060 KB |
Output is correct |
9 |
Correct |
202 ms |
96796 KB |
Output is correct |
10 |
Correct |
203 ms |
96852 KB |
Output is correct |
11 |
Correct |
93 ms |
95408 KB |
Output is correct |
12 |
Correct |
116 ms |
96664 KB |
Output is correct |
13 |
Correct |
147 ms |
96776 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
61 ms |
94284 KB |
Output is correct |
2 |
Correct |
64 ms |
94288 KB |
Output is correct |
3 |
Correct |
66 ms |
94128 KB |
Output is correct |
4 |
Correct |
63 ms |
94292 KB |
Output is correct |
5 |
Correct |
63 ms |
94376 KB |
Output is correct |
6 |
Correct |
73 ms |
94288 KB |
Output is correct |
7 |
Correct |
62 ms |
94328 KB |
Output is correct |
8 |
Correct |
114 ms |
95060 KB |
Output is correct |
9 |
Correct |
202 ms |
96796 KB |
Output is correct |
10 |
Correct |
203 ms |
96852 KB |
Output is correct |
11 |
Correct |
93 ms |
95408 KB |
Output is correct |
12 |
Correct |
116 ms |
96664 KB |
Output is correct |
13 |
Correct |
147 ms |
96776 KB |
Output is correct |
14 |
Correct |
332 ms |
96952 KB |
Output is correct |
15 |
Correct |
338 ms |
96852 KB |
Output is correct |
16 |
Correct |
349 ms |
96812 KB |
Output is correct |
17 |
Correct |
349 ms |
96756 KB |
Output is correct |
18 |
Correct |
342 ms |
96956 KB |
Output is correct |