# | 제출 시각 | 아이디 | 문제 | 언어 | 결과 | 실행 시간 | 메모리 |
---|---|---|---|---|---|---|---|
49892 | daniel_02 | Construction of Highway (JOI18_construction) | C++17 | 735 ms | 16196 KiB |
이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
#define ll long long
#define pb push_back
#define ppb pop_back
#define pf push_front
#define ppf pop_front
using namespace std;
const int N = 4000 + 7;
const int inf = 4000+7;
int a[N], pr[N];
int t[N];
int cnt;
deque<int>dq;
struct con
{
con * l; con * r;
int cnt;
con()
{
l = NULL; r = NULL;
cnt = 0;
}
};
con * root;
ll create_and_get(int x)
{
con * cur = root;
ll ans = 0;
for (int i = 25; i >= 0; i--)
{
if (x & (1 << i))
{
if (cur -> r == NULL)cur -> r = new con();
cur = cur -> r;
}
else
{
if (cur -> l == NULL)cur -> l = new con();
if (cur -> r != NULL)ans += cur -> r -> cnt;
cur = cur -> l;
}
cur -> cnt ++;
}
return ans;
}
void dfs(int v)
{
while (v != 0) {
dq.pf(a[v]);
v = pr[v];
}
}
void conquer(int v)
{
ll ans = 0;
dfs(v);
cnt = 1;
root = new con();
for (int i = 0; i < dq.size(); i++)
{
ans += create_and_get(dq[i]);
}
delete root;
// for (int i = 0; i < dq.size(); i++)
// update(1, 1, inf, dq[i], -inf);
dq.clear();
printf("%lld\n", ans);
}
void upd(int v, int x)
{
while (v != 0) {
a[v] = x;
v = pr[v];
}
}
vector <int> vec;
main()
{
int n;
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
scanf("%d", &a[i]);
vec.pb(a[i]);
}
sort(vec.begin(),vec.end());
vec.erase(unique(vec.begin(),vec.end()),vec.end());
for (int i = 1; i <= n; i ++) {
a[i] = lower_bound(vec.begin(),vec.end(),a[i]) - vec.begin() + 1;
}
for (int i = 1; i < n; i++)
{
int q, w;
scanf("%d%d", &q, &w);
if (i == 1)
{
puts("0");
pr[w] = q;
a[q] = a[w];
continue;
}
conquer(q);
upd(q, a[w]);
pr[w] = q;
}
}
컴파일 시 표준 에러 (stderr) 메시지
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |