This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include "bits/stdc++.h"
using namespace std;
int NODES = 0;
int L[50000001],R[50000001],ST[50000001];
int newleaf(int val){
NODES++;
L[NODES] = -1;
R[NODES] = -1;
ST[NODES] = val;
return NODES;
}
int newparent(int l,int r){
NODES++;
L[NODES] = l;
R[NODES] = r;
ST[NODES] = ST[l]+ST[r];
return NODES;
}
int build(int l,int r){
if(l==r)return newleaf(0);
int md = (l+r)/2;
return newparent(build(l,md),build(md+1,r));
}
int update(int p,int l,int r,int idx){
if(l==r)return newleaf(1+ST[p]);
int md = (l+r)/2;
if(idx<=md)return newparent(update(L[p],l,md,idx),R[p]);
else return newparent(L[p],update(R[p],md+1,r,idx));
}
int query(int p1,int p2,int l,int r,int lq,int rq){
if(l>=lq&&r<=rq){
return ST[p2]-ST[p1];
}
if(r<lq||l>rq)return 0;
int md = (l+r)/2;
return query(L[p1],L[p2],l,md,lq,rq)+query(R[p1],R[p2],md+1,r,lq,rq);
}
int root[500001],n;
vector<pair<int,int>> v;
void init(int N,int A[],int B[]){
n = N;
for(int i = 0;i<N;i++){
v.push_back({A[i],B[i]});
}
sort(v.begin(),v.end());
root[0] = build(1,n);
for(int i = 1;i<=v.size();i++){
root[i] = update(root[i-1],1,n,v[i-1].second);
}
}
int w(int a,int b){
int it = lower_bound(v.begin(),v.end(),make_pair(a+1,0))-v.begin();
int it2 = lower_bound(v.begin(),v.end(),make_pair(b+1,0))-v.begin();
return query(root[it],root[it2],1,n,b,n);
}
bool can(int m,int k[]){
vector<int> K;
K.push_back(0);
for(int i = 0;i<m;i++){
K.push_back(k[i]);
}
sort(K.begin(),K.end());
long long dp[m+1]; dp[0] =0;
deque<pair<long long,long long> > v; // start pos, best-k
v.push_back(make_pair(0LL,0LL));
for (int x=1; x <= m; x++) {
int lol = (--lower_bound(v.begin(), v.end(), make_pair((long long)x+1, 0LL)))->second;
dp[x] = dp[lol] + w(K[lol], K[x]) - K[x];
while(v.size()){
int y = (v.size()==1?m:v[1].first-1), oldk = v[0].second;
if (y <= x || (y > x && dp[x] + w(K[x], K[y]) - K[y] <= dp[oldk] + w(K[oldk], K[y]) - K[y]))
v.pop_front();
else {
//cerr<<x<<endl;
int lo = max(x+1,(int)v[0].first), hi = m , ans = m+1;
while(lo <= hi) {
int mid = (lo+hi)/2;
//cout<<mid<<endl;
if (dp[x] + w(K[x], K[mid]) - K[mid] <= dp[oldk] + w(K[oldk], K[mid]) - K[mid]){
lo = mid+1;
ans = mid;
}else hi = mid-1;
}
if (ans != m+1){
v[0].first = ans+1;
v.push_front(make_pair(x+1, x));
}else{
if(v[0].first>x+1){
v.push_front(make_pair(x+1, x));
}
}
break;
}
}
if (v.size() == 0)
v.push_back(make_pair(x+1LL, x));
}
for(int i = 1;i<=m;i++){
if(dp[i]<0)return 0;
}
return 1;
}
Compilation message (stderr)
teams.cpp: In function 'void init(int, int*, int*)':
teams.cpp:47:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
47 | for(int i = 1;i<=v.size();i++){
| ~^~~~~~~~~~
teams.cpp: In function 'int w(int, int)':
teams.cpp:52:61: warning: conversion from '__gnu_cxx::__normal_iterator<std::pair<int, int>*, std::vector<std::pair<int, int> > >::difference_type' {aka 'long int'} to 'int' may change value [-Wconversion]
52 | int it = lower_bound(v.begin(),v.end(),make_pair(a+1,0))-v.begin();
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~
teams.cpp:53:62: warning: conversion from '__gnu_cxx::__normal_iterator<std::pair<int, int>*, std::vector<std::pair<int, int> > >::difference_type' {aka 'long int'} to 'int' may change value [-Wconversion]
53 | int it2 = lower_bound(v.begin(),v.end(),make_pair(b+1,0))-v.begin();
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~
teams.cpp: In function 'bool can(int, int*)':
teams.cpp:64:39: warning: declaration of 'v' shadows a global declaration [-Wshadow]
64 | deque<pair<long long,long long> > v; // start pos, best-k
| ^
teams.cpp:39:23: note: shadowed declaration is here
39 | vector<pair<int,int>> v;
| ^
teams.cpp:67:88: warning: conversion from 'long long int' to 'int' may change value [-Wconversion]
67 | int lol = (--lower_bound(v.begin(), v.end(), make_pair((long long)x+1, 0LL)))->second;
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~
teams.cpp:70:33: warning: conversion from 'long long int' to 'int' may change value [-Wconversion]
70 | int y = (v.size()==1?m:v[1].first-1), oldk = v[0].second;
| ~~~~~~~~~~~~^~~~~~~~~~~~~~~~
teams.cpp:70:63: warning: conversion from 'long long int' to 'int' may change value [-Wconversion]
70 | int y = (v.size()==1?m:v[1].first-1), oldk = v[0].second;
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |