This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#define DEBUG 0
#include <bits/stdc++.h>
using namespace std;
#if DEBUG
// basic debugging macros
int __i__,__j__;
#define printLine(l) for(__i__=0;__i__<l;__i__++){cout<<"-";}cout<<endl
#define printLine2(l,c) for(__i__=0;__i__<l;__i__++){cout<<c;}cout<<endl
#define printVar(n) cout<<#n<<": "<<n<<endl
#define printArr(a,l) cout<<#a<<": ";for(__i__=0;__i__<l;__i__++){cout<<a[__i__]<<" ";}cout<<endl
#define print2dArr(a,r,c) cout<<#a<<":\n";for(__i__=0;__i__<r;__i__++){for(__j__=0;__j__<c;__j__++){cout<<a[__i__][__j__]<<" ";}cout<<endl;}
#define print2dArr2(a,r,c,l) cout<<#a<<":\n";for(__i__=0;__i__<r;__i__++){for(__j__=0;__j__<c;__j__++){cout<<setw(l)<<setfill(' ')<<a[__i__][__j__]<<" ";}cout<<endl;}
// advanced debugging class
// debug 1,2,'A',"test";
class _Debug {
public:
template<typename T>
_Debug& operator,(T val) {
cout << val << endl;
return *this;
}
};
#define debug _Debug(),
#else
#define printLine(l)
#define printLine2(l,c)
#define printVar(n)
#define printArr(a,l)
#define print2dArr(a,r,c)
#define print2dArr2(a,r,c,l)
#define debug
#endif
// define
#define MAX_VAL 999999999
#define MAX_VAL_2 999999999999999999LL
#define EPS 1e-6
#define mp make_pair
#define pb push_back
// typedef
typedef unsigned int UI;
typedef long long int LLI;
typedef unsigned long long int ULLI;
typedef unsigned short int US;
typedef pair<int,int> pii;
typedef pair<LLI,LLI> plli;
typedef vector<int> vi;
typedef vector<LLI> vlli;
typedef vector<pii> vpii;
typedef vector<plli> vplli;
// ---------- END OF TEMPLATE ----------
#pragma GCC optimize("Ofast")
int S[200000],l[200000],r[200000];
LLI ans[200000];
struct event { int y,l,r,t,i; };
bool comp(event a,event b) {
if (a.y == b.y) return abs(a.t) > abs(b.t);
else return a.y < b.y;
}
vector<event> v1,v2;
vi s;
LLI bit1[400001],bit2[400001];
int update(int s,int e,int num,int N) {
int i;
for (i = s+1; i <= N; i += i & (-i)) bit1[i] += num,bit2[i] -= (LLI) (s-1)*num;
for (i = e+1; i <= N; i += i & (-i)) bit1[i] -= num,bit2[i] += (LLI) e*num;
return 0;
}
LLI query(int s,int e) {
int i;
LLI ans = 0;
for (i = e+1; i > 0; i -= i & (-i)) ans += bit1[i]*e+bit2[i];
for (i = s; i > 0; i -= i & (-i)) ans -= bit1[i]*(s-1)+bit2[i];
return ans;
}
int main() {
int i;
int N,Q,T,L,R;
scanf("%d %d",&N,&Q);
for (i = 0; i < N; i++) scanf("%d",&S[i]);
for (i = 0; i < Q; i++) {
scanf("%d %d %d",&T,&L,&R);
L--,R--;
v1.pb((event){T,L,R,0,i});
v2.pb((event){T,L-T,R-T,0,i});
}
for (i = 0; i < N; i++) {
while (!s.empty() && (S[s.back()] < S[i])) s.pop_back();
if (s.empty()) l[i] = -1;
else l[i] = s.back();
s.pb(i);
}
s.clear();
for (i = N-1; i >= 0; i--) {
while (!s.empty() && (S[s.back()] <= S[i])) s.pop_back();
if (s.empty()) r[i] = N;
else r[i] = s.back();
s.pb(i);
}
for (i = 0; i < N; i++) {
v1.pb((event){r[i]-i,i,r[i]-1,S[i],0});
v1.pb((event){0,i,N-1,S[i],0});
v1.pb((event){r[i]-i,i,N-1,-S[i],0});
v2.pb((event){0,i+1,N-1,-S[i],0});
v2.pb((event){r[i]-i,i+1,N-1,S[i],0});
if (l[i] != -1) {
v1.pb((event){r[i]-l[i]-1,i,r[i]-1,-S[i],0});
v1.pb((event){i-l[i],i,N-1,-S[i],0});
v1.pb((event){r[i]-l[i]-1,i,N-1,S[i],0});
v2.pb((event){i-l[i],l[i]+1,N-1,S[i],0});
v2.pb((event){r[i]-l[i]-1,l[i]+1,N-1,-S[i],0});
}
}
sort(v1.begin(),v1.end(),comp);
sort(v2.begin(),v2.end(),comp);
for (i = 0; i < v1.size(); i++) {
if (v1[i].t != 0) update(v1[i].l+N,v1[i].r+N,v1[i].t,2*N);
else ans[v1[i].i] += query(v1[i].l+N,v1[i].r+N);
}
for (i = 0; i <= 2*N; i++) bit1[i] = bit2[i] = 0;
for (i = 0; i < v2.size(); i++) {
if (v2[i].t != 0) update(v2[i].l+N,v2[i].r+N,v2[i].t,2*N);
else ans[v2[i].i] += query(v2[i].l+N,v2[i].r+N);
}
for (i = 0; i < Q; i++) printf("%lld\n",ans[i]);
return 0;
}
Compilation message (stderr)
ho_t5.cpp: In function 'int main()':
ho_t5.cpp:123:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<event>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
123 | for (i = 0; i < v1.size(); i++) {
| ~~^~~~~~~~~~~
ho_t5.cpp:128:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<event>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
128 | for (i = 0; i < v2.size(); i++) {
| ~~^~~~~~~~~~~
ho_t5.cpp:85:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
85 | scanf("%d %d",&N,&Q);
| ~~~~~^~~~~~~~~~~~~~~
ho_t5.cpp:86:34: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
86 | for (i = 0; i < N; i++) scanf("%d",&S[i]);
| ~~~~~^~~~~~~~~~~~
ho_t5.cpp:88:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
88 | scanf("%d %d %d",&T,&L,&R);
| ~~~~~^~~~~~~~~~~~~~~~~~~~~
# | 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... |