#include <bits/stdc++.h>
#define all(v) v.begin(),v.end()
using namespace std;
typedef long long ll;
struct line{
ll a,b;
ll get_val(ll x){
return a*x+b;
}
};
line operator+(const line &l1,const line &l2){
return {l1.a+l2.a,l1.b+l2.b};
}
line operator*(const line &l,const ll k){
return {l.a*k,l.b*k};
}
struct us{
int t,x;
line l;
bool operator<(const us &tmp){
if (t!=tmp.t)
return t<tmp.t;
return l.a>tmp.l.a;
}
};
struct qs{
int t,l,r,x;
bool operator<(const qs &tmp){
return t<tmp.t;
}
};
struct node{
line sum,cnt;
};
node operator+(const node &a,const node &b){
return {a.sum+b.sum,a.cnt+b.cnt};
}
const ll MAX=1e9+1;
int N,Q;
ll A[200002];
int D1[200001],D2[200001];
node Tree[1<<19];
node update(int x,line v,int l,int r,int n){
if (x<l||r<x)
return Tree[n];
if (l==r)
return Tree[n]={v*A[x],v};
int m=(l+r)>>1;
return Tree[n]=update(x,v,l,m,n<<1)+update(x,v,m+1,r,n<<1|1);
}
ll query(int x,int t,int l,int r,int n){
if (l==r)
return A[l]*x;
int m=(l+r)>>1;
if (x>Tree[n<<1].cnt.get_val(t))
return Tree[n<<1].sum.get_val(t)+query(x-Tree[n<<1].cnt.get_val(t),t,m+1,r,n<<1|1);
return query(x,t,l,m,n<<1);
}
int main(){
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin>>N>>Q;
for (int i=1; i<=N; i++)
cin>>A[i];
A[N+1]=MAX;
stack<int> s;
for (int i=1; i<=N; i++){
while (!s.empty()&&A[s.top()]<A[i])
s.pop();
if (!s.empty())
D1[i]=s.top();
s.push(i);
}
while (!s.empty())
s.pop();
s.push(N+1);
for (int i=N; i; i--){
while (A[s.top()]<=A[i])
s.pop();
D2[i]=s.top();
s.push(i);
}
vector<us> vu;
for (int i=1; i<=N; i++){
if (D1[i]){
int t1=i-D1[i]-1,t2=D2[i]-i-1;
if (t1>t2)
swap(t1,t2);
vu.push_back({0,i,{1,1}});
vu.push_back({t1,i,{0,t1+1}});
vu.push_back({t2,i,{-1,t1+t2+1}});
vu.push_back({t1+t2+1,i,{0,0}});
}
else{
int t=D2[i]-i-1;
vu.push_back({0,i,{1,1}});
vu.push_back({t,i,{0,t+1}});
}
}
sort(all(vu));
vector<qs> vq;
for (int i=0,t,l,r; i<Q; i++){
cin>>t>>l>>r;
vq.push_back({t,l,r,i});
}
sort(all(vq));
vector<ll> ans(Q);
for (int t=0,iu=0,iq=0; t<=N; t++){
for (; iu<(int)vu.size()&&vu[iu].t==t; iu++)
update(vu[iu].x,vu[iu].l,1,N,1);
for (; iq<(int)vq.size()&&vq[iq].t==t; iq++)
ans[vq[iq].x]=query(vq[iq].r,t,1,N,1)-query(vq[iq].l-1,t,1,N,1);
}
for (int i=0; i<Q; i++)
cout<<ans[i]<<"\n";
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
336 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
312 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
332 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
332 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
332 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
2 ms |
336 KB |
Output is correct |
21 |
Correct |
2 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
1 ms |
332 KB |
Output is correct |
25 |
Correct |
1 ms |
328 KB |
Output is correct |
26 |
Correct |
1 ms |
340 KB |
Output is correct |
27 |
Correct |
1 ms |
336 KB |
Output is correct |
28 |
Correct |
1 ms |
340 KB |
Output is correct |
29 |
Correct |
1 ms |
340 KB |
Output is correct |
30 |
Correct |
1 ms |
340 KB |
Output is correct |
31 |
Correct |
1 ms |
332 KB |
Output is correct |
32 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
690 ms |
48080 KB |
Output is correct |
3 |
Correct |
696 ms |
53572 KB |
Output is correct |
4 |
Correct |
776 ms |
53780 KB |
Output is correct |
5 |
Correct |
686 ms |
54176 KB |
Output is correct |
6 |
Correct |
717 ms |
53632 KB |
Output is correct |
7 |
Correct |
719 ms |
54056 KB |
Output is correct |
8 |
Correct |
706 ms |
54176 KB |
Output is correct |
9 |
Correct |
657 ms |
54020 KB |
Output is correct |
10 |
Correct |
659 ms |
53428 KB |
Output is correct |
11 |
Correct |
690 ms |
54300 KB |
Output is correct |
12 |
Correct |
705 ms |
53364 KB |
Output is correct |
13 |
Correct |
692 ms |
53916 KB |
Output is correct |
14 |
Correct |
695 ms |
53816 KB |
Output is correct |
15 |
Correct |
760 ms |
53900 KB |
Output is correct |
16 |
Correct |
651 ms |
53704 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
640 ms |
47148 KB |
Output is correct |
3 |
Correct |
637 ms |
52296 KB |
Output is correct |
4 |
Correct |
674 ms |
53484 KB |
Output is correct |
5 |
Correct |
646 ms |
52544 KB |
Output is correct |
6 |
Correct |
651 ms |
52856 KB |
Output is correct |
7 |
Correct |
674 ms |
52932 KB |
Output is correct |
8 |
Correct |
670 ms |
52792 KB |
Output is correct |
9 |
Correct |
661 ms |
52368 KB |
Output is correct |
10 |
Correct |
640 ms |
52028 KB |
Output is correct |
11 |
Correct |
670 ms |
53376 KB |
Output is correct |
12 |
Correct |
694 ms |
53024 KB |
Output is correct |
13 |
Correct |
653 ms |
53252 KB |
Output is correct |
14 |
Correct |
656 ms |
52480 KB |
Output is correct |
15 |
Correct |
659 ms |
53240 KB |
Output is correct |
16 |
Correct |
667 ms |
52996 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
648 ms |
46264 KB |
Output is correct |
2 |
Correct |
663 ms |
50584 KB |
Output is correct |
3 |
Correct |
689 ms |
51276 KB |
Output is correct |
4 |
Correct |
637 ms |
50384 KB |
Output is correct |
5 |
Correct |
649 ms |
50444 KB |
Output is correct |
6 |
Correct |
664 ms |
50584 KB |
Output is correct |
7 |
Correct |
677 ms |
51100 KB |
Output is correct |
8 |
Correct |
678 ms |
50816 KB |
Output is correct |
9 |
Correct |
667 ms |
50580 KB |
Output is correct |
10 |
Correct |
662 ms |
50788 KB |
Output is correct |
11 |
Correct |
658 ms |
50632 KB |
Output is correct |
12 |
Correct |
669 ms |
50748 KB |
Output is correct |
13 |
Correct |
670 ms |
50748 KB |
Output is correct |
14 |
Correct |
688 ms |
50716 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
336 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
312 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
332 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
332 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
332 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
2 ms |
336 KB |
Output is correct |
21 |
Correct |
2 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
1 ms |
332 KB |
Output is correct |
25 |
Correct |
1 ms |
328 KB |
Output is correct |
26 |
Correct |
1 ms |
340 KB |
Output is correct |
27 |
Correct |
1 ms |
336 KB |
Output is correct |
28 |
Correct |
1 ms |
340 KB |
Output is correct |
29 |
Correct |
1 ms |
340 KB |
Output is correct |
30 |
Correct |
1 ms |
340 KB |
Output is correct |
31 |
Correct |
1 ms |
332 KB |
Output is correct |
32 |
Correct |
1 ms |
340 KB |
Output is correct |
33 |
Correct |
690 ms |
48080 KB |
Output is correct |
34 |
Correct |
696 ms |
53572 KB |
Output is correct |
35 |
Correct |
776 ms |
53780 KB |
Output is correct |
36 |
Correct |
686 ms |
54176 KB |
Output is correct |
37 |
Correct |
717 ms |
53632 KB |
Output is correct |
38 |
Correct |
719 ms |
54056 KB |
Output is correct |
39 |
Correct |
706 ms |
54176 KB |
Output is correct |
40 |
Correct |
657 ms |
54020 KB |
Output is correct |
41 |
Correct |
659 ms |
53428 KB |
Output is correct |
42 |
Correct |
690 ms |
54300 KB |
Output is correct |
43 |
Correct |
705 ms |
53364 KB |
Output is correct |
44 |
Correct |
692 ms |
53916 KB |
Output is correct |
45 |
Correct |
695 ms |
53816 KB |
Output is correct |
46 |
Correct |
760 ms |
53900 KB |
Output is correct |
47 |
Correct |
651 ms |
53704 KB |
Output is correct |
48 |
Correct |
640 ms |
47148 KB |
Output is correct |
49 |
Correct |
637 ms |
52296 KB |
Output is correct |
50 |
Correct |
674 ms |
53484 KB |
Output is correct |
51 |
Correct |
646 ms |
52544 KB |
Output is correct |
52 |
Correct |
651 ms |
52856 KB |
Output is correct |
53 |
Correct |
674 ms |
52932 KB |
Output is correct |
54 |
Correct |
670 ms |
52792 KB |
Output is correct |
55 |
Correct |
661 ms |
52368 KB |
Output is correct |
56 |
Correct |
640 ms |
52028 KB |
Output is correct |
57 |
Correct |
670 ms |
53376 KB |
Output is correct |
58 |
Correct |
694 ms |
53024 KB |
Output is correct |
59 |
Correct |
653 ms |
53252 KB |
Output is correct |
60 |
Correct |
656 ms |
52480 KB |
Output is correct |
61 |
Correct |
659 ms |
53240 KB |
Output is correct |
62 |
Correct |
667 ms |
52996 KB |
Output is correct |
63 |
Correct |
648 ms |
46264 KB |
Output is correct |
64 |
Correct |
663 ms |
50584 KB |
Output is correct |
65 |
Correct |
689 ms |
51276 KB |
Output is correct |
66 |
Correct |
637 ms |
50384 KB |
Output is correct |
67 |
Correct |
649 ms |
50444 KB |
Output is correct |
68 |
Correct |
664 ms |
50584 KB |
Output is correct |
69 |
Correct |
677 ms |
51100 KB |
Output is correct |
70 |
Correct |
678 ms |
50816 KB |
Output is correct |
71 |
Correct |
667 ms |
50580 KB |
Output is correct |
72 |
Correct |
662 ms |
50788 KB |
Output is correct |
73 |
Correct |
658 ms |
50632 KB |
Output is correct |
74 |
Correct |
669 ms |
50748 KB |
Output is correct |
75 |
Correct |
670 ms |
50748 KB |
Output is correct |
76 |
Correct |
688 ms |
50716 KB |
Output is correct |
77 |
Correct |
682 ms |
53600 KB |
Output is correct |
78 |
Correct |
668 ms |
54308 KB |
Output is correct |
79 |
Correct |
729 ms |
54060 KB |
Output is correct |
80 |
Correct |
689 ms |
53636 KB |
Output is correct |
81 |
Correct |
690 ms |
53444 KB |
Output is correct |
82 |
Correct |
714 ms |
53940 KB |
Output is correct |
83 |
Correct |
740 ms |
53696 KB |
Output is correct |
84 |
Correct |
776 ms |
53468 KB |
Output is correct |
85 |
Correct |
706 ms |
54076 KB |
Output is correct |
86 |
Correct |
715 ms |
53640 KB |
Output is correct |
87 |
Correct |
712 ms |
54356 KB |
Output is correct |
88 |
Correct |
737 ms |
54256 KB |
Output is correct |
89 |
Correct |
693 ms |
53360 KB |
Output is correct |
90 |
Correct |
727 ms |
54092 KB |
Output is correct |
91 |
Correct |
709 ms |
53600 KB |
Output is correct |
92 |
Correct |
678 ms |
53340 KB |
Output is correct |
93 |
Correct |
670 ms |
53768 KB |
Output is correct |
94 |
Correct |
747 ms |
54600 KB |
Output is correct |
95 |
Correct |
727 ms |
54448 KB |
Output is correct |
96 |
Correct |
695 ms |
53792 KB |
Output is correct |
97 |
Correct |
655 ms |
53908 KB |
Output is correct |
98 |
Correct |
694 ms |
53180 KB |
Output is correct |
99 |
Correct |
665 ms |
53528 KB |
Output is correct |
100 |
Correct |
673 ms |
53708 KB |
Output is correct |
101 |
Correct |
694 ms |
53304 KB |
Output is correct |
102 |
Correct |
673 ms |
54056 KB |
Output is correct |