답안 #134435

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
134435 2019-07-22T16:43:28 Z duality Construction of Highway (JOI18_construction) C++11
100 / 100
733 ms 22968 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 ----------

int C[100000];
vi adjList[100000];
int parent[100000][17],order[100000];
int depth[100000];
int disc[100000],fin[100000],inv[100000],num = 0;
int doDFS(int u,int d) {
    int i;
    depth[u] = d,disc[u] = num++,inv[num-1] = u;
    for (i = 0; i < adjList[u].size(); i++) doDFS(adjList[u][i],d+1);
    fin[u] = num;
    return 0;
}
int good[100000],pref[100000];
vpii chains;
vi sorted;
int bit[100001],bit2[100001];
int update(int u,int N) {
    int i;
    for (i = disc[u]+1; i <= N; i += i & (-i)) bit[i] += good[u] ? -1:1;
    for (i = fin[u]+1; i <= N; i += i & (-i)) bit[i] += good[u] ? 1:-1;
    good[u] ^= 1;
    return 0;
}
int query(int u) {
    int i,sum = 0;
    for (i = disc[u]+1; i > 0; i -= i & (-i)) sum += bit[i];
    return sum;
}
int main() {
    int i;
    int N,A,B;
    scanf("%d",&N);
    for (i = 0; i < N; i++) scanf("%d",&C[i]);
    parent[0][0] = -1;
    for (i = 0; i < N-1; i++) {
        scanf("%d %d",&A,&B);
        A--,B--;
        adjList[A].pb(B),parent[B][0] = A;
        order[i] = B;
    }
    doDFS(0,0);

    int j,k;
    for (i = 1; (1 << i) < N; i++) {
        for (j = 0; j < N; j++) {
            if (parent[j][i-1] != -1) parent[j][i] = parent[parent[j][i-1]][i-1];
            else parent[j][i] = -1;
        }
    }
    int logn = i;
    for (i = 0; i < N; i++) pref[i] = -1;
    for (i = 0; i < N-1; i++) {
        int u = order[i];
        while (u != 0) {
            int v = pref[parent[u][0]];
            if (v != -1) update(v,N);
            pref[parent[u][0]] = u,update(u,N);
            int w = parent[u][0];
            for (j = logn-1; j >= 0; j--) {
                if ((parent[w][j] != -1) && (query(w) == (query(parent[w][j])+(1 << j)))) w = parent[w][j];
            }
            if (v != -1) C[v] = C[w];
            chains.pb(mp(C[w],depth[u]-depth[w])),sorted.pb(C[w]);
            u = w;
        }
        LLI ans = 0;
        sort(sorted.begin(),sorted.end());
        sorted.resize(unique(sorted.begin(),sorted.end())-sorted.begin());
        for (j = 0; j < chains.size(); j++) {
            int p = lower_bound(sorted.begin(),sorted.end(),chains[j].first)-sorted.begin();
            for (k = p; k > 0; k -= k & (-k)) ans += (LLI) chains[j].second*bit2[k];
            for (k = p+1; k <= sorted.size(); k += k & (-k)) bit2[k] += chains[j].second;
        }
        printf("%lld\n",ans);
        C[0] = C[order[i]];
        for (j = 0; j <= sorted.size(); j++) bit2[j] = 0;
        chains.clear(),sorted.clear();
    }

    return 0;
}

Compilation message

construction.cpp: In function 'int doDFS(int, int)':
construction.cpp:66:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for (i = 0; i < adjList[u].size(); i++) doDFS(adjList[u][i],d+1);
                 ~~^~~~~~~~~~~~~~~~~~~
