답안 #211426

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
211426 2020-03-20T10:25:04 Z VEGAnn 다리 (APIO19_bridges) C++14
44 / 100
3000 ms 30364 KB
#include <bits/stdc++.h>
#define all(x) x.begin(),x.end()
#define sz(x) ((int)x.size())
#define pll pair<ll, ll>
#define MP make_pair
#define PB push_back
#define pii pair<int, int>
#define pi3 pair<pii, int>
#define ft first
#define sd second
#define MP3(a,b,c) MP(MP(a,b),c)
using namespace std;
typedef long long ll;
const int N = 50100;
const int M = 100100;
const int Q = 100100;
const int CO = 200100;
const int oo = 2e9;
const int B = 2000;
const int MB = M / B + 10;
stack<pi3> stk;
vector<int> g[N], vc, ed[CO], vec, qr;
int n, m, U[M], V[M], D[M], q, ans[Q], tt = 0, mrk[M], loc[M];
int vls[B][B], tp[Q], l[Q], r[Q], siz[N], pr[N];

bool cmp(int x, int y){
    return r[x] > r[y];
}

int get(int x, bool tp){
    if (!tp)
        return (x == pr[x] ? x : pr[x] = get(pr[x], 0));
    else return (x == pr[x] ? x : get(pr[x], 1));
}

void link(int x, int y, bool tp){
    x = get(x, tp);
    y = get(y, tp);

    if (x == y) return;

    if (siz[x] < siz[y]) swap(x, y);

    if (tp) stk.push(MP3(x, y, pr[y]));
    pr[y] = x;
    siz[x] += siz[y];
}

