Submission #549416

# Submission time Handle Problem Language Result Execution time Memory
549416 2022-04-15T18:50:37 Z duality Reconstruction Project (JOI22_reconstruction) C++17
38 / 100
5000 ms 23872 KB
#define DEBUG 0

#include <bits/stdc++.h>
using namespace std;

#if DEBUG
// basic debugging macros
int __i__,__j__;
#define printLine(l) for(__i__=0;__i__<l;__i__++){cout<<"-";}cout<<endl
#define printLine2(l,c) for(__i__=0;__i__<l;__i__++){cout<<c;}cout<<endl
#define printVar(n) cout<<#n<<": "<<n<<endl
#define printArr(a,l) cout<<#a<<": ";for(__i__=0;__i__<l;__i__++){cout<<a[__i__]<<" ";}cout<<endl
#define print2dArr(a,r,c) cout<<#a<<":\n";for(__i__=0;__i__<r;__i__++){for(__j__=0;__j__<c;__j__++){cout<<a[__i__][__j__]<<" ";}cout<<endl;}
#define print2dArr2(a,r,c,l) cout<<#a<<":\n";for(__i__=0;__i__<r;__i__++){for(__j__=0;__j__<c;__j__++){cout<<setw(l)<<setfill(' ')<<a[__i__][__j__]<<" ";}cout<<endl;}

// advanced debugging class
// debug 1,2,'A',"test";
class _Debug {
    public:
        template<typename T>
        _Debug& operator,(T val) {
            cout << val << endl;
            return *this;
        }
};
#define debug _Debug(),
#else
#define printLine(l)
#define printLine2(l,c)
#define printVar(n)
#define printArr(a,l)
#define print2dArr(a,r,c)
#define print2dArr2(a,r,c,l)
#define debug
#endif

// define
#define MAX_VAL 999999999
#define MAX_VAL_2 999999999999999999LL
#define EPS 1e-6
#define mp make_pair
#define pb push_back

// typedef
typedef unsigned int UI;
typedef long long int LLI;
typedef unsigned long long int ULLI;
typedef unsigned short int US;
typedef pair<int,int> pii;
typedef pair<LLI,LLI> plli;
typedef vector<int> vi;
typedef vector<LLI> vlli;
typedef vector<pii> vpii;
typedef vector<plli> vplli;

// ---------- END OF TEMPLATE ----------
#pragma GCC optimize("Ofast")

struct edge { int u,v,w; };
edge edges[100000];
int X[1000000];
int l[100000],r[100000];
vpii adjList[500];
vi path;
int doDFS(int u,int p,int e) {
    if (u == e) return 1;
    for (auto &[v,i]: adjList[u]) {
        if (v != p) {
            path.pb(i);
            if (doDFS(v,u,e)) return 1;
            else path.pop_back();
        }
    }
    return 0;
}
vpii events;
multiset<int> S1,S2;
int main() {
    int i;
    int N,M,Q,A,B,W;
    scanf("%d %d",&N,&M);
    for (i = 0; i < M; i++) {
        scanf("%d %d %d",&A,&B,&W);
        edges[i] = (edge){A-1,B-1,W};
    }
    scanf("%d",&Q);
    for (i = 0; i < Q; i++) scanf("%d",&X[i]);

    sort(edges,edges+M,[](edge a,edge b) { return a.w < b.w; });
    for (i = 0; i < M; i++) {
        path.clear();
        if (!doDFS(edges[i].u,-1,edges[i].v)) l[i] = 0;
        else {
            int m = *min_element(path.begin(),path.end());
            l[i] = (edges[m].w+edges[i].w)/2+1;
            adjList[edges[m].u].erase(find(adjList[edges[m].u].begin(),adjList[edges[m].u].end(),mp(edges[m].v,m)));
            adjList[edges[m].v].erase(find(adjList[edges[m].v].begin(),adjList[edges[m].v].end(),mp(edges[m].u,m)));
        }
        adjList[edges[i].u].pb(mp(edges[i].v,i));
        adjList[edges[i].v].pb(mp(edges[i].u,i));
    }
    for (i = 0; i < N; i++) adjList[i].clear();
    for (i = M-1; i >= 0; i--) {
        path.clear();
        if (!doDFS(edges[i].u,-1,edges[i].v)) r[i] = 1e9;
        else {
            int m = *max_element(path.begin(),path.end());
            r[i] = (edges[m].w+edges[i].w)/2;
            adjList[edges[m].u].erase(find(adjList[edges[m].u].begin(),adjList[edges[m].u].end(),mp(edges[m].v,m)));
            adjList[edges[m].v].erase(find(adjList[edges[m].v].begin(),adjList[edges[m].v].end(),mp(edges[m].u,m)));
        }
        adjList[edges[i].u].pb(mp(edges[i].v,i));
        adjList[edges[i].v].pb(mp(edges[i].u,i));
    }

    int j = 0;
    LLI sum1 = 0,sum2 = 0;
    for (i = 0; i < M; i++) events.pb(mp(l[i],i+1)),events.pb(mp(r[i]+1,-(i+1)));
    sort(events.begin(),events.end());
    for (i = 0; i < Q; i++) {
        while ((j < events.size()) && (events[j].first <= X[i])) {
            if (events[j].second > 0) S1.insert(edges[events[j].second-1].w),sum1 += edges[events[j].second-1].w;
            else {
                if (S1.find(edges[-events[j].second-1].w) != S1.end())
                    S1.erase(S1.find(edges[-events[j].second-1].w)),sum1 -= edges[-events[j].second-1].w;
                else S2.erase(S2.find(edges[-events[j].second-1].w)),sum2 -= edges[-events[j].second-1].w;
            }
            j++;
        }
        while (!S1.empty() && (*S1.rbegin() > X[i])) {
            S2.insert(*S1.rbegin()),sum2 += *S1.rbegin();
            sum1 -= *S1.rbegin(),S1.erase(--S1.end());
        }
        while (!S2.empty() && (*S2.begin() <= X[i])) {
            S1.insert(*S2.begin()),sum1 += *S2.begin();
            sum2 -= *S2.begin(),S2.erase(S2.begin());
        }
        printf("%lld\n",(LLI) S1.size()*X[i]-sum1+sum2-(LLI) S2.size()*X[i]);
    }

    return 0;
}

Compilation message

reconstruction.cpp: In function 'int main()':
reconstruction.cpp:121:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  121 |         while ((j < events.size()) && (events[j].first <= X[i])) {
      |                 ~~^~~~~~~~~~~~~~~
reconstruction.cpp:81:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   81 |     scanf("%d %d",&N,&M);
      |     ~~~~~^~~~~~~~~~~~~~~
reconstruction.cpp:83:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   83 |         scanf("%d %d %d",&A,&B,&W);
      |         ~~~~~^~~~~~~~~~~~~~~~~~~~~
reconstruction.cpp:86:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   86 |     scanf("%d",&Q);
      |     ~~~~~^~~~~~~~~
reconstruction.cpp:87:34: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   87 |     for (i = 0; i < Q; i++) scanf("%d",&X[i]);
      |                             ~~~~~^~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 0 ms 340 KB Output is correct
4 Correct 1 ms 320 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 0 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 0 ms 340 KB Output is correct
16 Correct 1 ms 320 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 1 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 0 ms 340 KB Output is correct
4 Correct 1 ms 320 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 0 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 0 ms 340 KB Output is correct
16 Correct 1 ms 320 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 1 ms 340 KB Output is correct
19 Correct 1 ms 320 KB Output is correct
20 Correct 1100 ms 6404 KB Output is correct
21 Correct 623 ms 4740 KB Output is correct
22 Correct 920 ms 4496 KB Output is correct
23 Correct 978 ms 4600 KB Output is correct
24 Correct 920 ms 4472 KB Output is correct
25 Correct 1052 ms 4548 KB Output is correct
26 Correct 1085 ms 6284 KB Output is correct
27 Correct 1088 ms 6104 KB Output is correct
28 Correct 1088 ms 6172 KB Output is correct
29 Execution timed out 5050 ms 6036 KB Time limit exceeded
30 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 738 ms 22260 KB Output is correct
5 Correct 696 ms 22200 KB Output is correct
6 Correct 717 ms 22136 KB Output is correct
7 Correct 341 ms 23700 KB Output is correct
8 Correct 332 ms 23872 KB Output is correct
9 Correct 324 ms 23816 KB Output is correct
10 Correct 696 ms 21848 KB Output is correct
11 Correct 310 ms 23640 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 0 ms 340 KB Output is correct
4 Correct 1 ms 320 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 0 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 0 ms 340 KB Output is correct
16 Correct 1 ms 320 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 1 ms 340 KB Output is correct
19 Correct 1 ms 340 KB Output is correct
20 Correct 246 ms 20052 KB Output is correct
21 Correct 240 ms 20880 KB Output is correct
22 Correct 238 ms 19932 KB Output is correct
23 Correct 261 ms 19792 KB Output is correct
24 Correct 240 ms 19684 KB Output is correct
25 Correct 239 ms 19612 KB Output is correct
26 Correct 238 ms 19212 KB Output is correct
27 Correct 240 ms 19276 KB Output is correct
28 Correct 234 ms 19020 KB Output is correct
29 Correct 237 ms 18892 KB Output is correct
30 Correct 232 ms 18704 KB Output is correct
31 Correct 251 ms 18740 KB Output is correct
32 Correct 224 ms 19148 KB Output is correct
33 Correct 234 ms 17696 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 0 ms 340 KB Output is correct
4 Correct 1 ms 320 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 0 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 0 ms 340 KB Output is correct
16 Correct 1 ms 320 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 1 ms 340 KB Output is correct
19 Correct 1 ms 320 KB Output is correct
20 Correct 1100 ms 6404 KB Output is correct
21 Correct 623 ms 4740 KB Output is correct
22 Correct 920 ms 4496 KB Output is correct
23 Correct 978 ms 4600 KB Output is correct
24 Correct 920 ms 4472 KB Output is correct
25 Correct 1052 ms 4548 KB Output is correct
26 Correct 1085 ms 6284 KB Output is correct
27 Correct 1088 ms 6104 KB Output is correct
28 Correct 1088 ms 6172 KB Output is correct
29 Execution timed out 5050 ms 6036 KB Time limit exceeded
30 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 0 ms 340 KB Output is correct
4 Correct 1 ms 320 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 0 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 0 ms 340 KB Output is correct
16 Correct 1 ms 320 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 1 ms 340 KB Output is correct
19 Correct 1 ms 320 KB Output is correct
20 Correct 1100 ms 6404 KB Output is correct
21 Correct 623 ms 4740 KB Output is correct
22 Correct 920 ms 4496 KB Output is correct
23 Correct 978 ms 4600 KB Output is correct
24 Correct 920 ms 4472 KB Output is correct
25 Correct 1052 ms 4548 KB Output is correct
26 Correct 1085 ms 6284 KB Output is correct
27 Correct 1088 ms 6104 KB Output is correct
28 Correct 1088 ms 6172 KB Output is correct
29 Execution timed out 5050 ms 6036 KB Time limit exceeded
30 Halted 0 ms 0 KB -