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;
struct node
{
int l, r, v;
long long sum;
}nd;
node pst[5000010];
pair<int, int>A[200010];
map<int, int>x, y;
map<int, int>::iterator it;
int t, n, q, a, b, c, d, i, j, sz, m, rt, cnt, root[200010], opt[200010], X[200010], Y[200010];
long long dap=-1e18;
void make_node(){pst[sz++]={-1, -1, 0, 0};}
void init(int id, int s, int e)
{
if(s==e)return;
int m=s+e>>1;
if(pst[id].l==-1)
{
pst[id].l=sz;
make_node();
}
init(pst[id].l, s, m);
if(pst[id].r==-1)
{
pst[id].r=sz;
make_node();
}
init(pst[id].r, m+1, e);
}
void update(int prev, int id, int s, int e, int x)
{
pst[id].v=pst[prev].v+1;
pst[id].sum=pst[prev].sum+Y[x];
if(s==e)return;
int m=s+e>>1;
if(x<=m)
{
pst[id].l=sz;
make_node();
pst[id].r=pst[prev].r;
update(pst[prev].l, pst[id].l, s, m, x);
}
else
{
pst[id].r=sz;
make_node();
pst[id].l=pst[prev].l;
update(pst[prev].r, pst[id].r, m+1, e, x);
}
}
/*long long get(int prev, int id, int s, int e, int k)
{
if(s==e)return Y[s];
long long d=pst[pst[id].r].sum-pst[pst[prev].r].sum;
int ee=pst[pst[id].r].v-pst[pst[prev].r].v;
int m=s+e>>1;
if(k<=ee)return get(pst[prev].r, pst[id].r, m+1, e, k);
else return d+get(pst[prev].l, pst[id].l, s, m, k-ee);
}*/
long long get(int prev, int id, int s, int e, int k)
{
vector<int>v;
for(int i=prev;i<=id;i++)v.push_back(Y[A[i].second]);
sort(v.begin(), v.end());
long long ans=0;
for(int i=(int)v.size()-1;i>=(int)v.size()-k;i--)ans+=v[i];
return ans;
}
void dnc(int s, int e, int l, int r)
{
if(s>e)return;
int mid=s+e>>1;
long long ans=-1e18;
int pos;
for(int i=max(l, mid+m-1);i<=r;i++)
{
long long res=get(mid+1, i-1, 1, cnt, m-2)+Y[A[mid].second]+Y[A[i].second]-(X[A[i].first]-X[A[mid].first]);
if(ans<res)
{
ans=res;
pos=i;
}
}
dap=max(dap, ans);
opt[mid]=pos;
dnc(s, mid-1, l, pos);
dnc(mid+1, e, pos, r);
}
main()
{
for(scanf("%d %d", &n, &m);i++<n;)
{
scanf("%d %d", &b, &a);
a*=2;
A[i].first=a,A[i].second=b;
x[a]++;y[b]++;
}
sort(A+1, A+n+1);
for(it=x.begin();it!=x.end();it++)it->second=++cnt,X[cnt]=it->first;cnt=0;
for(it=y.begin();it!=y.end();it++)it->second=++cnt,Y[cnt]=it->first;
make_node();
init(0, 1, cnt);
for(i=0;i++<n;)
{
A[i].first=x[A[i].first];
A[i].second=y[A[i].second];
root[i]=sz;
make_node();
update(root[i-1], root[i], 1, cnt, A[i].second);
}
dnc(1, n-m+1, 1, n);
cout<<dap;
}
Compilation message (stderr)
cake3.cpp: In function 'void init(int, int, int)':
cake3.cpp:18:12: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
18 | int m=s+e>>1;
| ~^~
cake3.cpp: In function 'void update(int, int, int, int, int)':
cake3.cpp:37:12: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
37 | int m=s+e>>1;
| ~^~
cake3.cpp: In function 'void dnc(int, int, int, int)':
cake3.cpp:74:14: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
74 | int mid=s+e>>1;
| ~^~
cake3.cpp: At global scope:
cake3.cpp:91:6: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
91 | main()
| ^
cake3.cpp: In function 'int main()':
cake3.cpp:101:5: warning: this 'for' clause does not guard... [-Wmisleading-indentation]
101 | for(it=x.begin();it!=x.end();it++)it->second=++cnt,X[cnt]=it->first;cnt=0;
| ^~~
cake3.cpp:101:73: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'for'
101 | for(it=x.begin();it!=x.end();it++)it->second=++cnt,X[cnt]=it->first;cnt=0;
| ^~~
cake3.cpp:93:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
93 | for(scanf("%d %d", &n, &m);i++<n;)
| ~~~~~^~~~~~~~~~~~~~~~~
cake3.cpp:95:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
95 | scanf("%d %d", &b, &a);
| ~~~~~^~~~~~~~~~~~~~~~~
cake3.cpp: In function 'void dnc(int, int, int, int)':
cake3.cpp:88:8: warning: 'pos' may be used uninitialized in this function [-Wmaybe-uninitialized]
88 | dnc(s, mid-1, l, pos);
| ~~~^~~~~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |