Submission #217740

#TimeUsernameProblemLanguageResultExecution timeMemory
217740_7_7_Treatment Project (JOI20_treatment)C++14
39 / 100
3040 ms208112 KiB
#include <bits/stdc++.h>                                           
 
//#define int long long
//#pragma GCC optimize("Ofast")
//#pragma comment(linker, "/stack:200000000")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4")
 
 
#define file(s) freopen(s".in","r",stdin); freopen(s".out","w",stdout);
#define all(x) x.begin(), x.end()
#define sz(s) (int)s.size()
#define pb push_back
#define ppb pop_back
#define mp make_pair
#define s second
#define f first
 
 
using namespace std;
 
 
typedef pair < long long, long long > pll;    
typedef unsigned long long ull;
typedef pair < int, int > pii;
typedef vector < pii > vpii;
typedef vector < int > vi;
typedef long double ldb;  
typedef long long ll;  
typedef double db;                             
 
 
const int inf = 1e9, maxn = 4e5 + 148, mod = 1e9 + 7, N = 1e5 + 11;
const int dx[] = {1, -1, 0, 0}, dy[] = {0, 0, 1, -1}, block = 333;
const pii base = mp(1171, 3307), Mod = mp(1e9 + 7, 1e9 + 9);
const db eps = 1e-12, pi = 3.14159265359;
const ll INF = 1e18;


ll d[N];
vi xx, toDel;
int n, m, x[N];
set < pii > T[N << 2][2];
set < pair < ll, int > > q;


struct project {
    ll c;
	int t, l, r;
} a[N];

void add (int pos, int lvl, pii x, int v = 1, int tl = 0, int tr = sz(xx) - 1) {
	T[v][lvl].insert(x);
	if (tl == tr)
		return;

	int tm = (tl + tr) >> 1;
	if (pos <= tm)
		add(pos, lvl, x, v << 1, tl, tm);
	else
		add(pos, lvl, x, v << 1 | 1, tm + 1, tr);
}

void del (int pos, int lvl, pii x, int v = 1, int tl = 0, int tr = sz(xx) - 1) {
	T[v][lvl].erase(x);
	if (tl == tr)
		return;
	
	int tm = (tl + tr) >> 1;
	if (pos <= tm)
		del(pos, lvl, x, v << 1, tl, tm);
	else
		del(pos, lvl, x, v << 1 | 1, tm + 1, tr);
}

void f (int l, int r, int lvl, int X, ll Y, int v = 1, int tl = 0, int tr = sz(xx) - 1) {
	if (l > r || l > tr || tl > r)
		return;

	if (l <= tl && tr <= r) {		
		auto it = T[v][lvl].begin();            
		while (it != T[v][lvl].end()  && it->f <= X) {
			toDel.pb(it->s);
			++it;
		}

		while (sz(toDel)) {
		    int i = toDel.back();
		    toDel.ppb();
		    
			d[i] = Y + a[i].c;
			q.insert({d[i], i});
			del(x[i], 0, {a[i].l + a[i].t, i});
			del(x[i], 1, {a[i].l - a[i].t, i});
		}
		return;
	}

	int tm = (tl + tr) >> 1;
	f(l, r, lvl, X, Y, v << 1, tl, tm);
	f(l, r, lvl, X, Y, v << 1 | 1, tm + 1, tr);
}
	


main () {
    memset(d, -1, sizeof(d));
	scanf("%d%d", &n, &m);
	for (int i = 1; i <= m; ++i) {
		scanf("%d%d%d%lld", &a[i].t, &a[i].l, &a[i].r, &a[i].c);
		++a[i].r;
		xx.pb(a[i].t);
	}
	
	sort(all(xx));
	xx.resize(unique(all(xx)) - xx.begin());
	
    for (int i = 1; i <= m; ++i) {
        x[i] = lower_bound(all(xx), a[i].t) - xx.begin();
        if (a[i].l == 1) {
            d[i] = a[i].c;
            q.insert({d[i], i});
        } else {
            add(x[i], 0, {a[i].l + a[i].t, i});
            add(x[i], 1, {a[i].l - a[i].t, i});
        }        
    }


	while (sz(q)) {
		int v = q.begin()->s;
		q.erase(q.begin());
		
		f(x[v], m - 1, 0, a[v].r + a[v].t, d[v]);
		f(0, x[v], 1, a[v].r - a[v].t, d[v]);
	}
	
		
	ll mn = INF;	
	for (int i = 1; i <= m; ++i)
		if (a[i].r == n + 1 && d[i] > -1)
			mn = min(mn, d[i]);

	if (mn == INF)
		mn = -1;
	printf("%lld\n", mn);
}




Compilation message (stderr)

treatment.cpp:105:7: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
 main () {
       ^
treatment.cpp: In function 'int main()':
treatment.cpp:107:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d%d", &n, &m);
  ~~~~~^~~~~~~~~~~~~~~~
treatment.cpp:109:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d%d%d%lld", &a[i].t, &a[i].l, &a[i].r, &a[i].c);
   ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...