#include<iostream>
#include<vector>
#include<algorithm>
#define ep emplace
#define eb emplace_back
#define fi first
#define se second
#define all(x) (x).begin(),(x).end()
#define semicolon ;
#define ryan bear
using namespace std;
typedef long long ll;
typedef pair<int,int>pi;
typedef pair<ll,ll>pl;
const int inf=1e9+7;
const ll INF=1e18;
struct fen
{
ll a[400010],b[400010];
fen(){}
inline void init()
{
fill(a,a+400010,0);
fill(b,b+400010,0);
return;
}
inline void fi(int x,ll p)
{
for(int i=x+1;i<400010;i+=i&-i)
a[i]+=p;
p*=1-x;
for(int i=x+1;i<400010;i+=i&-i)
b[i]+=p;
return;
}
inline ll fq(int x)
{
ll as=0,bs=0;
for(int i=x+1;i>0;i=i&i-1)
as+=a[i],bs+=b[i];
return as*x+bs;
}
inline ll fq(int l,int r)
{
return fq(r)-fq(l-1);
}
}ft1,ft2;
struct seg
{
ll t[800010];
seg(){}
inline void init()
{
fill(t,t+800010,-INF);
return;
}
void si(int n,int s,int e,int x,ll p)
{
if(s==e)
{
t[n]=p;
return;
}
int m=(s+e)/2;
if(x>m)
si(n*2+1,m+1,e,x,p);
else
si(n*2,s,m,x,p);
t[n]=max(t[n*2],t[n*2+1]);
return;
}
int sq(int n,int s,int e,ll p)
{
if(s==e)
return s;
int m=(s+e)/2;
if(t[n*2]>=p)
return sq(n*2,s,m,p);
return sq(n*2+1,m+1,e,p);
}
int sq2(int n,int s,int e,ll p)
{
if(s==e)
return s;
int m=(s+e)/2;
if(t[n*2+1]>p)
return sq2(n*2+1,m+1,e,p);
return sq2(n*2,s,m,p);
}
}st;
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
ft1.init();
ft2.init();
int n,q;
cin>>n>>q;
vector<ll>v(n);
for(ll&t:v)
cin>>t;
vector<int>lv(n),rv(n);
st.init();
for(int i=0;i<n;i++)
{
lv[i]=st.sq2(1,0,n-1,v[i]);
if(v[lv[i]]<=v[i]||i==0)
lv[i]=i-n-1;
st.si(1,0,n-1,i,v[i]);
}
st.init();
for(int i=n;i-->0;)
{
rv[i]=st.sq(1,0,n-1,v[i]);
if(v[rv[i]]<v[i]||i==n-1)
rv[i]=n;
st.si(1,0,n-1,i,v[i]);
}
for(int i=0;i<n;i++)
st.si(1,0,n-1,i,v[i]);
vector<vector<pl> >q1(n*2+2),q2(n*2+2);
for(int i=0;i<n;i++)
{
int l=lv[i];
int r=rv[i];
ll t=v[i];
int j=i+n+1;
l+=n+1;
r+=n+1;
q1[0].eb(j,t);
q2[0].eb(j+1,-t);
q1[r-j].eb(r,-t);
q2[r-j].eb(j+1,t);
q1[j-l].eb(j,-t);
q2[j-l].eb(l+1,t);
q1[r-l].eb(r,t);
q2[r-l].eb(l+1,-t);
}
vector<vector<pair<pi,int> > >qv(n+1);
vector<ll>ans(q,0);
for(int i=0;i<q;i++)
{
int t,l,r;
cin>>t>>l>>r;
qv[t].eb(pi(l+n,r+n),i);
}
for(int i=0;i<=n;i++)
{
for(pl&t:q1[i])
ft1.fi(t.fi,t.se);
for(pl&t:q2[i])
ft2.fi(t.fi,t.se);
for(auto&t:qv[i])
ans[t.se]=ft1.fq(t.fi.fi,t.fi.se)+ft2.fq(t.fi.fi-i,t.fi.se-i);
}
for(ll&t:ans)
cout<<t<<'\n';
cout.flush();
return 0;
}
Compilation message
ho_t5.cpp: In member function 'll fen::fq(int)':
ho_t5.cpp:39:32: warning: suggest parentheses around '-' in operand of '&' [-Wparentheses]
for(int i=x+1;i>0;i=i&i-1)
~^~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
17 ms |
19064 KB |
Output is correct |
2 |
Correct |
16 ms |
19192 KB |
Output is correct |
3 |
Correct |
18 ms |
19192 KB |
Output is correct |
4 |
Correct |
16 ms |
19192 KB |
Output is correct |
5 |
Correct |
17 ms |
19192 KB |
Output is correct |
6 |
Correct |
16 ms |
19192 KB |
Output is correct |
7 |
Correct |
17 ms |
19192 KB |
Output is correct |
8 |
Correct |
16 ms |
19192 KB |
Output is correct |
9 |
Correct |
17 ms |
19192 KB |
Output is correct |
10 |
Correct |
17 ms |
19192 KB |
Output is correct |
11 |
Correct |
16 ms |
19192 KB |
Output is correct |
12 |
Correct |
17 ms |
19192 KB |
Output is correct |
13 |
Correct |
17 ms |
19192 KB |
Output is correct |
14 |
Correct |
16 ms |
19192 KB |
Output is correct |
15 |
Correct |
16 ms |
19192 KB |
Output is correct |
16 |
Correct |
17 ms |
19192 KB |
Output is correct |
17 |
Correct |
17 ms |
19192 KB |
Output is correct |
18 |
Correct |
16 ms |
19192 KB |
Output is correct |
19 |
Correct |
17 ms |
19192 KB |
Output is correct |
20 |
Correct |
16 ms |
19192 KB |
Output is correct |
21 |
Correct |
16 ms |
19192 KB |
Output is correct |
22 |
Correct |
16 ms |
19196 KB |
Output is correct |
23 |
Correct |
16 ms |
19192 KB |
Output is correct |
24 |
Correct |
16 ms |
19192 KB |
Output is correct |
25 |
Correct |
16 ms |
19192 KB |
Output is correct |
26 |
Correct |
16 ms |
19196 KB |
Output is correct |
27 |
Correct |
16 ms |
19192 KB |
Output is correct |
28 |
Correct |
16 ms |
19192 KB |
Output is correct |
29 |
Correct |
16 ms |
19192 KB |
Output is correct |
30 |
Correct |
16 ms |
19192 KB |
Output is correct |
31 |
Correct |
16 ms |
19192 KB |
Output is correct |
32 |
Correct |
16 ms |
19192 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
17 ms |
19064 KB |
Output is correct |
2 |
Correct |
365 ms |
81284 KB |
Output is correct |
3 |
Correct |
364 ms |
80640 KB |
Output is correct |
4 |
Correct |
371 ms |
81156 KB |
Output is correct |
5 |
Correct |
372 ms |
82140 KB |
Output is correct |
6 |
Correct |
363 ms |
81284 KB |
Output is correct |
7 |
Correct |
382 ms |
81804 KB |
Output is correct |
8 |
Correct |
376 ms |
82804 KB |
Output is correct |
9 |
Correct |
363 ms |
81664 KB |
Output is correct |
10 |
Correct |
356 ms |
80492 KB |
Output is correct |
11 |
Correct |
372 ms |
81888 KB |
Output is correct |
12 |
Correct |
365 ms |
80444 KB |
Output is correct |
13 |
Correct |
374 ms |
81864 KB |
Output is correct |
14 |
Correct |
373 ms |
81988 KB |
Output is correct |
15 |
Correct |
375 ms |
81868 KB |
Output is correct |
16 |
Correct |
355 ms |
81412 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
17 ms |
19064 KB |
Output is correct |
2 |
Correct |
409 ms |
78980 KB |
Output is correct |
3 |
Correct |
405 ms |
78204 KB |
Output is correct |
4 |
Correct |
425 ms |
80464 KB |
Output is correct |
5 |
Correct |
404 ms |
78596 KB |
Output is correct |
6 |
Correct |
419 ms |
79364 KB |
Output is correct |
7 |
Correct |
419 ms |
79476 KB |
Output is correct |
8 |
Correct |
420 ms |
78840 KB |
Output is correct |
9 |
Correct |
419 ms |
78340 KB |
Output is correct |
10 |
Correct |
429 ms |
77696 KB |
Output is correct |
11 |
Correct |
434 ms |
80244 KB |
Output is correct |
12 |
Correct |
413 ms |
79728 KB |
Output is correct |
13 |
Correct |
426 ms |
80104 KB |
Output is correct |
14 |
Correct |
427 ms |
78724 KB |
Output is correct |
15 |
Correct |
436 ms |
80256 KB |
Output is correct |
16 |
Correct |
443 ms |
79868 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
389 ms |
77168 KB |
Output is correct |
2 |
Correct |
379 ms |
77116 KB |
Output is correct |
3 |
Correct |
397 ms |
79096 KB |
Output is correct |
4 |
Correct |
388 ms |
77312 KB |
Output is correct |
5 |
Correct |
388 ms |
77600 KB |
Output is correct |
6 |
Correct |
390 ms |
78068 KB |
Output is correct |
7 |
Correct |
405 ms |
79016 KB |
Output is correct |
8 |
Correct |
387 ms |
78348 KB |
Output is correct |
9 |
Correct |
388 ms |
77280 KB |
Output is correct |
10 |
Correct |
381 ms |
78460 KB |
Output is correct |
11 |
Correct |
393 ms |
77684 KB |
Output is correct |
12 |
Correct |
406 ms |
77836 KB |
Output is correct |
13 |
Correct |
394 ms |
77840 KB |
Output is correct |
14 |
Correct |
404 ms |
77544 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
17 ms |
19064 KB |
Output is correct |
2 |
Correct |
16 ms |
19192 KB |
Output is correct |
3 |
Correct |
18 ms |
19192 KB |
Output is correct |
4 |
Correct |
16 ms |
19192 KB |
Output is correct |
5 |
Correct |
17 ms |
19192 KB |
Output is correct |
6 |
Correct |
16 ms |
19192 KB |
Output is correct |
7 |
Correct |
17 ms |
19192 KB |
Output is correct |
8 |
Correct |
16 ms |
19192 KB |
Output is correct |
9 |
Correct |
17 ms |
19192 KB |
Output is correct |
10 |
Correct |
17 ms |
19192 KB |
Output is correct |
11 |
Correct |
16 ms |
19192 KB |
Output is correct |
12 |
Correct |
17 ms |
19192 KB |
Output is correct |
13 |
Correct |
17 ms |
19192 KB |
Output is correct |
14 |
Correct |
16 ms |
19192 KB |
Output is correct |
15 |
Correct |
16 ms |
19192 KB |
Output is correct |
16 |
Correct |
17 ms |
19192 KB |
Output is correct |
17 |
Correct |
17 ms |
19192 KB |
Output is correct |
18 |
Correct |
16 ms |
19192 KB |
Output is correct |
19 |
Correct |
17 ms |
19192 KB |
Output is correct |
20 |
Correct |
16 ms |
19192 KB |
Output is correct |
21 |
Correct |
16 ms |
19192 KB |
Output is correct |
22 |
Correct |
16 ms |
19196 KB |
Output is correct |
23 |
Correct |
16 ms |
19192 KB |
Output is correct |
24 |
Correct |
16 ms |
19192 KB |
Output is correct |
25 |
Correct |
16 ms |
19192 KB |
Output is correct |
26 |
Correct |
16 ms |
19196 KB |
Output is correct |
27 |
Correct |
16 ms |
19192 KB |
Output is correct |
28 |
Correct |
16 ms |
19192 KB |
Output is correct |
29 |
Correct |
16 ms |
19192 KB |
Output is correct |
30 |
Correct |
16 ms |
19192 KB |
Output is correct |
31 |
Correct |
16 ms |
19192 KB |
Output is correct |
32 |
Correct |
16 ms |
19192 KB |
Output is correct |
33 |
Correct |
469 ms |
79716 KB |
Output is correct |
34 |
Correct |
435 ms |
80632 KB |
Output is correct |
35 |
Correct |
431 ms |
80956 KB |
Output is correct |
36 |
Correct |
436 ms |
79620 KB |
Output is correct |
37 |
Correct |
421 ms |
79596 KB |
Output is correct |
38 |
Correct |
422 ms |
80560 KB |
Output is correct |
39 |
Correct |
427 ms |
80080 KB |
Output is correct |
40 |
Correct |
423 ms |
79108 KB |
Output is correct |
41 |
Correct |
444 ms |
81092 KB |
Output is correct |
42 |
Correct |
420 ms |
79608 KB |
Output is correct |
43 |
Correct |
400 ms |
80444 KB |
Output is correct |
44 |
Correct |
398 ms |
80748 KB |
Output is correct |
45 |
Correct |
370 ms |
78716 KB |
Output is correct |
46 |
Correct |
409 ms |
80112 KB |
Output is correct |
47 |
Correct |
404 ms |
79360 KB |
Output is correct |
48 |
Correct |
375 ms |
78540 KB |
Output is correct |
49 |
Correct |
388 ms |
79688 KB |
Output is correct |
50 |
Correct |
394 ms |
80884 KB |
Output is correct |
51 |
Correct |
396 ms |
80684 KB |
Output is correct |
52 |
Correct |
392 ms |
79484 KB |
Output is correct |
53 |
Correct |
389 ms |
79528 KB |
Output is correct |
54 |
Correct |
386 ms |
78988 KB |
Output is correct |
55 |
Correct |
375 ms |
79744 KB |
Output is correct |
56 |
Correct |
393 ms |
79976 KB |
Output is correct |
57 |
Correct |
376 ms |
79480 KB |
Output is correct |
58 |
Correct |
386 ms |
80376 KB |
Output is correct |
59 |
Correct |
365 ms |
81284 KB |
Output is correct |
60 |
Correct |
364 ms |
80640 KB |
Output is correct |
61 |
Correct |
371 ms |
81156 KB |
Output is correct |
62 |
Correct |
372 ms |
82140 KB |
Output is correct |
63 |
Correct |
363 ms |
81284 KB |
Output is correct |
64 |
Correct |
382 ms |
81804 KB |
Output is correct |
65 |
Correct |
376 ms |
82804 KB |
Output is correct |
66 |
Correct |
363 ms |
81664 KB |
Output is correct |
67 |
Correct |
356 ms |
80492 KB |
Output is correct |
68 |
Correct |
372 ms |
81888 KB |
Output is correct |
69 |
Correct |
365 ms |
80444 KB |
Output is correct |
70 |
Correct |
374 ms |
81864 KB |
Output is correct |
71 |
Correct |
373 ms |
81988 KB |
Output is correct |
72 |
Correct |
375 ms |
81868 KB |
Output is correct |
73 |
Correct |
355 ms |
81412 KB |
Output is correct |
74 |
Correct |
409 ms |
78980 KB |
Output is correct |
75 |
Correct |
405 ms |
78204 KB |
Output is correct |
76 |
Correct |
425 ms |
80464 KB |
Output is correct |
77 |
Correct |
404 ms |
78596 KB |
Output is correct |
78 |
Correct |
419 ms |
79364 KB |
Output is correct |
79 |
Correct |
419 ms |
79476 KB |
Output is correct |
80 |
Correct |
420 ms |
78840 KB |
Output is correct |
81 |
Correct |
419 ms |
78340 KB |
Output is correct |
82 |
Correct |
429 ms |
77696 KB |
Output is correct |
83 |
Correct |
434 ms |
80244 KB |
Output is correct |
84 |
Correct |
413 ms |
79728 KB |
Output is correct |
85 |
Correct |
426 ms |
80104 KB |
Output is correct |
86 |
Correct |
427 ms |
78724 KB |
Output is correct |
87 |
Correct |
436 ms |
80256 KB |
Output is correct |
88 |
Correct |
443 ms |
79868 KB |
Output is correct |
89 |
Correct |
389 ms |
77168 KB |
Output is correct |
90 |
Correct |
379 ms |
77116 KB |
Output is correct |
91 |
Correct |
397 ms |
79096 KB |
Output is correct |
92 |
Correct |
388 ms |
77312 KB |
Output is correct |
93 |
Correct |
388 ms |
77600 KB |
Output is correct |
94 |
Correct |
390 ms |
78068 KB |
Output is correct |
95 |
Correct |
405 ms |
79016 KB |
Output is correct |
96 |
Correct |
387 ms |
78348 KB |
Output is correct |
97 |
Correct |
388 ms |
77280 KB |
Output is correct |
98 |
Correct |
381 ms |
78460 KB |
Output is correct |
99 |
Correct |
393 ms |
77684 KB |
Output is correct |
100 |
Correct |
406 ms |
77836 KB |
Output is correct |
101 |
Correct |
394 ms |
77840 KB |
Output is correct |
102 |
Correct |
404 ms |
77544 KB |
Output is correct |