UOJ Logo

NOI.AC

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#205103#3645. 单调图cjwen1004879ms12236kbC++113.4kb2024-06-23 11:57:332024-06-23 13:04:37

answer

#include<bits/stdc++.h>

using namespace std;

typedef pair<int, int> PII;

const int N = 1e5 + 10, M = 4e5 + 10;

int tn, n, m;
int he[N], ne[M], to[M], w[M], ecnt;

void add(int x, int y, int z){
    ne[++ecnt] = he[x];
    he[x] = ecnt;
    to[ecnt] = y;
    w[ecnt] = z;
}

int dis[4][N];
bool vis[N];

void dij(int s){
    priority_queue<PII, vector<PII>, greater<PII> > q;
    memset(dis[s], 0x3f, sizeof(dis[s]));
    memset(vis, 0, sizeof(vis));
    q.push({dis[s][s]=0, s});
    while(!q.empty()){
        int x = q.top().second, d = q.top().first;
        q.pop();
        if(vis[x]) continue;
        vis[x] = 1;
        for(int i = he[x]; i; i = ne[i]){
            int j = to[i];
            if(d + w[i] < dis[s][j]){
                dis[s][j] = d + w[i];
                q.push({dis[s][j], j});
            }
        }
    }
}

struct node{
    int a = 1, b = 1, c = 1, ans = 0, cnt = 0;
}tx[N], x[N];

struct BIT{

    #define lowbit(x) (x&(-x))

    int s[10000010];

    void add(int x, int k){
        // puts("begin 1");
        // printf("%d\n", x);
        x++;
        for(; x < 10000005; x += lowbit(x)){
            s[x] += k;
        }
        // puts("end 1");
    }

    int query(int x){
        x++;
        // puts("begin 2");
        int qans = 0;
        for(; x; x -= lowbit(x)){
            qans += s[x];
        }
        return qans;
        // puts("end 2");
    }
}s;


void cdq(int l, int r){
    // printf("%d~%d\n", l, r);
    if(l == r) return ;
    int mid = (l+r) / 2;
    cdq(l, mid);
    cdq(mid+1, r);
    sort(x+l, x+mid+1, [](const node &cx, const node &cy){
        return cx.b < cy.b;
    });
    sort(x+mid+1, x+r+1, [](const node &cx, const node &cy){
        return cx.b < cy.b;
    });
    int i = l, j = mid+1;
    for(; j <= r; j++){
        // printf("%d~%d\n", l, r);
        while(i <= mid && x[i].b <= x[j].b){
            // printf("%d~%d\n", l, r);
            s.add(x[i].c, x[i].cnt);
            i++;
        }
        x[j].ans += s.query(x[j].c);
    }
    for(i--; i >= l; i--){
        // printf("%d~%d\n", l, r);
        s.add(x[i].c, -x[i].cnt);
    }
}

int main(){

    // freopen("a.out", "w", stdout);

    scanf("%d%d", &tn, &m);
    int xx = 0, yy = 0, zz = 0;
    for(int i = 1; i <= m; i++){
        scanf("%d%d%d", &xx, &yy, &zz);
        add(xx, yy, zz);
        add(yy, xx, zz);
    }

    dij(1); dij(2); dij(3);

    // for(int i = 1; i <= 3; i++){
    //     for(int j = 1; j <= n; j++){
    //         printf("%d ", dis[i][j]);
    //     }
    //     puts("");
    // }

    for(int i = 1; i <= tn; i++){
        tx[i].a = dis[1][i];
        tx[i].b = dis[2][i];
        tx[i].c = dis[3][i];
    }

    sort(tx+1, tx+1+tn, [](const node &cx, const node &cy){
        if(cx.a != cy.a) return cx.a < cy.a;
        if(cx.b != cy.b) return cx.b < cy.b;
        return cx.c < cy.c;
    });

    for(int i = 1, ct = 0; i <= tn; i++){
        ct++;
        if(i == tn || tx[i].a != tx[i+1].a || tx[i].b != tx[i+1].b || tx[i].c != tx[i+1].c){
            x[++n] = tx[i];
            x[n].cnt = ct;
            ct = 0;
        }
    }

    // puts("1");

    cdq(1, n);
    // puts("2");

    int ans = 0;
    for(int i = 1; i <= n; i++){
        if(x[i].ans == 0){
            ans += x[i].cnt;
        }
    }

    printf("%d\n", ans);

    return 0;
}

Details

小提示:点击横条可展开更详细的信息

Test #1:

score: 5
Accepted
time: 0ms
memory: 6500kb

input:

250 300
224 221 81
7 224 11
19 221 40
186 7 96
149 19 86
143 149 75
111 221 47
4 221 84
87 224 28
24...

output:

33

result:

ok single line: '33'

Test #2:

score: 5
Accepted
time: 3ms
memory: 6496kb

input:

250 300
15 55 47
44 15 29
36 44 39
147 15 76
114 55 20
47 15 99
5 36 44
185 55 37
224 5 67
113 147 4...

output:

27

