#include <bits/stdc++.h>
using namespace std;
//ifstream fin("a.in");
//ofstream fout("a.out");
typedef long long ll;
ll v[200000],l[200000],r[200000],stv[200000],ans[200000],n;
///i misspled square
ll cnt2 = 0,disp = 300000;
class superfenwick{
ll fen[550001][2];
private:
ll query2(ll pos,ll t){
pos+=disp;
ll r = 0;
for(ll i = pos;i > 0;i-=(i&-i))r+=fen[i][t];
return r;
}
void add2(ll pos,ll nr,ll t){
pos+=disp;
for(ll i = pos;i <= 550000;i+=(i&-i))fen[i][t]+=nr;
}
public:
ll query(ll l,ll r){
return (query2(r,0)*r - query2(l - 1,0)*(l - 1)) - query2(r,1) + query2(l - 1,1);
}
void add(ll l,ll r,ll nr){
add2(l,nr,0);
add2(r + 1,-nr,0);
add2(l,nr*(l - 1),1);
add2(r + 1,-nr*r,1);
}
};
superfenwick triangles,squares;
struct events{
ll tip,t,l,r,nr;
}v2[4000000];
bool cmp(events a,events b){
if(a.t == b.t)return a.tip < b.tip;
return a.t < b.t;
}
void addsqaure(ll l,ll r,ll h,ll nr){
//cout<<"squaredata:"<<l<<','<<r<<','<<h<<','<<nr<<'\n';
v2[cnt2++] = {0,0,l,r,nr};
v2[cnt2++] = {0,h,l,r,-nr};
}
void addtriangle(ll l,ll r,ll nr,ll ydisp = 0){
if(r == n - 1){
//cout<<"triangledata:"<<l<<','<<r<<','<<nr<<','<<ydisp<<'\n';
v2[cnt2++] = {1,ydisp,l - ydisp,r - ydisp,nr};
v2[cnt2++] = {1,r - l + 1 + ydisp,l - ydisp,r - ydisp,-nr};
}else{
addtriangle(l,n - 1,nr);
addtriangle(r + 1,n - 1,-nr,r - l + 1);
addsqaure(r + 1,n - 1,r - l + 1,-nr);
}
}
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
ll q,i,cnt = 0,j,a,b,c;
cin>>n>>q;
for(i = 0;i < n;i++){
cin>>v[i];
//v[i] = rand()%1000000000;
}
cnt = -1;
for(i = 0;i < n;i++){
while(cnt >= 0 && v[i] > v[stv[cnt]]){
r[stv[cnt--]] = i;
}
stv[++cnt] = i;
}
while(cnt-- >= 0){
r[stv[cnt + 1]] = n;
}
cnt = -1;
for(i = n - 1;i >= 0;i--){
while(cnt >= 0 && v[i] >= v[stv[cnt]]){
l[stv[cnt--]] = i;
}
stv[++cnt] = i;
}
while(cnt-- >= 0){
l[stv[cnt + 1]] = -200005;
}
for(i = 0;i < n;i++){
l[i]++;r[i]--;
//fout<<"For i = "<<i<<'\n';
//fout<<l[i]<<' '<<r[i]<<'\n';
addtriangle(l[i],r[i],v[i]);
addtriangle(l[i],i - 1,-v[i]);
addtriangle(i + 1,r[i],-v[i]);
//cout<<'\n';
}
for(i = 0;i < q;i++){
cin>>a>>b>>c;
//a = 0;
//b = 1;
//c = n;
v2[cnt2++] = {2,a,b - 1,c - 1,i};
}
sort(v2,v2 + cnt2,cmp);
for(i = 0;i < cnt2;i++){
if(v2[i].tip == 0){
squares.add(v2[i].l,v2[i].r,v2[i].nr);
}else if(v2[i].tip == 1){
triangles.add(v2[i].l,v2[i].r,v2[i].nr);
}else{
ans[v2[i].nr] = squares.query(v2[i].l,v2[i].r) + triangles.query(v2[i].l - v2[i].t,v2[i].r - v2[i].t);
}
}
for(i = 0;i < q;i++)cout<<ans[i]<<'\n';
return 0;
}
Compilation message
ho_t5.cpp: In function 'int main()':
ho_t5.cpp:60:20: warning: unused variable 'j' [-Wunused-variable]
60 | ll q,i,cnt = 0,j,a,b,c;
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
468 KB |
Output is correct |
2 |
Correct |
1 ms |
596 KB |
Output is correct |
3 |
Correct |
1 ms |
596 KB |
Output is correct |
4 |
Correct |
1 ms |
596 KB |
Output is correct |
5 |
Correct |
1 ms |
596 KB |
Output is correct |
6 |
Correct |
2 ms |
596 KB |
Output is correct |
7 |
Correct |
1 ms |
596 KB |
Output is correct |
8 |
Correct |
1 ms |
596 KB |
Output is correct |
9 |
Correct |
1 ms |
596 KB |
Output is correct |
10 |
Correct |
1 ms |
596 KB |
Output is correct |
11 |
Correct |
1 ms |
596 KB |
Output is correct |
12 |
Correct |
3 ms |
596 KB |
Output is correct |
13 |
Correct |
1 ms |
596 KB |
Output is correct |
14 |
Correct |
1 ms |
596 KB |
Output is correct |
15 |
Correct |
2 ms |
596 KB |
Output is correct |
16 |
Correct |
2 ms |
596 KB |
Output is correct |
17 |
Correct |
1 ms |
596 KB |
Output is correct |
18 |
Correct |
1 ms |
596 KB |
Output is correct |
19 |
Correct |
1 ms |
596 KB |
Output is correct |
20 |
Correct |
1 ms |
596 KB |
Output is correct |
21 |
Correct |
1 ms |
596 KB |
Output is correct |
22 |
Correct |
1 ms |
596 KB |
Output is correct |
23 |
Correct |
1 ms |
596 KB |
Output is correct |
24 |
Correct |
1 ms |
596 KB |
Output is correct |
25 |
Correct |
1 ms |
596 KB |
Output is correct |
26 |
Correct |
1 ms |
596 KB |
Output is correct |
27 |
Correct |
2 ms |
596 KB |
Output is correct |
28 |
Correct |
1 ms |
596 KB |
Output is correct |
29 |
Correct |
1 ms |
596 KB |
Output is correct |
30 |
Correct |
1 ms |
596 KB |
Output is correct |
31 |
Correct |
1 ms |
596 KB |
Output is correct |
32 |
Correct |
1 ms |
596 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
468 KB |
Output is correct |
2 |
Execution timed out |
1090 ms |
160556 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
468 KB |
Output is correct |
2 |
Execution timed out |
1057 ms |
159252 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
865 ms |
127276 KB |
Output is correct |
2 |
Correct |
837 ms |
132428 KB |
Output is correct |
3 |
Correct |
857 ms |
135588 KB |
Output is correct |
4 |
Correct |
720 ms |
130808 KB |
Output is correct |
5 |
Correct |
903 ms |
131684 KB |
Output is correct |
6 |
Correct |
807 ms |
132756 KB |
Output is correct |
7 |
Correct |
830 ms |
135244 KB |
Output is correct |
8 |
Correct |
927 ms |
133824 KB |
Output is correct |
9 |
Correct |
921 ms |
131656 KB |
Output is correct |
10 |
Correct |
866 ms |
133936 KB |
Output is correct |
11 |
Correct |
894 ms |
132356 KB |
Output is correct |
12 |
Correct |
882 ms |
132796 KB |
Output is correct |
13 |
Correct |
845 ms |
132224 KB |
Output is correct |
14 |
Correct |
899 ms |
132660 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
468 KB |
Output is correct |
2 |
Correct |
1 ms |
596 KB |
Output is correct |
3 |
Correct |
1 ms |
596 KB |
Output is correct |
4 |
Correct |
1 ms |
596 KB |
Output is correct |
5 |
Correct |
1 ms |
596 KB |
Output is correct |
6 |
Correct |
2 ms |
596 KB |
Output is correct |
7 |
Correct |
1 ms |
596 KB |
Output is correct |
8 |
Correct |
1 ms |
596 KB |
Output is correct |
9 |
Correct |
1 ms |
596 KB |
Output is correct |
10 |
Correct |
1 ms |
596 KB |
Output is correct |
11 |
Correct |
1 ms |
596 KB |
Output is correct |
12 |
Correct |
3 ms |
596 KB |
Output is correct |
13 |
Correct |
1 ms |
596 KB |
Output is correct |
14 |
Correct |
1 ms |
596 KB |
Output is correct |
15 |
Correct |
2 ms |
596 KB |
Output is correct |
16 |
Correct |
2 ms |
596 KB |
Output is correct |
17 |
Correct |
1 ms |
596 KB |
Output is correct |
18 |
Correct |
1 ms |
596 KB |
Output is correct |
19 |
Correct |
1 ms |
596 KB |
Output is correct |
20 |
Correct |
1 ms |
596 KB |
Output is correct |
21 |
Correct |
1 ms |
596 KB |
Output is correct |
22 |
Correct |
1 ms |
596 KB |
Output is correct |
23 |
Correct |
1 ms |
596 KB |
Output is correct |
24 |
Correct |
1 ms |
596 KB |
Output is correct |
25 |
Correct |
1 ms |
596 KB |
Output is correct |
26 |
Correct |
1 ms |
596 KB |
Output is correct |
27 |
Correct |
2 ms |
596 KB |
Output is correct |
28 |
Correct |
1 ms |
596 KB |
Output is correct |
29 |
Correct |
1 ms |
596 KB |
Output is correct |
30 |
Correct |
1 ms |
596 KB |
Output is correct |
31 |
Correct |
1 ms |
596 KB |
Output is correct |
32 |
Correct |
1 ms |
596 KB |
Output is correct |
33 |
Execution timed out |
1090 ms |
160556 KB |
Time limit exceeded |
34 |
Halted |
0 ms |
0 KB |
- |