Submission #549420

# Submission time Handle Problem Language Result Execution time Memory
549420 2022-04-15T19:03:56 Z duality Reconstruction Project (JOI22_reconstruction) C++17
38 / 100
5000 ms 23716 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 ----------

struct edge { int u,v,w; };
edge edges[100000];
int X[1000000];
int l[100000],r[100000];
vpii adjList[500];
int parent[500],pi[500],qq[500];
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++) {
        r[i] = 1e9;
        memset(parent,-1,sizeof(parent));
        int ll = 0,rr = 1;
        qq[0] = edges[i].u;
        while (ll < rr) {
            int u = qq[ll++];

            if (u == edges[i].v) break;
            for (auto [v,x]: adjList[u]) {
                if ((v != edges[i].u) && (parent[v] == -1)) {
                    parent[v] = u,pi[v] = x;
                    qq[rr++] = v;
                }
            }
        }
        if (parent[edges[i].v] == -1) l[i] = 0;
        else {
            int u = edges[i].v;
            int m = pi[u];
            while (u != edges[i].u) m = min(m,pi[u]),u = parent[u];
            l[i] = (edges[m].w+edges[i].w)/2+1,r[m] = (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:112: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]
  112 |         while ((j < events.size()) && (events[j].first <= X[i])) {
      |                 ~~^~~~~~~~~~~~~~~
reconstruction.cpp:69:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   69 |     scanf("%d %d",&N,&M);
      |     ~~~~~^~~~~~~~~~~~~~~
reconstruction.cpp:71:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   71 |         scanf("%d %d %d",&A,&B,&W);
      |         ~~~~~^~~~~~~~~~~~~~~~~~~~~
reconstruction.cpp:74:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   74 |     scanf("%d",&Q);
      |     ~~~~~^~~~~~~~~
reconstruction.cpp:75:34: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   75 |     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 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 340 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 212 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 340 KB Output is correct
17 Correct 0 ms 340 KB Output is correct
18 Correct 1 ms 328 KB Output is correct
# 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 1 ms 340 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 212 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 340 KB Output is correct
17 Correct 0 ms 340 KB Output is correct
18 Correct 1 ms 328 KB Output is correct
19 Correct 1 ms 340 KB Output is correct
20 Correct 549 ms 5404 KB Output is correct
21 Correct 93 ms 4464 KB Output is correct
22 Correct 96 ms 4416 KB Output is correct
23 Correct 117 ms 4472 KB Output is correct
24 Correct 246 ms 4596 KB Output is correct
25 Correct 506 ms 4484 KB Output is correct
26 Correct 540 ms 5412 KB Output is correct
27 Correct 560 ms 5440 KB Output is correct
28 Correct 534 ms 5312 KB Output is correct
29 Execution timed out 5052 ms 5420 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 307 ms 20844 KB Output is correct
5 Correct 299 ms 20892 KB Output is correct
6 Correct 304 ms 20792 KB Output is correct
7 Correct 310 ms 22712 KB Output is correct
8 Correct 302 ms 22892 KB Output is correct
9 Correct 289 ms 22920 KB Output is correct
10 Correct 276 ms 21588 KB Output is correct
11 Correct 287 ms 23716 KB Output is correct
# 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 1 ms 340 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 212 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 340 KB Output is correct
17 Correct 0 ms 340 KB Output is correct
18 Correct 1 ms 328 KB Output is correct
19 Correct 1 ms 340 KB Output is correct
20 Correct 206 ms 18696 KB Output is correct
21 Correct 201 ms 17592 KB Output is correct
22 Correct 212 ms 19052 KB Output is correct
23 Correct 215 ms 18508 KB Output is correct
24 Correct 218 ms 18488 KB Output is correct
25 Correct 207 ms 18500 KB Output is correct
26 Correct 208 ms 19404 KB Output is correct
27 Correct 214 ms 18504 KB Output is correct
28 Correct 216 ms 19464 KB Output is correct
29 Correct 218 ms 18508 KB Output is correct
30 Correct 211 ms 18460 KB Output is correct
31 Correct 208 ms 18384 KB Output is correct
32 Correct 210 ms 19264 KB Output is correct
33 Correct 210 ms 18488 KB Output is correct
# 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 1 ms 340 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 212 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 340 KB Output is correct
17 Correct 0 ms 340 KB Output is correct
18 Correct 1 ms 328 KB Output is correct
19 Correct 1 ms 340 KB Output is correct
20 Correct 549 ms 5404 KB Output is correct
21 Correct 93 ms 4464 KB Output is correct
22 Correct 96 ms 4416 KB Output is correct
23 Correct 117 ms 4472 KB Output is correct
24 Correct 246 ms 4596 KB Output is correct
25 Correct 506 ms 4484 KB Output is correct
26 Correct 540 ms 5412 KB Output is correct
27 Correct 560 ms 5440 KB Output is correct
28 Correct 534 ms 5312 KB Output is correct
29 Execution timed out 5052 ms 5420 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 1 ms 340 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 212 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 340 KB Output is correct
17 Correct 0 ms 340 KB Output is correct
18 Correct 1 ms 328 KB Output is correct
19 Correct 1 ms 340 KB Output is correct
20 Correct 549 ms 5404 KB Output is correct
21 Correct 93 ms 4464 KB Output is correct
22 Correct 96 ms 4416 KB Output is correct
23 Correct 117 ms 4472 KB Output is correct
24 Correct 246 ms 4596 KB Output is correct
25 Correct 506 ms 4484 KB Output is correct
26 Correct 540 ms 5412 KB Output is correct
27 Correct 560 ms 5440 KB Output is correct
28 Correct 534 ms 5312 KB Output is correct
29 Execution timed out 5052 ms 5420 KB Time limit exceeded
30 Halted 0 ms 0 KB -