#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <vector>
#include <algorithm>
#include <fstream>
#include <queue>
#include <deque>
#include <iomanip>
#include <cmath>
#include <set>
#include <stack>
#include <map>
#include <unordered_map>
#define FOR(i,n) for(int i=0;i<n;i++)
#define FORE(i,a,b) for(int i=a;i<=b;i++)
#define ll long long
#define ld long double
#define int ll
//#define int short
#define vi vector<int>
#define pb push_back
#define ff first
#define ss second
#define ii pair<int,int>
#define iii pair<int,ii>
#define iiii pair<iii,int>
#define pll pair<ll,ll>
#define plll pair<ll,pll>
//#define mp make_pair
#define vv vector
#define endl '\n'
using namespace std;
const int MAXN = 200*1000 + 5;
const int MAXT = 263*1000;
struct SegmentTree{
int st[2*MAXT];
void update(int node,int ss,int se,int pos,int val){
if(pos < ss or se < pos)return;
if(ss == se){
st[node] = val;
return;
}
int mid = (ss+se)/2;
update(node*2+1,ss,mid,pos,val);
update(node*2+2,mid+1,se,pos,val);
st[node] = st[node*2+1]+st[node*2+2];
}
int get(int node,int ss,int se,int l,int r){
if(l > se or r < ss)return 0;
if(l <= ss and se <= r)return st[node];
int mid = (ss+se)/2;
return get(node*2+1,ss,mid,l,r)+get(node*2+2,mid+1,se,l,r);
}
};
/*
stage 1: still growing towards left. : sum of elements*time
stage 2(a): end growing and stabilise: sum of elementrange.
stage 2(b): grow and decay, hence no change: sum of element range
stage 3: just decay: sum(elementrange) - sum(element size)*time
stage 4: dead. We dont really need to keep track of this.
// lets see how we can better the implementation. We have a period when there is a growth
and then we have a period where there is a decay. Stage 2(a/b) can be calculated in terms of those.
stage 4 is bleh, just kick it out.
growth_end(i) = time when growth stops.
decay_start(i) = time when decay starts.
dead(i) = when we dont need to consider it.
for every viable i, ans is Arr[i]*(min(growth_end(i),t) - max(0,t-decay_start(i)))
=> Arr[i]*(min(growth_end(i),t)) - Arr[i]*(max(0,t-decay_start(i)));
*/
int D = 0;
int n;
struct Event{
// =1 means growth_end(i) has been reached, and it wont grow anymore;
// =2 means decay has been started.
// type2 = ded.
int type;
int time;
int item;
int data; // depends on the event.
Event(int a,int b,int c,int d=0){
type = a;
time = b;
item = c;
data = d;
}
};
int prev_greater[MAXN];
int next_greater[MAXN];
int arr[MAXN];
void generateGreaters(int n){
stack<int> st1;
stack<int> st2;
FOR(i,n){
while(!st1.empty() and arr[st1.top()] < arr[i])st1.pop();
if(st1.empty())prev_greater[i] = -3*n;
else prev_greater[i] = st1.top();
st1.push(i);
while(!st2.empty() and arr[st2.top()] <= arr[n-i-1])st2.pop();
if(st2.empty())next_greater[n-i-1] = n;
else next_greater[n-i-1] = st2.top();
st2.push(n-i-1);
}
}
vv<Event> eventlist;
void formEventList(int n){
FOR(i,n){
int delta1 = next_greater[i] - i;
eventlist.pb(Event(1,delta1,i));
int delta2 = i - prev_greater[i];
delta2--;
eventlist.pb(Event(2,delta2,i));
eventlist.pb(Event(3,delta2+delta1,i));
}
sort(eventlist.begin(), eventlist.end(),[&](Event e1,Event e2){
if(e1.time == e2.time)return e1.type < e2.type;
return e1.time < e2.time;
});
}
SegmentTree segtree_expansion;// for expansion
SegmentTree segtree_stable;// when no expansion
SegmentTree segtree_decay;// when there is decay
SegmentTree segtree_decay_helper;
bool growthState[MAXN];
bool stableState[MAXN];
bool decayState[MAXN];
bool deadState[MAXN];
ll getAnsForPrefix(int x,int t){
ll cost = 0;
int xcp = x;
while(x >= 0 and xcp-prev_greater[x]<=t)x = prev_greater[x];
if(D)cout << "PREFIX: " << xcp << " " << x << endl;
cost += segtree_expansion.get(0,0,n,0,x)*(t+1);// the expansion
if(D)cout << "cost1 : " << cost << endl;
cost += segtree_stable.get(0,0,n,0,x);
if(D)cout << "cost2 : " << cost << endl;
cost -= segtree_decay.get(0,0,n,0,x)*(t) - segtree_decay_helper.get(0,0,n,0,x);
if(D)cout << "cost3 : " << cost << endl;
// now fix for the right most point;
cost -= (arr[x])*(min(max(0LL,t-(xcp-x)),(next_greater[x]-xcp-1)));
if(D)cout << "cost4 : " << cost << endl;
if(D)cout << endl;
return cost;
}
signed main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
int q;
cin >> n >> q;
FOR(i,n)cin >> arr[i];
iiii queries[q];
FOR(i,q){
int a,b,c;
cin >> a >> b >> c;
b--;c--;
queries[i] = {{a,{b,c}},i};
}
sort(queries,queries+q);
generateGreaters(n);
formEventList(n);
reverse(eventlist.begin(), eventlist.end());
//
if(D)cout << "OUTPUT DATA : " << endl;
FOR(i,n)if(D)cout << next_greater[i] << " ";if(D)cout << endl;
FOR(i,n)if(D)cout << prev_greater[i] << " ";if(D)cout << endl;
if(D)cout << "EVENTS:" << endl;
for(auto e : eventlist){
if(D)cout << e.type << " " << e.item << " " << e.time << endl;
}
if(D)cout << "ACTUAL OUTPUT " << endl;
FOR(i,n)growthState[i] = 1;
FOR(i,n)segtree_expansion.update(0,0,n,i,arr[i]);
ll ans[q];
for(auto f : queries){
auto e = f.ff;
int t = e.ff;
int a = e.ss.ff;int b = e.ss.ss;
if(D)cout << "QUERY: " << t << " " << a << " " << b << endl;
// at time t, in range a to b;
while(!eventlist.empty() and eventlist.back().time <= t){
// we have got more events to process yay !!
Event e = eventlist.back();eventlist.pop_back();
int i = e.item;
if(e.type == 1){
segtree_expansion.update(0,0,n,i,0);
segtree_stable.update(0,0,n,i,arr[i]*e.time);
growthState[i] = 0;
stableState[i] = 1;
}else if(e.type == 2){
segtree_decay.update(0,0,n,i,arr[i]);
segtree_decay_helper.update(0,0,n,i,arr[i]*e.time);
decayState[i] = 1;
}else{
// the first two are redundant
//segtree_expansion.update(0,0,n,e.item,0);
segtree_stable.update(0,0,n,i,0);
segtree_decay_helper.update(0,0,n,i,0);
segtree_decay.update(0,0,n,i,0);
decayState[i] = 0;growthState[i] =0; growthState[i] = 0;
deadState[i] = 1;
}
}
ll cost = getAnsForPrefix(b,t);
if(a != 0)cost -= getAnsForPrefix(a-1,t);
ans[f.ss] = cost;
//if(D)cout << cost << endl;
}
if(D)cout << endl;
FOR(i,q){
cout << ans[i] << endl;
}
return 0;
}
Compilation message
ho_t5.cpp: In function 'int main()':
ho_t5.cpp:16:18: warning: this 'for' clause does not guard... [-Wmisleading-indentation]
#define FOR(i,n) for(int i=0;i<n;i++)
^
ho_t5.cpp:187:2: note: in expansion of macro 'FOR'
FOR(i,n)if(D)cout << next_greater[i] << " ";if(D)cout << endl;
^~~
ho_t5.cpp:187:46: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'for'
FOR(i,n)if(D)cout << next_greater[i] << " ";if(D)cout << endl;
^~
ho_t5.cpp:16:18: warning: this 'for' clause does not guard... [-Wmisleading-indentation]
#define FOR(i,n) for(int i=0;i<n;i++)
^
ho_t5.cpp:188:2: note: in expansion of macro 'FOR'
FOR(i,n)if(D)cout << prev_greater[i] << " ";if(D)cout << endl;
^~~
ho_t5.cpp:188:46: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'for'
FOR(i,n)if(D)cout << prev_greater[i] << " ";if(D)cout << endl;
^~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
5 ms |
512 KB |
Output is correct |
3 |
Correct |
5 ms |
512 KB |
Output is correct |
4 |
Correct |
5 ms |
512 KB |
Output is correct |
5 |
Correct |
5 ms |
512 KB |
Output is correct |
6 |
Correct |
6 ms |
512 KB |
Output is correct |
7 |
Correct |
5 ms |
512 KB |
Output is correct |
8 |
Correct |
5 ms |
512 KB |
Output is correct |
9 |
Correct |
5 ms |
512 KB |
Output is correct |
10 |
Correct |
5 ms |
512 KB |
Output is correct |
11 |
Correct |
5 ms |
512 KB |
Output is correct |
12 |
Correct |
5 ms |
512 KB |
Output is correct |
13 |
Correct |
5 ms |
512 KB |
Output is correct |
14 |
Correct |
5 ms |
512 KB |
Output is correct |
15 |
Correct |
5 ms |
512 KB |
Output is correct |
16 |
Correct |
5 ms |
512 KB |
Output is correct |
17 |
Correct |
5 ms |
512 KB |
Output is correct |
18 |
Correct |
5 ms |
512 KB |
Output is correct |
19 |
Correct |
5 ms |
512 KB |
Output is correct |
20 |
Correct |
5 ms |
512 KB |
Output is correct |
21 |
Correct |
5 ms |
512 KB |
Output is correct |
22 |
Correct |
5 ms |
512 KB |
Output is correct |
23 |
Correct |
5 ms |
512 KB |
Output is correct |
24 |
Correct |
5 ms |
512 KB |
Output is correct |
25 |
Correct |
5 ms |
512 KB |
Output is correct |
26 |
Correct |
5 ms |
512 KB |
Output is correct |
27 |
Correct |
5 ms |
512 KB |
Output is correct |
28 |
Correct |
5 ms |
512 KB |
Output is correct |
29 |
Correct |
5 ms |
512 KB |
Output is correct |
30 |
Correct |
5 ms |
512 KB |
Output is correct |
31 |
Correct |
5 ms |
512 KB |
Output is correct |
32 |
Correct |
6 ms |
512 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
676 ms |
52124 KB |
Output is correct |
3 |
Correct |
679 ms |
52124 KB |
Output is correct |
4 |
Correct |
677 ms |
52380 KB |
Output is correct |
5 |
Correct |
679 ms |
52508 KB |
Output is correct |
6 |
Correct |
677 ms |
52252 KB |
Output is correct |
7 |
Correct |
691 ms |
52628 KB |
Output is correct |
8 |
Correct |
696 ms |
52820 KB |
Output is correct |
9 |
Correct |
695 ms |
52508 KB |
Output is correct |
10 |
Correct |
665 ms |
51740 KB |
Output is correct |
11 |
Correct |
689 ms |
52600 KB |
Output is correct |
12 |
Correct |
645 ms |
51652 KB |
Output is correct |
13 |
Correct |
681 ms |
52428 KB |
Output is correct |
14 |
Correct |
680 ms |
52504 KB |
Output is correct |
15 |
Correct |
690 ms |
52380 KB |
Output is correct |
16 |
Correct |
689 ms |
52380 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
687 ms |
51392 KB |
Output is correct |
3 |
Correct |
678 ms |
50844 KB |
Output is correct |
4 |
Correct |
679 ms |
51996 KB |
Output is correct |
5 |
Correct |
684 ms |
50948 KB |
Output is correct |
6 |
Correct |
669 ms |
51356 KB |
Output is correct |
7 |
Correct |
681 ms |
51480 KB |
Output is correct |
8 |
Correct |
688 ms |
51276 KB |
Output is correct |
9 |
Correct |
682 ms |
51108 KB |
Output is correct |
10 |
Correct |
669 ms |
50520 KB |
Output is correct |
11 |
Correct |
710 ms |
51868 KB |
Output is correct |
12 |
Correct |
700 ms |
51356 KB |
Output is correct |
13 |
Correct |
698 ms |
51596 KB |
Output is correct |
14 |
Correct |
680 ms |
50848 KB |
Output is correct |
15 |
Correct |
685 ms |
51612 KB |
Output is correct |
16 |
Correct |
671 ms |
51064 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
1090 ms |
49656 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
5 ms |
512 KB |
Output is correct |
3 |
Correct |
5 ms |
512 KB |
Output is correct |
4 |
Correct |
5 ms |
512 KB |
Output is correct |
5 |
Correct |
5 ms |
512 KB |
Output is correct |
6 |
Correct |
6 ms |
512 KB |
Output is correct |
7 |
Correct |
5 ms |
512 KB |
Output is correct |
8 |
Correct |
5 ms |
512 KB |
Output is correct |
9 |
Correct |
5 ms |
512 KB |
Output is correct |
10 |
Correct |
5 ms |
512 KB |
Output is correct |
11 |
Correct |
5 ms |
512 KB |
Output is correct |
12 |
Correct |
5 ms |
512 KB |
Output is correct |
13 |
Correct |
5 ms |
512 KB |
Output is correct |
14 |
Correct |
5 ms |
512 KB |
Output is correct |
15 |
Correct |
5 ms |
512 KB |
Output is correct |
16 |
Correct |
5 ms |
512 KB |
Output is correct |
17 |
Correct |
5 ms |
512 KB |
Output is correct |
18 |
Correct |
5 ms |
512 KB |
Output is correct |
19 |
Correct |
5 ms |
512 KB |
Output is correct |
20 |
Correct |
5 ms |
512 KB |
Output is correct |
21 |
Correct |
5 ms |
512 KB |
Output is correct |
22 |
Correct |
5 ms |
512 KB |
Output is correct |
23 |
Correct |
5 ms |
512 KB |
Output is correct |
24 |
Correct |
5 ms |
512 KB |
Output is correct |
25 |
Correct |
5 ms |
512 KB |
Output is correct |
26 |
Correct |
5 ms |
512 KB |
Output is correct |
27 |
Correct |
5 ms |
512 KB |
Output is correct |
28 |
Correct |
5 ms |
512 KB |
Output is correct |
29 |
Correct |
5 ms |
512 KB |
Output is correct |
30 |
Correct |
5 ms |
512 KB |
Output is correct |
31 |
Correct |
5 ms |
512 KB |
Output is correct |
32 |
Correct |
6 ms |
512 KB |
Output is correct |
33 |
Correct |
706 ms |
51740 KB |
Output is correct |
34 |
Correct |
710 ms |
52368 KB |
Output is correct |
35 |
Correct |
694 ms |
52252 KB |
Output is correct |
36 |
Correct |
691 ms |
51888 KB |
Output is correct |
37 |
Correct |
669 ms |
51612 KB |
Output is correct |
38 |
Correct |
682 ms |
51996 KB |
Output is correct |
39 |
Correct |
710 ms |
51868 KB |
Output is correct |
40 |
Correct |
696 ms |
51500 KB |
Output is correct |
41 |
Correct |
687 ms |
52124 KB |
Output is correct |
42 |
Correct |
698 ms |
51740 KB |
Output is correct |
43 |
Correct |
701 ms |
52452 KB |
Output is correct |
44 |
Correct |
701 ms |
52128 KB |
Output is correct |
45 |
Correct |
659 ms |
51160 KB |
Output is correct |
46 |
Correct |
694 ms |
52056 KB |
Output is correct |
47 |
Correct |
679 ms |
51548 KB |
Output is correct |
48 |
Correct |
707 ms |
51076 KB |
Output is correct |
49 |
Correct |
684 ms |
51612 KB |
Output is correct |
50 |
Correct |
685 ms |
52124 KB |
Output is correct |
51 |
Correct |
691 ms |
52252 KB |
Output is correct |
52 |
Correct |
691 ms |
51488 KB |
Output is correct |
53 |
Correct |
678 ms |
51484 KB |
Output is correct |
54 |
Correct |
733 ms |
50980 KB |
Output is correct |
55 |
Correct |
707 ms |
51252 KB |
Output is correct |
56 |
Correct |
735 ms |
51604 KB |
Output is correct |
57 |
Correct |
732 ms |
51100 KB |
Output is correct |
58 |
Correct |
730 ms |
52000 KB |
Output is correct |
59 |
Correct |
676 ms |
52124 KB |
Output is correct |
60 |
Correct |
679 ms |
52124 KB |
Output is correct |
61 |
Correct |
677 ms |
52380 KB |
Output is correct |
62 |
Correct |
679 ms |
52508 KB |
Output is correct |
63 |
Correct |
677 ms |
52252 KB |
Output is correct |
64 |
Correct |
691 ms |
52628 KB |
Output is correct |
65 |
Correct |
696 ms |
52820 KB |
Output is correct |
66 |
Correct |
695 ms |
52508 KB |
Output is correct |
67 |
Correct |
665 ms |
51740 KB |
Output is correct |
68 |
Correct |
689 ms |
52600 KB |
Output is correct |
69 |
Correct |
645 ms |
51652 KB |
Output is correct |
70 |
Correct |
681 ms |
52428 KB |
Output is correct |
71 |
Correct |
680 ms |
52504 KB |
Output is correct |
72 |
Correct |
690 ms |
52380 KB |
Output is correct |
73 |
Correct |
689 ms |
52380 KB |
Output is correct |
74 |
Correct |
687 ms |
51392 KB |
Output is correct |
75 |
Correct |
678 ms |
50844 KB |
Output is correct |
76 |
Correct |
679 ms |
51996 KB |
Output is correct |
77 |
Correct |
684 ms |
50948 KB |
Output is correct |
78 |
Correct |
669 ms |
51356 KB |
Output is correct |
79 |
Correct |
681 ms |
51480 KB |
Output is correct |
80 |
Correct |
688 ms |
51276 KB |
Output is correct |
81 |
Correct |
682 ms |
51108 KB |
Output is correct |
82 |
Correct |
669 ms |
50520 KB |
Output is correct |
83 |
Correct |
710 ms |
51868 KB |
Output is correct |
84 |
Correct |
700 ms |
51356 KB |
Output is correct |
85 |
Correct |
698 ms |
51596 KB |
Output is correct |
86 |
Correct |
680 ms |
50848 KB |
Output is correct |
87 |
Correct |
685 ms |
51612 KB |
Output is correct |
88 |
Correct |
671 ms |
51064 KB |
Output is correct |
89 |
Execution timed out |
1090 ms |
49656 KB |
Time limit exceeded |
90 |
Halted |
0 ms |
0 KB |
- |