Submission #68581

# Submission time Handle Problem Language Result Execution time Memory
68581 2018-08-17T11:56:08 Z funcsr Fireworks (APIO16_fireworks) C++17
100 / 100
1372 ms 169100 KB
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <string>
#include <cstring>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <cmath>
#include <iomanip>
#include <cassert>
#include <bitset>
#include <random>
#include <ctime>
using namespace std;

typedef pair<int, int> P;
#define rep(i, n) for (int i=0; i<(n); i++)
#define all(c) (c).begin(), (c).end()
#define uniq(c) c.erase(unique(all(c)), (c).end())
#define index(xs, x) (int)(lower_bound(all(xs), x) - xs.begin())
#define _1 first
#define _2 second
#define pb push_back
#define INF (1LL<<50)
#define MOD 1000000007
long long diff = 0;
struct DS {
  multiset<long long> left, right;

  void balance() {
    if ((left.size()+right.size())%2 == 0) {
      while (left.size() > right.size()) {
        auto it = --left.end();
        right.insert(*it);
        left.erase(it);
      }
      while (left.size() < right.size()) {
        auto it = right.begin();
        left.insert(*it);
        right.erase(it);
      }
    }
  }
  void add(long long x) {
    if (!right.empty() && x <= *right.begin()) left.insert(x);
    else right.insert(x);
    balance();
  }

  void extend(long long p) {
    if (left.empty()) return;
    auto it = --left.end();
    long long last = *it;
    left.erase(it);
    //cout<<"diff-="<<p<<"\n";
    //diff -= p;
    left.insert(last+p);
    long long lp = *right.begin();
    right.erase(right.begin());

    while (!right.empty() && *right.begin() < INF) {
      auto it = right.begin();
      long long cur = *it;
      //cout<<"diff-=INF\n";
      diff -= INF;
      right.erase(it);
      right.insert(cur+INF);
    }
    right.insert(lp+p);
    assert(left.size() == right.size());
    //balance();
  }
  int size() {
    assert(left.size() == right.size());
    return left.size();
  }
  vector<long long> enumerate() {
    vector<long long> ret;
    for (long long x : left) ret.pb(x);
    for (long long x : right) ret.pb(x);
    return ret;
  }
};

int N, M;
int U[300000], C[300000];
DS dp[300000];

