Submission #120726

#TimeUsernameProblemLanguageResultExecution timeMemory
120726nvmdavaSalesman (IOI09_salesman)C++17
100 / 100
563 ms29804 KiB
#include <bits/stdc++.h>
#define ff first
#define ss second
using namespace std;
#define N 500005
#define pii pair<int, int>
#define inf 2000000005
set<pii> in;
int n, u, d, s, t, l, m;

vector<pii> fair[N];

int dis(int f, int t){
	return f <= t ? (t - f) * d : (f - t) * u;
}

int query(int loc){
	int res1, res2;
	auto it = in.lower_bound({loc, -inf});
	res1 = it == in.end() ? -inf : it -> ss - dis(it -> ff, loc);
	res2 = it == in.begin() ? -inf : (--it) -> ss - dis(it -> ff, loc);
	return max(res1, res2);
}

void update(int loc, int val){
	if(query(loc) >= val) return;
	auto it = in.insert({loc, val}).ff;
	it++;
	while(it != in.end() && it -> ss <= val - dis(loc, it -> ff)) it = in.erase(it);
	it--;
	while(it != in.begin() && (--it) -> ss <= val - dis(loc, it -> ff)) it = in.erase(it);
}


vector<pii> tmp;

int main(){
	scanf("%d%d%d%d", &n, &u, &d, &s);
	for(int i = 1; i <= n; i++){
		scanf("%d%d%d", &t, &l, &m);
		fair[t].push_back({l, m});
	}
	update(s, 0);
	int sz;
	for(int i = 1; i < N; i++){
		sz = fair[i].size();
		if(sz == 0) continue;
		tmp.clear();
		sort(fair[i].begin(), fair[i].end());
		
		for(int j = 0; j < sz; j++){
			int res = fair[i][j].ss + query(fair[i][j].ff);
			tmp.push_back({res, res});
		}
		
		for(int j = 1; j < sz; j++)
			tmp[j].ff = max(tmp[j].ff, tmp[j - 1].ff - dis(fair[i][j - 1].ff, fair[i][j].ff) + fair[i][j].ss);
		for(int j = sz - 2; j >= 0; j--)
			tmp[j].ss = max(tmp[j].ss, tmp[j + 1].ss - dis(fair[i][j + 1].ff, fair[i][j].ff) + fair[i][j].ss);
		for(int j = 0; j < sz; j++)
			update(fair[i][j].ff, max(tmp[j].ff, tmp[j].ss));
	}
	printf("%d", query(s));
}

Compilation message (stderr)

salesman.cpp: In function 'int main()':
salesman.cpp:38:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d%d%d%d", &n, &u, &d, &s);
  ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~
salesman.cpp:40:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d%d%d", &t, &l, &m);
   ~~~~~^~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...