#include <bits/stdc++.h> using namespace std; using ll = long long; int ri() { int x; scanf("%d",&x); return x; } ll rll() { ll x; scanf("%lld",&x); return x; } void print(int x) { printf("%d",x); } void print(ll x) { printf("%lld",x); } template<class T> void remin(T& var, T x) { var = min(var,x); } template<class T> void remax(T& var, T x) { var = max(var,x); } #define REP(i,n) for(int i=0; i<int(n); ++i) int curr_pie = 0; struct Node { ll x; int num = 1; ll to() const { return x + 1LL * (num-1) * curr_pie; } bool deleted = false; list<int>::iterator where_linked; }; vector<Node> nodes; list<int> list_nodes; multimap<ll, int> breaks; vector<int> pie; vector<ll> res; ll time_until_merge(int curr) { auto it = next( nodes[curr].where_linked ); int next = *it; return ((nodes[next].x - nodes[curr].x) + nodes[curr].num-1) / nodes[curr].num; } ll get_break(int curr) { auto it = next( nodes[curr].where_linked ); int next = *it; return nodes[next].x - nodes[curr].to() - curr_pie; } void add_break(int curr) { auto it = nodes[curr].where_linked; if(next(it) == list_nodes.end()) return; breaks.insert({time_until_merge(curr), curr}); } void add_kli(ll x){ auto list_it = list_nodes.insert(list_nodes.end(), nodes.size()); Node node; node.x = x; node.num = 1; node.where_linked = list_it; nodes.emplace_back( std::move(node) ); } ll num_links_for_len(int len) { return 1LL * len * (len-1) / 2; } int main() { int num_kli = ri(); int num_pie = ri(); add_kli(0); REP(i,num_kli) { add_kli(rll()); } REP(i,num_pie) { pie.push_back( ri() ); } for(auto curr : list_nodes) { add_break(curr); } vector<int> pie_order; REP(i,pie.size()) pie_order.push_back(i); sort(pie_order.begin(), pie_order.end(), [](int a, int b){ return pie[a] < pie[b]; }); ll result = 0; ll num_links = 0; res.resize(pie.size()); REP(i, pie_order.size()){ auto prev_pie = curr_pie; curr_pie = pie[pie_order[i]]; result += 1LL * num_links * (curr_pie - prev_pie); //printf("curr_pie %d\n", curr_pie); while(breaks.size()) { auto curr = breaks.begin()->second; if(nodes[curr].deleted) { breaks.erase( breaks.begin() ); continue; } if(next( nodes[curr].where_linked ) == list_nodes.end()) { breaks.erase( breaks.begin() ); continue; } auto space = get_break( curr ); if(space > 0) break; breaks.erase( breaks.begin() ); int next = *std::next( nodes[curr].where_linked ); result += -space * 1LL * nodes[next].num; //printf("merge %d and %d\n", curr, next); num_links -= num_links_for_len(nodes[curr].num); num_links -= num_links_for_len(nodes[next].num); num_links += num_links_for_len(nodes[curr].num + nodes[next].num); nodes[curr].num += nodes[next].num; list_nodes.erase(nodes[next].where_linked); nodes[next].deleted = true; add_break( curr ); } res[pie_order[i]] = result; } for(auto r : res) printf("%lld\n",r); return 0; }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 | #include <bits/stdc++.h> using namespace std; using ll = long long; int ri() { int x; scanf("%d",&x); return x; } ll rll() { ll x; scanf("%lld",&x); return x; } void print(int x) { printf("%d",x); } void print(ll x) { printf("%lld",x); } template<class T> void remin(T& var, T x) { var = min(var,x); } template<class T> void remax(T& var, T x) { var = max(var,x); } #define REP(i,n) for(int i=0; i<int(n); ++i) int curr_pie = 0; struct Node { ll x; int num = 1; ll to() const { return x + 1LL * (num-1) * curr_pie; } bool deleted = false; list<int>::iterator where_linked; }; vector<Node> nodes; list<int> list_nodes; multimap<ll, int> breaks; vector<int> pie; vector<ll> res; ll time_until_merge(int curr) { auto it = next( nodes[curr].where_linked ); int next = *it; return ((nodes[next].x - nodes[curr].x) + nodes[curr].num-1) / nodes[curr].num; } ll get_break(int curr) { auto it = next( nodes[curr].where_linked ); int next = *it; return nodes[next].x - nodes[curr].to() - curr_pie; } void add_break(int curr) { auto it = nodes[curr].where_linked; if(next(it) == list_nodes.end()) return; breaks.insert({time_until_merge(curr), curr}); } void add_kli(ll x){ auto list_it = list_nodes.insert(list_nodes.end(), nodes.size()); Node node; node.x = x; node.num = 1; node.where_linked = list_it; nodes.emplace_back( std::move(node) ); } ll num_links_for_len(int len) { return 1LL * len * (len-1) / 2; } int main() { int num_kli = ri(); int num_pie = ri(); add_kli(0); REP(i,num_kli) { add_kli(rll()); } REP(i,num_pie) { pie.push_back( ri() ); } for(auto curr : list_nodes) { add_break(curr); } vector<int> pie_order; REP(i,pie.size()) pie_order.push_back(i); sort(pie_order.begin(), pie_order.end(), [](int a, int b){ return pie[a] < pie[b]; }); ll result = 0; ll num_links = 0; res.resize(pie.size()); REP(i, pie_order.size()){ auto prev_pie = curr_pie; curr_pie = pie[pie_order[i]]; result += 1LL * num_links * (curr_pie - prev_pie); //printf("curr_pie %d\n", curr_pie); while(breaks.size()) { auto curr = breaks.begin()->second; if(nodes[curr].deleted) { breaks.erase( breaks.begin() ); continue; } if(next( nodes[curr].where_linked ) == list_nodes.end()) { breaks.erase( breaks.begin() ); continue; } auto space = get_break( curr ); if(space > 0) break; breaks.erase( breaks.begin() ); int next = *std::next( nodes[curr].where_linked ); result += -space * 1LL * nodes[next].num; //printf("merge %d and %d\n", curr, next); num_links -= num_links_for_len(nodes[curr].num); num_links -= num_links_for_len(nodes[next].num); num_links += num_links_for_len(nodes[curr].num + nodes[next].num); nodes[curr].num += nodes[next].num; list_nodes.erase(nodes[next].where_linked); nodes[next].deleted = true; add_break( curr ); } res[pie_order[i]] = result; } for(auto r : res) printf("%lld\n",r); return 0; } |