-
Notifications
You must be signed in to change notification settings - Fork 0
/
Gas Station.cpp
91 lines (83 loc) · 2.15 KB
/
Gas Station.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#define ls (p<<1)
#define rs (p<<1|1)
#define lson ls,l,m
#define rson rs,m+1,r
class Solution {
public:
vector< pair<int,int> > tr;
vector<int> diff;
vector<int> sum;
int len;
int Min(int a,int b){
return a<b?a:b;
}
void init(int p,int l,int r){
tr[p].second=0;
if(l==r){
if(l<=len) tr[p].first=sum[l];
else tr[p].first=0;
return;
}
int m=(l+r)>>1;
init(lson);
init(rson);
tr[p].first=Min(tr[ls].first,tr[rs].first);
}
void push_down(int p){
tr[ls].first += tr[p].second;
tr[ls].second += tr[p].second;
tr[rs].first += tr[p].second;
tr[rs].second += tr[p].second;
}
void update(int p,int l,int r,int a,int b,int x){
if(l==a && r==b){
tr[p].first+=x;
tr[p].second+=x;
return;
}
if(tr[p].second !=0 ){
push_down(p);
tr[p].second = 0;
}
int m=(l+r)>>1;
if(b<=m) update(lson,a,b,x);
else if(a>m) update(rson,a,b,x);
else{
update(lson,a,m,x);
update(rson,m+1,b,x);
}
tr[p].first=Min(tr[ls].first,tr[rs].first);
}
void insert(int p,int l,int r,int a,int x){
if(l==r){
tr[p].first = x;
return;
}
if(tr[p].second !=0 ){
push_down(p);
tr[p].second = 0;
}
int m=(l+r)>>1;
if(a<=m) insert(lson,a,x);
else insert(rson,a,x);
tr[p].first=Min(tr[ls].first,tr[rs].first);
}
int canCompleteCircuit(vector<int> &gas, vector<int> &cost) {
len=gas.size();
tr.assign(len*8,make_pair(0,0));
diff.assign(len+1,0);
sum.assign(len+1,0);
for(int i=0;i<len;i++){
diff[i+1]=gas[i]-cost[i];
sum[i+1]=sum[i]+diff[i+1];
}
init(1,1,2*len);
int ans=0;
while(tr[1].first<0 && ans<len){
update(1,1,2*len,ans+1,ans+len,-diff[ans+1]);
insert(1,1,2*len,len+ans+1,sum[len]);
ans++;
}
return ans==len?-1:ans;
}
};