답안 #1113137

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1113137 2024-11-15T21:14:33 Z FIFI_cpp 나일강 (IOI24_nile) C++17
100 / 100
102 ms 24260 KB
#include <bits/stdc++.h>
#include <iostream>
#include <vector>
#include <algorithm>
#include <numeric>
#include <cstdlib>
#include <cmath>
#include <queue>
#include <stack>
#include <deque>
#include <fstream>
#include <iterator>
#include <set>
#include <map>
#include <unordered_map>
#include <iomanip>
#include <cctype>
#include <string>
#include <cassert>
#include <set>
#include <bitset>
#include <unordered_set>
#include <numeric>

#define all(a) a.begin(), a.end()
#define fast ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define pb push_back
#define ppi pair<int,pair<int,int>>
//#define int long long
#define ll long long

using namespace std;
//    /\_/\
//   (= ._.)
//   / >  \>
// encouraging cat
const ll INF = 10000000000000000;
const int mod = 1000000007;
//const int mod = 998244353;
const int MAXN = 200005;
//ifstream fin('xor.in');
//ofstream fout('xor.out');
ll curr_res = 0;

struct Index {
    ll a,b,w;
};
struct Node {
    ll parent, min_value, min_index, size, sb, l, r, minEven, minOdd;
};
vector<Index> vals;
vector<Node> info;
vector<pair<ll,ll>> e;
bool cmp(const Index &x, const Index &y) { return x.w < y.w; }
ll find_parent(ll x)
{
    if (x == info[x].parent)
    {
        return x;
    }
    return info[x].parent = find_parent(info[x].parent);
}
void remove(Node x, int ix)
{
    if (x.size == 1)
    {
        curr_res -= vals[ix].a;
    }
    else if (x.size % 2 == 0)
    {
        curr_res -= x.sb;
    }
    else
    {
        curr_res -= x.sb + min(x.min_value, x.minEven);
    }
}
void add(Node x)
{
    if (x.size % 2 == 0)
    {
        curr_res += x.sb;
    }
    else
    {
        curr_res += x.sb + min(x.min_value, x.minEven);
    }
}
pair<Node, Node> merge(Node x,Node y, ll ix, ll iy)
{
    y.parent = ix;
    if (y.min_value < x.min_value)
    {
        x.min_value = y.min_value;
        x.min_index = y.min_index;
    }
    if (x.l < y.l)
    {
        if (x.size % 2 == 1)
        {
            //ll minEven = x.minEven;
            x.minEven = min(x.minEven, y.minOdd);
            x.minOdd = min(x.minOdd, y.minEven);
        }
        else
        {
            x.minEven = min(x.minEven, y.minEven);
            x.minOdd = min(x.minOdd, y.minOdd);
        }
    }
    else
    {
        if (y.size % 2 == 0)
        {
            x.minEven = min(y.minEven, x.minEven);
            x.minOdd = min(y.minOdd, x.minOdd);
        }
        else
        {
            ll minEven = x.minEven;
            x.minEven = min(y.minEven, x.minOdd);
            x.minOdd = min(y.minOdd, minEven);
        }
    }
    x.size += y.size;
    x.sb += y.sb;
    x.l = min(x.l, y.l);
    x.r = max(x.r,y.r);
    return {x,y};
}
void onion (ll x, ll y)
{
    x = find_parent(x);
    y = find_parent(y);
    if (x == y)
        return;
    remove(info[x], x);
    remove(info[y], y);
    if (info[y].size > info[x].size)
    {
        swap(x,y);
    }
    pair<Node, Node> m = merge(info[x], info[y], x, y);
    info[x] = m.first;
    info[y] = m.second;
    add(info[x]);
}
Node set_best(Node x, ll index)
{
    remove(x, index);
    if (vals[index].a - vals[index].b < x.min_value)
    {
        x.min_value = vals[index].a - vals[index].b;
        x.min_index = index;
    }
    add(x);
    return x;
}
std::vector<ll> calculate_costs(std::vector<int> W, std::vector<int> A, std::vector<int> B, std::vector<int> E)
{
    ll n = A.size();
    info.resize(n);
    vals.resize(n);
    for (ll i = 0;i < n;i++)
    {
        vals[i].a = A[i];
        vals[i].b = B[i];
        vals[i].w = W[i];
    }
    sort(all(vals), cmp);
    for (ll i = 0;i < n;i++)
    {
        info[i].l = i;
        info[i].r = i;
        info[i].parent = i;
        info[i].min_index = i;
        info[i].min_value = INF;
        info[i].sb = vals[i].b;
        info[i].size = 1;
        info[i].minEven = vals[i].a - vals[i].b;
        info[i].minOdd = INF;
        curr_res += vals[i].a;
    }
    ll q = E.size();
    e.resize(q);
    for (ll i = 0;i < q;i++)
    {
        e[i].first = E[i];
        e[i].second = i;
    }
    sort(all(e));
    vector<pair<ll,ll>> diff;
    for (ll i = 0;i < n - 1;i++)
    {
        diff.push_back({vals[i + 1].w - (ll)vals[i].w, -i - 1});
        if (i < n - 2)
        {
            diff.pb({vals[i + 2].w - (ll)vals[i].w, i + 1});
        }
    }
    sort(all(diff));
    vector<ll> res(q);
    ll e_index = 0;
    info[0].min_value = vals[0].a - vals[0].b;
    info[n - 1].min_value = vals[n - 1].a - vals[n - 1].b;
    for (ll i = 0;i < diff.size();i++)
    {
        if (diff[i].first <= e[e_index].first)
        {
            if (diff[i].second < 0)
            {
                int mergei = diff[i].second * -1 - 1;
                ll a = mergei;
                ll b = mergei + 1;
                onion(a,b);
            }
            else
            {
                diff[i].second--;
                int nd = find_parent(diff[i].second + 1);
                info[nd] = set_best(info[nd], diff[i].second + 1);
            }
        }
        else if (e_index < q)
        {
            //cout << "here" << '\n';
            //cout << info[n - 2].size << '\n';
            //cout << info[n - 2].min_value + info[n - 2].sb << '\n';
            res[e[e_index].second] = curr_res;
            e_index++;
            i--;
        }
        
    }
    for (; e_index < q;e_index++)
    {
        res[e[e_index].second] = curr_res;
    }
    //cout << info[n - 2].size << '\n';
    //cout << info[n - 2].min_value << '\n';
    return res;
}

Compilation message

nile.cpp:33:1: warning: multi-line comment [-Wcomment]
   33 | //    /\_/\
      | ^
nile.cpp: In function 'std::vector<long long int> calculate_costs(std::vector<int>, std::vector<int>, std::vector<int>, std::vector<int>)':
nile.cpp:206:21: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  206 |     for (ll i = 0;i < diff.size();i++)
      |                   ~~^~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 336 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 592 KB Output is correct
2 Correct 2 ms 592 KB Output is correct
3 Correct 1 ms 592 KB Output is correct
4 Correct 1 ms 592 KB Output is correct
5 Correct 1 ms 592 KB Output is correct
6 Correct 2 ms 592 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 44 ms 18368 KB Output is correct
2 Correct 44 ms 18360 KB Output is correct
3 Correct 44 ms 18360 KB Output is correct
4 Correct 48 ms 18360 KB Output is correct
5 Correct 47 ms 18360 KB Output is correct
6 Correct 48 ms 18360 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 46 ms 18436 KB Output is correct
2 Correct 47 ms 18368 KB Output is correct
3 Correct 56 ms 18360 KB Output is correct
4 Correct 66 ms 18368 KB Output is correct
5 Correct 53 ms 18360 KB Output is correct
6 Correct 64 ms 18360 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 592 KB Output is correct
2 Correct 2 ms 592 KB Output is correct
3 Correct 1 ms 592 KB Output is correct
4 Correct 1 ms 592 KB Output is correct
5 Correct 1 ms 592 KB Output is correct
6 Correct 2 ms 592 KB Output is correct
7 Correct 2 ms 592 KB Output is correct
8 Correct 2 ms 592 KB Output is correct
9 Correct 2 ms 592 KB Output is correct
10 Correct 2 ms 592 KB Output is correct
11 Correct 2 ms 592 KB Output is correct
12 Correct 2 ms 592 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 592 KB Output is correct
2 Correct 2 ms 592 KB Output is correct
3 Correct 1 ms 592 KB Output is correct
4 Correct 1 ms 592 KB Output is correct
5 Correct 1 ms 592 KB Output is correct
6 Correct 2 ms 592 KB Output is correct
7 Correct 44 ms 18368 KB Output is correct
8 Correct 44 ms 18360 KB Output is correct
9 Correct 44 ms 18360 KB Output is correct
10 Correct 48 ms 18360 KB Output is correct
11 Correct 47 ms 18360 KB Output is correct
12 Correct 48 ms 18360 KB Output is correct
13 Correct 46 ms 18436 KB Output is correct
14 Correct 47 ms 18368 KB Output is correct
15 Correct 56 ms 18360 KB Output is correct
16 Correct 66 ms 18368 KB Output is correct
17 Correct 53 ms 18360 KB Output is correct
18 Correct 64 ms 18360 KB Output is correct
19 Correct 2 ms 592 KB Output is correct
20 Correct 2 ms 592 KB Output is correct
21 Correct 2 ms 592 KB Output is correct
22 Correct 2 ms 592 KB Output is correct
23 Correct 2 ms 592 KB Output is correct
24 Correct 2 ms 592 KB Output is correct
25 Correct 64 ms 18360 KB Output is correct
26 Correct 49 ms 18364 KB Output is correct
27 Correct 69 ms 18632 KB Output is correct
28 Correct 63 ms 18360 KB Output is correct
29 Correct 58 ms 18368 KB Output is correct
30 Correct 64 ms 18360 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 46 ms 18436 KB Output is correct
2 Correct 47 ms 18368 KB Output is correct
3 Correct 56 ms 18360 KB Output is correct
4 Correct 66 ms 18368 KB Output is correct
5 Correct 53 ms 18360 KB Output is correct
6 Correct 64 ms 18360 KB Output is correct
7 Correct 68 ms 20664 KB Output is correct
8 Correct 68 ms 22704 KB Output is correct
9 Correct 86 ms 22816 KB Output is correct
10 Correct 78 ms 22712 KB Output is correct
11 Correct 93 ms 22712 KB Output is correct
12 Correct 75 ms 22712 KB Output is correct
13 Correct 76 ms 22712 KB Output is correct
14 Correct 75 ms 22456 KB Output is correct
15 Correct 91 ms 22712 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 336 KB Output is correct
2 Correct 1 ms 592 KB Output is correct
3 Correct 2 ms 592 KB Output is correct
4 Correct 1 ms 592 KB Output is correct
5 Correct 1 ms 592 KB Output is correct
6 Correct 1 ms 592 KB Output is correct
7 Correct 2 ms 592 KB Output is correct
8 Correct 44 ms 18368 KB Output is correct
9 Correct 44 ms 18360 KB Output is correct
10 Correct 44 ms 18360 KB Output is correct
11 Correct 48 ms 18360 KB Output is correct
12 Correct 47 ms 18360 KB Output is correct
13 Correct 48 ms 18360 KB Output is correct
14 Correct 46 ms 18436 KB Output is correct
15 Correct 47 ms 18368 KB Output is correct
16 Correct 56 ms 18360 KB Output is correct
17 Correct 66 ms 18368 KB Output is correct
18 Correct 53 ms 18360 KB Output is correct
19 Correct 64 ms 18360 KB Output is correct
20 Correct 2 ms 592 KB Output is correct
21 Correct 2 ms 592 KB Output is correct
22 Correct 2 ms 592 KB Output is correct
23 Correct 2 ms 592 KB Output is correct
24 Correct 2 ms 592 KB Output is correct
25 Correct 2 ms 592 KB Output is correct
26 Correct 64 ms 18360 KB Output is correct
27 Correct 49 ms 18364 KB Output is correct
28 Correct 69 ms 18632 KB Output is correct
29 Correct 63 ms 18360 KB Output is correct
30 Correct 58 ms 18368 KB Output is correct
31 Correct 64 ms 18360 KB Output is correct
32 Correct 68 ms 20664 KB Output is correct
33 Correct 68 ms 22704 KB Output is correct
34 Correct 86 ms 22816 KB Output is correct
35 Correct 78 ms 22712 KB Output is correct
36 Correct 93 ms 22712 KB Output is correct
37 Correct 75 ms 22712 KB Output is correct
38 Correct 76 ms 22712 KB Output is correct
39 Correct 75 ms 22456 KB Output is correct
40 Correct 91 ms 22712 KB Output is correct
41 Correct 77 ms 23992 KB Output is correct
42 Correct 80 ms 24100 KB Output is correct
43 Correct 89 ms 24236 KB Output is correct
44 Correct 89 ms 24244 KB Output is correct
45 Correct 82 ms 24260 KB Output is correct
46 Correct 82 ms 24252 KB Output is correct
47 Correct 83 ms 24248 KB Output is correct
48 Correct 102 ms 23984 KB Output is correct
49 Correct 91 ms 24104 KB Output is correct