Submission #549414

# Submission time Handle Problem Language Result Execution time Memory
549414 2022-04-15T18:47:19 Z duality Reconstruction Project (JOI22_reconstruction) C++17
38 / 100
5000 ms 32432 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];
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:120: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]
  120 |         while ((j < events.size()) && (events[j].first <= X[i])) {
      |                 ~~^~~~~~~~~~~~~~~
reconstruction.cpp:80:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   80 |     scanf("%d %d",&N,&M);
      |     ~~~~~^~~~~~~~~~~~~~~
reconstruction.cpp:82:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   82 |         scanf("%d %d %d",&A,&B,&W);
      |         ~~~~~^~~~~~~~~~~~~~~~~~~~~
reconstruction.cpp:85:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   85 |     scanf("%d",&Q);
      |     ~~~~~^~~~~~~~~
reconstruction.cpp:86:34: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   86 |     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 320 KB Output is correct
3 Correct 1 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 0 ms 320 KB Output is correct
12 Correct 1 ms 212 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 1 ms 316 KB Output is correct
18 Correct 1 ms 316 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 320 KB Output is correct
3 Correct 1 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 0 ms 320 KB Output is correct
12 Correct 1 ms 212 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 1 ms 316 KB Output is correct
18 Correct 1 ms 316 KB Output is correct
19 Correct 1 ms 340 KB Output is correct
20 Correct 799 ms 5964 KB Output is correct
21 Correct 447 ms 4476 KB Output is correct
22 Correct 655 ms 4412 KB Output is correct
23 Correct 719 ms 4460 KB Output is correct
24 Correct 663 ms 4468 KB Output is correct
25 Correct 768 ms 4548 KB Output is correct
26 Correct 782 ms 6220 KB Output is correct
27 Correct 784 ms 6012 KB Output is correct
28 Correct 783 ms 6016 KB Output is correct
29 Execution timed out 5043 ms 6180 KB Time limit exceeded
30 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 316 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 320 KB Output is correct
4 Correct 653 ms 29512 KB Output is correct
5 Correct 657 ms 29720 KB Output is correct
6 Correct 669 ms 29704 KB Output is correct
7 Correct 343 ms 32108 KB Output is correct
8 Correct 337 ms 32184 KB Output is correct
9 Correct 315 ms 32432 KB Output is correct
10 Correct 627 ms 30556 KB Output is correct
11 Correct 295 ms 32000 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 320 KB Output is correct
3 Correct 1 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 0 ms 320 KB Output is correct
12 Correct 1 ms 212 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 1 ms 316 KB Output is correct
18 Correct 1 ms 316 KB Output is correct
19 Correct 1 ms 340 KB Output is correct
20 Correct 237 ms 25800 KB Output is correct
21 Correct 227 ms 21012 KB Output is correct
22 Correct 232 ms 25440 KB Output is correct
23 Correct 227 ms 26200 KB Output is correct
24 Correct 230 ms 25964 KB Output is correct
25 Correct 232 ms 25920 KB Output is correct
26 Correct 240 ms 25824 KB Output is correct
27 Correct 228 ms 25896 KB Output is correct
28 Correct 224 ms 25964 KB Output is correct
29 Correct 230 ms 26068 KB Output is correct
30 Correct 236 ms 25972 KB Output is correct
31 Correct 226 ms 25716 KB Output is correct
32 Correct 227 ms 26136 KB Output is correct
33 Correct 239 ms 25808 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 320 KB Output is correct
3 Correct 1 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 0 ms 320 KB Output is correct
12 Correct 1 ms 212 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 1 ms 316 KB Output is correct
18 Correct 1 ms 316 KB Output is correct
19 Correct 1 ms 340 KB Output is correct
20 Correct 799 ms 5964 KB Output is correct
21 Correct 447 ms 4476 KB Output is correct
22 Correct 655 ms 4412 KB Output is correct
23 Correct 719 ms 4460 KB Output is correct
24 Correct 663 ms 4468 KB Output is correct
25 Correct 768 ms 4548 KB Output is correct
26 Correct 782 ms 6220 KB Output is correct
27 Correct 784 ms 6012 KB Output is correct
28 Correct 783 ms 6016 KB Output is correct
29 Execution timed out 5043 ms 6180 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 320 KB Output is correct
3 Correct 1 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 0 ms 320 KB Output is correct
12 Correct 1 ms 212 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 1 ms 316 KB Output is correct
18 Correct 1 ms 316 KB Output is correct
19 Correct 1 ms 340 KB Output is correct
20 Correct 799 ms 5964 KB Output is correct
21 Correct 447 ms 4476 KB Output is correct
22 Correct 655 ms 4412 KB Output is correct
23 Correct 719 ms 4460 KB Output is correct
24 Correct 663 ms 4468 KB Output is correct
25 Correct 768 ms 4548 KB Output is correct
26 Correct 782 ms 6220 KB Output is correct
27 Correct 784 ms 6012 KB Output is correct
28 Correct 783 ms 6016 KB Output is correct
29 Execution timed out 5043 ms 6180 KB Time limit exceeded
30 Halted 0 ms 0 KB -