#include <bits/stdc++.h>
using namespace std;
int n,q,pod=1,nale[200001],napr[200001];
pair<pair<int,int>,pair<int,int>>pyta[200001];
int drzemax[1000001],pocz,kon;
int itmi,itma,gmi,gma,gze;
pair<int,int>zmmin[200001],zmmax[200001],zmzer[200001];
stack<int>stos;
long long odp[200001],drze[1000001][2],ciag[200002];
int znajdz(int ter,int odte,int dote)
{
if(ter>=pod)
return ter-pod;
if(odte>=pocz&&dote<=kon)
{
if(drzemax[((ter<<1)^1)]>=drzemax[(ter<<1)])
return znajdz(((ter<<1)^1),((odte+dote)>>1)+1,dote);
else
return znajdz((ter<<1),odte,((odte+dote)>>1));
}
int pom1=0,pom2=0;
if(((odte+dote)>>1)+1<=kon)
pom1=znajdz(((ter<<1)^1),((odte+dote)>>1)+1,dote);
if(((odte+dote)>>1)>=pocz)
pom2=znajdz((ter<<1),odte,((odte+dote)>>1));
if(ciag[pom1]>=ciag[pom2])
return pom1;
else
return pom2;
}
long long naprze(int odte,int dote,long long te)
{
long long ret=0;
odte+=pod;
dote+=pod;
while(odte<dote)
{
if(odte%2==0)
odte>>=1;
else
{
ret+=drze[odte][0]+drze[odte][1]*te;
odte>>=1;
++odte;
}
if(dote%2==1)
dote>>=1;
else
{
ret+=drze[dote][0]+drze[dote][1]*te;
dote>>=1;
--dote;
}
}
if(odte==dote)
{
ret+=drze[dote][0]+drze[dote][1]*te;
}
return ret;
}
int main()
{
scanf("%d%d",&n,&q);
while(pod<=n)
pod<<=1;
for(int i=1;i<=n;++i)
{
scanf("%lld",&ciag[i]);
drze[i+pod][0]=ciag[i];
drze[i+pod][1]=ciag[i];
drzemax[i+pod]=ciag[i];
while(!stos.empty())
{
if(ciag[stos.top()]>ciag[i])
break;
stos.pop();
}
if(!stos.empty())
nale[i]=stos.top();
stos.push(i);
}
while(!stos.empty())
stos.pop();
ciag[n+1]=1000000001;
stos.push(n+1);
for(int i=n;i>0;--i)
{
while(!stos.empty())
{
if(ciag[stos.top()]>=ciag[i])
break;
stos.pop();
}
napr[i]=stos.top()-1;
stos.push(i);
zmmin[itmi].second=i;
zmmin[itmi].first=napr[i]-i;
++itmi;
if(nale[i])
{
zmmax[itma].second=i;
zmmax[itma].first=i-nale[i];
zmzer[itma].second=i;
zmzer[itma].first=napr[i]-nale[i];
++itma;
}
}
for(int i=pod-1;i>=0;--i)
{
drzemax[i]=max(drzemax[(i<<1)],drzemax[((i<<1)^1)]);
drze[i][0]=drze[(i<<1)][0]+drze[((i<<1)^1)][0];
drze[i][1]=drze[(i<<1)][1]+drze[((i<<1)^1)][1];
}
for(int i=0;i<q;++i)
{
scanf("%d%d%d",&pyta[i].first.first,&pyta[i].second.first,&pyta[i].second.second);
pyta[i].first.second=i;
}
sort(pyta,pyta+q);
sort(zmmin,zmmin+itmi);
sort(zmmax,zmmax+itma);
sort(zmzer,zmzer+itma);
for(int i=0;i<q;++i)
{
for(int j=gmi;j<itmi;++j)
{
if(zmmin[j].first>pyta[i].first.first)
break;
++gmi;
int ter=zmmin[j].second+pod;
drze[ter][0]-=ciag[ter-pod]*(ter-pod);
drze[ter][1]-=ciag[ter-pod];
drze[ter][0]+=ciag[ter-pod]*napr[ter-pod];
ter/=2;
while(ter)
{
drze[ter][0]=drze[(ter<<1)][0]+drze[((ter<<1)^1)][0];
drze[ter][1]=drze[(ter<<1)][1]+drze[((ter<<1)^1)][1];
ter>>=1;
}
}
for(int j=gma;j<itma;++j)
{
if(zmmax[j].first>pyta[i].first.first)
break;
++gma;
int ter=zmmax[j].second+pod;
drze[ter][0]+=ciag[ter-pod]*(ter-pod-1);
drze[ter][1]-=ciag[ter-pod];
drze[ter][0]-=ciag[ter-pod]*nale[ter-pod];
ter/=2;
while(ter)
{
drze[ter][0]=drze[(ter<<1)][0]+drze[((ter<<1)^1)][0];
drze[ter][1]=drze[(ter<<1)][1]+drze[((ter<<1)^1)][1];
ter>>=1;
}
}
for(int j=gze;j<itma;++j)
{
if(zmzer[j].first>pyta[i].first.first)
break;
++gze;
int ter=zmzer[j].second+pod;
drze[ter][0]=0;
drze[ter][1]=0;
drze[ter][0]=0;
ter/=2;
while(ter)
{
drze[ter][0]=drze[(ter<<1)][0]+drze[((ter<<1)^1)][0];
drze[ter][1]=drze[(ter<<1)][1]+drze[((ter<<1)^1)][1];
ter>>=1;
}
}
pocz=max(1,pyta[i].second.first-pyta[i].first.first)+pod;
kon=pyta[i].second.first+pod;
int p1=znajdz(1,pod,pod+pod-1);
pocz=max(1,pyta[i].second.second-pyta[i].first.first)+pod;
kon=pyta[i].second.second+pod;
int p2=znajdz(1,pod,pod+pod-1);
if(p1==p2)
odp[pyta[i].first.second]=(pyta[i].second.second-pyta[i].second.first+1)*ciag[p1];
else
{
odp[pyta[i].first.second]=naprze(p1+1,p2-1,pyta[i].first.first);
odp[pyta[i].first.second]+=(min(napr[p1],p1+pyta[i].first.first)-pyta[i].second.first+1)*ciag[p1];
if(nale[p2])
odp[pyta[i].first.second]+=(pyta[i].second.second-max(p2-1,nale[p2]+pyta[i].first.first))*ciag[p2];
else
odp[pyta[i].first.second]+=(pyta[i].second.second-p2+1)*ciag[p2];
}
}
for(int i=0;i<q;++i)
printf("%lld\n",odp[i]);
return 0;
}
Compilation message
ho_t5.cpp: In function 'int main()':
ho_t5.cpp:63:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
63 | scanf("%d%d",&n,&q);
| ~~~~~^~~~~~~~~~~~~~
ho_t5.cpp:68:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
68 | scanf("%lld",&ciag[i]);
| ~~~~~^~~~~~~~~~~~~~~~~
ho_t5.cpp:116:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
116 | scanf("%d%d%d",&pyta[i].first.first,&pyta[i].second.first,&pyta[i].second.second);
| ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
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 |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
320 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 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 |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
320 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
320 KB |
Output is correct |
17 |
Correct |
1 ms |
316 KB |
Output is correct |
18 |
Correct |
1 ms |
316 KB |
Output is correct |
19 |
Correct |
1 ms |
320 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
1 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 |
340 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
1 ms |
340 KB |
Output is correct |
27 |
Correct |
1 ms |
340 KB |
Output is correct |
28 |
Correct |
1 ms |
324 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 |
340 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 |
582 ms |
26164 KB |
Output is correct |
3 |
Correct |
550 ms |
29976 KB |
Output is correct |
4 |
Correct |
613 ms |
30076 KB |
Output is correct |
5 |
Correct |
585 ms |
30240 KB |
Output is correct |
6 |
Correct |
598 ms |
29840 KB |
Output is correct |
7 |
Correct |
612 ms |
30400 KB |
Output is correct |
8 |
Correct |
614 ms |
30260 KB |
Output is correct |
9 |
Correct |
587 ms |
30288 KB |
Output is correct |
10 |
Correct |
594 ms |
29896 KB |
Output is correct |
11 |
Correct |
571 ms |
30428 KB |
Output is correct |
12 |
Correct |
554 ms |
29712 KB |
Output is correct |
13 |
Correct |
591 ms |
29892 KB |
Output is correct |
14 |
Correct |
606 ms |
29996 KB |
Output is correct |
15 |
Correct |
583 ms |
29968 KB |
Output is correct |
16 |
Correct |
617 ms |
29940 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
545 ms |
25248 KB |
Output is correct |
3 |
Correct |
541 ms |
24820 KB |
Output is correct |
4 |
Correct |
587 ms |
25564 KB |
Output is correct |
5 |
Correct |
526 ms |
24844 KB |
Output is correct |
6 |
Correct |
523 ms |
25120 KB |
Output is correct |
7 |
Correct |
533 ms |
25180 KB |
Output is correct |
8 |
Correct |
544 ms |
25388 KB |
Output is correct |
9 |
Correct |
540 ms |
25056 KB |
Output is correct |
10 |
Correct |
520 ms |
24684 KB |
Output is correct |
11 |
Correct |
543 ms |
25536 KB |
Output is correct |
12 |
Correct |
528 ms |
25148 KB |
Output is correct |
13 |
Correct |
531 ms |
25420 KB |
Output is correct |
14 |
Correct |
526 ms |
24960 KB |
Output is correct |
15 |
Correct |
550 ms |
25460 KB |
Output is correct |
16 |
Correct |
511 ms |
24992 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
478 ms |
22708 KB |
Output is correct |
2 |
Correct |
473 ms |
22800 KB |
Output is correct |
3 |
Correct |
515 ms |
23152 KB |
Output is correct |
4 |
Correct |
508 ms |
22856 KB |
Output is correct |
5 |
Correct |
481 ms |
22888 KB |
Output is correct |
6 |
Correct |
500 ms |
22836 KB |
Output is correct |
7 |
Correct |
483 ms |
23120 KB |
Output is correct |
8 |
Correct |
487 ms |
23008 KB |
Output is correct |
9 |
Correct |
489 ms |
22976 KB |
Output is correct |
10 |
Correct |
529 ms |
22932 KB |
Output is correct |
11 |
Correct |
497 ms |
22980 KB |
Output is correct |
12 |
Correct |
532 ms |
23052 KB |
Output is correct |
13 |
Correct |
492 ms |
22904 KB |
Output is correct |
14 |
Correct |
530 ms |
22924 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 |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
320 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 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 |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
320 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
320 KB |
Output is correct |
17 |
Correct |
1 ms |
316 KB |
Output is correct |
18 |
Correct |
1 ms |
316 KB |
Output is correct |
19 |
Correct |
1 ms |
320 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
1 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 |
340 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
1 ms |
340 KB |
Output is correct |
27 |
Correct |
1 ms |
340 KB |
Output is correct |
28 |
Correct |
1 ms |
324 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 |
340 KB |
Output is correct |
32 |
Correct |
1 ms |
340 KB |
Output is correct |
33 |
Correct |
582 ms |
26164 KB |
Output is correct |
34 |
Correct |
550 ms |
29976 KB |
Output is correct |
35 |
Correct |
613 ms |
30076 KB |
Output is correct |
36 |
Correct |
585 ms |
30240 KB |
Output is correct |
37 |
Correct |
598 ms |
29840 KB |
Output is correct |
38 |
Correct |
612 ms |
30400 KB |
Output is correct |
39 |
Correct |
614 ms |
30260 KB |
Output is correct |
40 |
Correct |
587 ms |
30288 KB |
Output is correct |
41 |
Correct |
594 ms |
29896 KB |
Output is correct |
42 |
Correct |
571 ms |
30428 KB |
Output is correct |
43 |
Correct |
554 ms |
29712 KB |
Output is correct |
44 |
Correct |
591 ms |
29892 KB |
Output is correct |
45 |
Correct |
606 ms |
29996 KB |
Output is correct |
46 |
Correct |
583 ms |
29968 KB |
Output is correct |
47 |
Correct |
617 ms |
29940 KB |
Output is correct |
48 |
Correct |
545 ms |
25248 KB |
Output is correct |
49 |
Correct |
541 ms |
24820 KB |
Output is correct |
50 |
Correct |
587 ms |
25564 KB |
Output is correct |
51 |
Correct |
526 ms |
24844 KB |
Output is correct |
52 |
Correct |
523 ms |
25120 KB |
Output is correct |
53 |
Correct |
533 ms |
25180 KB |
Output is correct |
54 |
Correct |
544 ms |
25388 KB |
Output is correct |
55 |
Correct |
540 ms |
25056 KB |
Output is correct |
56 |
Correct |
520 ms |
24684 KB |
Output is correct |
57 |
Correct |
543 ms |
25536 KB |
Output is correct |
58 |
Correct |
528 ms |
25148 KB |
Output is correct |
59 |
Correct |
531 ms |
25420 KB |
Output is correct |
60 |
Correct |
526 ms |
24960 KB |
Output is correct |
61 |
Correct |
550 ms |
25460 KB |
Output is correct |
62 |
Correct |
511 ms |
24992 KB |
Output is correct |
63 |
Correct |
478 ms |
22708 KB |
Output is correct |
64 |
Correct |
473 ms |
22800 KB |
Output is correct |
65 |
Correct |
515 ms |
23152 KB |
Output is correct |
66 |
Correct |
508 ms |
22856 KB |
Output is correct |
67 |
Correct |
481 ms |
22888 KB |
Output is correct |
68 |
Correct |
500 ms |
22836 KB |
Output is correct |
69 |
Correct |
483 ms |
23120 KB |
Output is correct |
70 |
Correct |
487 ms |
23008 KB |
Output is correct |
71 |
Correct |
489 ms |
22976 KB |
Output is correct |
72 |
Correct |
529 ms |
22932 KB |
Output is correct |
73 |
Correct |
497 ms |
22980 KB |
Output is correct |
74 |
Correct |
532 ms |
23052 KB |
Output is correct |
75 |
Correct |
492 ms |
22904 KB |
Output is correct |
76 |
Correct |
530 ms |
22924 KB |
Output is correct |
77 |
Correct |
616 ms |
29884 KB |
Output is correct |
78 |
Correct |
613 ms |
30312 KB |
Output is correct |
79 |
Correct |
602 ms |
30160 KB |
Output is correct |
80 |
Correct |
606 ms |
29872 KB |
Output is correct |
81 |
Correct |
599 ms |
29748 KB |
Output is correct |
82 |
Correct |
608 ms |
30108 KB |
Output is correct |
83 |
Correct |
611 ms |
29896 KB |
Output is correct |
84 |
Correct |
599 ms |
29952 KB |
Output is correct |
85 |
Correct |
614 ms |
29956 KB |
Output is correct |
86 |
Correct |
596 ms |
30008 KB |
Output is correct |
87 |
Correct |
604 ms |
30488 KB |
Output is correct |
88 |
Correct |
570 ms |
30288 KB |
Output is correct |
89 |
Correct |
547 ms |
29776 KB |
Output is correct |
90 |
Correct |
571 ms |
30232 KB |
Output is correct |
91 |
Correct |
550 ms |
29812 KB |
Output is correct |
92 |
Correct |
562 ms |
29724 KB |
Output is correct |
93 |
Correct |
572 ms |
29920 KB |
Output is correct |
94 |
Correct |
576 ms |
30692 KB |
Output is correct |
95 |
Correct |
586 ms |
30492 KB |
Output is correct |
96 |
Correct |
543 ms |
30120 KB |
Output is correct |
97 |
Correct |
589 ms |
30148 KB |
Output is correct |
98 |
Correct |
597 ms |
29652 KB |
Output is correct |
99 |
Correct |
605 ms |
29636 KB |
Output is correct |
100 |
Correct |
611 ms |
29824 KB |
Output is correct |
101 |
Correct |
629 ms |
29588 KB |
Output is correct |
102 |
Correct |
613 ms |
30220 KB |
Output is correct |