Submission #312689

# Submission time Handle Problem Language Result Execution time Memory
312689 2020-10-14T06:45:37 Z YJU Construction of Highway (JOI18_construction) C++14
100 / 100
357 ms 8688 KB
#include<bits/stdc++.h>
#pragma GCC optimize("unroll-loops,no-stack-protector")
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<ll,ll> pll;
const ll MOD=1e9+7;
const ll MOD2=998244353;
const ll K=350;
const ld pi=3.14159265359;
const ll INF=(1LL<<40);
#define SQ(i) ((i)*(i))
#define REP(i,n) for(ll i=0;i<n;i++)
#define REP1(i,n) for(ll i=1;i<=n;i++)
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define setp setprecision
#define lwb lower_bound
#define SZ(_a) (ll)_a.size()

const int N = 1e5 + 7;

struct LCT {
    int f[N], ch[N][2], s[N], z[N], a[N];
    #define lc ch[p][0]
    #define rc ch[p][1]
    #define upd(p) s[p] = s[ch[p][0]] + s[ch[p][1]] + 1
    #define get(p) (p == ch[f[p]][1])
    #define rev(p) v[p] ^= 1, swap(ch[p][0], ch[p][1])
    #define isrt(p) (ch[f[p]][0] != p && ch[f[p]][1] != p)
    inline void spd(int p) {
        if (z[p]) {
            if (lc) a[lc] = z[lc] = z[p];
            if (rc) a[rc] = z[rc] = z[p];
            z[p] = 0;
        }
    }
    inline void rot(int p) {
        int x = f[p], y = f[x], u = get(p), v = get(x), o = isrt(x);
        f[ch[p][u^1]] = x, ch[x][u] = ch[p][u^1];
        f[x] = p, ch[p][u^1] = x, upd(x), upd(p);
        if ((f[p] = y) && !o) ch[y][v] = p;
    }
    void pre(int p) {
        if (!isrt(p)) pre(f[p]);
        spd(p);
    }
    inline void splay(int p) {
        pre(p);
        for (int x = f[p]; !isrt(p); rot(p), x = f[p])
            if (!isrt(x)) rot(get(p) == get(x) ? x : p);
    }
    inline vector<pll> access(int p) {
        vector<pll> ret;
        int w = 0;
        for (int x = 0; p; p = f[x=p])
            splay(p), rc = x, upd(p),
            ret.pb(mp(a[p], s[p] - w)), w = s[p];
        return ret;
    }
    inline void link(int p, int x) {
        int w = a[x];
        splay(p), rc = x, f[x] = p, splay(x), upd(x), a[x] = z[x] = w;
    }
} lct;

struct BIT {
    vector<ll> c;
    inline BIT() {}
    inline BIT(int n) { c.resize(n); }
    inline void add(ll x, ll k) {
        while (x < c.size()) c[x] += k, x += x & -x;
    }
    inline ll ask(ll x) {
        ll k = 0;
        while (x) k += c[x], x -= x & -x;
        return k;
    }
};

int n, a[N];

int main() {
	ios_base::sync_with_stdio(0);cin.tie(0);
	cin>>n;
    for (int i = 1; i <= n; i++) cin>>a[i], lct.a[i] = a[i];
    sort(a + 1, a + n + 1);
    int t = unique(a + 1, a + n + 1) - (a + 1);
    BIT c(t + 1);
    for (int i = 1; i <= n; i++)
        lct.a[i] = lower_bound(a + 1, a + t + 1, lct.a[i]) - a;
    lct.s[1] = 1;
    for (int i = 1, x, y; i < n; i++) {
    	cin>>x>>y;
        auto ret = lct.access(x);
        ll ans = 0;
        for (pll o : ret)
            ans += o.se * c.ask(o.fi - 1), c.add(o.fi, o.se);
            cout<<ans<<"\n";
        for (pll o : ret) c.add(o.fi, -o.se);
        lct.link(x, y);
    }
    return 0;
}

Compilation message

construction.cpp: In member function 'void BIT::add(ll, ll)':
construction.cpp:74:18: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   74 |         while (x < c.size()) c[x] += k, x += x & -x;
      |                ~~^~~~~~~~~~
construction.cpp: In function 'int main()':
construction.cpp:99:9: warning: this 'for' clause does not guard... [-Wmisleading-indentation]
   99 |         for (pll o : ret)
      |         ^~~
construction.cpp:101:13: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'for'
  101 |             cout<<ans<<"\n";
      |             ^~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 384 KB Output is correct
