Submission #1003458

# Submission time Handle Problem Language Result Execution time Memory
1003458 2024-06-20T11:06:08 Z onbert Constellation 3 (JOI20_constellation3) C++17
35 / 100
755 ms 82408 KB
#include <bits/stdc++.h>
using namespace std;
#define int long long
const int maxn = 2e5 + 5, 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];
      |         ^
# Verdict Execution time Memory Grader output
1 Correct 2 ms 14940 KB Output is correct
2 Correct 3 ms 19036 KB Output is correct
3 Correct 2 ms 14948 KB Output is correct
4 Correct 3 ms 16988 KB Output is correct
5 Correct 3 ms 14896 KB Output is correct
6 Correct 2 ms 14940 KB Output is correct
7 Correct 2 ms 14940 KB Output is correct
8 Correct 2 ms 14824 KB Output is correct
9 Correct 3 ms 14940 KB Output is correct
10 Correct 2 ms 14936 KB Output is correct
11 Correct 3 ms 16984 KB Output is correct
12 Correct 2 ms 14940 KB Output is correct
13 Correct 2 ms 14828 KB Output is correct
14 Correct 3 ms 16984 KB Output is correct
15 Correct 2 ms 14940 KB Output is correct
16 Correct 2 ms 14940 KB Output is correct
17 Correct 2 ms 16988 KB Output is correct
18 Correct 2 ms 16988 KB Output is correct
19 Correct 2 ms 14940 KB Output is correct
20 Correct 3 ms 16988 KB Output is correct
21 Correct 2 ms 14940 KB Output is correct
22 Correct 2 ms 14936 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 14940 KB Output is correct
2 Correct 3 ms 19036 KB Output is correct
3 Correct 2 ms 14948 KB Output is correct
4 Correct 3 ms 16988 KB Output is correct
5 Correct 3 ms 14896 KB Output is correct
6 Correct 2 ms 14940 KB Output is correct
7 Correct 2 ms 14940 KB Output is correct
8 Correct 2 ms 14824 KB Output is correct
9 Correct 3 ms 14940 KB Output is correct
10 Correct 2 ms 14936 KB Output is correct
11 Correct 3 ms 16984 KB Output is correct
12 Correct 2 ms 14940 KB Output is correct
13 Correct 2 ms 14828 KB Output is correct
14 Correct 3 ms 16984 KB Output is correct
15 Correct 2 ms 14940 KB Output is correct
16 Correct 2 ms 14940 KB Output is correct
17 Correct 2 ms 16988 KB Output is correct
18 Correct 2 ms 16988 KB Output is correct
19 Correct 2 ms 14940 KB Output is correct
20 Correct 3 ms 16988 KB Output is correct
21 Correct 2 ms 14940 KB Output is correct
22 Correct 2 ms 14936 KB Output is correct
23 Correct 6 ms 17752 KB Output is correct
24 Correct 6 ms 15452 KB Output is correct
25 Correct 6 ms 15448 KB Output is correct
26 Correct 6 ms 15452 KB Output is correct
27 Correct 6 ms 15452 KB Output is correct
28 Correct 6 ms 15452 KB Output is correct
29 Correct 6 ms 15452 KB Output is correct
30 Correct 6 ms 17500 KB Output is correct
31 Correct 6 ms 17364 KB Output is correct
32 Correct 6 ms 15452 KB Output is correct
33 Correct 7 ms 17500 KB Output is correct
34 Correct 6 ms 15608 KB Output is correct
35 Correct 6 ms 15344 KB Output is correct
36 Correct 4 ms 15196 KB Output is correct
37 Correct 4 ms 15196 KB Output is correct
38 Correct 5 ms 15708 KB Output is correct
39 Correct 6 ms 15320 KB Output is correct
40 Correct 6 ms 15452 KB Output is correct
41 Correct 5 ms 17240 KB Output is correct
42 Correct 5 ms 15192 KB Output is correct
43 Correct 7 ms 15452 KB Output is correct
44 Correct 5 ms 15192 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 14940 KB Output is correct
2 Correct 3 ms 19036 KB Output is correct
3 Correct 2 ms 14948 KB Output is correct
4 Correct 3 ms 16988 KB Output is correct
5 Correct 3 ms 14896 KB Output is correct
6 Correct 2 ms 14940 KB Output is correct
7 Correct 2 ms 14940 KB Output is correct
8 Correct 2 ms 14824 KB Output is correct
9 Correct 3 ms 14940 KB Output is correct
10 Correct 2 ms 14936 KB Output is correct
11 Correct 3 ms 16984 KB Output is correct
12 Correct 2 ms 14940 KB Output is correct
13 Correct 2 ms 14828 KB Output is correct
14 Correct 3 ms 16984 KB Output is correct
15 Correct 2 ms 14940 KB Output is correct
16 Correct 2 ms 14940 KB Output is correct
17 Correct 2 ms 16988 KB Output is correct
18 Correct 2 ms 16988 KB Output is correct
19 Correct 2 ms 14940 KB Output is correct
20 Correct 3 ms 16988 KB Output is correct
21 Correct 2 ms 14940 KB Output is correct
22 Correct 2 ms 14936 KB Output is correct
23 Correct 6 ms 17752 KB Output is correct
24 Correct 6 ms 15452 KB Output is correct
25 Correct 6 ms 15448 KB Output is correct
26 Correct 6 ms 15452 KB Output is correct
27 Correct 6 ms 15452 KB Output is correct
28 Correct 6 ms 15452 KB Output is correct
29 Correct 6 ms 15452 KB Output is correct
30 Correct 6 ms 17500 KB Output is correct
31 Correct 6 ms 17364 KB Output is correct
32 Correct 6 ms 15452 KB Output is correct
33 Correct 7 ms 17500 KB Output is correct
34 Correct 6 ms 15608 KB Output is correct
35 Correct 6 ms 15344 KB Output is correct
36 Correct 4 ms 15196 KB Output is correct
37 Correct 4 ms 15196 KB Output is correct
38 Correct 5 ms 15708 KB Output is correct
39 Correct 6 ms 15320 KB Output is correct
40 Correct 6 ms 15452 KB Output is correct
41 Correct 5 ms 17240 KB Output is correct
42 Correct 5 ms 15192 KB Output is correct
43 Correct 7 ms 15452 KB Output is correct
44 Correct 5 ms 15192 KB Output is correct
45 Correct 755 ms 81308 KB Output is correct
46 Incorrect 716 ms 82408 KB Output isn't correct
47 Halted 0 ms 0 KB -