답안 #549417

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
549417 2022-04-15T18:54:26 Z duality Reconstruction Project (JOI22_reconstruction) C++17
38 / 100
5000 ms 21588 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(),r[i] = 1e9;
        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,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:107: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]
  107 |         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]);
      |                             ~~~~~^~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 316 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 312 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 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 340 KB Output is correct
18 Correct 1 ms 340 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 316 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 312 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 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 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 448 ms 4720 KB Output is correct
21 Correct 262 ms 4444 KB Output is correct
22 Correct 377 ms 4404 KB Output is correct
23 Correct 393 ms 4424 KB Output is correct
24 Correct 365 ms 4508 KB Output is correct
25 Correct 424 ms 4512 KB Output is correct
26 Correct 468 ms 4852 KB Output is correct
27 Correct 454 ms 4672 KB Output is correct
28 Correct 422 ms 4708 KB Output is correct
29 Execution timed out 5058 ms 4804 KB Time limit exceeded
30 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 316 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 517 ms 19496 KB Output is correct
5 Correct 500 ms 19604 KB Output is correct
6 Correct 504 ms 19588 KB Output is correct
7 Correct 319 ms 21380 KB Output is correct
8 Correct 330 ms 21588 KB Output is correct
9 Correct 294 ms 21564 KB Output is correct
10 Correct 434 ms 19380 KB Output is correct
11 Correct 270 ms 21560 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 316 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 312 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 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 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 221 ms 17196 KB Output is correct
21 Correct 245 ms 17156 KB Output is correct
22 Correct 205 ms 17188 KB Output is correct
23 Correct 214 ms 17156 KB Output is correct
24 Correct 216 ms 17100 KB Output is correct
25 Correct 213 ms 17164 KB Output is correct
26 Correct 214 ms 17100 KB Output is correct
27 Correct 216 ms 17364 KB Output is correct
28 Correct 210 ms 17240 KB Output is correct
29 Correct 210 ms 17064 KB Output is correct
30 Correct 207 ms 17308 KB Output is correct
31 Correct 209 ms 17128 KB Output is correct
32 Correct 214 ms 17924 KB Output is correct
33 Correct 208 ms 17032 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 316 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 312 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 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 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 448 ms 4720 KB Output is correct
21 Correct 262 ms 4444 KB Output is correct
22 Correct 377 ms 4404 KB Output is correct
23 Correct 393 ms 4424 KB Output is correct
24 Correct 365 ms 4508 KB Output is correct
25 Correct 424 ms 4512 KB Output is correct
26 Correct 468 ms 4852 KB Output is correct
27 Correct 454 ms 4672 KB Output is correct
28 Correct 422 ms 4708 KB Output is correct
29 Execution timed out 5058 ms 4804 KB Time limit exceeded
30 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 316 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 312 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 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 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 448 ms 4720 KB Output is correct
21 Correct 262 ms 4444 KB Output is correct
22 Correct 377 ms 4404 KB Output is correct
23 Correct 393 ms 4424 KB Output is correct
24 Correct 365 ms 4508 KB Output is correct
25 Correct 424 ms 4512 KB Output is correct
26 Correct 468 ms 4852 KB Output is correct
27 Correct 454 ms 4672 KB Output is correct
28 Correct 422 ms 4708 KB Output is correct
29 Execution timed out 5058 ms 4804 KB Time limit exceeded
30 Halted 0 ms 0 KB -