2 Correct 1 ms 384 KB Output is correct
3 Correct 1 ms 384 KB Output is correct
4 Correct 1 ms 384 KB Output is correct
5 Correct 1 ms 384 KB Output is correct
6 Correct 1 ms 384 KB Output is correct
7 Correct 1 ms 384 KB Output is correct
8 Correct 1 ms 384 KB Output is correct
9 Correct 1 ms 384 KB Output is correct
10 Correct 1 ms 384 KB Output is correct
11 Correct 1 ms 384 KB Output is correct
12 Correct 1 ms 384 KB Output is correct
13 Correct 1 ms 384 KB Output is correct
14 Correct 1 ms 384 KB Output is correct
15 Correct 1 ms 384 KB Output is correct
16 Correct 1 ms 384 KB Output is correct
17 Correct 1 ms 384 KB Output is correct
18 Correct 1 ms 384 KB Output is correct
19 Correct 1 ms 384 KB Output is correct
20 Correct 1 ms 384 KB Output is correct
21 Correct 1 ms 384 KB Output is correct
22 Correct 1 ms 384 KB Output is correct
23 Correct 1 ms 384 KB Output is correct
24 Correct 1 ms 384 KB Output is correct
25 Correct 1 ms 384 KB Output is correct
26 Correct 1 ms 384 KB Output is correct
27 Correct 1 ms 384 KB Output is correct
28 Correct 1 ms 384 KB Output is correct
29 Correct 1 ms 384 KB Output is correct
30 Correct 1 ms 384 KB Output is correct
31 Correct 1 ms 384 KB Output is correct
32 Correct 1 ms 384 KB Output is correct
33 Correct 1 ms 384 KB Output is correct
34 Correct 1 ms 384 KB Output is correct
35 Correct 1 ms 384 KB Output is correct
36 Correct 1 ms 384 KB Output is correct
37 Correct 1 ms 384 KB Output is correct
38 Correct 1 ms 384 KB Output is correct
39 Correct 1 ms 384 KB Output is correct
40 Correct 1 ms 384 KB Output is correct
41 Correct 1 ms 384 KB Output is correct
42 Correct 2 ms 384 KB Output is correct
43 Correct 1 ms 384 KB Output is correct
44 Correct 1 ms 384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 384 KB Output is correct
2 Correct 1 ms 384 KB Output is correct
3 Correct 1 ms 384 KB Output is correct
4 Correct 1 ms 384 KB Output is correct
5 Correct 1 ms 384 KB Output is correct
6 Correct 1 ms 384 KB Output is correct
7 Correct 1 ms 384 KB Output is correct
8 Correct 1 ms 384 KB Output is correct
9 Correct 1 ms 384 KB Output is correct
10 Correct 1 ms 384 KB Output is correct
11 Correct 1 ms 384 KB Output is correct
12 Correct 1 ms 384 KB Output is correct
13 Correct 1 ms 384 KB Output is correct
14 Correct 1 ms 384 KB Output is correct
15 Correct 1 ms 384 KB Output is correct
16 Correct 1 ms 384 KB Output is correct
17 Correct 1 ms 384 KB Output is correct
18 Correct 1 ms 384 KB Output is correct
19 Correct 1 ms 384 KB Output is correct
20 Correct 1 ms 384 KB Output is correct
21 Correct 1 ms 384 KB Output is correct
22 Correct 1 ms 384 KB Output is correct
23 Correct 1 ms 384 KB Output is correct
24 Correct 1 ms 384 KB Output is correct
25 Correct 1 ms 384 KB Output is correct
26 Correct 1 ms 384 KB Output is correct
27 Correct 1 ms 384 KB Output is correct
28 Correct 1 ms 384 KB Output is correct
29 Correct 1 ms 384 KB Output is correct
30 Correct 1 ms 384 KB Output is correct
31 Correct 1 ms 384 KB Output is correct
32 Correct 1 ms 384 KB Output is correct
33 Correct 1 ms 384 KB Output is correct
34 Correct 1 ms 384 KB Output is correct
35 Correct 1 ms 384 KB Output is correct
36 Correct 1 ms 384 KB Output is correct
37 Correct 1 ms 384 KB Output is correct
38 Correct 1 ms 384 KB Output is correct
39 Correct 1 ms 384 KB Output is correct
40 Correct 1 ms 384 KB Output is correct
41 Correct 1 ms 384 KB Output is correct
42 Correct 2 ms 384 KB Output is correct
43 Correct 1 ms 384 KB Output is correct
44 Correct 1 ms 384 KB Output is correct
45 Correct 2 ms 384 KB Output is correct
46 Correct 6 ms 640 KB Output is correct
47 Correct 7 ms 640 KB Output is correct
48 Correct 6 ms 640 KB Output is correct
49 Correct 3 ms 640 KB Output is correct
50 Correct 3 ms 640 KB Output is correct
51 Correct 3 ms 640 KB Output is correct
52 Correct 3 ms 768 KB Output is correct
53 Correct 4 ms 768 KB Output is correct
54 Correct 4 ms 768 KB Output is correct
55 Correct 4 ms 768 KB Output is correct
56 Correct 4 ms 640 KB Output is correct
57 Correct 8 ms 640 KB Output is correct
58 Correct 8 ms 584 KB Output is correct
59 Correct 8 ms 640 KB Output is correct
60 Correct 8 ms 640 KB Output is correct
61 Correct 5 ms 640 KB Output is correct
62 Correct 5 ms 768 KB Output is correct
63 Correct 5 ms 640 KB Output is correct
64 Correct 5 ms 640 KB Output is correct
65 Correct 5 ms 512 KB Output is correct
66 Correct 6 ms 512 KB Output is correct
67 Correct 6 ms 640 KB Output is correct
68 Correct 3 ms 512 KB Output is correct
69 Correct 4 ms 768 KB Output is correct
70 Correct 3 ms 640 KB Output is correct
71 Correct 3 ms 640 KB Output is correct
72 Correct 8 ms 640 KB Output is correct
73 Correct 7 ms 512 KB Output is correct
74 Correct 4 ms 640 KB Output is correct
75 Correct 4 ms 640 KB Output is correct
76 Correct 4 ms 640 KB Output is correct
77 Correct 4 ms 640 KB Output is correct
78 Correct 3 ms 512 KB Output is correct
79 Correct 3 ms 512 KB Output is correct
80 Correct 3 ms 512 KB Output is correct
81 Correct 5 ms 640 KB Output is correct
82 Correct 5 ms 640 KB Output is correct
83 Correct 5 ms 640 KB Output is correct
84 Correct 4 ms 512 KB Output is correct
85 Correct 4 ms 512 KB Output is correct
86 Correct 4 ms 512 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 384 KB Output is correct
2 Correct 1 ms 384 KB Output is correct
3 Correct 1 ms 384 KB Output is correct
4 Correct 1 ms 384 KB Output is correct
5 Correct 1 ms 384 KB Output is correct
6 Correct 1 ms 384 KB Output is correct
7 Correct 1 ms 384 KB Output is correct
8 Correct 1 ms 384 KB Output is correct
9 Correct 1 ms 384 KB Output is correct
10 Correct 1 ms 384 KB Output is correct
11 Correct 1 ms 384 KB Output is correct
12 Correct 1 ms 384 KB Output is correct
13 Correct 1 ms 384 KB Output is correct
14 Correct 1 ms 384 KB Output is correct
15 Correct 1 ms 384 KB Output is correct
16 Correct 1 ms 384 KB Output is correct
17 Correct 1 ms 384 KB Output is correct
18 Correct 1 ms 384 KB Output is correct
19 Correct 1 ms 384 KB Output is correct
20 Correct 1 ms 384 KB Output is correct
21 Correct 1 ms 384 KB Output is correct
22 Correct 1 ms 384 KB Output is correct
23 Correct 1 ms 384 KB Output is correct
24 Correct 1 ms 384 KB Output is correct
25 Correct 1 ms 384 KB Output is correct
26 Correct 1 ms 384 KB Output is correct
27 Correct 1 ms 384 KB Output is correct
28 Correct 1 ms 384 KB Output is correct
29 Correct 1 ms 384 KB Output is correct
30 Correct 1 ms 384 KB Output is correct
31 Correct 1 ms 384 KB Output is correct
32 Correct 1 ms 384 KB Output is correct
33 Correct 1 ms 384 KB Output is correct
34 Correct 1 ms 384 KB Output is correct
35 Correct 1 ms 384 KB Output is correct
36 Correct 1 ms 384 KB Output is correct
37 Correct 1 ms 384 KB Output is correct
38 Correct 1 ms 384 KB Output is correct
39 Correct 1 ms 384 KB Output is correct
40 Correct 1 ms 384 KB Output is correct
41 Correct 1 ms 384 KB Output is correct
42 Correct 2 ms 384 KB Output is correct
43 Correct 1 ms 384 KB Output is correct
44 Correct 1 ms 384 KB Output is correct
45 Correct 2 ms 384 KB Output is correct
46 Correct 6 ms 640 KB Output is correct
47 Correct 7 ms 640 KB Output is correct
48 Correct 6 ms 640 KB Output is correct
49 Correct 3 ms 640 KB Output is correct
50 Correct 3 ms 640 KB Output is correct
51 Correct 3 ms 640 KB Output is correct
52 Correct 3 ms 768 KB Output is correct
53 Correct 4 ms 768 KB Output is correct
54 Correct 4 ms 768 KB Output is correct
55 Correct 4 ms 768 KB Output is correct
56 Correct 4 ms 640 KB Output is correct
57 Correct 8 ms 640 KB Output is correct
58 Correct 8 ms 584 KB Output is correct
59 Correct 8 ms 640 KB Output is correct
60 Correct 8 ms 640 KB Output is correct
61 Correct 5 ms 640 KB Output is correct
62 Correct 5 ms 768 KB Output is correct
63 Correct 5 ms 640 KB Output is correct
64 Correct 5 ms 640 KB Output is correct
65 Correct 5 ms 512 KB Output is correct
66 Correct 6 ms 512 KB Output is correct
67 Correct 6 ms 640 KB Output is correct
68 Correct 3 ms 512 KB Output is correct
69 Correct 4 ms 768 KB Output is correct
70 Correct 3 ms 640 KB Output is correct
71 Correct 3 ms 640 KB Output is correct
72 Correct 8 ms 640 KB Output is correct
73 Correct 7 ms 512 KB Output is correct
74 Correct 4 ms 640 KB Output is correct
75 Correct 4 ms 640 KB Output is correct
76 Correct 4 ms 640 KB Output is correct
77 Correct 4 ms 640 KB Output is correct
78 Correct 3 ms 512 KB Output is correct
79 Correct 3 ms 512 KB Output is correct
80 Correct 3 ms 512 KB Output is correct
81 Correct 5 ms 640 KB Output is correct
82 Correct 5 ms 640 KB Output is correct
83 Correct 5 ms 640 KB Output is correct
84 Correct 4 ms 512 KB Output is correct
85 Correct 4 ms 512 KB Output is correct
86 Correct 4 ms 512 KB Output is correct
87 Correct 16 ms 896 KB Output is correct
88 Correct 54 ms 2168 KB Output is correct
89 Correct 229 ms 6392 KB Output is correct
90 Correct 225 ms 6268 KB Output is correct
91 Correct 227 ms 6264 KB Output is correct
92 Correct 87 ms 6264 KB Output is correct
93 Correct 86 ms 6136 KB Output is correct
94 Correct 89 ms 6392 KB Output is correct
95 Correct 94 ms 8304 KB Output is correct
96 Correct 101 ms 8680 KB Output is correct
97 Correct 98 ms 8688 KB Output is correct
98 Correct 106 ms 8688 KB Output is correct
99 Correct 113 ms 8008 KB Output is correct
100 Correct 306 ms 5984 KB Output is correct
101 Correct 353 ms 6392 KB Output is correct
102 Correct 345 ms 6264 KB Output is correct
103 Correct 357 ms 6264 KB Output is correct
104 Correct 157 ms 7288 KB Output is correct
105 Correct 167 ms 7544 KB Output is correct
106 Correct 171 ms 7160 KB Output is correct
107 Correct 159 ms 4728 KB Output is correct
108 Correct 186 ms 4728 KB Output is correct
109 Correct 221 ms 5368 KB Output is correct
110 Correct 64 ms 4728 KB Output is correct
111 Correct 89 ms 8304 KB Output is correct
112 Correct 76 ms 7280 KB Output is correct
113 Correct 80 ms 6520 KB Output is correct
114 Correct 305 ms 6028 KB Output is correct
115 Correct 277 ms 4984 KB Output is correct
116 Correct 141 ms 5772 KB Output is correct
117 Correct 109 ms 6776 KB Output is correct
118 Correct 110 ms 6520 KB Output is correct
119 Correct 106 ms 6264 KB Output is correct
120 Correct 81 ms 5624 KB Output is correct
121 Correct 82 ms 5368 KB Output is correct
122 Correct 80 ms 5240 KB Output is correct
123 Correct 162 ms 6972 KB Output is correct
124 Correct 159 ms 6520 KB Output is correct
125 Correct 158 ms 6392 KB Output is correct
126 Correct 140 ms 5752 KB Output is correct
127 Correct 136 ms 5496 KB Output is correct
128 Correct 134 ms 5296 KB Output is correct