#include <iostream>
#include <set>
#define endl '\n'
typedef long long ll;
struct slope_trick
{
const int inf = 1e9;
std::multiset < ll > left_set, right_set;
ll offset, height;
slope_trick()
{
offset = 0;
height = 0;
}
void add_basic(int v) /// adds |x - v|
{
ll left = -inf;
ll right = inf;
if (!left_set.empty())
left = *left_set.rbegin() + offset;
if (!right_set.empty())
right = *right_set.begin() + offset;
if (v >= left && v <= right) /// function on base
{
left_set.insert(v - offset);
right_set.insert(v - offset);
}
else
if (v < left)
{
left_set.erase(left_set.find(left));
right_set.insert(left);
left_set.insert(v - offset);
left_set.insert(v - offset);
height = height + abs(left - v);
}
else
if (v > right)
{
///std::cout << v << " " << right << endl;
right_set.erase(right_set.find(right));
left_set.insert(right);
right_set.insert(v - offset);
right_set.insert(v - offset);
height = height + abs(right - v);
}
}
void print_slope_trick()
{
std::cout << "left_slope" << endl;
for (int cor : left_set)
std::cout << cor << " ";
std::cout << endl;
std::cout << "right_slope" << endl;
for (int cor : right_set)
std::cout << cor << " ";
std::cout << endl;
std::cout << "height " << height << endl;
}
ll get_right()
{
if (right_set.empty())
return inf;
return *right_set.begin();
}
ll get_left()
{
if (left_set.empty())
return -inf;
return *left_set.rbegin();
}
void prefix_minimum()
{
while(left_set.size() > 0)
{
int val = *left_set.begin();
if (val >= 0)
break;
left_set.erase(left_set.find(val));
}
ll slope = 0, base = height, last = get_right();
while (get_right() <= 0)
{
ll distance = get_right() - last;
base = base + distance * slope;
slope ++;
last = last + distance;
right_set.erase(right_set.begin());
}
if (slope != 0)
{
base = base + (- last) * slope;
right_set.insert(offset);
}
height = base;
right_set.clear();
///right_set.clear();
}
ll query_vlaue(ll debit)
{
ll last = get_left(), slope = 0, base = height;
while(last > debit)
{
ll distance = last - get_left();
base = base + distance * slope;
slope ++;
last = last - distance;
left_set.erase(left_set.find(*left_set.rbegin()));
}
if (slope != 0)
{
base = base + (last - debit) * slope;
}
return base;
}
};
const int maxn = 500010;
int n;
ll a[maxn], b[maxn], x[maxn], pref[maxn];
void solve()
{
std::cin >> n;
for (int i = 1; i <= n; i ++)
{
std::cin >> a[i] >> b[i];
x[i] = a[i] - b[i];
pref[i] = pref[i - 1] + x[i];
}
slope_trick st;
for (int i = 1; i <= n; i ++)
{
st.add_basic(pref[i]);
st.prefix_minimum();
//std::cout << "--------------------" << endl;
//st.print_slope_trick();
}
///std::cout << "dasdas" << endl;
ll ans = st.query_vlaue(pref[n]);
///std::cout << "-----------------" << endl;
std::cout << ans << endl;
}
int main()
{
solve();
return 0;
}
Compilation message
bulves.cpp: In member function 'void slope_trick::print_slope_trick()':
bulves.cpp:64:9: warning: this 'for' clause does not guard... [-Wmisleading-indentation]
64 | for (int cor : left_set)
| ^~~
bulves.cpp:66:13: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'for'
66 | std::cout << endl;
| ^~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
2 ms |
468 KB |
Output is correct |
3 |
Correct |
2 ms |
340 KB |
Output is correct |
4 |
Correct |
25 ms |
3160 KB |
Output is correct |
5 |
Correct |
61 ms |
6164 KB |
Output is correct |
6 |
Correct |
206 ms |
22812 KB |
Output is correct |
7 |
Correct |
276 ms |
24100 KB |
Output is correct |
8 |
Execution timed out |
1086 ms |
20848 KB |
Time limit exceeded |
9 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
2 ms |
468 KB |
Output is correct |
3 |
Correct |
2 ms |
340 KB |
Output is correct |
4 |
Correct |
25 ms |
3160 KB |
Output is correct |
5 |
Correct |
61 ms |
6164 KB |
Output is correct |
6 |
Correct |
206 ms |
22812 KB |
Output is correct |
7 |
Correct |
276 ms |
24100 KB |
Output is correct |
8 |
Execution timed out |
1086 ms |
20848 KB |
Time limit exceeded |
9 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
2 ms |
468 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
452 KB |
Output is correct |
6 |
Correct |
2 ms |
468 KB |
Output is correct |
7 |
Correct |
2 ms |
468 KB |
Output is correct |
8 |
Correct |
2 ms |
468 KB |
Output is correct |
9 |
Correct |
2 ms |
468 KB |
Output is correct |
10 |
Correct |
2 ms |
468 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
2 ms |
468 KB |
Output is correct |
3 |
Correct |
2 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
452 KB |
Output is correct |
7 |
Correct |
2 ms |
468 KB |
Output is correct |
8 |
Correct |
2 ms |
468 KB |
Output is correct |
9 |
Correct |
2 ms |
468 KB |
Output is correct |
10 |
Correct |
2 ms |
468 KB |
Output is correct |
11 |
Correct |
2 ms |
468 KB |
Output is correct |
12 |
Correct |
2 ms |
468 KB |
Output is correct |
13 |
Correct |
3 ms |
584 KB |
Output is correct |
14 |
Correct |
3 ms |
596 KB |
Output is correct |
15 |
Correct |
3 ms |
524 KB |
Output is correct |
16 |
Execution timed out |
1070 ms |
468 KB |
Time limit exceeded |
17 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
2 ms |
468 KB |
Output is correct |
3 |
Correct |
2 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
452 KB |
Output is correct |
7 |
Correct |
2 ms |
468 KB |
Output is correct |
8 |
Correct |
2 ms |
468 KB |
Output is correct |
9 |
Correct |
2 ms |
468 KB |
Output is correct |
10 |
Correct |
2 ms |
468 KB |
Output is correct |
11 |
Correct |
25 ms |
3160 KB |
Output is correct |
12 |
Correct |
61 ms |
6164 KB |
Output is correct |
13 |
Correct |
206 ms |
22812 KB |
Output is correct |
14 |
Correct |
276 ms |
24100 KB |
Output is correct |
15 |
Execution timed out |
1086 ms |
20848 KB |
Time limit exceeded |
16 |
Halted |
0 ms |
0 KB |
- |