Submission #568827

# Submission time Handle Problem Language Result Execution time Memory
568827 2022-05-26T08:38:52 Z tqbfjotld Reconstruction Project (JOI22_reconstruction) C++14
42 / 100
5000 ms 38888 KB
#include <bits/stdc++.h>
using namespace std;

int p[505];
int rt(int a){return p[a]==a?a:p[a]=rt(p[a]);}
int A[100005];
int B[100005];
int W[100005];

int n;

struct node{
    int s,e;
    vector<int> usefulmin,usefulmax;
    node *l,*r;
    node (int _s, int _e){
        s = _s; e = _e;
        if (s!=e){
            l = new node(s,(s+e)/2);
            r = new node((s+e)/2+1,e);
        }
    }
    void construct(){
        for (int x = 1; x<=n; x++){
            p[x] = x;
        }
        for (int x = s; x<=e; x++){
            if (rt(A[x])!=rt(B[x])){
                usefulmin.push_back(x);
                p[rt(A[x])] = rt(B[x]);
            }
        }
        for (int x = 1; x<=n; x++){
            p[x] = x;
        }
        for (int x = e; x>=s; x--){
            if (rt(A[x])!=rt(B[x])){
                usefulmax.push_back(x);
                p[rt(A[x])] = rt(B[x]);
            }
        }
        if (s==e) return;
        l->construct();
        r->construct();
    }
    void qu(vector<int> &low, vector<int> &high, int val){
        //printf("qu %d-%d, %d,   %d %d\n",s,e,val,W[s],W[e]);
        if (W[s]>val){
            for (auto x : usefulmin){
                //printf("high add %d\n",x);
                high.push_back(x);
            }
            return;
        }
        if (W[e]<=val){
            for (auto x : usefulmax){
                //printf("low add %d\n",x);
                low.push_back(x);
            }
            return;
        }
        if (W[(s+e)/2]>val){
            l->qu(low,high,val);
            r->qu(low,high,val);
        }
        else{
            r->qu(low,high,val);
            l->qu(low,high,val);
        }
    }
}*root;

int curval;
bool cmp1(int a, int b){
    return abs(W[a]-curval)<abs(W[b]-curval);
}

 main(){
    scanf("%d",&n);
    int m;
    scanf("%d",&m);
    vector<pair<int,pair<int,int>>> edgel;
    for (int x = 0; x<m; x++){
        int a,b,c;
        scanf("%d%d%d",&a,&b,&c);
        edgel.push_back({c,{a,b}});
    }
    sort(edgel.begin(),edgel.end());
    for (int x = 0; x<m; x++){
        A[x] = edgel[x].second.first;
        B[x] = edgel[x].second.second;
        W[x] = edgel[x].first;
    }
    root = new node(0,m-1);
    root->construct();
    int q;
    scanf("%d",&q);
    while (q--){
        int v;
        scanf("%d",&v);
        vector<int> v1,v2;
        root->qu(v1,v2,v);
        //printf("qu fin\n");
        curval = v;
        //sort(v1.begin(),v1.end(),cmp1);
        //sort(v2.begin(),v2.end(),cmp1);
        int c1 = 0;
        int c2 = 0;
        long long ans = 0;
        for (int x = 1; x<=n; x++) p[x] = x;
        while (c1<v1.size() || c2<v2.size()){
            //printf("%d %d\n",c1,c2);
            if (c2==v2.size() || c1<v1.size() && v-W[v1[c1]]<W[v2[c2]]-v){
                if (rt(A[v1[c1]])!=rt(B[v1[c1]])){
                    ans += v-W[v1[c1]];
                    p[rt(A[v1[c1]])] = rt(B[v1[c1]]);
                }
                c1++;
            }
            else{
                if (rt(A[v2[c2]])!=rt(B[v2[c2]])){
                    ans += W[v2[c2]]-v;
                    p[rt(A[v2[c2]])] = rt(B[v2[c2]]);
                }
                c2++;
            }
        }
        printf("%lld\n",ans);
    }
}