result:

ok single line: '27'

Test #3:

score: 5
Accepted
time: 0ms
memory: 6496kb

input:

250 300
145 60 26
26 145 19
131 26 62
73 131 66
228 26 4
250 60 32
30 73 32
210 26 33
161 131 68
70 ...

output:

33

result:

ok single line: '33'

Test #4:

score: 5
Accepted
time: 0ms
memory: 6500kb

input:

250 300
212 227 26
70 227 83
122 70 31
245 122 1
162 122 18
89 212 45
233 89 78
159 162 49
87 70 89
...

output:

37

result:

ok single line: '37'

Test #5:

score: 5
Accepted
time: 6ms
memory: 6556kb

input:

1800 2000
481 73 36
1145 481 90
899 481 12
855 899 71
117 1145 87
17 855 51
1223 17 13
1708 1145 55
...

output:

43

result:

ok single line: '43'

Test #6:

score: 5
Accepted
time: 6ms
memory: 6552kb

input:

1800 2000
1533 1556 16
1551 1556 96
1076 1556 37
31 1551 83
40 1076 18
1648 1551 97
1760 1533 89
141...

output:

58

result:

ok single line: '58'

Test #7:

score: 5
Accepted
time: 3ms
memory: 6556kb

input:

1800 2000
554 498 60
442 554 18
54 498 9
1798 54 33
422 1798 14
1696 54 41
201 1798 40
297 1798 63
8...

output:

49

result:

ok single line: '49'

Test #8:

score: 5
Accepted
time: 177ms
memory: 10912kb

input:

100000 99999
5092 35397 69
59669 5092 13
66060 5092 45
45290 66060 57
86709 5092 79
37616 86709 66
7...

output:

5616

result:

ok single line: '5616'

Test #9:

score: 5
Accepted
time: 180ms
memory: 10568kb

input:

100000 99999
76853 94 59
27726 94 13
90492 76853 33
97933 90492 71
46476 97933 18
6568 90492 37
3242...

output:

3202

result:

ok single line: '3202'

Test #10:

score: 5
Accepted
time: 197ms
memory: 10436kb

input:

100000 99999
20874 85984 48
8458 20874 88
50743 8458 100
41101 8458 84
8267 20874 86
30201 8458 55
3...

output:

4659

result:

ok single line: '4659'

Test #11:

score: 5
Accepted
time: 366ms
memory: 12236kb

input:

100000 200000
53018 80768 61
5832 80768 43
2997 80768 91
66393 80768 56
81366 5832 59
85664 5832 16
...

output:

40

result:

ok single line: '40'

Test #12:

score: 5
Accepted
time: 469ms
memory: 12232kb

input:

100000 200000
28908 36036 66
91872 36036 84
1622 28908 74
60237 1622 1
17200 28908 44
89734 28908 82...

output:

39

result:

ok single line: '39'

Test #13:

score: 5
Accepted
time: 396ms
memory: 12232kb

input:

100000 200000
80169 49117 81
59533 80169 11
38687 80169 66
73361 49117 69
86848 80169 39
97609 73361...

output:

37

result:

ok single line: '37'

Test #14:

score: 5
Accepted
time: 410ms
memory: 12232kb

input:

100000 200000
8518 10111 89
80152 8518 82
28398 8518 42
64281 8518 89
79057 10111 31
67454 64281 64
...

output:

52

result:

ok single line: '52'

Test #15:

score: 5
Accepted
time: 453ms
memory: 12232kb

input:

100000 200000
63008 51968 67
14619 63008 33
38273 63008 86
30436 51968 98
95240 30436 21
44387 51968...

output:

118

result:

ok single line: '118'

Test #16:

score: 5
Accepted
time: 348ms
memory: 12232kb

input:

100000 200000
38428 6838 6
57875 38428 62
23341 6838 42
77486 57875 67
36886 57875 99
35263 6838 58
...

output:

106

result:

ok single line: '106'

Test #17:

score: 5
Accepted
time: 425ms
memory: 12232kb

input:

100000 200000
29745 3984 50
23260 3984 62
53496 23260 79
81207 53496 13
79920 81207 44
90071 53496 1...

output:

99

result:

ok single line: '99'

Test #18:

score: 5
Accepted
time: 476ms
memory: 12236kb

input:

100000 200000
46691 643 96
325 46691 74
74789 325 7
65177 46691 54
98613 46691 46
69449 643 77
60913...

output:

62

result:

ok single line: '62'

Test #19:

score: 5
Accepted
time: 481ms
memory: 12232kb

input:

100000 200000
33629 97358 73
44862 33629 12
25151 33629 46
87268 25151 34
81508 25151 83
45636 81508...

output:

160

result:

ok single line: '160'

Test #20:

score: 5
Accepted
time: 483ms
memory: 12236kb

input:

100000 200000
68252 47666 98
62764 68252 87
84831 68252 91
49616 68252 73
58627 68252 71
33313 68252...

output:

166

result:

ok single line: '166'

Extra Test:

score: 0
Extra Test Passed