#pragma GCC optimize("Ofast,no-stack-protector")
#include <bits/stdc++.h>
#define int long long
using namespace std;
#define endl "\n"
using ull=unsigned long long;
using pii=pair<int,int>;
const int dx[4] = {-1,0,1,0}, dy[4] = {0,1,0,-1};
#define OVL(x,s) for(auto y:x) cout<<y<<s; cout<<"\n";
template <typename T> istream& operator>>(istream& is, vector<T> &a) {
copy_n(istream_iterator<T>(is), a.size(), a.begin()); return is;}
#ifdef IOI
template<typename A, typename B> ostream& operator<<(ostream &os, const pair<A, B> &p) { return os << '(' << p.first << ", " << p.second << ')'; }
template<typename T_container, typename T = typename enable_if<!is_same<T_container, string>::value, typename T_container::value_type>::type> ostream& operator<<(ostream &os, const T_container &v) { os << '{'; string sep; for (const T &x : v) os << sep << x, sep = ", "; return os << '}'; }
void dbg_out() { cout << endl; }
template<typename Head, typename... Tail> void dbg_out(Head H, Tail... T) { cout << ' ' << H; dbg_out(T...); }
#define dbg(...) cout << "(" << #__VA_ARGS__ << "):", dbg_out(__VA_ARGS__);
#else
#define dbg(...) 1337;
#endif
#define pb push_back
#define F first
#define S second
#define all(v) v.begin(),v.end()
const int maxn = 1e6+100;
const int mod = 1e9+7;
template < int MOD = 1000000007, typename T = int > struct ModInt {
T val;
ModInt(T V = 0) : val(V) { val %= MOD; }
ModInt& operator += (const ModInt& rhs) {
if ((val += rhs.val) >= MOD) val -= MOD;
return *this;
}
ModInt& operator += (const T rhs) {
if ((val += rhs) >= MOD) val -= MOD;
return *this;
}
ModInt& operator -= (const ModInt& rhs) {
if ((val += MOD - rhs.val) >= MOD) val -= MOD;
return *this;
}
ModInt& operator -= (const T rhs) {
if ((val += MOD - rhs) >= MOD) val -= MOD;
return *this;
}
ModInt& operator *= (const ModInt& rhs) { val = (1ll * val * rhs.val) % MOD; return *this; }
ModInt& operator *= (const T rhs) { val = (1ll * val * rhs) % MOD; return *this; }
ModInt& operator /= (const ModInt& rhs) { return *this *= rhs.inverse(); }
ModInt& operator /= (const T rhs) { return *this *= ModInt(rhs).inverse(); }
ModInt& operator %= (const ModInt& rhs) { val %= rhs.val; return *this; }
ModInt& operator %= (const T rhs) { val %= rhs; return *this; }
ModInt& operator ++() { return *this += 1; }
ModInt& operator --() { return *this -= 1; }
ModInt operator + (const ModInt& rhs) const { ModInt res(*this); return res += rhs; }
ModInt operator + (const T rhs) const { ModInt res(*this); return res += rhs; }
ModInt operator % (const ModInt& rhs) const { ModInt res(*this); return res %= rhs; }
ModInt operator % (const T rhs) const { ModInt res(*this); return res %= rhs; }
ModInt operator - (const ModInt& rhs) const { ModInt res(*this); return res -= rhs; }
ModInt operator - (const T rhs) const { ModInt res(*this); return res -= rhs; }
ModInt operator * (const ModInt& rhs) const { ModInt res(*this); return res *= rhs; }
ModInt operator * (const T rhs) const { ModInt res(*this); return res *= rhs; }
ModInt operator / (const ModInt& rhs) const { ModInt res(*this); return res /= rhs; }
ModInt operator / (const T rhs) const { ModInt res(*this); return res /= rhs; }
ModInt& operator = (const ModInt& rhs) { val = rhs.val; return *this; }
ModInt& operator = (const T rhs) { val = rhs; return *this; }
T operator ~ () { return ~val; }
bool operator ! () { return !val; }
bool operator == (const ModInt& rhs) const { return val == rhs.val; }
bool operator == (const T rhs) const { return val == rhs; }
bool operator != (const ModInt& rhs) const { return val != rhs.val; }
bool operator != (const T rhs) const { return val != rhs; }
bool operator < (const ModInt& rhs) const { return val < rhs.val; }
bool operator < (const T rhs) const { return val < rhs; }
bool operator <= (const ModInt& rhs) const { return val <= rhs.val; }
bool operator <= (const T rhs) const { return val <= rhs; }
bool operator > (const ModInt& rhs) const { return val > rhs.val; }
bool operator > (const T rhs) const { return val > rhs; }
bool operator >= (const ModInt& rhs) const { return val >= rhs.val; }
bool operator >= (const T rhs) const { return val >= rhs; }
T operator () () const { return val; }
ModInt inverse() const { return power(MOD - 2); }
ModInt power(T n) const {
ModInt a = *this, res = 1;
while (n > 0) {
if (n & 1) res *= a;
a *= a, n >>= 1;
}
return res;
}
ModInt power(ModInt n) const {
ModInt a = *this, res = 1;
T e = n();
while (e > 0) {
if (e & 1) res *= a;
a *= a, e >>= 1;
}
return res;
}
friend ModInt operator ^ (ModInt rhs, T n) { return rhs.power(n); }
friend ModInt operator ^ (ModInt rhs, ModInt n) { return rhs.power(n); }
friend std::istream& operator>>(std::istream& is, ModInt& x) noexcept { return is >> x.val; }
friend std::ostream& operator<<(std::ostream& os, const ModInt& x) noexcept { return os << x.val; }
};
using mint = ModInt < (int)1e9+7 >;
signed main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
int n;
cin>>n;
int m;
cin>>m;
vector<vector<int>> v(m);
for(int i = 0;i<m;i++){
int t,l,r,c;
cin>>t>>l>>r>>c;
v[i] = {t,l,r,c};
}
sort(all(v));
int ans = 1e18;
for(int mask =0;mask<(1LL<<m);mask++){
vector<vector<int>> arr;
int cost = 0;
for(int j = 0;j<m;j++){
if((1LL<<j)&mask){
arr.pb(v[j]);
cost+=arr.back()[3];
}
}
vector<pair<int,int>> ranges;
ranges.pb({1,n});
int lst = 1;
for(auto x : arr){
int t = x[0],l = x[1],r = x[2];
int c = t-lst;
lst = t;
vector<pair<int,int>> rng;
for(auto x : ranges){
int a = x.F;
int b = x.S;//ranges [a,b] was infected
a-=c;
b+=c;
a = max(a,1LL);
b = min(b,n);
rng.pb({a,b});
}
ranges = rng;
sort(all(ranges));
rng.clear();
rng.pb(ranges[0]);
for(int i = 1;i!=ranges.size();i++){
if(rng.back().S>=ranges[i].F){
rng.back().S = max(rng.back().S,ranges[i].S);
}else{
rng.push_back(ranges[i]);
}
}
ranges = rng;
//now remove range [l,r]
rng.clear();
for(auto x : ranges){
if(x.S<l||x.F>r) {
rng.pb(x);
continue;
}
pair<int,int> p = x;
if(l<=p.F&&p.S<=r) continue;
if(p.F<l){
rng.pb({p.F,l-1});
}
if(p.S>r){
rng.pb({r+1,p.S});
}
}
ranges = rng;
if(ranges.empty()){
ans = min(ans,cost);
break;
}
sort(all(ranges));
rng.clear();
rng.pb(ranges[0]);
for(int i = 1;i!=ranges.size();i++){
if(rng.back().S>=ranges[i].F){
rng.back().S = max(rng.back().S,ranges[i].S);
}else{
rng.push_back(ranges[i]);
}
}
if(rng.empty()){
ans = min(ans,cost);
break;
}
ranges = rng;
}
}
if(ans==1e18) ans = -1;
cout<<ans<<endl;
}
# | 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... |