QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#89135 | #5258. Mortgage | Denisov | TL | 2649ms | 62440kb | C++20 | 14.0kb | 2023-03-19 04:51:41 | 2023-03-19 04:51:42 |
Judging History
answer
#pragma GCC optimize("Ofast", "unroll-loops")
//#pragma GCC target("sse", "sse2", "sse3", "ssse3", "sse4")
#ifdef __APPLE__
#include <iostream>
#include <cmath>
#include <algorithm>
#include <stdio.h>
#include <cstdint>
#include <cstring>
#include <string>
#include <cstdlib>
#include <vector>
#include <bitset>
#include <map>
#include <queue>
#include <ctime>
#include <stack>
#include <set>
#include <list>
#include <random>
#include <deque>
#include <functional>
#include <iomanip>
#include <sstream>
#include <fstream>
#include <complex>
#include <numeric>
#include <immintrin.h>
#include <cassert>
#include <array>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <thread>
#else
#include <bits/stdc++.h>
#endif
#define all(a) a.begin(),a.end()
#define len(a) (int)(a.size())
#define mp make_pair
#define pb push_back
#define fir first
#define sec second
#define fi first
#define se second
using namespace std;
typedef pair<int, int> pii;
typedef long long ll;
typedef long double ld;
template<typename T>
bool umin(T &a, T b) {
if (b < a) {
a = b;
return true;
}
return false;
}
template<typename T>
bool umax(T &a, T b) {
if (a < b) {
a = b;
return true;
}
return false;
}
#if __APPLE__
#define D for (bool _FLAG = true; _FLAG; _FLAG = false)
#define LOG(...) print(#__VA_ARGS__" ::", __VA_ARGS__) << endl
template <class ...Ts> auto &print(Ts ...ts) { return ((cerr << ts << " "), ...); }
#else
#define D while (false)
#define LOG(...)
#endif
const int max_n = 2e5+10;
const int max_log=31;
const long long inf = 4000111222000111222LL;
/*struct Line {
long double k, b;
Line(long double k, long double b): k(k), b(b) {
}
long double get_value(long double x) const {
return k * x + b;
}
bool operator < (const Line &l) const {
return k > l.k || (k == l.k && b < l.b);
}
};
long double intersection(const Line &l1, const Line &l2) {
return (l2.b - l1.b) / (l1.k - l2.k);
}*/
struct Line {
long long k, b;
Line(long long k = 0, long long b = 0): k(k), b(b) {
}
long long get_value(long long x) const {
return k * x + b;
}
bool operator < (const Line &l) const {
return k > l.k || (k == l.k && b < l.b);
}
};
long long intersection(const Line &l1, const Line &l2) {
long long a = l2.b - l1.b;
long long b = l1.k - l2.k;
return a / b - ((a ^ b) < 0 && a % b);
}
template<int x_increase>
class LineContainer {
public:
LineContainer(): pos(0) {
}
void clear() {
pos = 0;
// last_x = (x_increase == 1 ? numeric_limits<long long>::min() : numeric_limits<long long>::max());
v.clear();
}
void add(const Line &l) {
if (!v.empty()){
assert(v.back().k >= l.k);
if (v.back().k == l.k) {
if (l.b >= v.back().b) {
return;
}
v.pop_back();
}
while (v.size() >= 2 && intersection(v[v.size() - 2], v.back()) >= intersection(v.back(), l)) {
v.pop_back();
}
}
v.push_back(l);
}
long long get_min(long long x) {
if (v.empty()) {
return inf;
}
if (x_increase == 1) {
// assert(last_x <= x);
// last_x = x;
pos = min(pos, v.size() - 1);
while (pos + 1 < v.size() && v[pos].get_value(x) > v[pos + 1].get_value(x)) {
++pos;
}
return v[pos].get_value(x);
} else if (x_increase == -1) {
// assert(last_x >= x);
// last_x = x;
while (v.size() >= 2 && v.back().get_value(x) > v[v.size() - 2].get_value(x)) {
v.pop_back();
}
return v.back().get_value(x);
} else {
int l = -1, r = v.size() - 1;
while (r - l > 1) {
int mid = (l + r) / 2;
if (x <= intersection(v[mid], v[mid + 1])) {
r = mid;
} else {
l = mid;
}
}
return v[r].get_value(x);
}
}
size_t pos;
// long long last_x;
vector<Line> v;
};
namespace fastio {
const int buf_size = 1 << 14, small = 30;
char buf_read[buf_size + small];
char buf_write[buf_size + small];
char *ptr_read = buf_read + buf_size;
char *ptr_write = buf_write;
long long read_int() {
#if __APPLE__
ll x;
cin>>x;
return x;
#endif
auto getChar = []() {
if (ptr_read == buf_read + buf_size){
buf_read[fread(buf_read, 1, buf_size, stdin)] = 0;
ptr_read = buf_read;
}
return *ptr_read++;
};
char c = getChar();
while (c && (c < '0' || c > '9') && c != '-') {
c = getChar();
}
long long z = 1;
if (c == '-') {
z = -1;
c = getChar();
}
long long res = 0;
while (c >= '0' && c <= '9'){
res = res * 10 + (c - '0');
c = getChar();
}
return z * res;
}
void write_flush() {
fwrite(buf_write, 1, ptr_write - buf_write, stdout);
ptr_write = buf_write;
}
void write_int(long long x) {
if (x < 0) {
*ptr_write++ = '-';
x = -x;
}
char *start = ptr_write;
if (!x) {
*ptr_write++ = '0';
}
while (x) {
*ptr_write++ = x % 10 + '0';
x /= 10;
}
reverse(start, ptr_write);
if (ptr_write >= buf_write + buf_size) {
write_flush();
}
}
void write_char(char c) {
*ptr_write++ = c;
if (ptr_write >= buf_write + buf_size) {
write_flush();
}
}
}
LineContainer<0> lc0;
LineContainer<+1> lc1;
int n,m;
ll pref[max_n];
int l[max_n];
int r[max_n];
int L[max_n];
int R[max_n];
int M[max_n];
int ans[max_n];
bool solved_req[max_n];
ll mn[max_n];
LineContainer<+1> lc1_tree[2*max_n];
//vector<int> req_vh[4*max_n];
//vector<int> on_left;
//vector<int> on_right_partially;
//vector<int> on_right_fully;
//
//void solve_mn_fast(int l1,int r1,vector<int> req)
//{
// if (l1==r1){
// for (auto i:req){
// mn[i]=min(mn[i],pref[l1]-1ll*l1*M[i]);
// }
// return;
// }
// int m1=(l1+r1)/2;
// vector<int> req_l;
// vector<int> req_r;
// on_left.clear();
// on_right_partially.clear();
// on_right_fully.clear();
// for (auto i:req){
// if (r[i]<=m1){
// req_l.pb(i);
// }
// else if (l[i]>=m1+1){
// req_r.pb(i);
// }
// else{
// req_l.pb(i);
// if (r[i]<r1){
// on_right_partially.pb(i);
// }
// else{
// on_right_fully.pb(i);
// }
// }
// }
// sort(all(on_right_partially),[&](const int& lhs,const int &rhs){
// return r[lhs]>r[rhs];
// });
// {
// lc0.clear();
// for (int j=m1+1;j<=r1;j++){
// lc0.add(Line(-j,pref[j]));
// while (!on_right_partially.empty() && r[on_right_partially.back()]==j){
// int id=on_right_partially.back();
// on_right_partially.pop_back();
// mn[id]=min(mn[id],lc0.get_min(M[id]));
// }
// }
// assert(on_right_partially.empty());
// }
// {
// lc1.clear();
// for (int j=m1+1;j<=r1;j++){
// lc1.add(Line(-j,pref[j]));
// }
// for (auto i:on_right_fully){
// mn[i]=min(mn[i],lc1.get_min(M[i]));
// }
// }
// solve_mn_fast(l1,m1,req_l);
// solve_mn_fast(m1+1,r1,req_r);
//}
void build(int v,int l,int r)
{
lc1_tree[v].v.reserve(r-l+1);
for (int i=l;i<=r;i++){
lc1_tree[v].add(Line(-i,pref[i]));
}
if (l==r){
return;
}
int m=(l+r)/2;
build(v+1,l,m);
build(v+2*(m-l+1),m+1,r);
}
//void push_req(int v,int l,int r,int tl,int tr,int id)
//{
// if (l>tr || r<tl){
// return;
// }
// if (l>=tl&&r<=tr){
// req_vh[v].pb(id);
// return;
// }
// int m=(l+r)/2;
// push_req(2*v,l,m,tl,tr,id);
// push_req(2*v+1,m+1,r,tl,tr,id);
//}
vector<int> req_on_deep[20];
void solve_mn_fast2(int v,int l1,int r1,int deep=0)
{
if (req_on_deep[deep].empty()){
return;
}
if (l1==r1){
for (auto i:req_on_deep[deep]){
mn[i]=min(mn[i],pref[l1]-1ll*l1*M[i]);
}
return;
}
int m1=(l1+r1)/2;
vector<int> left;
vector<int> right;
lc1_tree[v].pos = 0;
// lc1_tree[v].last_x = numeric_limits<long long>::min();
req_on_deep[deep+1].clear();
for (auto i:req_on_deep[deep]){
if (l[i]<=l1 && r1<=r[i]){
mn[i]=min(mn[i],lc1_tree[v].get_min(M[i]));
}
else{
if (l[i]<=m1){
req_on_deep[deep+1].pb(i);
}
}
}
solve_mn_fast2(v+1,l1,m1,deep+1);
req_on_deep[deep+1].clear();
for (auto i:req_on_deep[deep]){
if (l[i]<=l1 && r1<=r[i]){
// mn[i]=min(mn[i],lc1_tree[v].get_min(M[i]));
}
else{
if (r[i]>=m1+1){
req_on_deep[deep+1].pb(i);
}
}
}
solve_mn_fast2(v+2*(m1-l1+1),m1+1,r1,deep+1);
}
ll sub;
void get_kostya(int v, int tl, int tr, int l, int r,int id) {
LOG(v,tl,tr,l,r,id);
if (mn[id]-sub<0) return;
ll val = lc1_tree[v].get_min(M[id]);
if (val > mn[id]) return;
if (tl == l && tr == r) {
mn[id]=min(mn[id], val);
return;
}
if (val-sub>=0) return;
if (l <= -lc1_tree[v].v[lc1_tree[v].pos].k && -lc1_tree[v].v[lc1_tree[v].pos].k <= r) {
mn[id]=min(mn[id], val);
return;
}
int mid = (tl + tr) / 2;
if (r <= mid) {
get_kostya(v+1, tl, mid, l, r,id);
return;
} else if (l > mid) {
get_kostya(v + 2*(mid-tl+1), mid + 1, tr, l, r,id);
return;
}
get_kostya(v+1, tl, mid, l, mid,id);
get_kostya(v+2*(mid-tl+1), mid + 1, tr, mid + 1, r,id);
}
void solve_req(vector<int> req)
{
LOG("solve_req");
for (auto i:req){
solved_req[i]=1;
mn[i]=1e18;
}
// for (auto i:req){
// for (int j=l[i];j<=r[i];j++){
// mn[i]=min(mn[i],pref[j]-1ll*j*M[i]);
// }
// }
sort(all(req),[&](const int& lhs,const int &rhs){
return M[lhs]<M[rhs];
});
// req_on_deep[0]=req;
// solve_mn_fast2(1,0,n-1,0);
for (int i=0;i<2*max_n;i++){
lc1_tree[i].pos=0;
}
for (auto i:req){
if (l[i] - 1 >= 0) {
sub = pref[::l[i] - 1] - 1ll * (::l[i] - 1) * M[i];
}
else {
sub = +M[i];
}
get_kostya(1,0,n-1,l[i],r[i],i);
}
// solve_mn_fast(0,n-1,req);
// for (int i=0;i<4*max_n;i++){
// lc1_tree[i].pos = 0;
// lc1_tree[i].last_x = numeric_limits<long long>::min();
// sort(all(req_vh[i]),[&](const int& lhs,const int &rhs){
// return M[lhs]<M[rhs];
// });
// for (auto j:req_vh[i]){
// mn[j]=min(mn[j],lc1_tree[i].get_min(M[j]));
// }
// }
for (auto i:req){
LOG(i,M[i],mn[i]);
if (mn[i]-pref[l[i]-1]+1ll*(l[i]-1)*M[i]<0){
solved_req[i]=0;
}
}
}
/*
i,M[i],mn[i] :: 0 500000005 -3500000002
i,M[i],mn[i] :: 1 500000005 -1499999989
i,M[i],mn[i] :: 2 500000005 -1999999989
i,M[i],mn[i] :: 3 500000005 -2499999996
i,M[i],mn[i] :: 4 500000005 -4000000008
i,M[i],mn[i] :: 0 500000005 -3500000002
i,M[i],mn[i] :: 1 500000005 -1499999989
i,M[i],mn[i] :: 2 500000005 -1999999989
i,M[i],mn[i] :: 3 500000005 -2499999996
i,M[i],mn[i] :: 4 500000005 -4000000008
*/
const bool gen=0;
int main() {
// freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
if (gen){
n=2e5;
m=2e5;
}
else{
n=fastio::read_int();
m=fastio::read_int();
}
for (int i=0;i<n;i++){
int a;
if (gen){
a=rand()%10000;
}
else{
a=fastio::read_int();
}
pref[i] = (i==0 ? 0 : pref[i-1]) + a;
}
build(1,0,n-1);
for (int i=0;i<m;i++){
if (gen){
l[i]=rand()%n;
r[i]=rand()%n;
if (l[i]>r[i]){
swap(l[i],r[i]);
}
}
else{
int s,k;
s=fastio::read_int();
k=fastio::read_int();
l[i]=s-1;
r[i]=s+k-2;
}
// push_req(1,0,n-1,l[i],r[i],i);
L[i]=-1;
R[i]=min(pref[l[i]] - (l[i] ? pref[l[i] - 1] : 0ll), (pref[r[i]] - (l[i] ? pref[l[i] - 1] : 0ll)) / (r[i] - l[i] + 1));
if (R[i] < 0) R[i] = -1;
}
for (int iter=0;iter<max_log;iter++){
vector<int> req;
for (int i=0;i<m;i++){
if (L[i]!=R[i]){
M[i]=(L[i]+R[i]+1)/2;
req.pb(i);
}
}
solve_req(req);
for (int i=0;i<m;i++){
if (L[i]!=R[i]){
if (solved_req[i]){
L[i]=M[i];
}
else{
R[i]=M[i]-1;
}
}
}
}
for (int i=0;i<m;i++){
if (L[i]==-1){
cout<<"stay with parents"<<"\n";
}
else{
cout<<L[i]<<"\n";
}
}
}
/*
9 5
6 1 10 9 5 -2 3 1 -1
3 6
1 4
3 3
6 1
8 2
*/
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 16ms
memory: 21980kb
input:
10 10 21 18 19 18 16 15 13 13 13 10 10 1 6 4 7 3 2 2 6 5 2 6 3 2 4 1 1 5 6 3
output:
10 13 13 18 12 16 18 18 18 13
result:
ok 10 lines
Test #2:
score: 0
Accepted
time: 25ms
memory: 18144kb
input:
1000 1000 196 207 195 207 200 203 202 204 205 191 190 188 203 198 201 188 203 198 196 196 200 195 200 206 193 198 186 196 200 185 202 195 203 199 185 199 202 191 184 194 195 194 193 195 184 197 189 193 186 187 193 193 196 186 195 193 186 192 188 188 187 197 179 188 195 196 197 186 194 183 189 185 19...
output:
158 16 110 64 160 118 169 63 172 128 93 82 118 119 86 32 174 145 139 84 149 120 133 155 108 110 65 178 90 99 118 91 133 85 151 76 130 50 91 99 95 78 110 87 119 141 68 81 172 82 112 139 136 81 79 16 51 31 104 116 108 38 75 176 156 55 165 112 146 74 68 172 112 157 94 177 111 166 110 112 98 155 109 155...
result:
ok 1000 lines
Test #3:
score: 0
Accepted
time: 24ms
memory: 19884kb
input:
1000 1000 1023 912 1076 907 1015 1078 1075 1031 930 925 925 960 1013 893 1052 920 967 1046 960 1077 888 883 1045 1049 1034 1062 967 1021 986 938 871 916 901 1032 1003 1020 900 909 920 1048 884 859 1016 922 945 955 1002 1033 947 1025 970 862 929 908 912 956 873 845 933 873 921 918 904 884 1033 900 99...
output:
220 651 401 454 516 692 597 294 779 418 468 679 293 348 920 172 554 303 282 104 580 824 569 376 221 226 454 232 794 469 788 925 394 308 245 624 614 422 252 407 149 529 178 612 584 429 629 357 477 886 896 517 114 762 560 546 516 735 275 818 304 608 214 685 205 561 470 826 844 335 618 327 651 799 843 ...
result:
ok 1000 lines
Test #4:
score: 0
Accepted
time: 23ms
memory: 19284kb
input:
1000 1000 938 360 206 545 664 694 717 653 217 681 574 932 125 191 677 87 875 177 253 29 748 915 222 288 771 785 233 112 922 847 473 267 365 610 76 404 776 3 821 971 730 988 652 263 525 233 91 821 319 876 467 617 271 899 53 650 874 369 746 307 592 915 309 93 387 885 800 387 617 382 928 963 540 943 22...
output:
335 9 125 426 415 352 362 292 456 475 399 477 341 387 500 475 201 483 246 509 280 496 407 433 470 445 340 459 472 439 472 411 458 327 227 428 207 363 77 267 231 204 333 387 137 315 23 33 471 313 37 202 31 177 425 125 403 177 371 480 329 468 163 340 416 458 202 202 143 345 478 17 383 375 493 213 382 ...
result:
ok 1000 lines
Test #5:
score: 0
Accepted
time: 23ms
memory: 18904kb
input:
1000 1000 45 -80 65 -47 -17 75 49 79 38 65 25 64 53 74 0 -43 38 22 -48 -6 37 12 24 -36 -9 9 -64 -66 -31 91 -5 29 62 98 56 133 64 59 93 -6 97 89 7 121 125 21 39 95 131 60 -33 -4 6 63 52 44 25 -38 24 -40 94 135 129 129 13 87 28 -10 -13 168 128 140 1 152 52 88 99 107 88 81 176 18 73 29 93 165 50 146 13...
output:
419 136 345 312 338 1 490 557 177 84 50 688 315 784 141 244 stay with parents 53 408 218 764 451 127 371 823 805 349 552 167 523 stay with parents 500 3 52 411 51 699 422 435 278 187 134 252 585 108 583 44 55 109 310 167 879 712 stay with parents 336 311 108 stay with parents 51 434 stay with parent...
result:
ok 1000 lines
Test #6:
score: 0
Accepted
time: 23ms
memory: 19280kb
input:
1000 1000 210 201 195 208 191 209 201 195 201 192 199 200 205 188 200 198 197 207 189 199 194 197 206 186 198 204 203 187 200 201 197 197 191 197 192 191 192 188 189 194 197 191 192 182 184 195 192 183 200 193 193 193 192 190 190 194 182 197 180 191 184 181 189 184 186 180 194 197 194 187 196 196 18...
output:
92 92 102 93 89 114 149 105 72 114 87 71 77 171 90 36 127 136 39 165 92 62 157 135 101 82 87 78 136 99 141 73 109 141 23 73 76 112 58 110 105 97 84 181 19 100 127 76 108 109 49 150 165 159 152 101 108 94 25 87 67 78 127 56 37 50 161 131 142 40 47 73 117 169 87 157 109 121 59 75 184 80 67 82 48 61 65...
result:
ok 1000 lines
Test #7:
score: 0
Accepted
time: 19ms
memory: 19568kb
input:
1000 1000 1000 998 998 998 997 994 994 992 991 991 991 989 987 987 987 986 984 983 982 982 981 979 978 976 975 974 973 972 973 971 969 968 968 968 965 964 963 963 963 960 961 959 957 958 956 955 955 952 951 951 950 949 950 948 947 947 945 943 944 942 940 940 940 938 938 936 936 935 932 932 930 931 9...
output:
598 365 299 422 294 499 645 316 469 475 612 702 612 344 324 424 597 418 241 488 471 618 640 946 673 350 728 925 628 315 389 499 314 216 215 692 600 149 283 826 648 415 639 666 849 273 536 683 339 747 564 380 483 368 555 471 500 959 364 337 441 648 655 342 542 605 676 386 643 80 628 581 149 544 560 9...
result:
ok 1000 lines
Test #8:
score: 0
Accepted
time: 24ms
memory: 19600kb
input:
1000 1000 1 3 2 4 4 6 7 7 10 9 10 12 12 14 14 15 18 19 18 21 21 23 23 23 24 27 27 29 28 29 31 31 33 33 34 36 37 38 38 39 41 41 42 43 44 46 46 49 49 49 50 52 52 55 55 56 58 59 59 59 60 63 64 63 65 65 68 69 69 70 72 71 72 73 76 77 77 79 80 81 82 83 82 83 86 86 88 87 90 89 91 93 93 95 94 97 98 98 98 10...
output:
783 72 634 21 295 234 337 241 823 69 52 39 9 209 270 21 478 802 576 49 232 145 583 516 361 46 519 651 150 459 195 204 31 602 553 266 10 656 398 98 331 584 29 332 296 357 198 595 241 239 401 414 543 288 144 283 187 277 222 803 732 503 2 195 620 481 320 155 438 276 59 142 538 684 127 192 446 243 491 2...
result:
ok 1000 lines
Test #9:
score: 0
Accepted
time: 2649ms
memory: 62440kb
input:
200000 200000 4700652 -5734778 3503665 -6188002 8657410 2276806 -6431469 9627247 9673670 9221683 -3908423 -3069613 -653905 2608836 -4959288 -3108645 2053787 -5840353 259545 9847990 2267414 4231267 8319662 9920009 1973822 3854711 9434531 5938280 -5348422 -4062145 -5060918 -3946777 -7934092 7908462 83...
output:
531534449 60349244 698110493 683183122 239017077 155761236 198479042 224389969 53360136 280044750 690025597 359180266 155492131 100215867 7489229 371149856 257552128 756373561 625758735 10281065 33154057 331682245 187783019 356644222 284214063 184871804 232220709 282778596 659349476 574267601 271559...
result:
ok 200000 lines
Test #10:
score: -100
Time Limit Exceeded
input:
200000 200000 1000000000 1000000000 1000000000 994665379 991463472 993592249 990545330 1000000000 1000000000 993709443 993048450 993175969 1000000000 1000000000 1000000000 1000000000 996652523 1000000000 992737565 1000000000 995381198 999029778 994486837 1000000000 1000000000 1000000000 993523414 99...