//
#ifndef __SIZEOF_INT128__
#define __SIZEOF_INT128__
#endif
#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace chrono;
using namespace __gnu_pbds;
template <typename T> using oset = tree<T, null_type, less_equal<T>, rb_tree_tag, tree_order_statistics_node_update>;
#define rep(i, p, k) for(int i(p); i < (k); ++i)
#define per(i, p, k) for(int i(p); i > (k); --i)
#define sz(x) (int)(x).size()
#define sc static_cast
typedef long long ll;
typedef long double ld;
typedef unsigned int uint;
typedef unsigned long long ull;
typedef __int128_t lll;
//#define int ll
template <typename T = int> using par = std::pair <T, T>;
#define fi first
#define se second
#define test int _number_of_tests(in()); while(_number_of_tests--)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define pb emplace_back
struct Timer {
string name{""};
time_point<high_resolution_clock> end, start{high_resolution_clock::now()};
duration<float, std::milli> dur;
Timer() = default;
Timer(string nm): name(nm) {}
~Timer() {
end = high_resolution_clock::now(); dur= end - start;
cout << "@" << name << "> " << dur.count() << " ms" << '\n';
}
};
template <typename T = int> inline T in()
{
static T x;
std::cin >> x;
return x;
}
std::string yn(bool b)
{
if(b) return "YES\n";
else return "NO\n";
}
template <typename F, typename S> std::ostream& operator<<(std::ostream& out, const std::pair <F, S>& par);
template <typename T> std::ostream& operator<< (std::ostream& out, const std::vector <T>& wek)
{
for(const auto& i : wek)out << i << ' ';
return out;
}
template <typename F, typename S> std::ostream& operator<<(std::ostream& out, const std::pair <F, S>& par)
{
out << '{'<<par.first<<", "<<par.second<<"}";
return out;
}
#define show(x) cerr << #x << " = " << x << '\n';
struct prt{ int a, b, c; };
vector <prt> v;
int t;
constexpr int maxn = 2e5 + 3;
vector <int> zp[maxn];
vector <ll> pr;
struct kult
{
int b, c, i;
};
bool operator<(kult a, kult b){return a.b < b.b;}
int n;
bool da(int x)
{
// cerr << "da(" << x << ")\n";
// rep(i, 1, n+1)cerr << pr[i] << ' ';
// cerr << '\n';
for(auto odw: {pr[t]-x, pr[t]-x+1}){
vector <pair <int, int>> zm;
if(odw < 0)continue;
ll us(0);
priority_queue <kult> kul;
vector <ll> pr2(n+1);
bool d(1);
rep(i, 1, t+1){
for(auto j: zp[i])kul.push(kult{v[j].b, v[j].c, j});
int z((pr[i] + odw - x - 2*us + 1)/2);
if(i == t)z = odw - us;
// cerr << i << ": " << z << '\n';
while(z > 0){
if(!sz(kul))goto nie;
auto [b, c, j] = kul.top(); kul.pop();
if(c > z){
us += z;
zm.pb(j, z);
kul.push(kult{b, c-z, j});
z = 0;
}
else{
us += c;
zm.pb(j, c);
z -= c;
}
}
// cerr << "zm: " << zm << '\n';
}
for(auto [i,c]: zm){
pr2[1] += c;
pr2[v[i].a] -= 2*c;
pr2[v[i].b+1] += 2*c;
}
rep(i, 1, n+1)pr2[i] += pr2[i-1];
rep(i, 1, n+1)d &= pr2[i] + pr[i] <= x;
if(d)return 1;
nie:;
}
return 0;
}
std::int32_t main()
{
std::cout.tie(nullptr); //for luck
std::cin.tie(nullptr); std::ios_base::sync_with_stdio(0);
cin >> n;
int m(in());
rep(i, 0, m){
int a(in()), b(in());
v.pb(prt{min(a, b), max(a,b)-1, in()});
}
pr.resize(n+1);
rep(i, 0, m){
pr[v[i].a] += v[i].c;
pr[v[i].b+1] -= v[i].c;
}
rep(i, 1, n+1)pr[i] += pr[i-1];
rep(i, 1, n+1)if(pr[i] >= pr[t])t = i;
rep(i, 0, m)if(v[i].a <= t && t <= v[i].b)zp[v[i].a].pb(i);
int p(0), k(pr[t]);
while(k > p){
int s((p+k)/2);
if(da(s))k = s;
else p = s+1;
}
cout << p << '\n';
return 0;
}
# | 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... |