#include <bits/stdc++.h> #define REP(i,n) for (int _n=(n), i=0;i<_n;++i) #define FOR(i,a,b) for (int i=(a),_b=(b);i<=_b;++i) using std::int64_t; void init_io() { std::cin.tie(nullptr); std::ios::sync_with_stdio(false); } struct Query { int index = 0; int endpoint[2] = {}; int cost() const { return endpoint[1] - endpoint[0] + 1; } }; int group_size; std::vector<Query> queries; std::vector<Query> queries_by_endpoint[2]; std::vector<int64_t> group_value; std::vector<int64_t> answers; void read_input() { int n, num_queries; std::cin >> n >> group_size >> num_queries; std::vector<int> value; value.reserve(n); REP(i, n) { int x; std::cin >> x; value.push_back(x); } group_value.reserve(n - group_size + 1); int64_t gv = 0; REP(i, group_size) gv += value[i]; group_value.push_back(gv); FOR(i, group_size, n-1) { gv += value[i]; gv -= value[i - group_size]; group_value.push_back(gv); } queries.reserve(num_queries); answers.assign(num_queries, 0); REP(i, num_queries) { Query query; query.index = i; std::cin >> query.endpoint[0] >> query.endpoint[1]; query.endpoint[0] -= 1; query.endpoint[1] -= group_size; if (query.endpoint[1] >= query.endpoint[0]) { queries.push_back(query); } } } struct QueriesAtEndpoint { int64_t total_cost = 0; std::vector<Query> queries; }; struct PQEntry { int endpoint = 0; int position = 0; int64_t total_cost = 0; }; inline bool operator<(const PQEntry &a, const PQEntry &b) { return a.total_cost < b.total_cost; } void sort_queries() { REP(i,2) { queries_by_endpoint[i].reserve(queries.size()); } std::vector<char> unallocated(answers.size(), 0); int n = group_value.size(); std::vector<QueriesAtEndpoint> queries_at_endpoint[2]; REP(i,2) queries_at_endpoint[i].resize(n); for (const Query &query: queries) { unallocated[query.index] = 1; REP(i,2) { auto &qe = queries_at_endpoint[i][query.endpoint[i]]; qe.total_cost += query.cost(); qe.queries.push_back(query); } } std::priority_queue<PQEntry> pq; REP(i, 2) REP(x, n) { const QueriesAtEndpoint &qe = queries_at_endpoint[i][x]; if (qe.total_cost > 0) { PQEntry pq_entry; pq_entry.endpoint = i; pq_entry.position = x; pq_entry.total_cost = qe.total_cost; pq.push(pq_entry); } } while (!pq.empty()) { PQEntry pq_entry = pq.top(); pq.pop(); QueriesAtEndpoint &qe = queries_at_endpoint[pq_entry.endpoint][pq_entry.position]; if (qe.total_cost != pq_entry.total_cost) continue; const int endpoint2 = pq_entry.endpoint ^ 1; for (const Query &q : qe.queries) { if (unallocated[q.index]) { queries_by_endpoint[pq_entry.endpoint].push_back(q); unallocated[q.index] = 0; const int x2 = q.endpoint[endpoint2]; QueriesAtEndpoint &qe2 = queries_at_endpoint[endpoint2][x2]; qe2.total_cost -= q.cost(); if (qe2.total_cost > 0) { PQEntry pq_entry2; pq_entry2.endpoint = endpoint2; pq_entry2.position = x2; pq_entry2.total_cost = qe2.total_cost; pq.push(pq_entry2); } } } qe.queries.clear(); qe.total_cost = 0; } } std::vector<int64_t> solution; void generate_solution(const int start, const int to) { int64_t sol = 0; int limit = std::min(start + group_size - 1, to); int i = start; while (i <= limit) { sol = std::max(sol, group_value[i]); solution[i] = sol; ++i; } while (i <= to) { sol = std::max(sol, group_value[i] + solution[i - group_size]); solution[i] = sol; ++i; } } void solve(const std::vector<Query> &queries_to_solve) { int n = group_value.size(); solution.resize(n); auto it = queries_to_solve.begin(); while (it != queries_to_solve.end()) { auto to = it->endpoint[1]; auto jt = it + 1; while (jt != queries_to_solve.end() && it->endpoint[0] == jt->endpoint[0]) { to = std::max(to, jt->endpoint[1]); ++jt; } generate_solution(it->endpoint[0], to); while (it != jt) { auto answer = solution[it->endpoint[1]]; answers[it->index] = answer; ++it; } } } void print_answers() { for (auto answer : answers) { std::cout << answer << '\n'; } } int main() { init_io(); read_input(); sort_queries(); solve(queries_by_endpoint[0]); std::reverse(group_value.begin(), group_value.end()); int n = group_value.size(); for (Query &query : queries_by_endpoint[1]) { std::swap(query.endpoint[0], query.endpoint[1]); REP(i,2) query.endpoint[i] = n-1-query.endpoint[i]; } solve(queries_by_endpoint[1]); print_answers(); }
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 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 | #include <bits/stdc++.h> #define REP(i,n) for (int _n=(n), i=0;i<_n;++i) #define FOR(i,a,b) for (int i=(a),_b=(b);i<=_b;++i) using std::int64_t; void init_io() { std::cin.tie(nullptr); std::ios::sync_with_stdio(false); } struct Query { int index = 0; int endpoint[2] = {}; int cost() const { return endpoint[1] - endpoint[0] + 1; } }; int group_size; std::vector<Query> queries; std::vector<Query> queries_by_endpoint[2]; std::vector<int64_t> group_value; std::vector<int64_t> answers; void read_input() { int n, num_queries; std::cin >> n >> group_size >> num_queries; std::vector<int> value; value.reserve(n); REP(i, n) { int x; std::cin >> x; value.push_back(x); } group_value.reserve(n - group_size + 1); int64_t gv = 0; REP(i, group_size) gv += value[i]; group_value.push_back(gv); FOR(i, group_size, n-1) { gv += value[i]; gv -= value[i - group_size]; group_value.push_back(gv); } queries.reserve(num_queries); answers.assign(num_queries, 0); REP(i, num_queries) { Query query; query.index = i; std::cin >> query.endpoint[0] >> query.endpoint[1]; query.endpoint[0] -= 1; query.endpoint[1] -= group_size; if (query.endpoint[1] >= query.endpoint[0]) { queries.push_back(query); } } } struct QueriesAtEndpoint { int64_t total_cost = 0; std::vector<Query> queries; }; struct PQEntry { int endpoint = 0; int position = 0; int64_t total_cost = 0; }; inline bool operator<(const PQEntry &a, const PQEntry &b) { return a.total_cost < b.total_cost; } void sort_queries() { REP(i,2) { queries_by_endpoint[i].reserve(queries.size()); } std::vector<char> unallocated(answers.size(), 0); int n = group_value.size(); std::vector<QueriesAtEndpoint> queries_at_endpoint[2]; REP(i,2) queries_at_endpoint[i].resize(n); for (const Query &query: queries) { unallocated[query.index] = 1; REP(i,2) { auto &qe = queries_at_endpoint[i][query.endpoint[i]]; qe.total_cost += query.cost(); qe.queries.push_back(query); } } std::priority_queue<PQEntry> pq; REP(i, 2) REP(x, n) { const QueriesAtEndpoint &qe = queries_at_endpoint[i][x]; if (qe.total_cost > 0) { PQEntry pq_entry; pq_entry.endpoint = i; pq_entry.position = x; pq_entry.total_cost = qe.total_cost; pq.push(pq_entry); } } while (!pq.empty()) { PQEntry pq_entry = pq.top(); pq.pop(); QueriesAtEndpoint &qe = queries_at_endpoint[pq_entry.endpoint][pq_entry.position]; if (qe.total_cost != pq_entry.total_cost) continue; const int endpoint2 = pq_entry.endpoint ^ 1; for (const Query &q : qe.queries) { if (unallocated[q.index]) { queries_by_endpoint[pq_entry.endpoint].push_back(q); unallocated[q.index] = 0; const int x2 = q.endpoint[endpoint2]; QueriesAtEndpoint &qe2 = queries_at_endpoint[endpoint2][x2]; qe2.total_cost -= q.cost(); if (qe2.total_cost > 0) { PQEntry pq_entry2; pq_entry2.endpoint = endpoint2; pq_entry2.position = x2; pq_entry2.total_cost = qe2.total_cost; pq.push(pq_entry2); } } } qe.queries.clear(); qe.total_cost = 0; } } std::vector<int64_t> solution; void generate_solution(const int start, const int to) { int64_t sol = 0; int limit = std::min(start + group_size - 1, to); int i = start; while (i <= limit) { sol = std::max(sol, group_value[i]); solution[i] = sol; ++i; } while (i <= to) { sol = std::max(sol, group_value[i] + solution[i - group_size]); solution[i] = sol; ++i; } } void solve(const std::vector<Query> &queries_to_solve) { int n = group_value.size(); solution.resize(n); auto it = queries_to_solve.begin(); while (it != queries_to_solve.end()) { auto to = it->endpoint[1]; auto jt = it + 1; while (jt != queries_to_solve.end() && it->endpoint[0] == jt->endpoint[0]) { to = std::max(to, jt->endpoint[1]); ++jt; } generate_solution(it->endpoint[0], to); while (it != jt) { auto answer = solution[it->endpoint[1]]; answers[it->index] = answer; ++it; } } } void print_answers() { for (auto answer : answers) { std::cout << answer << '\n'; } } int main() { init_io(); read_input(); sort_queries(); solve(queries_by_endpoint[0]); std::reverse(group_value.begin(), group_value.end()); int n = group_value.size(); for (Query &query : queries_by_endpoint[1]) { std::swap(query.endpoint[0], query.endpoint[1]); REP(i,2) query.endpoint[i] = n-1-query.endpoint[i]; } solve(queries_by_endpoint[1]); print_answers(); } |