signed main() {
  ios::sync_with_stdio(false); cin.tie(0);
  cin >> N >> M;
  for (int i=1; i<N+M; i++) {
    cin >> U[i] >> C[i];
    U[i]--;
  }
  for (int i=N+M-1; i>0; i--) {
    if (i >= N) dp[i].add(0), dp[i].add(0);
    dp[i].extend(C[i]);
    //cout<<"dp["<<i<<"] = {"; for (long long x : dp[i].enumerate()) cout<<x<<",";cout<<"}\n";

    if (dp[U[i]].size() < dp[i].size()) swap(dp[U[i]], dp[i]);
    vector<long long> vs = dp[i].enumerate();
    for (long long x : vs) dp[U[i]].add(x);
  }
  long long sum = 0;
  //cout<<"dp["<<0<<"] = {"; for (long long x : dp[0].enumerate()) cout<<x<<",";cout<<"}\n";
  long long X = *dp[0].left.rbegin();
  //cout<<"X="<<X<<"\n";
  vector<long long> vs = dp[0].enumerate();
  for (long long x : vs) sum += abs(X-x);
  //cout<<"sum="<<sum<<", diff="<<diff<<"\n";
  sum += diff;
  sum /= 2;
  cout << sum << "\n";
  return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 28 ms 28536 KB Output is correct
2 Correct 27 ms 28652 KB Output is correct
3 Correct 27 ms 28732 KB Output is correct
4 Correct 33 ms 28732 KB Output is correct
5 Correct 26 ms 28732 KB Output is correct
6 Correct 26 ms 28804 KB Output is correct
7 Correct 27 ms 28804 KB Output is correct
8 Correct 27 ms 28804 KB Output is correct
9 Correct 28 ms 28824 KB Output is correct
10 Correct 26 ms 28824 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 27 ms 28824 KB Output is correct
2 Correct 34 ms 28824 KB Output is correct
3 Correct 26 ms 28932 KB Output is correct
4 Correct 27 ms 28932 KB Output is correct
5 Correct 27 ms 28932 KB Output is correct
6 Correct 27 ms 28932 KB Output is correct
7 Correct 27 ms 28932 KB Output is correct
8 Correct 27 ms 28932 KB Output is correct
9 Correct 27 ms 28932 KB Output is correct
10 Correct 27 ms 28984 KB Output is correct
11 Correct 29 ms 28984 KB Output is correct
12 Correct 27 ms 28984 KB Output is correct
13 Correct 27 ms 28984 KB Output is correct
14 Correct 27 ms 28984 KB Output is correct
15 Correct 28 ms 28996 KB Output is correct
16 Correct 28 ms 28996 KB Output is correct
17 Correct 26 ms 29028 KB Output is correct
18 Correct 27 ms 29028 KB Output is correct
19 Correct 28 ms 29028 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 28 ms 28536 KB Output is correct
2 Correct 27 ms 28652 KB Output is correct
3 Correct 27 ms 28732 KB Output is correct
4 Correct 33 ms 28732 KB Output is correct
5 Correct 26 ms 28732 KB Output is correct
6 Correct 26 ms 28804 KB Output is correct
7 Correct 27 ms 28804 KB Output is correct
8 Correct 27 ms 28804 KB Output is correct
9 Correct 28 ms 28824 KB Output is correct
10 Correct 26 ms 28824 KB Output is correct
11 Correct 27 ms 28824 KB Output is correct
12 Correct 34 ms 28824 KB Output is correct
13 Correct 26 ms 28932 KB Output is correct
14 Correct 27 ms 28932 KB Output is correct
15 Correct 27 ms 28932 KB Output is correct
16 Correct 27 ms 28932 KB Output is correct
17 Correct 27 ms 28932 KB Output is correct
18 Correct 27 ms 28932 KB Output is correct
19 Correct 27 ms 28932 KB Output is correct
20 Correct 27 ms 28984 KB Output is correct
21 Correct 29 ms 28984 KB Output is correct
22 Correct 27 ms 28984 KB Output is correct
23 Correct 27 ms 28984 KB Output is correct
24 Correct 27 ms 28984 KB Output is correct
25 Correct 28 ms 28996 KB Output is correct
26 Correct 28 ms 28996 KB Output is correct
27 Correct 26 ms 29028 KB Output is correct
28 Correct 27 ms 29028 KB Output is correct
29 Correct 28 ms 29028 KB Output is correct
30 Correct 27 ms 29028 KB Output is correct
31 Correct 27 ms 29048 KB Output is correct
32 Correct 28 ms 29192 KB Output is correct
33 Correct 30 ms 29344 KB Output is correct
34 Correct 32 ms 29504 KB Output is correct
35 Correct 32 ms 29636 KB Output is correct
36 Correct 33 ms 29856 KB Output is correct
37 Correct 33 ms 30040 KB Output is correct
38 Correct 35 ms 30112 KB Output is correct
39 Correct 34 ms 30184 KB Output is correct
40 Correct 30 ms 30184 KB Output is correct
41 Correct 31 ms 30184 KB Output is correct
42 Correct 31 ms 30184 KB Output is correct
43 Correct 36 ms 30412 KB Output is correct
44 Correct 33 ms 30412 KB Output is correct
45 Correct 35 ms 30544 KB Output is correct
46 Correct 42 ms 31596 KB Output is correct
47 Correct 40 ms 31768 KB Output is correct
48 Correct 38 ms 31768 KB Output is correct
49 Correct 44 ms 31768 KB Output is correct
50 Correct 36 ms 31768 KB Output is correct
51 Correct 34 ms 31768 KB Output is correct
52 Correct 40 ms 31768 KB Output is correct
53 Correct 37 ms 31768 KB Output is correct
54 Correct 36 ms 31768 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 28 ms 28536 KB Output is correct
2 Correct 27 ms 28652 KB Output is correct
3 Correct 27 ms 28732 KB Output is correct
4 Correct 33 ms 28732 KB Output is correct
5 Correct 26 ms 28732 KB Output is correct
6 Correct 26 ms 28804 KB Output is correct
7 Correct 27 ms 28804 KB Output is correct
8 Correct 27 ms 28804 KB Output is correct
9 Correct 28 ms 28824 KB Output is correct
10 Correct 26 ms 28824 KB Output is correct
11 Correct 27 ms 28824 KB Output is correct
12 Correct 34 ms 28824 KB Output is correct
13 Correct 26 ms 28932 KB Output is correct
14 Correct 27 ms 28932 KB Output is correct
15 Correct 27 ms 28932 KB Output is correct
16 Correct 27 ms 28932 KB Output is correct
17 Correct 27 ms 28932 KB Output is correct
18 Correct 27 ms 28932 KB Output is correct
19 Correct 27 ms 28932 KB Output is correct
20 Correct 27 ms 28984 KB Output is correct
21 Correct 29 ms 28984 KB Output is correct
22 Correct 27 ms 28984 KB Output is correct
23 Correct 27 ms 28984 KB Output is correct
24 Correct 27 ms 28984 KB Output is correct
25 Correct 28 ms 28996 KB Output is correct
26 Correct 28 ms 28996 KB Output is correct
27 Correct 26 ms 29028 KB Output is correct
28 Correct 27 ms 29028 KB Output is correct
29 Correct 28 ms 29028 KB Output is correct
30 Correct 27 ms 29028 KB Output is correct
31 Correct 27 ms 29048 KB Output is correct
32 Correct 28 ms 29192 KB Output is correct
33 Correct 30 ms 29344 KB Output is correct
34 Correct 32 ms 29504 KB Output is correct
35 Correct 32 ms 29636 KB Output is correct
36 Correct 33 ms 29856 KB Output is correct
37 Correct 33 ms 30040 KB Output is correct
38 Correct 35 ms 30112 KB Output is correct
39 Correct 34 ms 30184 KB Output is correct
40 Correct 30 ms 30184 KB Output is correct
41 Correct 31 ms 30184 KB Output is correct
42 Correct 31 ms 30184 KB Output is correct
43 Correct 36 ms 30412 KB Output is correct
44 Correct 33 ms 30412 KB Output is correct
45 Correct 35 ms 30544 KB Output is correct
46 Correct 42 ms 31596 KB Output is correct
47 Correct 40 ms 31768 KB Output is correct
48 Correct 38 ms 31768 KB Output is correct
49 Correct 44 ms 31768 KB Output is correct
50 Correct 36 ms 31768 KB Output is correct
51 Correct 34 ms 31768 KB Output is correct
52 Correct 40 ms 31768 KB Output is correct
53 Correct 37 ms 31768 KB Output is correct
54 Correct 36 ms 31768 KB Output is correct
55 Correct 49 ms 32764 KB Output is correct
56 Correct 150 ms 40448 KB Output is correct
57 Correct 230 ms 48752 KB Output is correct
58 Correct 283 ms 55620 KB Output is correct
59 Correct 491 ms 64512 KB Output is correct
60 Correct 522 ms 74592 KB Output is correct
61 Correct 586 ms 83020 KB Output is correct
62 Correct 682 ms 92488 KB Output is correct
63 Correct 821 ms 104164 KB Output is correct
64 Correct 801 ms 104164 KB Output is correct
65 Correct 174 ms 104164 KB Output is correct
66 Correct 145 ms 104164 KB Output is correct
67 Correct 147 ms 104164 KB Output is correct
68 Correct 591 ms 104164 KB Output is correct
69 Correct 663 ms 104164 KB Output is correct
70 Correct 673 ms 104164 KB Output is correct
71 Correct 1360 ms 169100 KB Output is correct
72 Correct 1372 ms 169100 KB Output is correct
73 Correct 1060 ms 169100 KB Output is correct
74 Correct 1091 ms 169100 KB Output is correct
75 Correct 1073 ms 169100 KB Output is correct
76 Correct 1068 ms 169100 KB Output is correct
77 Correct 1095 ms 169100 KB Output is correct
78 Correct 1002 ms 169100 KB Output is correct
79 Correct 840 ms 169100 KB Output is correct