int main(){

#ifdef _LOCAL
    freopen("in.txt","r",stdin);
#else
    ios_base::sync_with_stdio(0); cin.tie(0);
#endif // _LOCAL

    cin >> n >> m;

    for (int i = 0; i < m; i++){
        cin >> U[i] >> V[i] >> D[i];
        U[i]--; V[i]--;

        g[U[i]].PB(i);
        g[V[i]].PB(i);
        vc.PB(D[i]);
    }

    cin >> q;

    for (int i = 0; i < q; i++) {
        cin >> tp[i] >> l[i] >> r[i];
        l[i]--;
        vc.PB(r[i]);
    }

    sort(all(vc));
    vc.resize(unique(all(vc)) - vc.begin());

    for (int i = 0; i < m; i++)
        D[i] = lower_bound(all(vc), D[i]) - vc.begin();

    for (int i = 0; i < q; i++)
        r[i] = lower_bound(all(vc), r[i]) - vc.begin();

    for (int it = 0; it < q; it += B){
        int bd = min(q, it + B);

        tt++;
        vec.clear();
        qr.clear();

        for (int i = it; i < bd; i++)
            if (tp[i] == 1) {
                if (mrk[l[i]] < tt) {
                    mrk[l[i]] = tt;
                    loc[l[i]] = sz(vec);
                    vec.PB(l[i]);
                }
            } else qr.PB(i);

        for (int i = it; i < bd; i++){
            int nm = i - it;

            if (i == it){
                for (int j = 0; j < sz(vec); j++)
                    vls[nm][j] = D[vec[j]];
            } else {
                for (int j = 0; j < sz(vec); j++)
                    vls[nm][j] = vls[nm - 1][j];
            }

            if (tp[i] == 1)
                vls[nm][loc[l[i]]] = r[i];
        }

        for (int i = 0; i < m; i++)
            if (mrk[i] < tt)
                ed[D[i]].PB(i);

        for (int i = 0; i < n; i++){
            siz[i] = 1;
            pr[i] = i;
        }

        if (sz(qr)){
            sort(all(qr), cmp);
        }

        while (sz(stk)) stk.pop();
        int lst = sz(vc) - 1;

        for (int nm : qr){
            while (lst >= 0 && lst >= r[nm]){

                if (sz(ed[lst])){
                    for (int nw : ed[lst])
                        link(U[nw], V[nw], 0);
                    ed[lst].clear();
                }

                lst--;
            }

            int gt = nm - it;

            for (int i = 0; i < sz(vec); i++)
                if (vls[gt][i] >= r[nm])
                    link(U[vec[i]], V[vec[i]], 1);

            ans[nm] = siz[get(l[nm], 1)];

            while (sz(stk) > 0){
                pi3 cr = stk.top();
                stk.pop();

                siz[cr.ft.ft] -= siz[cr.ft.sd];
                pr[cr.ft.sd] = cr.sd;
            }
        }

        while (lst >= 0){
            ed[lst].clear();
            lst--;
        }

        //end
        for (int i = 0; i < sz(vec); i++)
            D[vec[i]] = vls[bd - it - 1][i];
    }

    for (int i = 0; i < q; i++)
        if (tp[i] == 2)
            cout << ans[i] << '\n';

    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 12 ms 6272 KB Output is correct
2 Correct 9 ms 6272 KB Output is correct
3 Correct 60 ms 19704 KB Output is correct
4 Correct 14 ms 6528 KB Output is correct
5 Correct 25 ms 15232 KB Output is correct
6 Correct 22 ms 15232 KB Output is correct
7 Correct 25 ms 15360 KB Output is correct
8 Correct 28 ms 15352 KB Output is correct
9 Correct 29 ms 15360 KB Output is correct
10 Correct 26 ms 15360 KB Output is correct
11 Correct 27 ms 15360 KB Output is correct
12 Correct 28 ms 15360 KB Output is correct
13 Correct 34 ms 15744 KB Output is correct
14 Correct 32 ms 15744 KB Output is correct
15 Correct 30 ms 15360 KB Output is correct
16 Correct 27 ms 15360 KB Output is correct
17 Correct 27 ms 15372 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2121 ms 30236 KB Output is correct
2 Correct 2176 ms 30364 KB Output is correct
3 Correct 2086 ms 30204 KB Output is correct
4 Correct 2249 ms 30312 KB Output is correct
5 Correct 2363 ms 30232 KB Output is correct
6 Execution timed out 3070 ms 29516 KB Time limit exceeded
7 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1877 ms 28596 KB Output is correct
2 Correct 1730 ms 26080 KB Output is correct
3 Correct 2293 ms 28664 KB Output is correct
4 Correct 1834 ms 28528 KB Output is correct
5 Correct 79 ms 8564 KB Output is correct
6 Correct 2263 ms 28668 KB Output is correct
7 Correct 1677 ms 28596 KB Output is correct
8 Correct 1374 ms 28504 KB Output is correct
9 Correct 1020 ms 21744 KB Output is correct
10 Correct 875 ms 21564 KB Output is correct
11 Correct 1389 ms 29460 KB Output is correct
12 Correct 1071 ms 29556 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1489 ms 15576 KB Output is correct
2 Correct 89 ms 8564 KB Output is correct
3 Correct 168 ms 12148 KB Output is correct
4 Correct 65 ms 9684 KB Output is correct
5 Correct 315 ms 13420 KB Output is correct
6 Correct 1312 ms 15688 KB Output is correct
7 Correct 306 ms 13424 KB Output is correct
8 Correct 650 ms 12856 KB Output is correct
9 Correct 606 ms 12724 KB Output is correct
10 Correct 518 ms 12892 KB Output is correct
11 Correct 1014 ms 14000 KB Output is correct
12 Correct 1036 ms 14008 KB Output is correct
13 Correct 833 ms 14340 KB Output is correct
14 Correct 314 ms 13684 KB Output is correct
15 Correct 303 ms 13656 KB Output is correct
16 Correct 1158 ms 15304 KB Output is correct
17 Correct 1169 ms 15468 KB Output is correct
18 Correct 1234 ms 15228 KB Output is correct
19 Correct 1253 ms 15356 KB Output is correct
20 Correct 885 ms 14700 KB Output is correct
21 Correct 831 ms 14704 KB Output is correct
22 Correct 1073 ms 15212 KB Output is correct
23 Correct 314 ms 12396 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2121 ms 30236 KB Output is correct
2 Correct 2176 ms 30364 KB Output is correct
3 Correct 2086 ms 30204 KB Output is correct
4 Correct 2249 ms 30312 KB Output is correct
5 Correct 2363 ms 30232 KB Output is correct
6 Execution timed out 3070 ms 29516 KB Time limit exceeded
7 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 12 ms 6272 KB Output is correct
2 Correct 9 ms 6272 KB Output is correct
3 Correct 60 ms 19704 KB Output is correct
4 Correct 14 ms 6528 KB Output is correct
5 Correct 25 ms 15232 KB Output is correct
6 Correct 22 ms 15232 KB Output is correct
7 Correct 25 ms 15360 KB Output is correct
8 Correct 28 ms 15352 KB Output is correct
9 Correct 29 ms 15360 KB Output is correct
10 Correct 26 ms 15360 KB Output is correct
11 Correct 27 ms 15360 KB Output is correct
12 Correct 28 ms 15360 KB Output is correct
13 Correct 34 ms 15744 KB Output is correct
14 Correct 32 ms 15744 KB Output is correct
15 Correct 30 ms 15360 KB Output is correct
16 Correct 27 ms 15360 KB Output is correct
17 Correct 27 ms 15372 KB Output is correct
18 Correct 2121 ms 30236 KB Output is correct
19 Correct 2176 ms 30364 KB Output is correct
20 Correct 2086 ms 30204 KB Output is correct
21 Correct 2249 ms 30312 KB Output is correct
22 Correct 2363 ms 30232 KB Output is correct
23 Execution timed out 3070 ms 29516 KB Time limit exceeded
24 Halted 0 ms 0 KB -