This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
//#include "railroad.h"
using namespace std;
const int INF = 1e9;
struct SegTree {
struct Node {
int ma, sum;
Node(int m1, int m2) : ma(m1), sum(m2) {}
Node(int v) : ma(v), sum(v) {}
Node() : ma(INF),sum(0) {}
};
vector<Node> seg;
int MAX;
SegTree(int N) {
int i;
for(i=1;2*i<N;i*=2) {}
seg.resize(i);
MAX = i;
}
Node f(Node x, Node y) {
return Node(max(x.ma,y.ma),x.sum+y.sum);
}
void cons() {
for(int i = MAX/2-1; i>0; i--) {
seg[i] = f(seg[2*i],seg[2*i+1]);
}
}
Node sum(int s, int e, int n, int ns, int ne) {
if(e<=ns||ne<=s) return Node();
if(s<=ns&&ne<=e) return seg[n];
int mid = ns + ne >> 1;
return f(sum(s,e,2*n,ns,mid),sum(s,e,2*n+1,mid,ne));
}
void update(int n) {
n += MAX/2;
n /= 2;
while(n) {
seg[n] = f(seg[2*n],seg[2*n+1]);
n /= 2;
}
}
void Plus(int n, int a) {
int b = seg[n+MAX/2].ma;
seg[n+MAX/2] = Node(b+a);
update(n);
}
void Edit(int n, int a) {
seg[n+MAX/2] = Node(a);
update(n);
}
Node sum(int s, int e) {
if(s >= e) return Node();
return sum(s, e, 1, 0, MAX/2);
}
int FindLeft(int s, int e) {
if(s + 1 == e) {
if(seg[s+MAX/2].ma==1) return s;
else return INF;
}
int mid = s + e >> 1;
if(sum(s, mid).ma >= 1) return FindLeft(s, mid);
return FindLeft(mid, e);
}
};
typedef pair<int,int> P;
int getidx(P n, vector<P> V) {
return lower_bound(V.begin(),V.end(),n) - V.begin();
}
long long plan_roller_coaster(vector<int> s,vector<int> t) {
int N = s.size();
int i;
vector<P> V;
for(i=0;i<N;i++) {
V.push_back(P(s[i],i));
}
vector<int> r;
r.resize(N);
for(i=0;i<N;i++) {
r[V[i].second] = i;
}
sort(V.begin(),V.end());
SegTree tree(N + 5);
int MAX = tree.MAX;
for(i=0;i<N;i++) {
tree.seg[i+MAX/2].ma = 1;
tree.seg[i+MAX/2].sum = 1;
}
//cout << "Yes";
tree.cons();
int pt = 0;
for(i=0;i<N-1;i++) {
tree.Plus(pt, -1);
//cout << "Minus";
int s = tree.FindLeft(pt, N);
//cout << "Find";
if(s == INF) return 1;
int e = r[s];
pt = getidx(P(t[e],-1),V);
}
return 0;
}
Compilation message (stderr)
railroad.cpp: In member function 'SegTree::Node SegTree::sum(int, int, int, int, int)':
railroad.cpp:31:22: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
31 | int mid = ns + ne >> 1;
| ~~~^~~~
railroad.cpp: In member function 'int SegTree::FindLeft(int, int)':
railroad.cpp:60:21: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
60 | int mid = s + e >> 1;
| ~~^~~
# | 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... |