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 "aliens.h"
#include <bits/stdc++.h>
#include <bits/extc++.h>
#define StarBurstStream ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#define iter(a) a.begin(), a.end()
#define riter(a) a.rbegin(), a.rend()
#define lsort(a) sort(iter(a))
#define gsort(a) sort(riter(a))
#define pb(a) push_back(a)
#define eb(a) emplace_back(a)
#define pf(a) push_front(a)
#define ef(a) emplace_front(a)
#define pob pop_back()
#define pof pop_front()
#define mp(a, b) make_pair(a, b)
#define F first
#define S second
#define mt make_tuple
#define gt(t, i) get<i>(t)
#define iceil(a, b) (((a) + (b) - 1) / (b))
#define tomax(a, b) ((a) = max((a), (b)))
#define tomin(a, b) ((a) = min((a), (b)))
#define topos(a) ((a) = (((a) % MOD + MOD) % MOD))
#define uni(a) a.resize(unique(iter(a)) - a.begin())
#define printv(a, b) {bool pvaspace=false; \
for(auto pva : a){ \
if(pvaspace) b << " "; pvaspace=true;\
b << pva;\
}\
b << "\n";}
using namespace std;
using namespace __gnu_pbds;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using pdd = pair<ld, ld>;
using tiii = tuple<int, int, int>;
const ll MOD = 1000000007;
const ll MAX = 2147483647;
template<typename A, typename B>
ostream& operator<<(ostream& o, pair<A, B> p){
return o << '(' << p.F << ',' << p.S << ')';
}
struct Line{
ll a, b, t;
Line(ll a = 0, ll b = 1LL << 60, ll t = 0): a(a), b(b), t(t) {}
pll operator()(ll x){
return mp(a * x + b, t);
}
};
struct Node{
Line ln;
int l = -1, r = -1;
};
int n, m;
struct LiChaoTree{
vector<Node> st;
int ts = 0;
int build(int l = 0, int r = m - 1){
int id = ts++;
if(l == r) return id;
int mid = (l + r) / 2;
st[id].l = build(l, mid);
st[id].r = build(mid + 1, r);
return id;
}
void modify(Line ln, int L = 0, int R = m - 1, int id = 0){
int M = (L + R) / 2;
// cerr << "modify " << ln(M) << " " << st[id].ln(M) << " " << L << " " << R << "\n";
if(ln(M) <= st[id].ln(M)) swap(ln, st[id].ln);
if(L == R) return;
if(ln.a >= st[id].ln.a) modify(ln, L, M, st[id].l);
else modify(ln, M + 1, R, st[id].r);
}
pll query(int pos, int L = 0, int R = m - 1, int id = 0){
if(L == R) return st[id].ln(pos);
int M = (L + R) / 2;
if(pos <= M) return min(st[id].ln(pos), query(pos, L, M, st[id].l));
else return min(st[id].ln(pos), query(pos, M + 1, R, st[id].r));
}
};
vector<pll> a;
pll operator+(pll a, pll b){
return mp(a.F + b.F, a.S + b.S);
}
pll calc(ll p){
// cerr << "calc " << p << "\n";
vector<pll> dp(n);
LiChaoTree st;
st.st.resize(2 * m);
st.build();
// cerr << "add " << -2 * a[0].F << " " << a[0].F * a[0].F << " " << 0 << "\n";
st.modify(Line(-2 * a[0].F, a[0].F * a[0].F, 0));
for(int i = 0; i < n; i++){
pll tmp = st.query(a[i].S);
// cerr << i << " " << tmp << "\n";
dp[i] = tmp + mp(a[i].S * a[i].S + p, 1);
if(i == n - 1) continue;
// cerr << "add " << -2 * a[i + 1].F << " " << a[i + 1].F * a[i + 1].F + dp[i].F - max(0LL, a[i].S - a[i + 1].F) * max(0LL, a[i].S - a[i + 1].F) << " " << dp[i].S << "\n";
st.modify(Line(-2 * a[i + 1].F,
a[i + 1].F * a[i + 1].F + dp[i].F - max(0LL, a[i].S - a[i + 1].F) * max(0LL, a[i].S - a[i + 1].F),
dp[i].S));
}
// printv(dp, cerr);
return dp[n - 1];
}
ll take_photos(int N, int M, int k, vector<int> R, vector<int> C){
n = N;
m = M;
multiset<int> st;
vector<vector<int>> e(m);
for(int i = 0; i < n; i++){
if(R[i] <= C[i]){
e[R[i]].eb(MAX);
e[C[i]].eb(R[i]);
}
else{
e[C[i]].eb(MAX);
e[R[i]].eb(C[i]);
}
}
for(int i = 0; i < m; i++){
gsort(e[i]);
for(int j : e[i]){
if(j == MAX) st.insert(i);
else{
st.erase(st.find(j));
if(!st.empty() && *st.begin() <= j) continue;
a.eb(mp(j - 1, i));
}
}
}
n = a.size();
// printv(a, cerr);
if(calc(0).S <= k){
// cerr << calc(0) << "\n";
return calc(0).F;
}
ll l = 0, r = 1LL << 60;
while(l < r){
ll mid = (l + r) / 2;
if(calc(mid).S <= k) r = mid;
else l = mid + 1;
}
// cerr << l << " " << calc(l) << "\n";
return calc(l).F - k * l;
}
# | 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... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |