답안 #1003460

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1003460 2024-06-20T11:07:55 Z onbert 별자리 3 (JOI20_constellation3) C++17
35 / 100
776 ms 90788 KB
#include <bits/stdc++.h>
using namespace std;
#define int long long
const int maxn = 4e5 + 5, INF = 1e18;
struct node {
    int x, y, c;
    bool operator < (const node &b) const {
        if (y!=b.y) return y < b.y;
        if (x!=b.x) return x < b.x;
        return c < b.c;
    }
};

int siz;
int fen[maxn];
void update(int id, int val) {
    while (id <= siz) {
        fen[id] += val;
        id += (id & -id);
    }
}
int qry(int id) {
    int val = 0;
    while (id>=1) {
        val += fen[id];
        id -= (id & -id);
    }
    return val;
}

vector<node> star;
int dc1(node x) {return lower_bound(star.begin(), star.end(), x) - star.begin();}
vector<pair<int,int>> ranges;
int dc2(pair<int,int> x) {return lower_bound(ranges.begin(), ranges.end(), x) - ranges.begin();}
pair<int,int> in[maxn];
vector<node> mem[maxn];

vector<int> ADJ[maxn];
int sz[maxn], d[maxn], newid[maxn], lim[maxn], nxt[maxn], fa[maxn], cnter = 0;
void dfs1(int u) {
    sz[u] = 1;
    for (int v:ADJ[u]) {
        dfs1(v);
        sz[u] += sz[v];
    }
    for (int i=1;i<ADJ[u].size();i++) if (sz[ADJ[u][i]] > sz[ADJ[u][0]]) swap(ADJ[u][i], ADJ[u][0]);
}
void dfs2(int u) {
    cnter++;
    newid[u] = cnter;
    // cout << u << " " << ranges[u].first << " " << ranges[u].second << endl;
    for (int v:ADJ[u]) {
        // cout << u << " " << v << endl;
        d[cnter+1] = d[newid[u]] + 1;
        fa[cnter+1] = newid[u];
        if (v == ADJ[u][0]) nxt[cnter+1] = nxt[newid[u]];
        else nxt[cnter+1] = cnter+1;
        dfs2(v);
    }
    lim[newid[u]] = cnter;
}

int PATH(int u, int v) {
    if (d[u] > d[v]) swap(u, v);
    vector<pair<int,int>> path;
    if (u!=v) {
        path = {{nxt[u], u}};
        u = nxt[u];
        while (u > v || v > lim[u]) {
            u = fa[u];
            path.push_back({nxt[u], u});
            u = nxt[u];
        }
        auto [l, r] = path.back();
        while (l<r) {
            int mid = (l+r+1)/2;
            if (mid<=v && v<=lim[mid]) l = mid;
            else r = mid-1;
        }
        path.back().first = u = l;
        path.push_back({nxt[v], v});
        v = nxt[v];
        while (fa[d[v]] > d[u]) {
            v = fa[v];
            path.push_back({nxt[v], v});
            v = nxt[v];
        }
        path.back().first += d[u] - d[v] + 1;
    } else path = {{u, u}};

    int val = 0;
    for (auto [L, R]:path) val += qry(R) - qry(L-1);
    // cout << val << endl;
    return val;
}


int ansR[maxn], ansS[maxn];
void dfs3(int u) {
    for (int v:ADJ[u]) dfs3(v);
    ansR[u] = 0;
    // cout << "RANGE " << u << " " << ranges[u].first << " " << ranges[u].second << endl;
    for (auto [x, y, c]:mem[u]) {
        // cout << x << " " << y << " " << c << endl;
        int id = dc1({x, y, c}), end = dc2(in[x]);
        // cout << "end " << " " << end << " " << in[x].first << " " << in[x].second << endl;
        ansS[id] = PATH(newid[u], newid[end]) + c;
        ansR[u] = max(ansS[id], ansR[u]);
        // cout << ansS[id] << endl;
    }
    update(newid[u], -ansR[u]);
    if (fa[newid[u]] != -1) update(fa[newid[u]], ansR[u]);
}


signed main() {
    ios::sync_with_stdio(0); cin.tie(0);
    int n;
    cin >> n;
    pair<int,int> a[n+1];
    int A[n+1];
    for (int i=1;i<=n;i++) {
        cin >> a[i].first;
        a[i].second = i;
        A[i] = a[i].first;
    }
    sort(a+1, a+n+1);
    set<int> s;
    for (int i=0;i<=n+1;i++) s.insert(i);
    int last = 1;
    for (int i=1;i<=n;i++) {
        for (; a[last].first < a[i].first; last++) {
            auto it = s.upper_bound(a[last].second);
            pair<int,int> cur = {*prev(it) + 1, *it - 1};
            ranges.push_back(cur);
            in[a[last].second] = cur;
            // cout << "bruh " << a[last].second << " " << cur.first << " " << cur.second << endl;
        }
        s.erase(a[i].second);
    }
    for (; last<=n; last++) {
        auto it = s.upper_bound(a[last].second);
        pair<int,int> cur = {*prev(it) + 1, *it - 1};
        ranges.push_back(cur);
        in[a[last].second] = cur;
        // cout << "bruh " << a[last].second << " " << cur.first << " " << cur.second << endl;
    }
    sort(ranges.begin(), ranges.end()); ranges.erase(unique(ranges.begin(), ranges.end()), ranges.end());


    int m, SUM = 0;
    cin >> m;
    for (int i=1;i<=m;i++) {
        int x, y, c;
        cin >> x >> y >> c;
        star.push_back({x, y, c});
        SUM += c;
    }
    for (int i=1;i<=n;i++) star.push_back({a[i].second, a[i].first + 1, 0});
    
    siz = ranges.size();

    sort(star.begin(), star.end());
    vector<pair<int,int>> sub[siz];
    for (int i=0;i<siz;i++) sub[i] = {{ranges[i].first, -1}};
    for (int i=1;i<=n;i++) s.insert(i);
    last = 1;
    for (auto [x, y, c]:star) {
        // cout << x << " " << y << " " << c << endl;
        for (;last <= n && a[last].first < y; last++) s.erase(a[last].second);
        auto it = s.lower_bound(x);
        int l = *prev(it) + 1, r = *it - 1;
        int id = dc2({l, r});
        mem[id].push_back({x, y, c});
        // cout << id << endl;
        if (c==0) {
            if (x-1>=sub[id].back().first) {
                sub[id].back().second = x-1;
                sub[id].push_back({x+1, -1});
            } else sub[id].back().first = x+1;
        }
    }

    for (int i=0;i<siz;i++) {
        if (sub[i].back().first <= ranges[i].second) sub[i].back().second = ranges[i].second;
        else sub[i].pop_back();
        for (auto [l, r]:sub[i]) ADJ[i].push_back(dc2({l, r}));
    }
    int first = dc2({1, n});
    d[1] = 0, nxt[1] = 1, fa[1] = -1;
    dfs1(first); dfs2(first);
    dfs3(first);
    // for (int i=1;i<=n;i++) cout << nxt[i] << " "; cout << endl;

    cout << SUM - ansR[first] << endl;
}

Compilation message

constellation3.cpp: In function 'void dfs1(long long int)':
constellation3.cpp:46:19: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   46 |     for (int i=1;i<ADJ[u].size();i++) if (sz[ADJ[u][i]] > sz[ADJ[u][0]]) swap(ADJ[u][i], ADJ[u][0]);
      |                  ~^~~~~~~~~~~~~~
constellation3.cpp: In function 'int main()':
constellation3.cpp:121:9: warning: variable 'A' set but not used [-Wunused-but-set-variable]
  121 |     int A[n+1];
      |         ^
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 19288 KB Output is correct
2 Correct 8 ms 19292 KB Output is correct
3 Correct 11 ms 19328 KB Output is correct
4 Correct 8 ms 19292 KB Output is correct
5 Correct 8 ms 19292 KB Output is correct
6 Correct 8 ms 19288 KB Output is correct
7 Correct 8 ms 19380 KB Output is correct
8 Correct 8 ms 19292 KB Output is correct
9 Correct 9 ms 19288 KB Output is correct
10 Correct 10 ms 19292 KB Output is correct
11 Correct 8 ms 19292 KB Output is correct
12 Correct 8 ms 19292 KB Output is correct
13 Correct 9 ms 19448 KB Output is correct
14 Correct 9 ms 19292 KB Output is correct
15 Correct 8 ms 19292 KB Output is correct
16 Correct 8 ms 19292 KB Output is correct
17 Correct 8 ms 19292 KB Output is correct
18 Correct 9 ms 19292 KB Output is correct
19 Correct 8 ms 19424 KB Output is correct
20 Correct 8 ms 19292 KB Output is correct
21 Correct 9 ms 19332 KB Output is correct
22 Correct 8 ms 19292 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 19288 KB Output is correct
2 Correct 8 ms 19292 KB Output is correct
3 Correct 11 ms 19328 KB Output is correct
4 Correct 8 ms 19292 KB Output is correct
5 Correct 8 ms 19292 KB Output is correct
6 Correct 8 ms 19288 KB Output is correct
7 Correct 8 ms 19380 KB Output is correct
8 Correct 8 ms 19292 KB Output is correct
9 Correct 9 ms 19288 KB Output is correct
10 Correct 10 ms 19292 KB Output is correct
11 Correct 8 ms 19292 KB Output is correct
12 Correct 8 ms 19292 KB Output is correct
13 Correct 9 ms 19448 KB Output is correct
14 Correct 9 ms 19292 KB Output is correct
15 Correct 8 ms 19292 KB Output is correct
16 Correct 8 ms 19292 KB Output is correct
17 Correct 8 ms 19292 KB Output is correct
18 Correct 9 ms 19292 KB Output is correct
19 Correct 8 ms 19424 KB Output is correct
20 Correct 8 ms 19292 KB Output is correct
21 Correct 9 ms 19332 KB Output is correct
22 Correct 8 ms 19292 KB Output is correct
23 Correct 11 ms 19804 KB Output is correct
24 Correct 11 ms 19992 KB Output is correct
25 Correct 11 ms 19836 KB Output is correct
26 Correct 11 ms 20056 KB Output is correct
27 Correct 12 ms 19804 KB Output is correct
28 Correct 11 ms 19804 KB Output is correct
29 Correct 11 ms 19888 KB Output is correct
30 Correct 13 ms 20060 KB Output is correct
31 Correct 12 ms 19804 KB Output is correct
32 Correct 10 ms 20056 KB Output is correct
33 Correct 11 ms 20060 KB Output is correct
34 Correct 11 ms 19908 KB Output is correct
35 Correct 11 ms 20060 KB Output is correct
36 Correct 10 ms 19548 KB Output is correct
37 Correct 9 ms 19804 KB Output is correct
38 Correct 10 ms 20172 KB Output is correct
39 Correct 12 ms 19800 KB Output is correct
40 Correct 11 ms 20060 KB Output is correct
41 Correct 11 ms 19868 KB Output is correct
42 Correct 12 ms 19804 KB Output is correct
43 Correct 11 ms 20060 KB Output is correct
44 Correct 12 ms 19804 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 19288 KB Output is correct
2 Correct 8 ms 19292 KB Output is correct
3 Correct 11 ms 19328 KB Output is correct
4 Correct 8 ms 19292 KB Output is correct
5 Correct 8 ms 19292 KB Output is correct
6 Correct 8 ms 19288 KB Output is correct
7 Correct 8 ms 19380 KB Output is correct
8 Correct 8 ms 19292 KB Output is correct
9 Correct 9 ms 19288 KB Output is correct
10 Correct 10 ms 19292 KB Output is correct
11 Correct 8 ms 19292 KB Output is correct
12 Correct 8 ms 19292 KB Output is correct
13 Correct 9 ms 19448 KB Output is correct
14 Correct 9 ms 19292 KB Output is correct
15 Correct 8 ms 19292 KB Output is correct
16 Correct 8 ms 19292 KB Output is correct
17 Correct 8 ms 19292 KB Output is correct
18 Correct 9 ms 19292 KB Output is correct
19 Correct 8 ms 19424 KB Output is correct
20 Correct 8 ms 19292 KB Output is correct
21 Correct 9 ms 19332 KB Output is correct
22 Correct 8 ms 19292 KB Output is correct
23 Correct 11 ms 19804 KB Output is correct
24 Correct 11 ms 19992 KB Output is correct
25 Correct 11 ms 19836 KB Output is correct
26 Correct 11 ms 20056 KB Output is correct
27 Correct 12 ms 19804 KB Output is correct
28 Correct 11 ms 19804 KB Output is correct
29 Correct 11 ms 19888 KB Output is correct
30 Correct 13 ms 20060 KB Output is correct
31 Correct 12 ms 19804 KB Output is correct
32 Correct 10 ms 20056 KB Output is correct
33 Correct 11 ms 20060 KB Output is correct
34 Correct 11 ms 19908 KB Output is correct
35 Correct 11 ms 20060 KB Output is correct
36 Correct 10 ms 19548 KB Output is correct
37 Correct 9 ms 19804 KB Output is correct
38 Correct 10 ms 20172 KB Output is correct
39 Correct 12 ms 19800 KB Output is correct
40 Correct 11 ms 20060 KB Output is correct
41 Correct 11 ms 19868 KB Output is correct
42 Correct 12 ms 19804 KB Output is correct
43 Correct 11 ms 20060 KB Output is correct
44 Correct 12 ms 19804 KB Output is correct
45 Correct 753 ms 90788 KB Output is correct
46 Incorrect 776 ms 89908 KB Output isn't correct
47 Halted 0 ms 0 KB -