Compilation message

reconstruction.cpp:78:2: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
   78 |  main(){
      |  ^~~~
reconstruction.cpp: In function 'int main()':
reconstruction.cpp:111:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  111 |         while (c1<v1.size() || c2<v2.size()){
      |                ~~^~~~~~~~~~
reconstruction.cpp:111:34: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  111 |         while (c1<v1.size() || c2<v2.size()){
      |                                ~~^~~~~~~~~~
reconstruction.cpp:113:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  113 |             if (c2==v2.size() || c1<v1.size() && v-W[v1[c1]]<W[v2[c2]]-v){
      |                 ~~^~~~~~~~~~~
reconstruction.cpp:113:36: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  113 |             if (c2==v2.size() || c1<v1.size() && v-W[v1[c1]]<W[v2[c2]]-v){
      |                                  ~~^~~~~~~~~~
reconstruction.cpp:113:47: warning: suggest parentheses around '&&' within '||' [-Wparentheses]
  113 |             if (c2==v2.size() || c1<v1.size() && v-W[v1[c1]]<W[v2[c2]]-v){
      |                                  ~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
reconstruction.cpp:79:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   79 |     scanf("%d",&n);
      |     ~~~~~^~~~~~~~~
reconstruction.cpp:81:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   81 |     scanf("%d",&m);
      |     ~~~~~^~~~~~~~~
reconstruction.cpp:85:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   85 |         scanf("%d%d%d",&a,&b,&c);
      |         ~~~~~^~~~~~~~~~~~~~~~~~~
reconstruction.cpp:97:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   97 |     scanf("%d",&q);
      |     ~~~~~^~~~~~~~~
reconstruction.cpp:100:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  100 |         scanf("%d",&v);
      |         ~~~~~^~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 0 ms 212 KB Output is correct
12 Correct 0 ms 212 KB Output is correct
13 Correct 0 ms 212 KB Output is correct
14 Correct 1 ms 212 KB Output is correct
15 Correct 0 ms 212 KB Output is correct
16 Correct 1 ms 212 KB Output is correct
17 Correct 1 ms 212 KB Output is correct
18 Correct 1 ms 212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 0 ms 212 KB Output is correct
12 Correct 0 ms 212 KB Output is correct
13 Correct 0 ms 212 KB Output is correct
14 Correct 1 ms 212 KB Output is correct
15 Correct 0 ms 212 KB Output is correct
16 Correct 1 ms 212 KB Output is correct
17 Correct 1 ms 212 KB Output is correct
18 Correct 1 ms 212 KB Output is correct
19 Correct 1 ms 212 KB Output is correct
20 Correct 218 ms 38084 KB Output is correct
21 Correct 211 ms 37968 KB Output is correct
22 Correct 212 ms 38012 KB Output is correct
23 Correct 208 ms 38004 KB Output is correct
24 Correct 210 ms 38088 KB Output is correct
25 Correct 244 ms 37956 KB Output is correct
26 Correct 212 ms 37976 KB Output is correct
27 Correct 235 ms 37952 KB Output is correct
28 Correct 213 ms 38020 KB Output is correct
29 Correct 224 ms 37512 KB Output is correct
30 Correct 230 ms 37988 KB Output is correct
31 Correct 213 ms 38052 KB Output is correct
32 Correct 222 ms 38104 KB Output is correct
33 Correct 247 ms 37924 KB Output is correct
34 Correct 218 ms 37580 KB Output is correct
35 Correct 222 ms 37960 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Execution timed out 5030 ms 38864 KB Time limit exceeded
5 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 0 ms 212 KB Output is correct
12 Correct 0 ms 212 KB Output is correct
13 Correct 0 ms 212 KB Output is correct
14 Correct 1 ms 212 KB Output is correct
15 Correct 0 ms 212 KB Output is correct
16 Correct 1 ms 212 KB Output is correct
17 Correct 1 ms 212 KB Output is correct
18 Correct 1 ms 212 KB Output is correct
19 Correct 0 ms 212 KB Output is correct
20 Execution timed out 5038 ms 3660 KB Time limit exceeded
21 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 0 ms 212 KB Output is correct
12 Correct 0 ms 212 KB Output is correct
13 Correct 0 ms 212 KB Output is correct
14 Correct 1 ms 212 KB Output is correct
15 Correct 0 ms 212 KB Output is correct
16 Correct 1 ms 212 KB Output is correct
17 Correct 1 ms 212 KB Output is correct
18 Correct 1 ms 212 KB Output is correct
19 Correct 1 ms 212 KB Output is correct
20 Correct 218 ms 38084 KB Output is correct
21 Correct 211 ms 37968 KB Output is correct
22 Correct 212 ms 38012 KB Output is correct
23 Correct 208 ms 38004 KB Output is correct
24 Correct 210 ms 38088 KB Output is correct
25 Correct 244 ms 37956 KB Output is correct
26 Correct 212 ms 37976 KB Output is correct
27 Correct 235 ms 37952 KB Output is correct
28 Correct 213 ms 38020 KB Output is correct
29 Correct 224 ms 37512 KB Output is correct
30 Correct 230 ms 37988 KB Output is correct
31 Correct 213 ms 38052 KB Output is correct
32 Correct 222 ms 38104 KB Output is correct
33 Correct 247 ms 37924 KB Output is correct
34 Correct 218 ms 37580 KB Output is correct
35 Correct 222 ms 37960 KB Output is correct
36 Correct 2440 ms 38504 KB Output is correct
37 Correct 2070 ms 38644 KB Output is correct
38 Correct 2344 ms 38596 KB Output is correct
39 Correct 2401 ms 38868 KB Output is correct
40 Correct 2415 ms 38668 KB Output is correct
41 Correct 2471 ms 38764 KB Output is correct
42 Correct 2453 ms 38700 KB Output is correct
43 Correct 2455 ms 38688 KB Output is correct
44 Correct 2176 ms 38560 KB Output is correct
45 Correct 1396 ms 38060 KB Output is correct
46 Correct 2433 ms 38888 KB Output is correct
47 Correct 2505 ms 38612 KB Output is correct
48 Correct 2456 ms 38700 KB Output is correct
49 Correct 2483 ms 38724 KB Output is correct
50 Correct 500 ms 38144 KB Output is correct
51 Correct 1937 ms 38636 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 0 ms 212 KB Output is correct
12 Correct 0 ms 212 KB Output is correct
13 Correct 0 ms 212 KB Output is correct
14 Correct 1 ms 212 KB Output is correct
15 Correct 0 ms 212 KB Output is correct
16 Correct 1 ms 212 KB Output is correct
17 Correct 1 ms 212 KB Output is correct
18 Correct 1 ms 212 KB Output is correct
19 Correct 1 ms 212 KB Output is correct
20 Correct 218 ms 38084 KB Output is correct
21 Correct 211 ms 37968 KB Output is correct
22 Correct 212 ms 38012 KB Output is correct
23 Correct 208 ms 38004 KB Output is correct
24 Correct 210 ms 38088 KB Output is correct
25 Correct 244 ms 37956 KB Output is correct
26 Correct 212 ms 37976 KB Output is correct
27 Correct 235 ms 37952 KB Output is correct
28 Correct 213 ms 38020 KB Output is correct
29 Correct 224 ms 37512 KB Output is correct
30 Correct 230 ms 37988 KB Output is correct
31 Correct 213 ms 38052 KB Output is correct
32 Correct 222 ms 38104 KB Output is correct
33 Correct 247 ms 37924 KB Output is correct
34 Correct 218 ms 37580 KB Output is correct
35 Correct 222 ms 37960 KB Output is correct
36 Correct 0 ms 212 KB Output is correct
37 Correct 0 ms 212 KB Output is correct
38 Correct 0 ms 212 KB Output is correct
39 Execution timed out 5030 ms 38864 KB Time limit exceeded
40 Halted 0 ms 0 KB -