/*input
20 20
2 1 2 2 1 1 1 1 2 2 2 1 2 1 1 2 1 2 1 1
1 1 14
2 3 18
4 10 15
8 2 17
9 20 20
4 8 19
7 2 20
11 1 5
13 2 8
20 1 20
2 12 15
7 1 14
12 7 18
14 2 17
9 19 20
12 12 12
6 2 15
11 2 15
19 12 17
4 1 20
*/
#include<bits/stdc++.h>
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
typedef tree<long long, null_type, less_equal<long long>, rb_tree_tag, tree_order_statistics_node_update> indexed_set;
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
//order_of_key #of elements less than x
// find_by_order kth element
using ll = long long;
using ld = long double;
using pii = pair<ll,int>;
#define f first
#define s second
#define pb push_back
#define REP(i,n) for(int i=0;i<n;i++)
#define REP1(i,n) for(int i=1;i<=n;i++)
#define FILL(n,x) memset(n,x,sizeof(n))
#define ALL(_a) _a.begin(),_a.end()
#define sz(x) (int)x.size()
#define SORT_UNIQUE(c) (sort(c.begin(),c.end()), c.resize(distance(c.begin(),unique(c.begin(),c.end()))))
const ll INF64=4e18;
const int INF=0x3f3f3f3f;
const ll MOD=1e9+7;
const ld PI=acos(-1);
const ld eps=1e-9;
#define lowb(x) x&(-x)
#define MNTO(x,y) x=min(x,(__typeof__(x))y)
#define MXTO(x,y) x=max(x,(__typeof__(x))y)
ll mult(ll a,ll b){
return a*b%MOD;
}
ll mypow(ll a,ll b){
if(b<=0) return 1;
ll res=1LL;
while(b){
if(b&1) res=(res*a)%MOD;
a=(a*a)%MOD;
b>>=1;
}
return res;
}
const ll maxn=4e5+5;
const ll maxlg=__lg(maxn)+2;
/** Interface */
inline int readChar();
inline int readInt();
inline void writeInt( ll x, char end = 0 );
inline void writeChar( int x );
inline void writeWord( const char *s );
/** Read */
static const int buf_size = 4096;
inline int getChar() {
static char buf[buf_size];
static int len = 0, pos = 0;
if (pos == len)
pos = 0, len = fread(buf, 1, buf_size, stdin);
if (pos == len)
return -1;
return buf[pos++];
}
inline int readChar() {
int c = getChar();
while (c <= 32)
c = getChar();
return c;
}
inline int readInt() {
int s = 1, c = readChar();
int x = 0;
if (c == '-')
s = -1, c = getChar();
while ('0' <= c && c <= '9')
x = x * 10 + c - '0', c = getChar();
return s == 1 ? x : -x;
}
/** Write */
static int write_pos = 0;
static char write_buf[buf_size];
inline void writeChar( int x ) {
if (write_pos == buf_size)
fwrite(write_buf, 1, buf_size, stdout), write_pos = 0;
write_buf[write_pos++] = x;
}
inline void writeInt(ll x, char end ) {
if (x < 0)
writeChar('-'), x = -x;
char s[24];
int n = 0;
while (x || !n)
s[n++] = '0' + x % 10, x /= 10;
while (n--)
writeChar(s[n]);
if (end)
writeChar(end);
}
inline void writeWord( const char *s ) {
while (*s)
writeChar(*s++);
}
struct Flusher {
~Flusher() {
if (write_pos)
fwrite(write_buf, 1, write_pos, stdout), write_pos = 0;
}
} flusher;
struct segt{
ll seg[4*maxn],lazy[4*maxn];
int n;
void resize(int _n){
n=_n;
}
inline void pushdown(int idx,int l,int r){
if(!lazy[idx]) return;
if(l==r){
lazy[idx]=0;
return;
}
int mid=l+r>>1;
seg[idx<<1]+=lazy[idx]*(mid-l+1);
seg[idx<<1|1]+=lazy[idx]*(r-mid);
lazy[idx<<1]+=lazy[idx];
lazy[idx<<1|1]+=lazy[idx];
lazy[idx]=0;
}
inline void upd(int idx,int l,int r,int ql,int qr,int x){
if(ql<=l and r<=qr){
lazy[idx]+=x;
seg[idx]+=(ll)x*(r-l+1);
return;
}
pushdown(idx,l,r);
int mid=l+r>>1;
if(qr<=mid) upd(idx<<1,l,mid,ql,qr,x);
else if(ql>mid) upd(idx<<1|1,mid+1,r,ql,qr,x);
else upd(idx<<1,l,mid,ql,qr,x),upd(idx<<1|1,mid+1,r,ql,qr,x);
seg[idx]=(seg[idx<<1]+seg[idx<<1|1]);
}
inline ll query(int idx,int l,int r,int ql,int qr){
pushdown(idx,l,r);
if(ql<=l and r<=qr) return seg[idx];
int mid=l+r>>1;
if(qr<=mid) return query(idx<<1,l,mid,ql,qr);
else if(ql>mid) return query(idx<<1|1,mid+1,r,ql,qr);
return query(idx<<1,l,mid,ql,qr)+query(idx<<1|1,mid+1,r,ql,qr);
}
inline ll query(int l,int r){
if(l>r) return 0;
return query(1,0,n-1,l,r);
}
inline void upd(int l,int r,ll x){
if(l>r) return;
upd(1,0,n-1,l,r,x);
}
}rect,par;
int n,q;
int arr[maxn];
ll ans[maxn];
vector<pair<pii,int>> upd[maxn];
vector<pair<pii,int>> query[maxn];
inline void tri(int l,int r,int v){
if(l>r) return;
//parallelogram-rectangle
par.upd(l+n,2*n+1,v);
rect.upd(r+n+1,2*n+1,-v);
//place where xiao and da intersect
if((r-l+1)<=n) upd[r-l+1].pb({{l,r},v});
}
int l[maxn],r[maxn];
int main(){
ios::sync_with_stdio(false),cin.tie(0);
rect.resize(maxn),par.resize(maxn);
n=readInt(),q=readInt();
REP(i,n) arr[i]=readInt();
vector<int> v;
REP(i,n){
while(sz(v) and arr[v.back()]<arr[i]) v.pop_back();
if(sz(v)) l[i]=v.back();
else l[i]=-n-1;
v.pb(i);
}
v.clear();
for(int i=n-1;i>=0;i--){
while(sz(v) and arr[v.back()]<=arr[i]) v.pop_back();
if(sz(v)) r[i]=v.back();
else r[i]=n+1;
v.pb(i);
}
REP(i,n){
tri(l[i]+1,r[i]-1,arr[i]);
tri(l[i]+1,i-1,-arr[i]);
tri(i+1,r[i]-1,-arr[i]);
}
REP(i,q){
int t=readInt(),l=readInt(),r=readInt();
--l,--r;
query[t].pb({{l,r},i});
}
REP1(i,n){
for(auto x:upd[i]){
int l=x.f.f,r=x.f.s,v=x.s;
par.upd(l+n,2*n+1,-v);
rect.upd(r+n+1,2*n+1,v);
}
for(auto x:query[i]){
int l=x.f.f,r=x.f.s;
ans[x.s]=par.query(l-i+n,r-i+n)+rect.query(l+n,r+n);
}
}
REP(i,q) writeInt(ans[i],'\n');
}
Compilation message
ho_t5.cpp: In member function 'void segt::pushdown(int, int, int)':
ho_t5.cpp:157:18: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
157 | int mid=l+r>>1;
| ~^~
ho_t5.cpp: In member function 'void segt::upd(int, int, int, int, int, int)':
ho_t5.cpp:171:18: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
171 | int mid=l+r>>1;
| ~^~
ho_t5.cpp: In member function 'll segt::query(int, int, int, int, int)':
ho_t5.cpp:180:18: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
180 | int mid=l+r>>1;
| ~^~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
19276 KB |
Output is correct |
2 |
Correct |
13 ms |
19276 KB |
Output is correct |
3 |
Correct |
13 ms |
19276 KB |
Output is correct |
4 |
Correct |
13 ms |
19332 KB |
Output is correct |
5 |
Correct |
13 ms |
19392 KB |
Output is correct |
6 |
Correct |
13 ms |
19276 KB |
Output is correct |
7 |
Correct |
13 ms |
19272 KB |
Output is correct |
8 |
Correct |
13 ms |
19392 KB |
Output is correct |
9 |
Correct |
12 ms |
19276 KB |
Output is correct |
10 |
Correct |
13 ms |
19276 KB |
Output is correct |
11 |
Correct |
13 ms |
19276 KB |
Output is correct |
12 |
Correct |
13 ms |
19276 KB |
Output is correct |
13 |
Correct |
13 ms |
19404 KB |
Output is correct |
14 |
Correct |
13 ms |
19276 KB |
Output is correct |
15 |
Correct |
13 ms |
19392 KB |
Output is correct |
16 |
Correct |
13 ms |
19276 KB |
Output is correct |
17 |
Correct |
13 ms |
19388 KB |
Output is correct |
18 |
Correct |
13 ms |
19356 KB |
Output is correct |
19 |
Correct |
13 ms |
19404 KB |
Output is correct |
20 |
Correct |
14 ms |
19404 KB |
Output is correct |
21 |
Correct |
13 ms |
19404 KB |
Output is correct |
22 |
Correct |
13 ms |
19276 KB |
Output is correct |
23 |
Correct |
12 ms |
19332 KB |
Output is correct |
24 |
Correct |
13 ms |
19396 KB |
Output is correct |
25 |
Correct |
13 ms |
19428 KB |
Output is correct |
26 |
Correct |
14 ms |
19392 KB |
Output is correct |
27 |
Correct |
13 ms |
19392 KB |
Output is correct |
28 |
Correct |
13 ms |
19320 KB |
Output is correct |
29 |
Correct |
13 ms |
19276 KB |
Output is correct |
30 |
Correct |
13 ms |
19276 KB |
Output is correct |
31 |
Correct |
13 ms |
19320 KB |
Output is correct |
32 |
Correct |
13 ms |
19388 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
19276 KB |
Output is correct |
2 |
Correct |
801 ms |
61064 KB |
Output is correct |
3 |
Correct |
792 ms |
57940 KB |
Output is correct |
4 |
Correct |
808 ms |
58160 KB |
Output is correct |
5 |
Correct |
749 ms |
64468 KB |
Output is correct |
6 |
Correct |
799 ms |
60792 KB |
Output is correct |
7 |
Correct |
837 ms |
61344 KB |
Output is correct |
8 |
Correct |
867 ms |
58652 KB |
Output is correct |
9 |
Correct |
788 ms |
61192 KB |
Output is correct |
10 |
Correct |
703 ms |
57668 KB |
Output is correct |
11 |
Correct |
895 ms |
61440 KB |
Output is correct |
12 |
Correct |
722 ms |
60400 KB |
Output is correct |
13 |
Correct |
849 ms |
64288 KB |
Output is correct |
14 |
Correct |
837 ms |
58340 KB |
Output is correct |
15 |
Correct |
814 ms |
64424 KB |
Output is correct |
16 |
Correct |
825 ms |
57692 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
19276 KB |
Output is correct |
2 |
Correct |
757 ms |
57916 KB |
Output is correct |
3 |
Correct |
716 ms |
57584 KB |
Output is correct |
4 |
Correct |
854 ms |
58984 KB |
Output is correct |
5 |
Correct |
791 ms |
57768 KB |
Output is correct |
6 |
Correct |
703 ms |
58116 KB |
Output is correct |
7 |
Correct |
794 ms |
58128 KB |
Output is correct |
8 |
Correct |
782 ms |
58164 KB |
Output is correct |
9 |
Correct |
703 ms |
57804 KB |
Output is correct |
10 |
Correct |
755 ms |
57184 KB |
Output is correct |
11 |
Correct |
757 ms |
58852 KB |
Output is correct |
12 |
Correct |
769 ms |
58656 KB |
Output is correct |
13 |
Correct |
760 ms |
58716 KB |
Output is correct |
14 |
Correct |
722 ms |
57888 KB |
Output is correct |
15 |
Correct |
753 ms |
58720 KB |
Output is correct |
16 |
Correct |
763 ms |
58384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
768 ms |
57816 KB |
Output is correct |
2 |
Correct |
759 ms |
58036 KB |
Output is correct |
3 |
Correct |
778 ms |
59016 KB |
Output is correct |
4 |
Correct |
717 ms |
57776 KB |
Output is correct |
5 |
Correct |
770 ms |
58032 KB |
Output is correct |
6 |
Correct |
784 ms |
58404 KB |
Output is correct |
7 |
Correct |
799 ms |
58928 KB |
Output is correct |
8 |
Correct |
787 ms |
58676 KB |
Output is correct |
9 |
Correct |
734 ms |
58272 KB |
Output is correct |
10 |
Correct |
812 ms |
58680 KB |
Output is correct |
11 |
Correct |
765 ms |
58424 KB |
Output is correct |
12 |
Correct |
809 ms |
58436 KB |
Output is correct |
13 |
Correct |
715 ms |
58400 KB |
Output is correct |
14 |
Correct |
758 ms |
58324 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
19276 KB |
Output is correct |
2 |
Correct |
13 ms |
19276 KB |
Output is correct |
3 |
Correct |
13 ms |
19276 KB |
Output is correct |
4 |
Correct |
13 ms |
19332 KB |
Output is correct |
5 |
Correct |
13 ms |
19392 KB |
Output is correct |
6 |
Correct |
13 ms |
19276 KB |
Output is correct |
7 |
Correct |
13 ms |
19272 KB |
Output is correct |
8 |
Correct |
13 ms |
19392 KB |
Output is correct |
9 |
Correct |
12 ms |
19276 KB |
Output is correct |
10 |
Correct |
13 ms |
19276 KB |
Output is correct |
11 |
Correct |
13 ms |
19276 KB |
Output is correct |
12 |
Correct |
13 ms |
19276 KB |
Output is correct |
13 |
Correct |
13 ms |
19404 KB |
Output is correct |
14 |
Correct |
13 ms |
19276 KB |
Output is correct |
15 |
Correct |
13 ms |
19392 KB |
Output is correct |
16 |
Correct |
13 ms |
19276 KB |
Output is correct |
17 |
Correct |
13 ms |
19388 KB |
Output is correct |
18 |
Correct |
13 ms |
19356 KB |
Output is correct |
19 |
Correct |
13 ms |
19404 KB |
Output is correct |
20 |
Correct |
14 ms |
19404 KB |
Output is correct |
21 |
Correct |
13 ms |
19404 KB |
Output is correct |
22 |
Correct |
13 ms |
19276 KB |
Output is correct |
23 |
Correct |
12 ms |
19332 KB |
Output is correct |
24 |
Correct |
13 ms |
19396 KB |
Output is correct |
25 |
Correct |
13 ms |
19428 KB |
Output is correct |
26 |
Correct |
14 ms |
19392 KB |
Output is correct |
27 |
Correct |
13 ms |
19392 KB |
Output is correct |
28 |
Correct |
13 ms |
19320 KB |
Output is correct |
29 |
Correct |
13 ms |
19276 KB |
Output is correct |
30 |
Correct |
13 ms |
19276 KB |
Output is correct |
31 |
Correct |
13 ms |
19320 KB |
Output is correct |
32 |
Correct |
13 ms |
19388 KB |
Output is correct |
33 |
Correct |
801 ms |
61064 KB |
Output is correct |
34 |
Correct |
792 ms |
57940 KB |
Output is correct |
35 |
Correct |
808 ms |
58160 KB |
Output is correct |
36 |
Correct |
749 ms |
64468 KB |
Output is correct |
37 |
Correct |
799 ms |
60792 KB |
Output is correct |
38 |
Correct |
837 ms |
61344 KB |
Output is correct |
39 |
Correct |
867 ms |
58652 KB |
Output is correct |
40 |
Correct |
788 ms |
61192 KB |
Output is correct |
41 |
Correct |
703 ms |
57668 KB |
Output is correct |
42 |
Correct |
895 ms |
61440 KB |
Output is correct |
43 |
Correct |
722 ms |
60400 KB |
Output is correct |
44 |
Correct |
849 ms |
64288 KB |
Output is correct |
45 |
Correct |
837 ms |
58340 KB |
Output is correct |
46 |
Correct |
814 ms |
64424 KB |
Output is correct |
47 |
Correct |
825 ms |
57692 KB |
Output is correct |
48 |
Correct |
757 ms |
57916 KB |
Output is correct |
49 |
Correct |
716 ms |
57584 KB |
Output is correct |
50 |
Correct |
854 ms |
58984 KB |
Output is correct |
51 |
Correct |
791 ms |
57768 KB |
Output is correct |
52 |
Correct |
703 ms |
58116 KB |
Output is correct |
53 |
Correct |
794 ms |
58128 KB |
Output is correct |
54 |
Correct |
782 ms |
58164 KB |
Output is correct |
55 |
Correct |
703 ms |
57804 KB |
Output is correct |
56 |
Correct |
755 ms |
57184 KB |
Output is correct |
57 |
Correct |
757 ms |
58852 KB |
Output is correct |
58 |
Correct |
769 ms |
58656 KB |
Output is correct |
59 |
Correct |
760 ms |
58716 KB |
Output is correct |
60 |
Correct |
722 ms |
57888 KB |
Output is correct |
61 |
Correct |
753 ms |
58720 KB |
Output is correct |
62 |
Correct |
763 ms |
58384 KB |
Output is correct |
63 |
Correct |
768 ms |
57816 KB |
Output is correct |
64 |
Correct |
759 ms |
58036 KB |
Output is correct |
65 |
Correct |
778 ms |
59016 KB |
Output is correct |
66 |
Correct |
717 ms |
57776 KB |
Output is correct |
67 |
Correct |
770 ms |
58032 KB |
Output is correct |
68 |
Correct |
784 ms |
58404 KB |
Output is correct |
69 |
Correct |
799 ms |
58928 KB |
Output is correct |
70 |
Correct |
787 ms |
58676 KB |
Output is correct |
71 |
Correct |
734 ms |
58272 KB |
Output is correct |
72 |
Correct |
812 ms |
58680 KB |
Output is correct |
73 |
Correct |
765 ms |
58424 KB |
Output is correct |
74 |
Correct |
809 ms |
58436 KB |
Output is correct |
75 |
Correct |
715 ms |
58400 KB |
Output is correct |
76 |
Correct |
758 ms |
58324 KB |
Output is correct |
77 |
Correct |
893 ms |
58892 KB |
Output is correct |
78 |
Correct |
912 ms |
60036 KB |
Output is correct |
79 |
Correct |
901 ms |
59208 KB |
Output is correct |
80 |
Correct |
860 ms |
64128 KB |
Output is correct |
81 |
Correct |
882 ms |
64424 KB |
Output is correct |
82 |
Correct |
881 ms |
64620 KB |
Output is correct |
83 |
Correct |
900 ms |
64236 KB |
Output is correct |
84 |
Correct |
813 ms |
64208 KB |
Output is correct |
85 |
Correct |
887 ms |
64860 KB |
Output is correct |
86 |
Correct |
843 ms |
64204 KB |
Output is correct |
87 |
Correct |
891 ms |
67248 KB |
Output is correct |
88 |
Correct |
839 ms |
66968 KB |
Output is correct |
89 |
Correct |
785 ms |
65884 KB |
Output is correct |
90 |
Correct |
782 ms |
66548 KB |
Output is correct |
91 |
Correct |
780 ms |
65828 KB |
Output is correct |
92 |
Correct |
708 ms |
65452 KB |
Output is correct |
93 |
Correct |
783 ms |
66276 KB |
Output is correct |
94 |
Correct |
872 ms |
67040 KB |
Output is correct |
95 |
Correct |
782 ms |
67108 KB |
Output is correct |
96 |
Correct |
812 ms |
66108 KB |
Output is correct |
97 |
Correct |
786 ms |
66312 KB |
Output is correct |
98 |
Correct |
837 ms |
64552 KB |
Output is correct |
99 |
Correct |
810 ms |
64968 KB |
Output is correct |
100 |
Correct |
853 ms |
65384 KB |
Output is correct |
101 |
Correct |
861 ms |
65388 KB |
Output is correct |
102 |
Correct |
928 ms |
65824 KB |
Output is correct |