/**
* code generated by JHelper
* More info: https://github.com/AlexeyDmitriev/JHelper
* @author Azat Ismagilov
*/
/*.. ....
..',,'..............''...'''.','..
.';'.. ..',,'.
.'.. .';,.
.'. .:ol;. .';.
.. .cO00k:. .,.
.. .'...;k00Od' .'.
... .;xOkxdk00Od, ...
... .,x0000000Od;. ..
.' .cO00OOOOOOd;'... ...
.'. .;lxkOOOO00Okkxo:. .'.
.'. .cxOOOOO000000Ol. .'.
.'. .ck00OOd::oxOOOOl. .,.
''...,;''''''''':kKK0Od,. ..:xOo' .,.
';,;;;:,..........;lxOx:. .';' .''
'cc;.. .,;:,.. ...
,oc. .......... .',:;,.. ...
'o:. .,;;;,,,,;;;;;'. ..,;;,.. ...
';. ... .,::. .,::'. .'.
.. .',. ..;;'. ...
.. .',,;;,'. ':;'. .,.
.,::;,. ....... .';:'. .,,.
.;dOK0x;. . .':;. .,.
...,cdxdl;. ..,;;;cloddoc;. .,:,. .,.
. ...;cl:. ':ldxxdocccclol;. ..:;.',.
. .;llc;',c;. .,:,''';::;'. .. .;:;'
.:xdoxko,',. .,:. .;llc:ldl. .;c.
.lxdoxkd:''. .;:. .cxl'';od;. .. .,,
.:clool;.',. .:' .:dxolccl;. .::;,:'
.;::,.. .,. .;. .,:;:;,,;' .,;,:ol.
.. ',. .;. .',,,,,. .,:llc;.
.,' .,. .;lxxc'
.,;. .;. .;lxxc.
.. .;;. .:' .;cc,.
...';' .;;. .;:. ,:'
.;c:;. .. .,;. ....,;. .'.
.,lc,. .':,..,:,''....;c:;'':'.. .. .'.
.;:;. .,:' .::,,;;;;;..',;,..,,. .,. .'.
.'',,. .,;. .;, .'. ...
.'',,. .,;. ';. .,. .'.
.'... .;. .,,. .,' ..
.'. .;,. .::. .,' .
.'. .,;. ,c' .,' .'.
.'. .:' .,' .. .,'
''. ,;. ..................... .;' .:,
.,. .,;. ..'''''''''''''''''','. .,;. .::.
.,. ,;. .,;. .;,.
.,,. .:,. .;;. .''.
.,' .'. ... .,.
.'' .,.
.,'. .'.
.'. .*/
#include <fstream>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <numeric>
#include <set>
#include <map>
#define ll long long
#define ld long double
#define fs first
#define sc second
#define pb push_back
#define ppb pop_back
#define pf push_front
#define ppf pop_front
#define eb emplace_back
#define mp make_pair
#define len(v) ((int)v.size())
#define vc vector
#define pr pair
#define all(v) v.begin(), v.end()
//#pragma GCC optimize("Ofast")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,tune=native")
//#pragma GCC optimize("unroll-loops")
using namespace std;
template<typename T, typename U>
inline ostream &operator<<(ostream &_out, const pair<T, U> &_p) {
_out << _p.first << ' ' << _p.second;
return _out;
}
template<typename T, typename U>
inline istream &operator>>(istream &_in, pair<T, U> &_p) {
_in >> _p.first >> _p.second;
return _in;
}
template<typename T>
inline ostream &operator<<(ostream &_out, const vector<T> &_v) {
if (_v.empty()) { return _out; }
_out << _v.front();
for (auto _it = ++_v.begin(); _it != _v.end(); ++_it) { _out << ' ' << *_it; }
return _out;
}
template<typename T>
inline istream &operator>>(istream &_in, vector<T> &_v) {
for (auto &_i : _v) { _in >> _i; }
return _in;
}
const int MAXN = 6e5;
const int INF = 1e9;
const int MOD = 1e9 + 7;
vc<pr<pr<int, int>, pr<int, int>>> lefts, rights;
void add_segment(int l1, int r1, int l2, int r2, int value) {
//cout << l1 << ' ' << r1 << ' ' << l2 << ' ' << r2 << ' ' << value << endl;
if (r1 - l1 <= r2 - l2) {
for (int i = l1; i <= r1; i++) {
lefts.pb({{i, value},
{l2 - i, r2 - i}});
}
} else {
for (int i = l2; i <= r2; i++) {
rights.pb({{i, value},
{i - r1, i - l1}});
}
}
}
int n;
ll t[MAXN];
void init(int nn) {
n = nn;
fill(t, t + n, 0);
}
void upd(int v, int x) {
for (; v < n; v = (v | (v + 1))) {
t[v] += x;
}
}
ll get(int v) {
ll ans = 0;
for (; v >= 0; v = (v & (v + 1)) - 1) {
ans += t[v];
}
return ans;
}
ll get(int l, int r) {
return get(r) - get(l - 1);
}
class pozhar {
public:
void solve(std::istream &in, std::ostream &out) {
int n, q;
in >> n >> q;
vc<int> s(n);
in >> s;
set<int> limiters;
limiters.insert(-1);
limiters.insert(n);
vc<pr<int, int>> dds;
for (int i = 0; i < n; i++) {
dds.pb({s[i], i});
}
sort(all(dds));
reverse(all(dds));
for (auto v : dds) {
auto r = (*limiters.lower_bound(v.sc)) - 1;
auto l = (*prev(limiters.lower_bound(v.sc))) + 1;
if (l <= v.sc && v.sc <= r) {
add_segment(l, v.sc, v.sc, r, v.fs);
}
limiters.insert(v.sc);
}
/*
cout << "lefts\n";
for (auto v : lefts) {
cout << v << endl;
}
cout << "rights\n";
for (auto v : rights) {
cout << v << endl;
}
*/
vc<ll> ans(q);
vc<pr<int, pr<int, int>>> que(q);
vc<vc<int>> bylen(n + 2);
int i = 0;
for (auto &v : que) {
in >> v.fs >> v.sc;
v.sc.fs--, v.sc.sc--;
bylen[v.fs].pb(i);
i++;
}
{
int maxi = 0;
for (int i = 0; i < n; i++) {
maxi = max(maxi, s[i]);
rights.pb({{i, maxi},
{i + 1, n}});
}
}
{
vc<vc<pr<int, int>>> gr(n + 2);
for (auto v : lefts) {
gr[v.sc.fs].pb(v.fs);
v.fs.sc *= -1;
gr[v.sc.sc + 1].pb(v.fs);
}
init(2 * n);
for (int i = 0; i <= n; i++) {
for (auto d : gr[i]) {
upd(d.fs + n, d.sc);
}
for (auto d : bylen[i]) {
ans[d] += get(que[d].sc.fs - i + n, que[d].sc.sc - i + n);
}
}
}
{
vc<vc<pr<int, int>>> gr(n + 2);
for (auto v : rights) {
gr[v.sc.fs].pb(v.fs);
v.fs.sc *= -1;
gr[v.sc.sc + 1].pb(v.fs);
}
init(n + 10);
for (int i = 0; i <= n; i++) {
for (auto d : gr[i]) {
upd(d.fs, d.sc);
}
for (auto d : bylen[i]) {
ans[d] += get(que[d].sc.fs, que[d].sc.sc);
}
}
}
for (auto v : ans) {
out << v << '\n';
}
}
};
signed main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
pozhar solver;
std::istream& in(std::cin);
std::ostream& out(std::cout);
solver.solve(in, out);
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
376 KB |
Output is correct |
2 |
Correct |
5 ms |
384 KB |
Output is correct |
3 |
Correct |
5 ms |
376 KB |
Output is correct |
4 |
Correct |
5 ms |
376 KB |
Output is correct |
5 |
Correct |
6 ms |
376 KB |
Output is correct |
6 |
Correct |
5 ms |
376 KB |
Output is correct |
7 |
Correct |
6 ms |
376 KB |
Output is correct |
8 |
Correct |
6 ms |
352 KB |
Output is correct |
9 |
Correct |
5 ms |
376 KB |
Output is correct |
10 |
Correct |
19 ms |
376 KB |
Output is correct |
11 |
Correct |
6 ms |
376 KB |
Output is correct |
12 |
Correct |
6 ms |
380 KB |
Output is correct |
13 |
Correct |
6 ms |
376 KB |
Output is correct |
14 |
Correct |
5 ms |
376 KB |
Output is correct |
15 |
Correct |
6 ms |
376 KB |
Output is correct |
16 |
Correct |
5 ms |
376 KB |
Output is correct |
17 |
Correct |
5 ms |
376 KB |
Output is correct |
18 |
Correct |
5 ms |
376 KB |
Output is correct |
19 |
Correct |
5 ms |
376 KB |
Output is correct |
20 |
Correct |
5 ms |
504 KB |
Output is correct |
21 |
Correct |
5 ms |
380 KB |
Output is correct |
22 |
Correct |
5 ms |
376 KB |
Output is correct |
23 |
Correct |
6 ms |
480 KB |
Output is correct |
24 |
Correct |
5 ms |
376 KB |
Output is correct |
25 |
Correct |
5 ms |
376 KB |
Output is correct |
26 |
Correct |
5 ms |
376 KB |
Output is correct |
27 |
Correct |
5 ms |
376 KB |
Output is correct |
28 |
Correct |
6 ms |
376 KB |
Output is correct |
29 |
Correct |
5 ms |
376 KB |
Output is correct |
30 |
Correct |
5 ms |
376 KB |
Output is correct |
31 |
Correct |
5 ms |
376 KB |
Output is correct |
32 |
Correct |
6 ms |
376 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
376 KB |
Output is correct |
2 |
Correct |
555 ms |
70812 KB |
Output is correct |
3 |
Correct |
548 ms |
70556 KB |
Output is correct |
4 |
Correct |
567 ms |
70900 KB |
Output is correct |
5 |
Correct |
552 ms |
73324 KB |
Output is correct |
6 |
Correct |
538 ms |
74576 KB |
Output is correct |
7 |
Correct |
560 ms |
72048 KB |
Output is correct |
8 |
Correct |
558 ms |
77080 KB |
Output is correct |
9 |
Correct |
545 ms |
72656 KB |
Output is correct |
10 |
Correct |
656 ms |
69932 KB |
Output is correct |
11 |
Correct |
612 ms |
77780 KB |
Output is correct |
12 |
Correct |
554 ms |
71784 KB |
Output is correct |
13 |
Correct |
685 ms |
72476 KB |
Output is correct |
14 |
Correct |
562 ms |
74488 KB |
Output is correct |
15 |
Correct |
529 ms |
70352 KB |
Output is correct |
16 |
Correct |
593 ms |
72716 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
376 KB |
Output is correct |
2 |
Correct |
749 ms |
74496 KB |
Output is correct |
3 |
Correct |
626 ms |
72352 KB |
Output is correct |
4 |
Correct |
675 ms |
78628 KB |
Output is correct |
5 |
Correct |
660 ms |
74912 KB |
Output is correct |
6 |
Correct |
648 ms |
74772 KB |
Output is correct |
7 |
Correct |
671 ms |
77276 KB |
Output is correct |
8 |
Correct |
773 ms |
75552 KB |
Output is correct |
9 |
Correct |
725 ms |
73628 KB |
Output is correct |
10 |
Correct |
635 ms |
75680 KB |
Output is correct |
11 |
Correct |
681 ms |
76836 KB |
Output is correct |
12 |
Correct |
652 ms |
75688 KB |
Output is correct |
13 |
Correct |
662 ms |
74312 KB |
Output is correct |
14 |
Correct |
697 ms |
73508 KB |
Output is correct |
15 |
Correct |
672 ms |
75836 KB |
Output is correct |
16 |
Correct |
701 ms |
73912 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
397 ms |
51000 KB |
Output is correct |
2 |
Correct |
403 ms |
51448 KB |
Output is correct |
3 |
Correct |
433 ms |
52740 KB |
Output is correct |
4 |
Correct |
440 ms |
50788 KB |
Output is correct |
5 |
Correct |
493 ms |
51388 KB |
Output is correct |
6 |
Correct |
440 ms |
51512 KB |
Output is correct |
7 |
Correct |
412 ms |
52684 KB |
Output is correct |
8 |
Correct |
411 ms |
52072 KB |
Output is correct |
9 |
Correct |
397 ms |
51364 KB |
Output is correct |
10 |
Correct |
410 ms |
51928 KB |
Output is correct |
11 |
Correct |
420 ms |
51328 KB |
Output is correct |
12 |
Correct |
446 ms |
51732 KB |
Output is correct |
13 |
Correct |
452 ms |
51620 KB |
Output is correct |
14 |
Correct |
428 ms |
51736 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
376 KB |
Output is correct |
2 |
Correct |
5 ms |
384 KB |
Output is correct |
3 |
Correct |
5 ms |
376 KB |
Output is correct |
4 |
Correct |
5 ms |
376 KB |
Output is correct |
5 |
Correct |
6 ms |
376 KB |
Output is correct |
6 |
Correct |
5 ms |
376 KB |
Output is correct |
7 |
Correct |
6 ms |
376 KB |
Output is correct |
8 |
Correct |
6 ms |
352 KB |
Output is correct |
9 |
Correct |
5 ms |
376 KB |
Output is correct |
10 |
Correct |
19 ms |
376 KB |
Output is correct |
11 |
Correct |
6 ms |
376 KB |
Output is correct |
12 |
Correct |
6 ms |
380 KB |
Output is correct |
13 |
Correct |
6 ms |
376 KB |
Output is correct |
14 |
Correct |
5 ms |
376 KB |
Output is correct |
15 |
Correct |
6 ms |
376 KB |
Output is correct |
16 |
Correct |
5 ms |
376 KB |
Output is correct |
17 |
Correct |
5 ms |
376 KB |
Output is correct |
18 |
Correct |
5 ms |
376 KB |
Output is correct |
19 |
Correct |
5 ms |
376 KB |
Output is correct |
20 |
Correct |
5 ms |
504 KB |
Output is correct |
21 |
Correct |
5 ms |
380 KB |
Output is correct |
22 |
Correct |
5 ms |
376 KB |
Output is correct |
23 |
Correct |
6 ms |
480 KB |
Output is correct |
24 |
Correct |
5 ms |
376 KB |
Output is correct |
25 |
Correct |
5 ms |
376 KB |
Output is correct |
26 |
Correct |
5 ms |
376 KB |
Output is correct |
27 |
Correct |
5 ms |
376 KB |
Output is correct |
28 |
Correct |
6 ms |
376 KB |
Output is correct |
29 |
Correct |
5 ms |
376 KB |
Output is correct |
30 |
Correct |
5 ms |
376 KB |
Output is correct |
31 |
Correct |
5 ms |
376 KB |
Output is correct |
32 |
Correct |
6 ms |
376 KB |
Output is correct |
33 |
Correct |
785 ms |
75528 KB |
Output is correct |
34 |
Correct |
666 ms |
78088 KB |
Output is correct |
35 |
Correct |
713 ms |
76760 KB |
Output is correct |
36 |
Correct |
862 ms |
77172 KB |
Output is correct |
37 |
Correct |
646 ms |
73664 KB |
Output is correct |
38 |
Correct |
640 ms |
73636 KB |
Output is correct |
39 |
Correct |
707 ms |
74664 KB |
Output is correct |
40 |
Correct |
642 ms |
74276 KB |
Output is correct |
41 |
Correct |
710 ms |
75120 KB |
Output is correct |
42 |
Correct |
848 ms |
76124 KB |
Output is correct |
43 |
Correct |
601 ms |
77476 KB |
Output is correct |
44 |
Correct |
609 ms |
72252 KB |
Output is correct |
45 |
Correct |
701 ms |
69360 KB |
Output is correct |
46 |
Correct |
602 ms |
72324 KB |
Output is correct |
47 |
Correct |
556 ms |
71464 KB |
Output is correct |
48 |
Correct |
551 ms |
72628 KB |
Output is correct |
49 |
Correct |
568 ms |
73576 KB |
Output is correct |
50 |
Correct |
588 ms |
72856 KB |
Output is correct |
51 |
Correct |
598 ms |
71544 KB |
Output is correct |
52 |
Correct |
583 ms |
73064 KB |
Output is correct |
53 |
Correct |
553 ms |
69768 KB |
Output is correct |
54 |
Correct |
561 ms |
69700 KB |
Output is correct |
55 |
Correct |
571 ms |
73804 KB |
Output is correct |
56 |
Correct |
567 ms |
72636 KB |
Output is correct |
57 |
Correct |
578 ms |
71048 KB |
Output is correct |
58 |
Correct |
600 ms |
73068 KB |
Output is correct |
59 |
Correct |
555 ms |
70812 KB |
Output is correct |
60 |
Correct |
548 ms |
70556 KB |
Output is correct |
61 |
Correct |
567 ms |
70900 KB |
Output is correct |
62 |
Correct |
552 ms |
73324 KB |
Output is correct |
63 |
Correct |
538 ms |
74576 KB |
Output is correct |
64 |
Correct |
560 ms |
72048 KB |
Output is correct |
65 |
Correct |
558 ms |
77080 KB |
Output is correct |
66 |
Correct |
545 ms |
72656 KB |
Output is correct |
67 |
Correct |
656 ms |
69932 KB |
Output is correct |
68 |
Correct |
612 ms |
77780 KB |
Output is correct |
69 |
Correct |
554 ms |
71784 KB |
Output is correct |
70 |
Correct |
685 ms |
72476 KB |
Output is correct |
71 |
Correct |
562 ms |
74488 KB |
Output is correct |
72 |
Correct |
529 ms |
70352 KB |
Output is correct |
73 |
Correct |
593 ms |
72716 KB |
Output is correct |
74 |
Correct |
749 ms |
74496 KB |
Output is correct |
75 |
Correct |
626 ms |
72352 KB |
Output is correct |
76 |
Correct |
675 ms |
78628 KB |
Output is correct |
77 |
Correct |
660 ms |
74912 KB |
Output is correct |
78 |
Correct |
648 ms |
74772 KB |
Output is correct |
79 |
Correct |
671 ms |
77276 KB |
Output is correct |
80 |
Correct |
773 ms |
75552 KB |
Output is correct |
81 |
Correct |
725 ms |
73628 KB |
Output is correct |
82 |
Correct |
635 ms |
75680 KB |
Output is correct |
83 |
Correct |
681 ms |
76836 KB |
Output is correct |
84 |
Correct |
652 ms |
75688 KB |
Output is correct |
85 |
Correct |
662 ms |
74312 KB |
Output is correct |
86 |
Correct |
697 ms |
73508 KB |
Output is correct |
87 |
Correct |
672 ms |
75836 KB |
Output is correct |
88 |
Correct |
701 ms |
73912 KB |
Output is correct |
89 |
Correct |
397 ms |
51000 KB |
Output is correct |
90 |
Correct |
403 ms |
51448 KB |
Output is correct |
91 |
Correct |
433 ms |
52740 KB |
Output is correct |
92 |
Correct |
440 ms |
50788 KB |
Output is correct |
93 |
Correct |
493 ms |
51388 KB |
Output is correct |
94 |
Correct |
440 ms |
51512 KB |
Output is correct |
95 |
Correct |
412 ms |
52684 KB |
Output is correct |
96 |
Correct |
411 ms |
52072 KB |
Output is correct |
97 |
Correct |
397 ms |
51364 KB |
Output is correct |
98 |
Correct |
410 ms |
51928 KB |
Output is correct |
99 |
Correct |
420 ms |
51328 KB |
Output is correct |
100 |
Correct |
446 ms |
51732 KB |
Output is correct |
101 |
Correct |
452 ms |
51620 KB |
Output is correct |
102 |
Correct |
428 ms |
51736 KB |
Output is correct |