//by szh
#include<bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define pii pair<int,int>
#define pll pair<long long,long long>
#define pb push_back
#define debug(x) cerr<<#x<<"="<<x<<endl
#define pq priority_queue
#define inf 0x3f
#define rep(i,a,b) for (int i=a;i<(b);i++)
#define MP make_pair
#define SZ(x) (int(x.size()))
#define ll long long
#define mod 1000000007
#define ALL(x) x.begin(),x.end()
void inc(int &a,int b) {a=(a+b)%mod;}
void dec(int &a,int b) {a=(a-b+mod)%mod;}
int lowbit(int x) {return x&(-x);}
ll p0w(ll base,ll p) {ll ret=1;while(p>0){if (p%2ll==1ll) ret=ret*base%mod;base=base*base%mod;p/=2ll;}return ret;}
const int maxn = 4e5+10;
struct rurq{
ll tree1[maxn],tree2[maxn];
void update(ll l,ll r,ll val) {
int pos = r+1;
while (pos<maxn) {
tree1[pos]-=val;
tree2[pos]-=r*val;
pos+=lowbit(pos);
}
pos = l;
while (pos<maxn) {
tree1[pos]+=val;
tree2[pos]+=(l-1)*val;
pos+=lowbit(pos);
}
}
ll query(int pos) {
ll ret = 0;
int tmp = pos;
while (pos>0) {
ret += tree1[pos]*tmp;
ret -= tree2[pos];
pos-=lowbit(pos);
}
return ret;
}
ll query(int l,int r) {
return query(r)-query(l-1);
}
}vert,diag;
int n,m;
int a[maxn];
int lt[maxn],rt[maxn];
ll ans[maxn];
vector <pair<pii,int>> q[maxn];
vector <pair<pii,int>> upd[maxn];
void tri(int l,int r,int val) {
if (l>r) return;
diag.update(l+n+1,n+n+1,val);
vert.update(r+1+n+1,n+n+1,-val);
if (r-l+1<=n) upd[r-l+1].pb({{l,r},val});
}
int main() {
// freopen("input.txt","r",stdin);
std::ios::sync_with_stdio(false);cin.tie(0);
cin>>n>>m;
rep(i,0,n) cin>>a[i];
rep(i,0,m) {
int t,l,r;
cin>>t>>l>>r;
l--,r--;
q[t].pb({{l,r},i});
}
stack<int> s;
rep(i,0,n) {
while (!s.empty() and a[s.top()]<=a[i]) s.pop();
if (s.empty()) lt[i] = i-n-1;
else lt[i] = s.top();
s.push(i);
}
while (!s.empty()) s.pop();
for (int i =n-1;i>=0;i--) {
while (!s.empty() and a[s.top()]<a[i]) s.pop();
if(s.empty()) rt[i] = n;
else rt[i] = s.top();
s.push(i);
}
rep(i,0,n) {
tri(lt[i]+1,rt[i]-1,a[i]);
tri(lt[i]+1,i-1,-a[i]);
tri(i+1,rt[i]-1,-a[i]);
}
debug(vert.query(n+1,2*n) + diag.query(n+1,2*n));
rep(i,1,n+1) {
for (auto j:upd[i]) {
int l = j.fi.fi,r = j.fi.se, val = j.se;
diag.update(l+n+1,n+n+1,-val);
vert.update(r+1+n+1,n+n+1,val);
}
for (auto j:q[i]) {
ans[j.se] = vert.query(j.fi.fi+n+1,j.fi.se+n+1) + diag.query(j.fi.fi-i+n+1,j.fi.se-i+n+1);
}
}
rep(i,0,m) cout<<ans[i]<<"\n";
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
19212 KB |
Output is correct |
2 |
Correct |
10 ms |
19284 KB |
Output is correct |
3 |
Correct |
10 ms |
19252 KB |
Output is correct |
4 |
Correct |
10 ms |
19300 KB |
Output is correct |
5 |
Correct |
10 ms |
19244 KB |
Output is correct |
6 |
Correct |
10 ms |
19284 KB |
Output is correct |
7 |
Correct |
10 ms |
19284 KB |
Output is correct |
8 |
Correct |
10 ms |
19284 KB |
Output is correct |
9 |
Correct |
10 ms |
19256 KB |
Output is correct |
10 |
Correct |
12 ms |
19284 KB |
Output is correct |
11 |
Correct |
10 ms |
19324 KB |
Output is correct |
12 |
Correct |
11 ms |
19300 KB |
Output is correct |
13 |
Correct |
11 ms |
19284 KB |
Output is correct |
14 |
Correct |
11 ms |
19248 KB |
Output is correct |
15 |
Correct |
10 ms |
19244 KB |
Output is correct |
16 |
Correct |
11 ms |
19284 KB |
Output is correct |
17 |
Correct |
10 ms |
19284 KB |
Output is correct |
18 |
Correct |
12 ms |
19252 KB |
Output is correct |
19 |
Correct |
10 ms |
19324 KB |
Output is correct |
20 |
Correct |
11 ms |
19252 KB |
Output is correct |
21 |
Correct |
10 ms |
19284 KB |
Output is correct |
22 |
Correct |
10 ms |
19284 KB |
Output is correct |
23 |
Correct |
10 ms |
19280 KB |
Output is correct |
24 |
Correct |
11 ms |
19412 KB |
Output is correct |
25 |
Correct |
10 ms |
19284 KB |
Output is correct |
26 |
Correct |
11 ms |
19284 KB |
Output is correct |
27 |
Correct |
11 ms |
19252 KB |
Output is correct |
28 |
Correct |
12 ms |
19284 KB |
Output is correct |
29 |
Correct |
13 ms |
19248 KB |
Output is correct |
30 |
Correct |
11 ms |
19320 KB |
Output is correct |
31 |
Correct |
12 ms |
19248 KB |
Output is correct |
32 |
Correct |
10 ms |
19284 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
19212 KB |
Output is correct |
2 |
Correct |
188 ms |
45464 KB |
Output is correct |
3 |
Correct |
189 ms |
45328 KB |
Output is correct |
4 |
Correct |
176 ms |
45880 KB |
Output is correct |
5 |
Correct |
202 ms |
47120 KB |
Output is correct |
6 |
Correct |
213 ms |
45448 KB |
Output is correct |
7 |
Correct |
177 ms |
45788 KB |
Output is correct |
8 |
Correct |
201 ms |
48544 KB |
Output is correct |
9 |
Correct |
168 ms |
46892 KB |
Output is correct |
10 |
Correct |
180 ms |
45400 KB |
Output is correct |
11 |
Correct |
171 ms |
47156 KB |
Output is correct |
12 |
Correct |
171 ms |
45632 KB |
Output is correct |
13 |
Correct |
161 ms |
47068 KB |
Output is correct |
14 |
Correct |
166 ms |
47220 KB |
Output is correct |
15 |
Correct |
175 ms |
47128 KB |
Output is correct |
16 |
Correct |
173 ms |
45740 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
19212 KB |
Output is correct |
2 |
Correct |
236 ms |
46672 KB |
Output is correct |
3 |
Correct |
256 ms |
46200 KB |
Output is correct |
4 |
Correct |
244 ms |
47824 KB |
Output is correct |
5 |
Correct |
254 ms |
46332 KB |
Output is correct |
6 |
Correct |
224 ms |
46700 KB |
Output is correct |
7 |
Correct |
242 ms |
46556 KB |
Output is correct |
8 |
Correct |
261 ms |
46640 KB |
Output is correct |
9 |
Correct |
213 ms |
46360 KB |
Output is correct |
10 |
Correct |
209 ms |
46088 KB |
Output is correct |
11 |
Correct |
217 ms |
47848 KB |
Output is correct |
12 |
Correct |
219 ms |
47560 KB |
Output is correct |
13 |
Correct |
243 ms |
46848 KB |
Output is correct |
14 |
Correct |
205 ms |
46460 KB |
Output is correct |
15 |
Correct |
227 ms |
46664 KB |
Output is correct |
16 |
Correct |
223 ms |
46424 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
243 ms |
41964 KB |
Output is correct |
2 |
Correct |
214 ms |
46068 KB |
Output is correct |
3 |
Correct |
232 ms |
47068 KB |
Output is correct |
4 |
Correct |
216 ms |
45768 KB |
Output is correct |
5 |
Correct |
246 ms |
46048 KB |
Output is correct |
6 |
Correct |
201 ms |
46152 KB |
Output is correct |
7 |
Correct |
186 ms |
46140 KB |
Output is correct |
8 |
Correct |
213 ms |
46732 KB |
Output is correct |
9 |
Correct |
205 ms |
45732 KB |
Output is correct |
10 |
Correct |
239 ms |
46216 KB |
Output is correct |
11 |
Correct |
210 ms |
46708 KB |
Output is correct |
12 |
Correct |
198 ms |
46044 KB |
Output is correct |
13 |
Correct |
223 ms |
46256 KB |
Output is correct |
14 |
Correct |
197 ms |
46660 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
19212 KB |
Output is correct |
2 |
Correct |
10 ms |
19284 KB |
Output is correct |
3 |
Correct |
10 ms |
19252 KB |
Output is correct |
4 |
Correct |
10 ms |
19300 KB |
Output is correct |
5 |
Correct |
10 ms |
19244 KB |
Output is correct |
6 |
Correct |
10 ms |
19284 KB |
Output is correct |
7 |
Correct |
10 ms |
19284 KB |
Output is correct |
8 |
Correct |
10 ms |
19284 KB |
Output is correct |
9 |
Correct |
10 ms |
19256 KB |
Output is correct |
10 |
Correct |
12 ms |
19284 KB |
Output is correct |
11 |
Correct |
10 ms |
19324 KB |
Output is correct |
12 |
Correct |
11 ms |
19300 KB |
Output is correct |
13 |
Correct |
11 ms |
19284 KB |
Output is correct |
14 |
Correct |
11 ms |
19248 KB |
Output is correct |
15 |
Correct |
10 ms |
19244 KB |
Output is correct |
16 |
Correct |
11 ms |
19284 KB |
Output is correct |
17 |
Correct |
10 ms |
19284 KB |
Output is correct |
18 |
Correct |
12 ms |
19252 KB |
Output is correct |
19 |
Correct |
10 ms |
19324 KB |
Output is correct |
20 |
Correct |
11 ms |
19252 KB |
Output is correct |
21 |
Correct |
10 ms |
19284 KB |
Output is correct |
22 |
Correct |
10 ms |
19284 KB |
Output is correct |
23 |
Correct |
10 ms |
19280 KB |
Output is correct |
24 |
Correct |
11 ms |
19412 KB |
Output is correct |
25 |
Correct |
10 ms |
19284 KB |
Output is correct |
26 |
Correct |
11 ms |
19284 KB |
Output is correct |
27 |
Correct |
11 ms |
19252 KB |
Output is correct |
28 |
Correct |
12 ms |
19284 KB |
Output is correct |
29 |
Correct |
13 ms |
19248 KB |
Output is correct |
30 |
Correct |
11 ms |
19320 KB |
Output is correct |
31 |
Correct |
12 ms |
19248 KB |
Output is correct |
32 |
Correct |
10 ms |
19284 KB |
Output is correct |
33 |
Correct |
188 ms |
45464 KB |
Output is correct |
34 |
Correct |
189 ms |
45328 KB |
Output is correct |
35 |
Correct |
176 ms |
45880 KB |
Output is correct |
36 |
Correct |
202 ms |
47120 KB |
Output is correct |
37 |
Correct |
213 ms |
45448 KB |
Output is correct |
38 |
Correct |
177 ms |
45788 KB |
Output is correct |
39 |
Correct |
201 ms |
48544 KB |
Output is correct |
40 |
Correct |
168 ms |
46892 KB |
Output is correct |
41 |
Correct |
180 ms |
45400 KB |
Output is correct |
42 |
Correct |
171 ms |
47156 KB |
Output is correct |
43 |
Correct |
171 ms |
45632 KB |
Output is correct |
44 |
Correct |
161 ms |
47068 KB |
Output is correct |
45 |
Correct |
166 ms |
47220 KB |
Output is correct |
46 |
Correct |
175 ms |
47128 KB |
Output is correct |
47 |
Correct |
173 ms |
45740 KB |
Output is correct |
48 |
Correct |
236 ms |
46672 KB |
Output is correct |
49 |
Correct |
256 ms |
46200 KB |
Output is correct |
50 |
Correct |
244 ms |
47824 KB |
Output is correct |
51 |
Correct |
254 ms |
46332 KB |
Output is correct |
52 |
Correct |
224 ms |
46700 KB |
Output is correct |
53 |
Correct |
242 ms |
46556 KB |
Output is correct |
54 |
Correct |
261 ms |
46640 KB |
Output is correct |
55 |
Correct |
213 ms |
46360 KB |
Output is correct |
56 |
Correct |
209 ms |
46088 KB |
Output is correct |
57 |
Correct |
217 ms |
47848 KB |
Output is correct |
58 |
Correct |
219 ms |
47560 KB |
Output is correct |
59 |
Correct |
243 ms |
46848 KB |
Output is correct |
60 |
Correct |
205 ms |
46460 KB |
Output is correct |
61 |
Correct |
227 ms |
46664 KB |
Output is correct |
62 |
Correct |
223 ms |
46424 KB |
Output is correct |
63 |
Correct |
243 ms |
41964 KB |
Output is correct |
64 |
Correct |
214 ms |
46068 KB |
Output is correct |
65 |
Correct |
232 ms |
47068 KB |
Output is correct |
66 |
Correct |
216 ms |
45768 KB |
Output is correct |
67 |
Correct |
246 ms |
46048 KB |
Output is correct |
68 |
Correct |
201 ms |
46152 KB |
Output is correct |
69 |
Correct |
186 ms |
46140 KB |
Output is correct |
70 |
Correct |
213 ms |
46732 KB |
Output is correct |
71 |
Correct |
205 ms |
45732 KB |
Output is correct |
72 |
Correct |
239 ms |
46216 KB |
Output is correct |
73 |
Correct |
210 ms |
46708 KB |
Output is correct |
74 |
Correct |
198 ms |
46044 KB |
Output is correct |
75 |
Correct |
223 ms |
46256 KB |
Output is correct |
76 |
Correct |
197 ms |
46660 KB |
Output is correct |
77 |
Correct |
242 ms |
47464 KB |
Output is correct |
78 |
Correct |
241 ms |
48040 KB |
Output is correct |
79 |
Correct |
239 ms |
47736 KB |
Output is correct |
80 |
Correct |
266 ms |
47456 KB |
Output is correct |
81 |
Correct |
233 ms |
47420 KB |
Output is correct |
82 |
Correct |
252 ms |
47568 KB |
Output is correct |
83 |
Correct |
227 ms |
47672 KB |
Output is correct |
84 |
Correct |
233 ms |
47512 KB |
Output is correct |
85 |
Correct |
198 ms |
48432 KB |
Output is correct |
86 |
Correct |
221 ms |
47500 KB |
Output is correct |
87 |
Correct |
191 ms |
47728 KB |
Output is correct |
88 |
Correct |
183 ms |
48332 KB |
Output is correct |
89 |
Correct |
210 ms |
46888 KB |
Output is correct |
90 |
Correct |
173 ms |
48028 KB |
Output is correct |
91 |
Correct |
204 ms |
47028 KB |
Output is correct |
92 |
Correct |
185 ms |
46756 KB |
Output is correct |
93 |
Correct |
246 ms |
46968 KB |
Output is correct |
94 |
Correct |
184 ms |
48480 KB |
Output is correct |
95 |
Correct |
204 ms |
48300 KB |
Output is correct |
96 |
Correct |
214 ms |
47160 KB |
Output is correct |
97 |
Correct |
192 ms |
47204 KB |
Output is correct |
98 |
Correct |
200 ms |
46696 KB |
Output is correct |
99 |
Correct |
198 ms |
46932 KB |
Output is correct |
100 |
Correct |
180 ms |
46764 KB |
Output is correct |
101 |
Correct |
201 ms |
46672 KB |
Output is correct |
102 |
Correct |
181 ms |
47972 KB |
Output is correct |