#include<bits/stdc++.h>
using namespace std;
const long long maxn=200000+10;
long long n,q;
long long vis[maxn],all[maxn],res[maxn],par[maxn],val[maxn],sz[maxn];
vector<pair<long long,long long>>allq[maxn];
vector<long long>adj[maxn];
pair<long long,long long>tof[maxn];
void vorod(){
cin>>n>>q;
for(long long i=1;i<=n;i++){
cin>>all[i];
}
for(long long i=1;i<=q;i++){
long long t,l,r;
cin>>t>>l>>r;
tof[i]=make_pair(l,r);
allq[l-1].push_back(make_pair(-i,t));
allq[r].push_back(make_pair(i,t));
}
}
void pre(){
vector<long long>v;
for(long long i=1;i<=n;i++){
while((long long)v.size()>0&&all[v.back()]<all[i]){
v.pop_back();
}
if((long long)v.size()>0){
par[i]=v.back();
val[i]=all[v.back()]-all[i];
adj[v.back()].push_back(i);
sz[i]=i-par[i]-1;
}
v.push_back(i);
}
}
void dfs(int u){
int fu=u;
while(fu!=0){
sz[fu]++;
fu=par[fu];
}
for(auto x:allq[u]){
long long mainres=0;
for(long long j=1;j<=n;j++){
mainres+=max(min(x.second,sz[j])-(j-par[j]-1),0ll)*val[j];
}
if(x.first<0){
x.first*=-1;
mainres*=-1;
}
res[x.first]+=mainres;
}
for(auto x:adj[u]){
dfs(x);
}
}
void solve(){
for(long long i=1;i<=n;i++){
if(vis[i]==0){
dfs(i);
}
}
}
void khor(){
for(long long i=1;i<=q;i++){
for(long long j=tof[i].first;j<=tof[i].second;j++){
res[i]+=all[j];
}
cout<<res[i]<<"\n";
}
}
int main(){
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
//freopen("inp.txt","r",stdin);
vorod();
pre();
solve();
khor();
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
16732 KB |
Output is correct |
2 |
Incorrect |
5 ms |
18780 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
16732 KB |
Output is correct |
2 |
Execution timed out |
1065 ms |
34268 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
16732 KB |
Output is correct |
2 |
Execution timed out |
1072 ms |
34644 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
1024 ms |
35780 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
16732 KB |
Output is correct |
2 |
Incorrect |
5 ms |
18780 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |