이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
typedef long long ll;
template <class T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
template<class A> ostream& operator<<(ostream &cout, vector<A> const &v) {cout << "["; for(int i = 0; i < v.size(); i++) {if (i) cout << ", "; cout << v[i];} return cout << "]";};
template<class A, class B> ostream& operator<<(ostream &cout, const pair<A,B> &x) {return cout << "(" <<x.first << ", " << x.second << ")";};
template <class T> void pv(T a, T b) {cerr << "["; for (T i = a; i != b; ++i) cerr << *i << " "; cerr << "]\n";}
void _print() {cerr << "]\n";}
template <class T, class... V> void _print(T t, V... v) {cerr << t; if (sizeof...(v)) cerr << ", "; _print(v...);}
#define debug(x...) cerr << "[" << #x << "] = ["; _print(x)
#define fi first
#define se second
#define SZ(x) (int)((x).size())
#define pii pair<ll,ll>
#define SIZE (1<<21)
#define Getchar() (pr1 == pr2 && (pr2 = (pr1 = fr) + fread(fr, 1, SIZE, stdin), pr1 == pr2) ? EOF : *pr1++)
#define Putchar(ch) (pw < SIZE ? fw[pw++] = (ch) : (fwrite(fw, 1, SIZE, stdout), fw[(pw = 0)++] = (ch)))
char fr[SIZE], *pr1 = fr, *pr2 = fr;
template<class T> inline void read(T& f) {
f = 0; T sgn = 1; char c = Getchar();
while (!isdigit(c)) {if (c == '-') sgn = -1; c = Getchar();}
while (isdigit(c)) {f = f*10+(c&15); c = Getchar();}
f *= sgn;
}
template<class T, class... V> inline void read(T& t, V&... v) {read(t); read(v...);}
const int mx = 500005;
pii a[mx];
int nxt[mx], prv[mx], las;
void unlink(int x) {
int y = nxt[x], z = prv[x];
if (y != 0) {
if (z != 0) {
prv[y] = z;
prv[z] = y;
} else {
nxt[y] = 0;
}
} else {
if (z != 0) {
las = z;
prv[z] = 0;
}
}
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
int n;
read(n);
map<ll,ll> ma;
for (int q = 0; q < n; q++) {
read(a[q].fi,a[q].se);
ma[a[q].fi] = 0;
}
sort(a, a+n);
int cur = 1;
auto it = ma.begin();
vector<ll> rma;
rma.push_back(0);
while (it != ma.end()) {
rma.push_back(it->fi);
it->se = cur++;
it++;
}
vector<ll> pref(cur+5);
int ptr = 0;
int upd = 1;
while (ptr < n) {
int pt = ptr;
ll tot = 0;
while (pt < n && a[pt].fi == a[ptr].fi) {
tot += a[pt].se;
pt++;
}
pref[upd] = pref[upd-1]+tot;
upd++;
ptr = pt;
}
vector<pii> f(cur);
for (int q = 1; q < cur; q++) {
f[q] = {pref[q]-rma[q],q};
}
sort(f.begin()+1, f.end());
for (int q = 1; q < cur; q++) {
if (q > 1) prv[f[q].se] = f[q-1].se;
if (q < cur-1) nxt[f[q].se] = f[q+1].se;
}
las = f[cur-1].se;
ll ans = LLONG_MIN;
for (int q = 0; q < cur-1; q++) {
ans = max(ans, pref[las]-rma[las]+rma[q+1]-pref[q]);
unlink(q+1);
}
cout << ans << "\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... |