#include <bits/stdc++.h>
using namespace std;
#define endl '\n'
#define st first
#define nd second
#define pb push_back
#define sz(x) (int)(x).size()
#define all(x) (x).begin(), (x).end()
#define ll long long
ll mod=1000000007;
int inf=1000000007;
ll infl=1000000000000000007;
const int N=600007;
ll BIT[N];
void ins(int v,ll x)
{
for(;v>0;v-=(v&-v)) BIT[v]+=x;
}
ll que(int v)
{
ll res=0;
for(;v<N;v+=(v&-v)) res+=BIT[v];
return res;
}
int a[N];
int L[N],R[N];
struct ev
{
int t,b,l,r,x;
};
bool cmp(ev l,ev r)
{
if(l.t==r.t) return l.b<r.b;
return l.t<r.t;
}
ll ans[N];
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n,q;
cin>>n>>q;
for(int i=1;i<=n;i++) cin>>a[i];
vector<pair<int,int>>S;
S.pb({inf,n+1});
for(int i=n;i>0;i--)
{
while(S.back().st<=a[i]) S.pop_back();
R[i]=S.back().nd-1;
S.pb({a[i],i});
}
S.clear();
S.pb({inf,-n});
for(int i=1;i<=n;i++)
{
while(S.back().st<i) S.pop_back();
L[i]=S.back().nd;
S.pb({R[i],i});
}
vector<ev>E1,E2;
for(int i=1;i<=n;i++)
{
int t=i-L[i]-1,d=R[i]-i+1;
if(d<=t)
{
for(int j=i;j<=R[i];j++)
{
E1.pb({j,0,j-i,j-i+t,a[i]});
}
}
else
{
for(int j=0;j<=t;j++)
{
E2.pb({i-j,0,j,j+d-1,a[i]});
}
}
}
for(int i=1;i<=q;i++)
{
int t,l,r;
cin>>t>>l>>r;
E1.pb({l,-i,t,0,0});
E1.pb({r,i,t,0,0});
E2.pb({l-t,-i,t,0,0});
E2.pb({r-t,i,t,0,0});
}
sort(all(E1),cmp);
for(auto x:E1)
{
if(x.b<0) ans[-x.b]-=que(x.l);
if(x.b>0) ans[x.b]+=que(x.l);
if(x.b==0)
{
ins(x.r,x.x);
ins(x.l-1,-x.x);
}
}
memset(BIT,0,sizeof BIT);
sort(all(E2),cmp);
for(auto x:E2)
{
if(x.b<0) ans[-x.b]-=que(x.l);
if(x.b>0) ans[x.b]+=que(x.l);
if(x.b==0)
{
ins(x.r,x.x);
ins(x.l-1,-x.x);
}
}
for(int i=1;i<=q;i++) cout<<ans[i]<<endl;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4948 KB |
Output is correct |
2 |
Correct |
3 ms |
5076 KB |
Output is correct |
3 |
Correct |
2 ms |
5076 KB |
Output is correct |
4 |
Correct |
3 ms |
5076 KB |
Output is correct |
5 |
Correct |
2 ms |
5076 KB |
Output is correct |
6 |
Correct |
3 ms |
5076 KB |
Output is correct |
7 |
Correct |
3 ms |
5076 KB |
Output is correct |
8 |
Correct |
2 ms |
5076 KB |
Output is correct |
9 |
Correct |
3 ms |
5076 KB |
Output is correct |
10 |
Correct |
2 ms |
5076 KB |
Output is correct |
11 |
Correct |
3 ms |
5076 KB |
Output is correct |
12 |
Correct |
3 ms |
5076 KB |
Output is correct |
13 |
Correct |
3 ms |
5076 KB |
Output is correct |
14 |
Correct |
3 ms |
5076 KB |
Output is correct |
15 |
Correct |
3 ms |
5076 KB |
Output is correct |
16 |
Correct |
4 ms |
5076 KB |
Output is correct |
17 |
Correct |
3 ms |
5076 KB |
Output is correct |
18 |
Correct |
2 ms |
5076 KB |
Output is correct |
19 |
Correct |
3 ms |
5076 KB |
Output is correct |
20 |
Correct |
2 ms |
5076 KB |
Output is correct |
21 |
Correct |
3 ms |
5076 KB |
Output is correct |
22 |
Correct |
2 ms |
5076 KB |
Output is correct |
23 |
Correct |
3 ms |
5076 KB |
Output is correct |
24 |
Correct |
2 ms |
5076 KB |
Output is correct |
25 |
Correct |
3 ms |
5076 KB |
Output is correct |
26 |
Correct |
2 ms |
5076 KB |
Output is correct |
27 |
Correct |
2 ms |
5076 KB |
Output is correct |
28 |
Correct |
2 ms |
5076 KB |
Output is correct |
29 |
Correct |
3 ms |
5076 KB |
Output is correct |
30 |
Correct |
3 ms |
5076 KB |
Output is correct |
31 |
Correct |
3 ms |
5076 KB |
Output is correct |
32 |
Correct |
3 ms |
5076 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4948 KB |
Output is correct |
2 |
Correct |
529 ms |
51932 KB |
Output is correct |
3 |
Correct |
561 ms |
67232 KB |
Output is correct |
4 |
Correct |
560 ms |
63416 KB |
Output is correct |
5 |
Correct |
556 ms |
63384 KB |
Output is correct |
6 |
Correct |
571 ms |
66404 KB |
Output is correct |
7 |
Correct |
574 ms |
61764 KB |
Output is correct |
8 |
Correct |
549 ms |
63488 KB |
Output is correct |
9 |
Correct |
545 ms |
65164 KB |
Output is correct |
10 |
Correct |
527 ms |
61956 KB |
Output is correct |
11 |
Correct |
535 ms |
64100 KB |
Output is correct |
12 |
Correct |
519 ms |
52744 KB |
Output is correct |
13 |
Correct |
572 ms |
62820 KB |
Output is correct |
14 |
Correct |
544 ms |
52568 KB |
Output is correct |
15 |
Correct |
563 ms |
65740 KB |
Output is correct |
16 |
Correct |
593 ms |
69096 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4948 KB |
Output is correct |
2 |
Correct |
556 ms |
72028 KB |
Output is correct |
3 |
Correct |
521 ms |
68652 KB |
Output is correct |
4 |
Correct |
562 ms |
71524 KB |
Output is correct |
5 |
Correct |
533 ms |
62424 KB |
Output is correct |
6 |
Correct |
538 ms |
65068 KB |
Output is correct |
7 |
Correct |
538 ms |
56036 KB |
Output is correct |
8 |
Correct |
550 ms |
62472 KB |
Output is correct |
9 |
Correct |
509 ms |
65156 KB |
Output is correct |
10 |
Correct |
546 ms |
65344 KB |
Output is correct |
11 |
Correct |
566 ms |
66712 KB |
Output is correct |
12 |
Correct |
549 ms |
65364 KB |
Output is correct |
13 |
Correct |
569 ms |
66552 KB |
Output is correct |
14 |
Correct |
530 ms |
51784 KB |
Output is correct |
15 |
Correct |
545 ms |
61144 KB |
Output is correct |
16 |
Correct |
545 ms |
68824 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
393 ms |
36112 KB |
Output is correct |
2 |
Correct |
388 ms |
39944 KB |
Output is correct |
3 |
Correct |
383 ms |
40292 KB |
Output is correct |
4 |
Correct |
390 ms |
40140 KB |
Output is correct |
5 |
Correct |
376 ms |
39904 KB |
Output is correct |
6 |
Correct |
357 ms |
39912 KB |
Output is correct |
7 |
Correct |
394 ms |
39976 KB |
Output is correct |
8 |
Correct |
383 ms |
39936 KB |
Output is correct |
9 |
Correct |
368 ms |
40032 KB |
Output is correct |
10 |
Correct |
390 ms |
39912 KB |
Output is correct |
11 |
Correct |
379 ms |
40208 KB |
Output is correct |
12 |
Correct |
376 ms |
40092 KB |
Output is correct |
13 |
Correct |
395 ms |
39964 KB |
Output is correct |
14 |
Correct |
362 ms |
40036 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4948 KB |
Output is correct |
2 |
Correct |
3 ms |
5076 KB |
Output is correct |
3 |
Correct |
2 ms |
5076 KB |
Output is correct |
4 |
Correct |
3 ms |
5076 KB |
Output is correct |
5 |
Correct |
2 ms |
5076 KB |
Output is correct |
6 |
Correct |
3 ms |
5076 KB |
Output is correct |
7 |
Correct |
3 ms |
5076 KB |
Output is correct |
8 |
Correct |
2 ms |
5076 KB |
Output is correct |
9 |
Correct |
3 ms |
5076 KB |
Output is correct |
10 |
Correct |
2 ms |
5076 KB |
Output is correct |
11 |
Correct |
3 ms |
5076 KB |
Output is correct |
12 |
Correct |
3 ms |
5076 KB |
Output is correct |
13 |
Correct |
3 ms |
5076 KB |
Output is correct |
14 |
Correct |
3 ms |
5076 KB |
Output is correct |
15 |
Correct |
3 ms |
5076 KB |
Output is correct |
16 |
Correct |
4 ms |
5076 KB |
Output is correct |
17 |
Correct |
3 ms |
5076 KB |
Output is correct |
18 |
Correct |
2 ms |
5076 KB |
Output is correct |
19 |
Correct |
3 ms |
5076 KB |
Output is correct |
20 |
Correct |
2 ms |
5076 KB |
Output is correct |
21 |
Correct |
3 ms |
5076 KB |
Output is correct |
22 |
Correct |
2 ms |
5076 KB |
Output is correct |
23 |
Correct |
3 ms |
5076 KB |
Output is correct |
24 |
Correct |
2 ms |
5076 KB |
Output is correct |
25 |
Correct |
3 ms |
5076 KB |
Output is correct |
26 |
Correct |
2 ms |
5076 KB |
Output is correct |
27 |
Correct |
2 ms |
5076 KB |
Output is correct |
28 |
Correct |
2 ms |
5076 KB |
Output is correct |
29 |
Correct |
3 ms |
5076 KB |
Output is correct |
30 |
Correct |
3 ms |
5076 KB |
Output is correct |
31 |
Correct |
3 ms |
5076 KB |
Output is correct |
32 |
Correct |
3 ms |
5076 KB |
Output is correct |
33 |
Correct |
529 ms |
51932 KB |
Output is correct |
34 |
Correct |
561 ms |
67232 KB |
Output is correct |
35 |
Correct |
560 ms |
63416 KB |
Output is correct |
36 |
Correct |
556 ms |
63384 KB |
Output is correct |
37 |
Correct |
571 ms |
66404 KB |
Output is correct |
38 |
Correct |
574 ms |
61764 KB |
Output is correct |
39 |
Correct |
549 ms |
63488 KB |
Output is correct |
40 |
Correct |
545 ms |
65164 KB |
Output is correct |
41 |
Correct |
527 ms |
61956 KB |
Output is correct |
42 |
Correct |
535 ms |
64100 KB |
Output is correct |
43 |
Correct |
519 ms |
52744 KB |
Output is correct |
44 |
Correct |
572 ms |
62820 KB |
Output is correct |
45 |
Correct |
544 ms |
52568 KB |
Output is correct |
46 |
Correct |
563 ms |
65740 KB |
Output is correct |
47 |
Correct |
593 ms |
69096 KB |
Output is correct |
48 |
Correct |
556 ms |
72028 KB |
Output is correct |
49 |
Correct |
521 ms |
68652 KB |
Output is correct |
50 |
Correct |
562 ms |
71524 KB |
Output is correct |
51 |
Correct |
533 ms |
62424 KB |
Output is correct |
52 |
Correct |
538 ms |
65068 KB |
Output is correct |
53 |
Correct |
538 ms |
56036 KB |
Output is correct |
54 |
Correct |
550 ms |
62472 KB |
Output is correct |
55 |
Correct |
509 ms |
65156 KB |
Output is correct |
56 |
Correct |
546 ms |
65344 KB |
Output is correct |
57 |
Correct |
566 ms |
66712 KB |
Output is correct |
58 |
Correct |
549 ms |
65364 KB |
Output is correct |
59 |
Correct |
569 ms |
66552 KB |
Output is correct |
60 |
Correct |
530 ms |
51784 KB |
Output is correct |
61 |
Correct |
545 ms |
61144 KB |
Output is correct |
62 |
Correct |
545 ms |
68824 KB |
Output is correct |
63 |
Correct |
393 ms |
36112 KB |
Output is correct |
64 |
Correct |
388 ms |
39944 KB |
Output is correct |
65 |
Correct |
383 ms |
40292 KB |
Output is correct |
66 |
Correct |
390 ms |
40140 KB |
Output is correct |
67 |
Correct |
376 ms |
39904 KB |
Output is correct |
68 |
Correct |
357 ms |
39912 KB |
Output is correct |
69 |
Correct |
394 ms |
39976 KB |
Output is correct |
70 |
Correct |
383 ms |
39936 KB |
Output is correct |
71 |
Correct |
368 ms |
40032 KB |
Output is correct |
72 |
Correct |
390 ms |
39912 KB |
Output is correct |
73 |
Correct |
379 ms |
40208 KB |
Output is correct |
74 |
Correct |
376 ms |
40092 KB |
Output is correct |
75 |
Correct |
395 ms |
39964 KB |
Output is correct |
76 |
Correct |
362 ms |
40036 KB |
Output is correct |
77 |
Correct |
598 ms |
70584 KB |
Output is correct |
78 |
Correct |
590 ms |
72952 KB |
Output is correct |
79 |
Correct |
562 ms |
69324 KB |
Output is correct |
80 |
Correct |
549 ms |
71276 KB |
Output is correct |
81 |
Correct |
557 ms |
66404 KB |
Output is correct |
82 |
Correct |
589 ms |
73632 KB |
Output is correct |
83 |
Correct |
561 ms |
69432 KB |
Output is correct |
84 |
Correct |
559 ms |
57276 KB |
Output is correct |
85 |
Correct |
598 ms |
67016 KB |
Output is correct |
86 |
Correct |
588 ms |
77644 KB |
Output is correct |
87 |
Correct |
569 ms |
67472 KB |
Output is correct |
88 |
Correct |
499 ms |
57416 KB |
Output is correct |
89 |
Correct |
507 ms |
71172 KB |
Output is correct |
90 |
Correct |
562 ms |
70408 KB |
Output is correct |
91 |
Correct |
512 ms |
69228 KB |
Output is correct |
92 |
Correct |
529 ms |
73528 KB |
Output is correct |
93 |
Correct |
529 ms |
63236 KB |
Output is correct |
94 |
Correct |
520 ms |
66732 KB |
Output is correct |
95 |
Correct |
534 ms |
68964 KB |
Output is correct |
96 |
Correct |
568 ms |
75204 KB |
Output is correct |
97 |
Correct |
545 ms |
67460 KB |
Output is correct |
98 |
Correct |
580 ms |
68704 KB |
Output is correct |
99 |
Correct |
591 ms |
68288 KB |
Output is correct |
100 |
Correct |
574 ms |
82552 KB |
Output is correct |
101 |
Correct |
556 ms |
70960 KB |
Output is correct |
102 |
Correct |
563 ms |
68940 KB |
Output is correct |