construction.cpp: In function 'int main()':
construction.cpp:126:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
         for (j = 0; j < chains.size(); j++) {
                     ~~^~~~~~~~~~~~~~~
construction.cpp:129:29: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
             for (k = p+1; k <= sorted.size(); k += k & (-k)) bit2[k] += chains[j].second;
                           ~~^~~~~~~~~~~~~~~~
construction.cpp:133:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
         for (j = 0; j <= sorted.size(); j++) bit2[j] = 0;
                     ~~^~~~~~~~~~~~~~~~
construction.cpp:89:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf("%d",&N);
     ~~~~~^~~~~~~~~
construction.cpp:90:34: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     for (i = 0; i < N; i++) scanf("%d",&C[i]);
                             ~~~~~^~~~~~~~~~~~
construction.cpp:93:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         scanf("%d %d",&A,&B);
         ~~~~~^~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 2808 KB Output is correct
2 Correct 4 ms 2940 KB Output is correct
3 Correct 4 ms 2808 KB Output is correct
4 Correct 4 ms 2808 KB Output is correct
5 Correct 5 ms 2808 KB Output is correct
6 Correct 5 ms 2812 KB Output is correct
7 Correct 5 ms 2808 KB Output is correct
8 Correct 5 ms 2808 KB Output is correct
9 Correct 5 ms 2808 KB Output is correct
10 Correct 4 ms 2808 KB Output is correct
11 Correct 4 ms 2808 KB Output is correct
12 Correct 5 ms 2936 KB Output is correct
13 Correct 5 ms 2808 KB Output is correct
14 Correct 6 ms 2808 KB Output is correct
15 Correct 5 ms 2812 KB Output is correct
16 Correct 5 ms 2808 KB Output is correct
17 Correct 5 ms 2808 KB Output is correct
18 Correct 5 ms 2808 KB Output is correct
19 Correct 5 ms 2808 KB Output is correct
20 Correct 6 ms 2808 KB Output is correct
21 Correct 5 ms 2808 KB Output is correct
22 Correct 5 ms 2808 KB Output is correct
23 Correct 5 ms 2808 KB Output is correct
24 Correct 5 ms 2808 KB Output is correct
25 Correct 5 ms 2808 KB Output is correct
26 Correct 5 ms 2808 KB Output is correct
27 Correct 5 ms 2812 KB Output is correct
28 Correct 5 ms 2808 KB Output is correct
29 Correct 5 ms 2808 KB Output is correct
30 Correct 5 ms 2808 KB Output is correct
31 Correct 5 ms 2808 KB Output is correct
32 Correct 4 ms 2812 KB Output is correct
33 Correct 4 ms 2728 KB Output is correct
34 Correct 4 ms 2808 KB Output is correct
35 Correct 4 ms 2808 KB Output is correct
36 Correct 4 ms 2808 KB Output is correct
37 Correct 5 ms 2808 KB Output is correct
38 Correct 4 ms 2808 KB Output is correct
39 Correct 5 ms 2812 KB Output is correct
40 Correct 5 ms 2808 KB Output is correct
41 Correct 5 ms 2808 KB Output is correct
42 Correct 5 ms 2808 KB Output is correct
43 Correct 5 ms 2808 KB Output is correct
44 Correct 5 ms 2808 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 2808 KB Output is correct
2 Correct 4 ms 2940 KB Output is correct
3 Correct 4 ms 2808 KB Output is correct
4 Correct 4 ms 2808 KB Output is correct
5 Correct 5 ms 2808 KB Output is correct
6 Correct 5 ms 2812 KB Output is correct
7 Correct 5 ms 2808 KB Output is correct
8 Correct 5 ms 2808 KB Output is correct
9 Correct 5 ms 2808 KB Output is correct
10 Correct 4 ms 2808 KB Output is correct
11 Correct 4 ms 2808 KB Output is correct
12 Correct 5 ms 2936 KB Output is correct
13 Correct 5 ms 2808 KB Output is correct
14 Correct 6 ms 2808 KB Output is correct
15 Correct 5 ms 2812 KB Output is correct
16 Correct 5 ms 2808 KB Output is correct
17 Correct 5 ms 2808 KB Output is correct
18 Correct 5 ms 2808 KB Output is correct
19 Correct 5 ms 2808 KB Output is correct
20 Correct 6 ms 2808 KB Output is correct
21 Correct 5 ms 2808 KB Output is correct
22 Correct 5 ms 2808 KB Output is correct
23 Correct 5 ms 2808 KB Output is correct
24 Correct 5 ms 2808 KB Output is correct
25 Correct 5 ms 2808 KB Output is correct
26 Correct 5 ms 2808 KB Output is correct
27 Correct 5 ms 2812 KB Output is correct
28 Correct 5 ms 2808 KB Output is correct
29 Correct 5 ms 2808 KB Output is correct
30 Correct 5 ms 2808 KB Output is correct
31 Correct 5 ms 2808 KB Output is correct
32 Correct 4 ms 2812 KB Output is correct
33 Correct 4 ms 2728 KB Output is correct
34 Correct 4 ms 2808 KB Output is correct
35 Correct 4 ms 2808 KB Output is correct
36 Correct 4 ms 2808 KB Output is correct
37 Correct 5 ms 2808 KB Output is correct
38 Correct 4 ms 2808 KB Output is correct
39 Correct 5 ms 2812 KB Output is correct
40 Correct 5 ms 2808 KB Output is correct
41 Correct 5 ms 2808 KB Output is correct
42 Correct 5 ms 2808 KB Output is correct
43 Correct 5 ms 2808 KB Output is correct
44 Correct 5 ms 2808 KB Output is correct
45 Correct 5 ms 2936 KB Output is correct
46 Correct 11 ms 3320 KB Output is correct
47 Correct 11 ms 3320 KB Output is correct
48 Correct 11 ms 3320 KB Output is correct
49 Correct 10 ms 3448 KB Output is correct
50 Correct 9 ms 3448 KB Output is correct
51 Correct 8 ms 3448 KB Output is correct
52 Correct 9 ms 3320 KB Output is correct
53 Correct 10 ms 3448 KB Output is correct
54 Correct 9 ms 3448 KB Output is correct
55 Correct 9 ms 3448 KB Output is correct
56 Correct 9 ms 3408 KB Output is correct
57 Correct 14 ms 3192 KB Output is correct
58 Correct 21 ms 3320 KB Output is correct
59 Correct 15 ms 3320 KB Output is correct
60 Correct 15 ms 3320 KB Output is correct
61 Correct 9 ms 3320 KB Output is correct
62 Correct 9 ms 3448 KB Output is correct
63 Correct 9 ms 3448 KB Output is correct
64 Correct 11 ms 3320 KB Output is correct
65 Correct 11 ms 3196 KB Output is correct
66 Correct 11 ms 3320 KB Output is correct
67 Correct 11 ms 3320 KB Output is correct
68 Correct 8 ms 3576 KB Output is correct
69 Correct 9 ms 3448 KB Output is correct
70 Correct 9 ms 3320 KB Output is correct
71 Correct 9 ms 3320 KB Output is correct
72 Correct 14 ms 3320 KB Output is correct
73 Correct 16 ms 3192 KB Output is correct
74 Correct 9 ms 3320 KB Output is correct
75 Correct 9 ms 3324 KB Output is correct
76 Correct 9 ms 3320 KB Output is correct
77 Correct 9 ms 3320 KB Output is correct
78 Correct 9 ms 3320 KB Output is correct
79 Correct 9 ms 3364 KB Output is correct
80 Correct 8 ms 3320 KB Output is correct
81 Correct 9 ms 3320 KB Output is correct
82 Correct 9 ms 3324 KB Output is correct
83 Correct 9 ms 3320 KB Output is correct
84 Correct 9 ms 3320 KB Output is correct
85 Correct 9 ms 3320 KB Output is correct
86 Correct 9 ms 3448 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 2808 KB Output is correct
2 Correct 4 ms 2940 KB Output is correct
3 Correct 4 ms 2808 KB Output is correct
4 Correct 4 ms 2808 KB Output is correct
5 Correct 5 ms 2808 KB Output is correct
6 Correct 5 ms 2812 KB Output is correct
7 Correct 5 ms 2808 KB Output is correct
8 Correct 5 ms 2808 KB Output is correct
9 Correct 5 ms 2808 KB Output is correct
10 Correct 4 ms 2808 KB Output is correct
11 Correct 4 ms 2808 KB Output is correct
12 Correct 5 ms 2936 KB Output is correct
13 Correct 5 ms 2808 KB Output is correct
14 Correct 6 ms 2808 KB Output is correct
15 Correct 5 ms 2812 KB Output is correct
16 Correct 5 ms 2808 KB Output is correct
17 Correct 5 ms 2808 KB Output is correct
18 Correct 5 ms 2808 KB Output is correct
19 Correct 5 ms 2808 KB Output is correct
20 Correct 6 ms 2808 KB Output is correct
21 Correct 5 ms 2808 KB Output is correct
22 Correct 5 ms 2808 KB Output is correct
23 Correct 5 ms 2808 KB Output is correct
24 Correct 5 ms 2808 KB Output is correct
25 Correct 5 ms 2808 KB Output is correct
26 Correct 5 ms 2808 KB Output is correct
27 Correct 5 ms 2812 KB Output is correct
28 Correct 5 ms 2808 KB Output is correct
29 Correct 5 ms 2808 KB Output is correct
30 Correct 5 ms 2808 KB Output is correct
31 Correct 5 ms 2808 KB Output is correct
32 Correct 4 ms 2812 KB Output is correct
33 Correct 4 ms 2728 KB Output is correct
34 Correct 4 ms 2808 KB Output is correct
35 Correct 4 ms 2808 KB Output is correct
36 Correct 4 ms 2808 KB Output is correct
37 Correct 5 ms 2808 KB Output is correct
38 Correct 4 ms 2808 KB Output is correct
39 Correct 5 ms 2812 KB Output is correct
40 Correct 5 ms 2808 KB Output is correct
41 Correct 5 ms 2808 KB Output is correct
42 Correct 5 ms 2808 KB Output is correct
43 Correct 5 ms 2808 KB Output is correct
44 Correct 5 ms 2808 KB Output is correct
45 Correct 5 ms 2936 KB Output is correct
46 Correct 11 ms 3320 KB Output is correct
47 Correct 11 ms 3320 KB Output is correct
48 Correct 11 ms 3320 KB Output is correct
49 Correct 10 ms 3448 KB Output is correct
50 Correct 9 ms 3448 KB Output is correct
51 Correct 8 ms 3448 KB Output is correct
52 Correct 9 ms 3320 KB Output is correct
53 Correct 10 ms 3448 KB Output is correct
54 Correct 9 ms 3448 KB Output is correct
55 Correct 9 ms 3448 KB Output is correct
56 Correct 9 ms 3408 KB Output is correct
57 Correct 14 ms 3192 KB Output is correct
58 Correct 21 ms 3320 KB Output is correct
59 Correct 15 ms 3320 KB Output is correct
60 Correct 15 ms 3320 KB Output is correct
61 Correct 9 ms 3320 KB Output is correct
62 Correct 9 ms 3448 KB Output is correct
63 Correct 9 ms 3448 KB Output is correct
64 Correct 11 ms 3320 KB Output is correct
65 Correct 11 ms 3196 KB Output is correct
66 Correct 11 ms 3320 KB Output is correct
67 Correct 11 ms 3320 KB Output is correct
68 Correct 8 ms 3576 KB Output is correct
69 Correct 9 ms 3448 KB Output is correct
70 Correct 9 ms 3320 KB Output is correct
71 Correct 9 ms 3320 KB Output is correct
72 Correct 14 ms 3320 KB Output is correct
73 Correct 16 ms 3192 KB Output is correct
74 Correct 9 ms 3320 KB Output is correct
75 Correct 9 ms 3324 KB Output is correct
76 Correct 9 ms 3320 KB Output is correct
77 Correct 9 ms 3320 KB Output is correct
78 Correct 9 ms 3320 KB Output is correct
79 Correct 9 ms 3364 KB Output is correct
80 Correct 8 ms 3320 KB Output is correct
81 Correct 9 ms 3320 KB Output is correct
82 Correct 9 ms 3324 KB Output is correct
83 Correct 9 ms 3320 KB Output is correct
84 Correct 9 ms 3320 KB Output is correct
85 Correct 9 ms 3320 KB Output is correct
86 Correct 9 ms 3448 KB Output is correct
87 Correct 25 ms 4088 KB Output is correct
88 Correct 82 ms 7032 KB Output is correct
89 Correct 377 ms 17016 KB Output is correct
90 Correct 386 ms 16960 KB Output is correct
91 Correct 364 ms 16888 KB Output is correct
92 Correct 175 ms 22960 KB Output is correct
93 Correct 183 ms 22968 KB Output is correct
94 Correct 176 ms 22904 KB Output is correct
95 Correct 217 ms 19784 KB Output is correct
96 Correct 243 ms 20168 KB Output is correct
97 Correct 232 ms 20052 KB Output is correct
98 Correct 234 ms 20084 KB Output is correct
99 Correct 222 ms 19324 KB Output is correct
100 Correct 694 ms 17028 KB Output is correct
101 Correct 732 ms 17424 KB Output is correct
102 Correct 732 ms 17272 KB Output is correct
103 Correct 733 ms 17528 KB Output is correct
104 Correct 271 ms 19164 KB Output is correct
105 Correct 286 ms 19316 KB Output is correct
106 Correct 252 ms 19192 KB Output is correct
107 Correct 423 ms 16120 KB Output is correct
108 Correct 483 ms 16376 KB Output is correct
109 Correct 373 ms 16708 KB Output is correct
110 Correct 159 ms 22392 KB Output is correct
111 Correct 219 ms 19816 KB Output is correct
112 Correct 204 ms 19316 KB Output is correct
113 Correct 202 ms 18676 KB Output is correct
114 Correct 650 ms 17072 KB Output is correct
115 Correct 715 ms 16740 KB Output is correct
116 Correct 266 ms 18680 KB Output is correct
117 Correct 211 ms 17784 KB Output is correct
118 Correct 195 ms 17468 KB Output is correct
119 Correct 203 ms 17184 KB Output is correct
120 Correct 200 ms 17400 KB Output is correct
121 Correct 189 ms 17016 KB Output is correct
122 Correct 236 ms 16928 KB Output is correct
123 Correct 273 ms 17892 KB Output is correct
124 Correct 267 ms 17400 KB Output is correct
125 Correct 268 ms 17272 KB Output is correct
126 Correct 259 ms 17400 KB Output is correct
127 Correct 256 ms 17036 KB Output is correct
128 Correct 263 ms 17040 KB Output is correct