이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
#define lsb(x) (x & (-x))
#define ll long long
#define ull unsigned long long
// 217
// 44
using namespace std;
const int MAXH = (int) 1e5;
struct SegTree {
vector < pair <int, int> > aint;
int n;
inline void init(int _n) {
n = _n;
aint.resize(4 * n + 1);
}
inline void combine(pair <int, int> &a, pair <int, int> b) {
if(a.first > b.first)
a = b;
}
void update(int nod, int left, int right, int pos, int val) {
if(left == right) {
aint[nod] = {val, left};
}
else {
int mid = (left + right) / 2;
if(pos <= mid) update(2 * nod, left, mid, pos, val);
else update(2 * nod + 1, mid + 1, right, pos, val);
aint[nod] = {MAXH + 1, 0};
combine(aint[nod], aint[2 * nod]);
combine(aint[nod], aint[2 * nod + 1]);
}
}
pair <int, int> query(int nod, int left, int right, int l, int r) {
if(l <= left && right <= r) {
return aint[nod];
}
else {
int mid = (left + right) / 2;
pair <int, int> ans = {MAXH + 1, 0};
if(l <= mid) combine(ans, query(2 * nod, left, mid, l, r));
if(mid < r) combine(ans, query(2 * nod + 1, mid + 1, right, l, r));
return ans;
}
}
};
int main() {
//ifstream cin("A.in");
//ofstream cout("A.out");
int i, n;
ios::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
cin >> n;
vector <int> h(n + 1), k(n + 1), ord(n + 1);
for(i = 1; i <= n; i++) {
cin >> h[i] >> k[i];
ord[i] = i;
}
sort(next(ord.begin()), ord.end(), [&](const int &a, const int &b) {
return h[a] < h[b];
});
SegTree st; st.init(MAXH);
for(i = 1; i <= MAXH; i++) {
st.update(1, 1, MAXH, i, 0);
}
for(i = 1; i <= n; i++) {
vector < pair <int, int> > aux;
while(k[ord[i]]--) {
auto cur = st.query(1, 1, MAXH, 1, h[ord[i]]);
aux.push_back({cur.first + 1, cur.second});
st.update(1, 1, MAXH, cur.second, MAXH + 1);
}
for(auto it : aux) {
st.update(1, 1, MAXH, it.second, it.first);
}
}
ll ans = 0;
for(i = 1; i <= MAXH; i++) {
int cur = st.query(1, 1, MAXH, i, i).first;
ans += 1LL * cur * (cur - 1) / 2;
}
cout << ans;
//cin.close();
//cout.close();
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... |
# | 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... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |