#include <bits/stdc++.h>
#include <bits/extc++.h>
#define StarBurstStream ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#define iter(a) a.begin(), a.end()
#define riter(a) a.rbegin(), a.rend()
#define lsort(a) sort(iter(a))
#define gsort(a) sort(riter(a))
#define pb(a) push_back(a)
#define eb(a) emplace_back(a)
#define pf(a) push_front(a)
#define ef(a) emplace_front(a)
#define pob pop_back()
#define pof pop_front()
#define mp(a, b) make_pair(a, b)
#define F first
#define S second
#define mt make_tuple
#define gt(t, i) get<i>(t)
#define tomax(a, b) ((a) = max((a), (b)))
#define tomin(a, b) ((a) = min((a), (b)))
#define topos(a) ((a) = (((a) % MOD + MOD) % MOD))
#define uni(a) a.resize(unique(iter(a)) - a.begin())
#define printv(a, b) {bool pvaspace=false; \
for(auto pva : a){ \
if(pvaspace) b << " "; pvaspace=true;\
b << pva;\
}\
b << "\n";}
using namespace std;
using namespace __gnu_pbds;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using pdd = pair<ld, ld>;
using tiii = tuple<int, int, int>;
const ll MOD = 1000000007;
const ll MAX = 2147483647;
template<typename A, typename B>
ostream& operator<<(ostream& o, pair<A, B> p){
return o << '(' << p.F << ',' << p.S << ')';
}
ll ifloor(ll a, ll b){
if(b < 0) a *= -1, b *= -1;
if(a < 0) return (a - b + 1) / b;
else return a / b;
}
ll iceil(ll a, ll b){
if(b < 0) a *= -1, b *= -1;
if(a > 0) return (a + b - 1) / b;
else return a / b;
}
int lowbit(int x){
return x & -x;
}
struct BIT{
vector<ll> bit;
void init(int n){
bit.resize(n + 1);
}
void modify(int x, ll v){
for(; x < bit.size(); x += lowbit(x)) bit[x] += v;
}
ll query(int x){
x = min(x, (int)bit.size() - 1);
ll ans = 0;
for(; x > 0; x -= lowbit(x)) ans += bit[x];
return ans;
}
};
struct BITSUM{
BIT bit1, bit2;
int offset;
void init(int n){
bit1.init(2 * n + 1);
bit2.init(2 * n + 1);
offset = n;
}
void modify(int x, ll v){
x += offset;
bit1.modify(x, v);
bit2.modify(x, x * v);
}
void modify(int l, int r, ll v){
modify(l, v);
modify(r + 1, -v);
}
ll query(int x){
x += offset;
return (x + 1) * bit1.query(x) - bit2.query(x);
}
ll query(int l, int r){
return query(r) - query(l - 1);
}
void off(){
modify(0, 0, query(1, 1));
offset--;
}
};
int main(){
StarBurstStream
int n, q;
cin >> n >> q;
vector<ll> a(n + 1);
for(int i = 1; i <= n; i++) cin >> a[i];
vector<vector<pii>> qry(n + 1);
for(int i = 1; i <= q; i++){
int t, l, r;
cin >> t >> l >> r;
qry[l - 1].eb(mp(t, -i));
qry[r].eb(mp(t, i));
}
BITSUM bit1, bit2, bit3;
bit1.init(n); bit2.init(n); bit3.init(n);
ll con = 0;
vector<int> pr(n + 1, -1);
vector<int> sz(n + 1, 1);
auto remove = [&](int i){
if(pr[i] == -1) return;
ll dp = i - pr[i] - 1;
ll da = a[pr[i]] - a[i];
bit1.modify(1, sz[i] + dp, -da);
bit2.modify(1, sz[i] + dp, da);
sz[pr[i]] += sz[i];
};
auto add = [&](int i){
if(pr[i] == -1) return;
ll dp = i - pr[i] - 1;
ll da = a[pr[i]] - a[i];
bit1.modify(1, 1 + dp, da);
bit3.modify(1, dp, da);
con += -dp * da;
};
auto query = [&](int T){
return bit1.query(T) + bit2.query(T) + bit3.query(T + 1, n) + con;
};
deque<int> dq;
vector<ll> ans(q + 1);
for(int i = 1; i <= n; i++){
while(!dq.empty() && a[dq.back()] <= a[i]){
remove(dq.back());
dq.pob;
}
bit1.off();
pr[i] = dq.empty() ? -1 : dq.back();
add(i);
dq.eb(i);
con += a[i];
/*cerr << "test " << i << " " << con << "\n";
printv(dq, cerr);
for(int i = 1; i <= n; i++) cerr << bit1.query(i) << " ";
cerr << "\n";
for(int i = 1; i <= n; i++) cerr << bit2.query(i) << " ";
cerr << "\n";
for(int i = 1; i <= n; i++) cerr << bit3.query(i) << " ";
cerr << "\n";*/
for(auto j : qry[i]){
ll tmp = query(j.F);
if(j.S > 0) ans[j.S] += tmp;
else ans[-j.S] -= tmp;
}
}
for(int i = 1; i <= q; i++) cout << ans[i] << "\n";
return 0;
}
Compilation message
ho_t5.cpp: In member function 'void BIT::modify(int, ll)':
ho_t5.cpp:73:17: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
73 | for(; x < bit.size(); x += lowbit(x)) bit[x] += v;
| ~~^~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
428 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
1 ms |
332 KB |
Output is correct |
12 |
Correct |
1 ms |
312 KB |
Output is correct |
13 |
Correct |
1 ms |
312 KB |
Output is correct |
14 |
Correct |
1 ms |
312 KB |
Output is correct |
15 |
Correct |
1 ms |
332 KB |
Output is correct |
16 |
Correct |
1 ms |
332 KB |
Output is correct |
17 |
Correct |
1 ms |
332 KB |
Output is correct |
18 |
Correct |
1 ms |
332 KB |
Output is correct |
19 |
Correct |
1 ms |
332 KB |
Output is correct |
20 |
Correct |
1 ms |
332 KB |
Output is correct |
21 |
Correct |
1 ms |
332 KB |
Output is correct |
22 |
Correct |
1 ms |
332 KB |
Output is correct |
23 |
Correct |
1 ms |
332 KB |
Output is correct |
24 |
Correct |
1 ms |
332 KB |
Output is correct |
25 |
Correct |
1 ms |
308 KB |
Output is correct |
26 |
Correct |
1 ms |
332 KB |
Output is correct |
27 |
Correct |
1 ms |
332 KB |
Output is correct |
28 |
Correct |
1 ms |
332 KB |
Output is correct |
29 |
Correct |
1 ms |
332 KB |
Output is correct |
30 |
Correct |
1 ms |
332 KB |
Output is correct |
31 |
Correct |
1 ms |
332 KB |
Output is correct |
32 |
Correct |
1 ms |
332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
316 ms |
42912 KB |
Output is correct |
3 |
Correct |
320 ms |
42692 KB |
Output is correct |
4 |
Correct |
322 ms |
42928 KB |
Output is correct |
5 |
Correct |
311 ms |
43312 KB |
Output is correct |
6 |
Correct |
349 ms |
42792 KB |
Output is correct |
7 |
Correct |
311 ms |
43412 KB |
Output is correct |
8 |
Correct |
351 ms |
43460 KB |
Output is correct |
9 |
Correct |
383 ms |
43272 KB |
Output is correct |
10 |
Correct |
314 ms |
42528 KB |
Output is correct |
11 |
Correct |
385 ms |
43684 KB |
Output is correct |
12 |
Correct |
307 ms |
42380 KB |
Output is correct |
13 |
Correct |
375 ms |
43048 KB |
Output is correct |
14 |
Correct |
371 ms |
42980 KB |
Output is correct |
15 |
Correct |
319 ms |
43112 KB |
Output is correct |
16 |
Correct |
356 ms |
42772 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
381 ms |
42216 KB |
Output is correct |
3 |
Correct |
361 ms |
41412 KB |
Output is correct |
4 |
Correct |
383 ms |
42972 KB |
Output is correct |
5 |
Correct |
346 ms |
41792 KB |
Output is correct |
6 |
Correct |
344 ms |
42116 KB |
Output is correct |
7 |
Correct |
409 ms |
42308 KB |
Output is correct |
8 |
Correct |
357 ms |
42128 KB |
Output is correct |
9 |
Correct |
374 ms |
41736 KB |
Output is correct |
10 |
Correct |
336 ms |
41332 KB |
Output is correct |
11 |
Correct |
405 ms |
42860 KB |
Output is correct |
12 |
Correct |
382 ms |
42436 KB |
Output is correct |
13 |
Correct |
434 ms |
42640 KB |
Output is correct |
14 |
Correct |
522 ms |
41796 KB |
Output is correct |
15 |
Correct |
418 ms |
42688 KB |
Output is correct |
16 |
Correct |
394 ms |
42164 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
472 ms |
39356 KB |
Output is correct |
2 |
Correct |
329 ms |
39392 KB |
Output is correct |
3 |
Correct |
367 ms |
40372 KB |
Output is correct |
4 |
Correct |
375 ms |
39292 KB |
Output is correct |
5 |
Correct |
352 ms |
39628 KB |
Output is correct |
6 |
Correct |
343 ms |
39492 KB |
Output is correct |
7 |
Correct |
388 ms |
40100 KB |
Output is correct |
8 |
Correct |
352 ms |
39956 KB |
Output is correct |
9 |
Correct |
344 ms |
39428 KB |
Output is correct |
10 |
Correct |
354 ms |
39792 KB |
Output is correct |
11 |
Correct |
347 ms |
39684 KB |
Output is correct |
12 |
Correct |
364 ms |
39880 KB |
Output is correct |
13 |
Correct |
334 ms |
39560 KB |
Output is correct |
14 |
Correct |
346 ms |
39768 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
428 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
1 ms |
332 KB |
Output is correct |
12 |
Correct |
1 ms |
312 KB |
Output is correct |
13 |
Correct |
1 ms |
312 KB |
Output is correct |
14 |
Correct |
1 ms |
312 KB |
Output is correct |
15 |
Correct |
1 ms |
332 KB |
Output is correct |
16 |
Correct |
1 ms |
332 KB |
Output is correct |
17 |
Correct |
1 ms |
332 KB |
Output is correct |
18 |
Correct |
1 ms |
332 KB |
Output is correct |
19 |
Correct |
1 ms |
332 KB |
Output is correct |
20 |
Correct |
1 ms |
332 KB |
Output is correct |
21 |
Correct |
1 ms |
332 KB |
Output is correct |
22 |
Correct |
1 ms |
332 KB |
Output is correct |
23 |
Correct |
1 ms |
332 KB |
Output is correct |
24 |
Correct |
1 ms |
332 KB |
Output is correct |
25 |
Correct |
1 ms |
308 KB |
Output is correct |
26 |
Correct |
1 ms |
332 KB |
Output is correct |
27 |
Correct |
1 ms |
332 KB |
Output is correct |
28 |
Correct |
1 ms |
332 KB |
Output is correct |
29 |
Correct |
1 ms |
332 KB |
Output is correct |
30 |
Correct |
1 ms |
332 KB |
Output is correct |
31 |
Correct |
1 ms |
332 KB |
Output is correct |
32 |
Correct |
1 ms |
332 KB |
Output is correct |
33 |
Correct |
316 ms |
42912 KB |
Output is correct |
34 |
Correct |
320 ms |
42692 KB |
Output is correct |
35 |
Correct |
322 ms |
42928 KB |
Output is correct |
36 |
Correct |
311 ms |
43312 KB |
Output is correct |
37 |
Correct |
349 ms |
42792 KB |
Output is correct |
38 |
Correct |
311 ms |
43412 KB |
Output is correct |
39 |
Correct |
351 ms |
43460 KB |
Output is correct |
40 |
Correct |
383 ms |
43272 KB |
Output is correct |
41 |
Correct |
314 ms |
42528 KB |
Output is correct |
42 |
Correct |
385 ms |
43684 KB |
Output is correct |
43 |
Correct |
307 ms |
42380 KB |
Output is correct |
44 |
Correct |
375 ms |
43048 KB |
Output is correct |
45 |
Correct |
371 ms |
42980 KB |
Output is correct |
46 |
Correct |
319 ms |
43112 KB |
Output is correct |
47 |
Correct |
356 ms |
42772 KB |
Output is correct |
48 |
Correct |
381 ms |
42216 KB |
Output is correct |
49 |
Correct |
361 ms |
41412 KB |
Output is correct |
50 |
Correct |
383 ms |
42972 KB |
Output is correct |
51 |
Correct |
346 ms |
41792 KB |
Output is correct |
52 |
Correct |
344 ms |
42116 KB |
Output is correct |
53 |
Correct |
409 ms |
42308 KB |
Output is correct |
54 |
Correct |
357 ms |
42128 KB |
Output is correct |
55 |
Correct |
374 ms |
41736 KB |
Output is correct |
56 |
Correct |
336 ms |
41332 KB |
Output is correct |
57 |
Correct |
405 ms |
42860 KB |
Output is correct |
58 |
Correct |
382 ms |
42436 KB |
Output is correct |
59 |
Correct |
434 ms |
42640 KB |
Output is correct |
60 |
Correct |
522 ms |
41796 KB |
Output is correct |
61 |
Correct |
418 ms |
42688 KB |
Output is correct |
62 |
Correct |
394 ms |
42164 KB |
Output is correct |
63 |
Correct |
472 ms |
39356 KB |
Output is correct |
64 |
Correct |
329 ms |
39392 KB |
Output is correct |
65 |
Correct |
367 ms |
40372 KB |
Output is correct |
66 |
Correct |
375 ms |
39292 KB |
Output is correct |
67 |
Correct |
352 ms |
39628 KB |
Output is correct |
68 |
Correct |
343 ms |
39492 KB |
Output is correct |
69 |
Correct |
388 ms |
40100 KB |
Output is correct |
70 |
Correct |
352 ms |
39956 KB |
Output is correct |
71 |
Correct |
344 ms |
39428 KB |
Output is correct |
72 |
Correct |
354 ms |
39792 KB |
Output is correct |
73 |
Correct |
347 ms |
39684 KB |
Output is correct |
74 |
Correct |
364 ms |
39880 KB |
Output is correct |
75 |
Correct |
334 ms |
39560 KB |
Output is correct |
76 |
Correct |
346 ms |
39768 KB |
Output is correct |
77 |
Correct |
403 ms |
42776 KB |
Output is correct |
78 |
Correct |
417 ms |
43460 KB |
Output is correct |
79 |
Correct |
420 ms |
43292 KB |
Output is correct |
80 |
Correct |
374 ms |
42872 KB |
Output is correct |
81 |
Correct |
406 ms |
42620 KB |
Output is correct |
82 |
Correct |
411 ms |
43148 KB |
Output is correct |
83 |
Correct |
387 ms |
42820 KB |
Output is correct |
84 |
Correct |
409 ms |
42640 KB |
Output is correct |
85 |
Correct |
438 ms |
43220 KB |
Output is correct |
86 |
Correct |
401 ms |
42880 KB |
Output is correct |
87 |
Correct |
366 ms |
43720 KB |
Output is correct |
88 |
Correct |
388 ms |
43588 KB |
Output is correct |
89 |
Correct |
346 ms |
42344 KB |
Output is correct |
90 |
Correct |
414 ms |
43340 KB |
Output is correct |
91 |
Correct |
325 ms |
42692 KB |
Output is correct |
92 |
Correct |
358 ms |
42244 KB |
Output is correct |
93 |
Correct |
328 ms |
42856 KB |
Output is correct |
94 |
Correct |
368 ms |
43972 KB |
Output is correct |
95 |
Correct |
370 ms |
43844 KB |
Output is correct |
96 |
Correct |
337 ms |
43180 KB |
Output is correct |
97 |
Correct |
325 ms |
43020 KB |
Output is correct |
98 |
Correct |
327 ms |
42216 KB |
Output is correct |
99 |
Correct |
329 ms |
42732 KB |
Output is correct |
100 |
Correct |
326 ms |
43024 KB |
Output is correct |
101 |
Correct |
343 ms |
42424 KB |
Output is correct |
102 |
Correct |
378 ms |
43332 KB |
Output is correct |