This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
//#include<i_am_noob_orz>
#include<bits/stdc++.h>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
using namespace std;
#define ll long long
#define int ll
#define ull unsigned long long
#define pii pair<int,int>
#define X first
#define Y second
#define mod ((ll)1e9+7)
#define pb push_back
#define mp make_pair
#define abs(x) ((x)>0?(x):(-(x)))
#define F(n) Fi(i,n)
#define Fi(i,n) Fl(i,0,n)
#define Fl(i,l,n) for(int i=l;i<n;i++)
#define memres(a) memset(a,0,sizeof(a))
#define all(a) a.begin(),a.end()
#define sz(a) ((int)a.size())
#define ceiling(a,b) (((a)+(b)-1)/(b))
#define endl '\n'
#define bit_count(x) __builtin_popcountll((x))
#define ykh mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
#define jimmy_is_kind false
typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> rbtree;
//#define LOCAL
#ifdef LOCAL
#define bug(...) cerr<<"#"<<__LINE__<<' '<<#__VA_ARGS__<<"- ", _do(__VA_ARGS__)
template<typename T> void _do(T && x) {cerr<<x<<endl;}
template<typename T, typename ...S> void _do(T && x, S&&...y) {cerr<<x<<", "; _do(y...);}
#define IOS()
#else
#define IOS() ios_base::sync_with_stdio(0), cin.tie(0)
#define endl '\n'
#define bug(...)
#endif
int add(int a,int b){return(a+b>=mod?a+b-mod:a+b);}
int sub(int a,int b){return(a<b?a+mod-b:a-b);}
int po(int a,int b){
if(b==0)return 1;
if(b==1)return(a%mod);
int tem=po(a,b/2);
if(b&1)return(((tem*tem)%mod)*a)%mod;
else return(tem*tem)%mod;
}
int GCD(int a,int b){
int x=0;
int ra,rb;
while(a&&b){
if(((a&1)==0)&&((b&1)==0)){
a>>=1,b>>=1,x++;
}
else if((a^b)&1){
if(a&1)b>>=1;
else a>>=1;
}
else{
ra=abs(a-b),rb=min(a,b);
a=ra,b=rb;
}
}
return max(a,b)<<x;
}
int gcd(int a,int b){if(b==0)return a;return gcd(b,a%b);}
pii seg1[800010],seg2[800010];
int n,q,a[200010],b[200010],o[200010];
pair<pii,pii> que[200010];
vector<pair<pii,pii> > event;
inline void pull1(int p,int l,int r){
int mi=(l+r)/2;
seg1[p].X=seg1[p*2].X+seg1[p*2+1].X;
seg1[p].Y=seg1[p*2+1].Y+seg1[p*2].Y+(r-mi)*seg1[p*2].X;
}
void build1(int p,int l,int r){
if(l==r){
if(b[l]>0)seg1[p].X=seg1[p].Y=b[l];
return;
}
int mi=(l+r)/2;
build1(p*2,l,mi),build1(p*2+1,mi+1,r);
pull1(p,l,r);
return;
}
void modify1(int p,int l,int r,int x,int v){
if(l==r){
seg1[p]={v,v};
return;
}
int mi=(l+r)/2;
if(mi>=x)modify1(p*2,l,mi,x,v);
else modify1(p*2+1,mi+1,r,x,v);
pull1(p,l,r);
}
pii query1(int p,int l,int r,int ql,int qr){
if(l>qr||r<ql)return {0,0};
if(l>=ql&&r<=qr)return seg1[p];
int mi=(l+r)/2;
pii t1=query1(p*2,l,mi,ql,qr),t2=query1(p*2+1,mi+1,r,ql,qr);
if(ql>mi)return t2;
else if(qr<=mi)return t1;
else{
if(qr<=r){t2.Y+=(t1.Y+(qr-mi)*t1.X);t2.X+=t1.X;}
else{t2.Y+=(t1.Y+(r-mi)*t1.X);t2.X+=t1.X;}
return t2;
}
}
///////////////////////////////////////////////
inline void pull2(int p,int l,int r){
int mi=(l+r)/2;
seg2[p].X=seg2[p*2].X+seg2[p*2+1].X;
seg2[p].Y=seg2[p*2+1].Y+seg2[p*2].Y+(r-mi)*seg2[p*2].X;
}
void build2(int p,int l,int r){
if(l==r){
if(b[l]<0)seg2[p].X=seg2[p].Y=b[l];
return;
}
int mi=(l+r)/2;
build2(p*2,l,mi),build2(p*2+1,mi+1,r);
pull2(p,l,r);
return;
}
void modify2(int p,int l,int r,int x,int v){
if(l==r){
seg2[p]={v,v};
return;
}
int mi=(l+r)/2;
if(mi>=x)modify2(p*2,l,mi,x,v);
else modify2(p*2+1,mi+1,r,x,v);
pull2(p,l,r);
}
pii query2(int p,int l,int r,int ql,int qr){
if(l>qr||r<ql)return {0,0};
if(l>=ql&&r<=qr){return seg2[p];}
int mi=(l+r)/2;
pii t1=query2(p*2,l,mi,ql,qr),t2=query2(p*2+1,mi+1,r,ql,qr);
if(ql>mi)return t2;
else if(qr<=mi){return t1;}
else{
if(qr<=r){t2.Y+=(t1.Y+(qr-mi)*t1.X);t2.X+=t1.X;}
else{t2.Y+=(t1.Y+(r-mi)*t1.X);t2.X+=t1.X;}
return t2;
}
}
signed main(){
IOS();
cin>>n>>q;
F(n)cin>>a[i];
b[0]=a[0];
F(n-1)b[i+1]=a[i+1]-a[i];
F(n)bug(b[i]);
build1(1,0,n-1);
build2(1,0,n-1);
F(q){
int t,l,r;
cin>>t>>l>>r;
--l,--r;
que[i]={{t,i},{l,r}};
}
vector<pii> qu;
qu.pb({a[n-1],n-1});
for(int i=n-1;i>0;i--){
while(sz(qu)&&qu.back().X<a[i-1])qu.pop_back();
if(b[i]<0){
if(sz(qu)){
int tt=qu.back().Y-i;
event.pb({{tt,1},{qu.back().Y,qu.back().X-a[i-1]}});
event.pb({{tt,2},{i,0}});
}
}
qu.pb({a[i-1],i-1});
}
qu.clear();
qu.pb({a[0],0});
for(int i=1;i<=n-1;i++){
while(sz(qu)&&qu.back().X<a[i])qu.pop_back();
if(b[i]>0){
if(sz(qu)){
int tt=i-qu.back().Y-1;
event.pb({{tt,1},{i,0}});
event.pb({{tt,2},{qu.back().Y+1,a[i]-qu.back().X}});
}
}
qu.pb({a[i],i});
}
sort(all(event));
sort(que,que+q);
//F(sz(event))cout<<event[i].X.X<<" "<<event[i].X.Y<<" "<<event[i].Y.X<<" "<<event[i].Y.Y<<endl;
//bug(query2(1,0,n-1,0,1).Y);
int p=0;
F(q){
while(p<sz(event)&&event[p].X.X<=que[i].X.X){
if(event[p].X.Y==1){
modify1(1,0,n-1,event[p].Y.X,event[p].Y.Y);
}
else{
modify2(1,0,n-1,event[p].Y.X,event[p].Y.Y);
}
p++;
}
int ss=0;
bug(p);
if(que[i].Y.X>0)ss+=(query1(1,0,n-1,0,que[i].Y.X-1).X*(que[i].Y.Y-que[i].Y.X+1));
bug(que[i].X.Y,ss);
bug(query1(1,0,n-1,que[i].Y.X,que[i].Y.Y).X);
ss+=query1(1,0,n-1,que[i].Y.X,que[i].Y.Y).Y;
bug(que[i].X.Y,ss);
int l=que[i].Y.X-que[i].X.X,r=que[i].Y.Y-que[i].X.X;
if(r>=0){
if(l>0){
ss+=(query2(1,0,n-1,0,l-1).X*(r-l+1));
}
bug(que[i].X.Y,ss);
l=max(0ll,l);
bug(l,r);
bug(query2(1,0,n-1,l,r).X);
ss+=query2(1,0,n-1,l,r).Y;
bug(que[i].X.Y,ss);
}
o[que[i].X.Y]=ss;
}
F(q)cout<<o[i]<<endl;
return 0;
}
/*
9 3 2 6 5
9 -6 -1 4 -1
9 0 0 4 0
0 -6 -1 0 -1
9 9 3 6 6
9 0 -6 3 0
9 0 0 3 0
0 0 -6 0 0
9 9 9 6 6
9 0 0 -3 0
9 0 0 0 0
0 0 0 -3 0
*/
Compilation message (stderr)
ho_t5.cpp: In function 'int main()':
ho_t5.cpp:19:19: warning: this 'for' clause does not guard... [-Wmisleading-indentation]
19 | #define Fl(i,l,n) for(int i=l;i<n;i++)
| ^~~
ho_t5.cpp:18:17: note: in expansion of macro 'Fl'
18 | #define Fi(i,n) Fl(i,0,n)
| ^~
ho_t5.cpp:17:14: note: in expansion of macro 'Fi'
17 | #define F(n) Fi(i,n)
| ^~
ho_t5.cpp:239:5: note: in expansion of macro 'F'
239 | F(q)cout<<o[i]<<endl;
| ^
ho_t5.cpp:240:2: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'for'
240 | return 0;
| ^~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |