Submission #131180

# Submission time Handle Problem Language Result Execution time Memory
131180 2019-07-16T18:09:53 Z osaaateiasavtnl Bridges (APIO19_bridges) C++17
100 / 100
2573 ms 14588 KB
#include<bits/stdc++.h>
using namespace std;
#define app push_back
const int N = 1e5 + 7, K = 1 << 9;
struct Edge {
    int u, v, c;
};  
struct Quer {
    int t, i, c;
    bool operator < (Quer p) {
        return c > p.c;
    }   
};  
struct Seg {
    int l, r, i, c;
    bool operator < (Seg p) {
        return c > p.c;
    }   
};
int n, m, q;
int par[N], cnt[N];
void init() {
    for (int i = 1; i <= n; ++i) {
        par[i] = i; cnt[i] = 1;
    }
}   
int root(int u) {
    if (u == par[u]) return u;
    else return par[u] = root(par[u]);
}   
void merge(int u, int v) {
    u = root(u); v = root(v);
    if (u == v) return;
    if (cnt[v] < cnt[u]) swap(u, v);
    par[u] = v; cnt[v] += cnt[u];
}   
Edge ed[N];
Quer d[N];
int lf[N], w[N];
vector <Seg> v;
int ans[N];
bool upd[N];
vector <int> g[N];
bool comp(int i, int j) {
    return d[i].c > d[j].c;
}
bool used[N];
vector <int> vis;
int dfs(int u) {
    int ans = cnt[u];
    used[u] = 1;
    vis.app(u);
    for (int v : g[u]) {
        if (!used[v]) {
            ans += dfs(v);
        }   
    }   
    return ans;
}     
void solve(int L, int R) {
    memset(upd, 0, sizeof upd);
    vector <int> q;
    for (int i = L; i <= R; ++i) {
        if (d[i].t == 1) {
            upd[d[i].i] = 1;
        }
        else {
            q.app(i);
        }   
    }   
    sort(q.begin(), q.end(), comp);
    int ptr = 0;
    vector <Seg> t;
    init();
    for (int i : q) {
        while (ptr < v.size() && v[ptr].c >= d[i].c) {
            if (R < v[ptr].l || v[ptr].r < L) {
                   
            }
            else if (v[ptr].l <= L && R <= v[ptr].r) {
                int e = v[ptr].i;
                merge(ed[e].u, ed[e].v);
            }
            else {
                t.app(v[ptr]);
            }   
            ++ptr;
        }   
        for (auto s : t) {
            if (s.l <= i && i <= s.r) {
                int u = root(ed[s.i].u), v = root(ed[s.i].v);
                g[u].app(v); g[v].app(u);
            }   
        }   
        ans[i] = dfs(root(d[i].i));
        for (int e : vis) used[e] = 0;
        vis.clear();
        for (auto s : t) {
            if (s.l <= i && i <= s.r) {
                int u = root(ed[s.i].u), v = root(ed[s.i].v);
                g[u].clear(); g[v].clear();
            }   
        }   
    }   
}   
signed main() {
    #ifdef HOME
    freopen("input.txt", "r", stdin);
    #else
    ios_base::sync_with_stdio(0); cin.tie(0);
    #endif
    cin >> n >> m;
    for (int i = 0; i < m; ++i) {
        cin >> ed[i].u >> ed[i].v >> ed[i].c;
        w[i] = ed[i].c;
    }   
    cin >> q;
    for (int i = 0; i < q; ++i) {
        cin >> d[i].t >> d[i].i >> d[i].c;
        if (d[i].t == 1) {
            --d[i].i;
            v.app({lf[d[i].i], i - 1, d[i].i, w[d[i].i]});
            lf[d[i].i] = i;
            w[d[i].i] = d[i].c;
        }   
    }   
    for (int i = 0; i < m; ++i) {
        v.app({lf[i], q - 1, i, w[i]});
    }   
    sort(v.begin(), v.end());
    for (int a = 0; ; ++a) {
        int L = a * K;
        int R = min(L + K - 1, q - 1);
        if (R < L) break;
        solve(L, R);
    }   
    for (int i = 0; i < q; ++i) {
        if (d[i].t == 2) cout << ans[i] << '\n';
    }   
}   

Compilation message

bridges.cpp: In function 'void solve(int, int)':
bridges.cpp:76:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
         while (ptr < v.size() && v[ptr].c >= d[i].c) {
                ~~~~^~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 4 ms 2808 KB Output is correct
2 Correct 4 ms 2808 KB Output is correct
3 Correct 42 ms 3400 KB Output is correct
4 Correct 9 ms 3064 KB Output is correct
5 Correct 32 ms 3320 KB Output is correct
6 Correct 26 ms 3320 KB Output is correct
7 Correct 30 ms 3320 KB Output is correct
8 Correct 28 ms 3320 KB Output is correct
9 Correct 40 ms 3320 KB Output is correct
10 Correct 29 ms 3320 KB Output is correct
11 Correct 28 ms 3400 KB Output is correct
12 Correct 33 ms 3320 KB Output is correct
13 Correct 37 ms 3448 KB Output is correct
14 Correct 32 ms 3320 KB Output is correct
15 Correct 33 ms 3292 KB Output is correct
16 Correct 31 ms 3320 KB Output is correct
17 Correct 30 ms 3320 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1461 ms 11776 KB Output is correct
2 Correct 1622 ms 11588 KB Output is correct
3 Correct 1420 ms 11628 KB Output is correct
4 Correct 1403 ms 11720 KB Output is correct
5 Correct 1390 ms 11812 KB Output is correct
6 Correct 2399 ms 10604 KB Output is correct
7 Correct 2224 ms 10672 KB Output is correct
8 Correct 2233 ms 10740 KB Output is correct
9 Correct 50 ms 6008 KB Output is correct
10 Correct 1538 ms 10460 KB Output is correct
11 Correct 1429 ms 10576 KB Output is correct
12 Correct 945 ms 9976 KB Output is correct
13 Correct 1674 ms 11756 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1228 ms 10244 KB Output is correct
2 Correct 1020 ms 7804 KB Output is correct
3 Correct 1461 ms 10096 KB Output is correct
4 Correct 1202 ms 10136 KB Output is correct
5 Correct 51 ms 5992 KB Output is correct
6 Correct 1350 ms 10360 KB Output is correct
7 Correct 1107 ms 10216 KB Output is correct
8 Correct 925 ms 10088 KB Output is correct
9 Correct 638 ms 9072 KB Output is correct
10 Correct 575 ms 9028 KB Output is correct
11 Correct 960 ms 10728 KB Output is correct
12 Correct 826 ms 10984 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1140 ms 12512 KB Output is correct
2 Correct 49 ms 5880 KB Output is correct
3 Correct 119 ms 8432 KB Output is correct
4 Correct 71 ms 8560 KB Output is correct
5 Correct 935 ms 11112 KB Output is correct
6 Correct 1213 ms 12272 KB Output is correct
7 Correct 927 ms 11008 KB Output is correct
8 Correct 631 ms 9584 KB Output is correct
9 Correct 654 ms 9624 KB Output is correct
10 Correct 638 ms 9456 KB Output is correct
11 Correct 1066 ms 10732 KB Output is correct
12 Correct 1016 ms 10956 KB Output is correct
13 Correct 1064 ms 11088 KB Output is correct
14 Correct 865 ms 11120 KB Output is correct
15 Correct 866 ms 10984 KB Output is correct
16 Correct 1285 ms 12388 KB Output is correct
17 Correct 1336 ms 12368 KB Output is correct
18 Correct 1292 ms 12344 KB Output is correct
19 Correct 1299 ms 12252 KB Output is correct
20 Correct 1212 ms 11500 KB Output is correct
21 Correct 1233 ms 11356 KB Output is correct
22 Correct 1277 ms 12280 KB Output is correct
23 Correct 1054 ms 10236 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1461 ms 11776 KB Output is correct
2 Correct 1622 ms 11588 KB Output is correct
3 Correct 1420 ms 11628 KB Output is correct
4 Correct 1403 ms 11720 KB Output is correct
5 Correct 1390 ms 11812 KB Output is correct
6 Correct 2399 ms 10604 KB Output is correct
7 Correct 2224 ms 10672 KB Output is correct
8 Correct 2233 ms 10740 KB Output is correct
9 Correct 50 ms 6008 KB Output is correct
10 Correct 1538 ms 10460 KB Output is correct
11 Correct 1429 ms 10576 KB Output is correct
12 Correct 945 ms 9976 KB Output is correct
13 Correct 1674 ms 11756 KB Output is correct
14 Correct 1228 ms 10244 KB Output is correct
15 Correct 1020 ms 7804 KB Output is correct
16 Correct 1461 ms 10096 KB Output is correct
17 Correct 1202 ms 10136 KB Output is correct
18 Correct 51 ms 5992 KB Output is correct
19 Correct 1350 ms 10360 KB Output is correct
20 Correct 1107 ms 10216 KB Output is correct
21 Correct 925 ms 10088 KB Output is correct
22 Correct 638 ms 9072 KB Output is correct
23 Correct 575 ms 9028 KB Output is correct
24 Correct 960 ms 10728 KB Output is correct
25 Correct 826 ms 10984 KB Output is correct
26 Correct 1583 ms 11824 KB Output is correct
27 Correct 1922 ms 11564 KB Output is correct
28 Correct 1561 ms 11564 KB Output is correct
29 Correct 1076 ms 11560 KB Output is correct
30 Correct 1860 ms 11604 KB Output is correct
31 Correct 1875 ms 11596 KB Output is correct
32 Correct 1901 ms 11500 KB Output is correct
33 Correct 1623 ms 11780 KB Output is correct
34 Correct 1548 ms 11624 KB Output is correct
35 Correct 1594 ms 11788 KB Output is correct
36 Correct 1136 ms 11668 KB Output is correct
37 Correct 1202 ms 11656 KB Output is correct
38 Correct 1128 ms 11788 KB Output is correct
39 Correct 767 ms 10380 KB Output is correct
40 Correct 785 ms 10348 KB Output is correct
41 Correct 765 ms 10352 KB Output is correct
42 Correct 1012 ms 12268 KB Output is correct
43 Correct 1006 ms 12472 KB Output is correct
44 Correct 961 ms 12356 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 2808 KB Output is correct
2 Correct 4 ms 2808 KB Output is correct
3 Correct 42 ms 3400 KB Output is correct
4 Correct 9 ms 3064 KB Output is correct
5 Correct 32 ms 3320 KB Output is correct
6 Correct 26 ms 3320 KB Output is correct
7 Correct 30 ms 3320 KB Output is correct
8 Correct 28 ms 3320 KB Output is correct
9 Correct 40 ms 3320 KB Output is correct
10 Correct 29 ms 3320 KB Output is correct
11 Correct 28 ms 3400 KB Output is correct
12 Correct 33 ms 3320 KB Output is correct
13 Correct 37 ms 3448 KB Output is correct
14 Correct 32 ms 3320 KB Output is correct
15 Correct 33 ms 3292 KB Output is correct
16 Correct 31 ms 3320 KB Output is correct
17 Correct 30 ms 3320 KB Output is correct
18 Correct 1461 ms 11776 KB Output is correct
19 Correct 1622 ms 11588 KB Output is correct
20 Correct 1420 ms 11628 KB Output is correct
21 Correct 1403 ms 11720 KB Output is correct
22 Correct 1390 ms 11812 KB Output is correct
23 Correct 2399 ms 10604 KB Output is correct
24 Correct 2224 ms 10672 KB Output is correct
25 Correct 2233 ms 10740 KB Output is correct
26 Correct 50 ms 6008 KB Output is correct
27 Correct 1538 ms 10460 KB Output is correct
28 Correct 1429 ms 10576 KB Output is correct
29 Correct 945 ms 9976 KB Output is correct
30 Correct 1674 ms 11756 KB Output is correct
31 Correct 1228 ms 10244 KB Output is correct
32 Correct 1020 ms 7804 KB Output is correct
33 Correct 1461 ms 10096 KB Output is correct
34 Correct 1202 ms 10136 KB Output is correct
35 Correct 51 ms 5992 KB Output is correct
36 Correct 1350 ms 10360 KB Output is correct
37 Correct 1107 ms 10216 KB Output is correct
38 Correct 925 ms 10088 KB Output is correct
39 Correct 638 ms 9072 KB Output is correct
40 Correct 575 ms 9028 KB Output is correct
41 Correct 960 ms 10728 KB Output is correct
42 Correct 826 ms 10984 KB Output is correct
43 Correct 1140 ms 12512 KB Output is correct
44 Correct 49 ms 5880 KB Output is correct
45 Correct 119 ms 8432 KB Output is correct
46 Correct 71 ms 8560 KB Output is correct
47 Correct 935 ms 11112 KB Output is correct
48 Correct 1213 ms 12272 KB Output is correct
49 Correct 927 ms 11008 KB Output is correct
50 Correct 631 ms 9584 KB Output is correct
51 Correct 654 ms 9624 KB Output is correct
52 Correct 638 ms 9456 KB Output is correct
53 Correct 1066 ms 10732 KB Output is correct
54 Correct 1016 ms 10956 KB Output is correct
55 Correct 1064 ms 11088 KB Output is correct
56 Correct 865 ms 11120 KB Output is correct
57 Correct 866 ms 10984 KB Output is correct
58 Correct 1285 ms 12388 KB Output is correct
59 Correct 1336 ms 12368 KB Output is correct
60 Correct 1292 ms 12344 KB Output is correct
61 Correct 1299 ms 12252 KB Output is correct
62 Correct 1212 ms 11500 KB Output is correct
63 Correct 1233 ms 11356 KB Output is correct
64 Correct 1277 ms 12280 KB Output is correct
65 Correct 1054 ms 10236 KB Output is correct
66 Correct 1583 ms 11824 KB Output is correct
67 Correct 1922 ms 11564 KB Output is correct
68 Correct 1561 ms 11564 KB Output is correct
69 Correct 1076 ms 11560 KB Output is correct
70 Correct 1860 ms 11604 KB Output is correct
71 Correct 1875 ms 11596 KB Output is correct
72 Correct 1901 ms 11500 KB Output is correct
73 Correct 1623 ms 11780 KB Output is correct
74 Correct 1548 ms 11624 KB Output is correct
75 Correct 1594 ms 11788 KB Output is correct
76 Correct 1136 ms 11668 KB Output is correct
77 Correct 1202 ms 11656 KB Output is correct
78 Correct 1128 ms 11788 KB Output is correct
79 Correct 767 ms 10380 KB Output is correct
80 Correct 785 ms 10348 KB Output is correct
81 Correct 765 ms 10352 KB Output is correct
82 Correct 1012 ms 12268 KB Output is correct
83 Correct 1006 ms 12472 KB Output is correct
84 Correct 961 ms 12356 KB Output is correct
85 Correct 1952 ms 14304 KB Output is correct
86 Correct 159 ms 8820 KB Output is correct
87 Correct 137 ms 8968 KB Output is correct
88 Correct 1735 ms 12768 KB Output is correct
89 Correct 1930 ms 14436 KB Output is correct
90 Correct 1763 ms 12280 KB Output is correct
91 Correct 1548 ms 11660 KB Output is correct
92 Correct 1526 ms 11724 KB Output is correct
93 Correct 2082 ms 11028 KB Output is correct
94 Correct 1920 ms 13092 KB Output is correct
95 Correct 1927 ms 13340 KB Output is correct
96 Correct 2381 ms 12280 KB Output is correct
97 Correct 1127 ms 11496 KB Output is correct
98 Correct 1489 ms 12816 KB Output is correct
99 Correct 2173 ms 14288 KB Output is correct
100 Correct 2118 ms 14276 KB Output is correct
101 Correct 2115 ms 14432 KB Output is correct
102 Correct 2126 ms 14588 KB Output is correct
103 Correct 2573 ms 13032 KB Output is correct
104 Correct 2413 ms 12912 KB Output is correct
105 Correct 1955 ms 13080 KB Output is correct
106 Correct 1598 ms 13024 KB Output is correct
107 Correct 1594 ms 11880 KB Output is correct
108 Correct 2393 ms 13556 KB Output is correct
109 Correct 2180 ms 11040 KB Output is correct