이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define F first
#define S second
#define all(x) x.begin(),x.end()
#define pii pair<int,int>
#define pb push_back
#define sz(x) (int)(x.size())
#define chmin(x,y) x=min(x,y)
#define chmax(x,y) x=max(x,y)
#define vi vector<int>
#define vp vector<pii>
#define vvi vector<vi>
#define ykh mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count())
#define __lg(x) 63-__builtin_clzll(x)
#define pow2(x) (1LL<<x)
void __print(int x) {cerr << x;}
void __print(float x) {cerr << x;}
void __print(double x) {cerr << x;}
void __print(long double x) {cerr << x;}
void __print(char x) {cerr << '\'' << x << '\'';}
void __print(const char *x) {cerr << '\"' << x << '\"';}
void __print(const string &x) {cerr << '\"' << x << '\"';}
void __print(bool x) {cerr << (x ? "true" : "false");}
template<typename T, typename V>
void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ','; __print(x.second); cerr << '}';}
template<typename T>
void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i: x) cerr << (f++ ? "," : ""), __print(i); cerr << "}";}
void _print() {cerr << "]\n";}
template <typename T, typename... V>
void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);}
#ifdef local
void setio(){freopen("/Users/iantsai/Library/Mobile Documents/com~apple~CloudDocs/cpp/Empty.md","r",stdin);}
#define debug(x...) cerr << "[" << #x << "] = ["; _print(x)
#else
void setio(){}
#define debug(x...)
#endif
void setIO(string s) {
freopen((s + ".in").c_str(), "r", stdin);
freopen((s + ".out").c_str(), "w", stdout);
}
#define why_TOI_is_so_de ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);setio();
const int mxn=2e5+5;
int n, m, c[mxn], v[mxn], ans, root[mxn], cnt=0;
vector<int>temp;
struct segtree{
struct node{
int l,r,sum,c;
}seg[mxn<<5];
int get(int v){
return lower_bound(all(temp),v)-temp.begin()+1;
}
int build(int l,int r){
int node=++cnt;
seg[node].sum=seg[node].c=0;
if(l==r){
return node;
}
int mm=l+r>>1;
seg[node].l=build(l,mm);
seg[node].r=build(mm+1,r);
return node;
}
int modify(int from,int l,int r,int p,int v){
int node=++cnt;
seg[node]=seg[from];
if(l==r){
seg[node].c++;
seg[node].sum+=v;
return node;
}
int mm=l+r>>1;
if(p<=mm){
seg[node].l=modify(seg[node].l,l,mm,p,v);
}
else{
seg[node].r=modify(seg[node].r,mm+1,r,p,v);
}
seg[node].sum=seg[seg[node].l].sum+seg[seg[node].r].sum;
seg[node].c=seg[seg[node].l].c+seg[seg[node].r].c;
return node;
}
void upd(int i,int v){
int x=get(v);
root[i]=modify(root[i-1],1,sz(temp),x,temp[x-1]);
}
int query(int u,int v,int l,int r,int k){
if(l==r){
return min(seg[v].c-seg[u].c,k)*temp[l-1];
}
int mm=l+r>>1;
int x=seg[seg[v].r].c-seg[seg[u].r].c;
int y=seg[seg[v].r].sum-seg[seg[u].r].sum;
if(x>=k){
return query(seg[u].r,seg[v].r,mm+1,r,k);
}
else{
return y+query(seg[u].l,seg[v].l,l,mm,k-x);
}
}
}tr;
int calc(int l,int r){
debug(l,r,m,tr.query(root[l-1],root[r],1,sz(temp),m));
return -2*c[r]+2*c[l]+tr.query(root[l-1],root[r],1,sz(temp),m);
}
void solve(int l,int r,int ql,int qr){
if(r<l)return;
int mm=l+r>>1;
int best=ql,mx=-1;
for(int i=ql;i+m-1<=mm and i<=qr;i++){
int x=calc(i,mm);
if(x>mx){
best=i;
mx=x;
}
}
chmax(ans,mx);
solve(l,mm-1,ql,best);
solve(mm+1,r,best,qr);
}
signed main(){
why_TOI_is_so_de;
cin>>n>>m;
vector<pii>ord;
for(int i=1;i<=n;i++){
int a,b;
cin>>a>>b;
ord.pb({b,a});
temp.pb(a);
}
sort(all(ord));
sort(all(temp));
temp.resize(unique(all(temp))-temp.begin());
root[0]=tr.build(1,sz(temp));
for(int i=1;i<=n;i++){
c[i]=ord[i-1].F;
v[i]=ord[i-1].S;
tr.upd(i,v[i]);
if(i>=2)debug(tr.query(root[i-2],root[i],1,sz(temp),2));
}
solve(m,n,1,n);
cout<<ans<<'\n';
#ifdef local
cerr << "\n[Time]: " << 1000.0 * clock() / CLOCKS_PER_SEC << " ms.\n";
#endif
}
/*
input:
*/
컴파일 시 표준 에러 (stderr) 메시지
cake3.cpp: In member function 'long long int segtree::build(long long int, long long int)':
cake3.cpp:62:17: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
62 | int mm=l+r>>1;
| ~^~
cake3.cpp: In member function 'long long int segtree::modify(long long int, long long int, long long int, long long int, long long int)':
cake3.cpp:75:17: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
75 | int mm=l+r>>1;
| ~^~
cake3.cpp: In member function 'long long int segtree::query(long long int, long long int, long long int, long long int, long long int)':
cake3.cpp:94:17: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
94 | int mm=l+r>>1;
| ~^~
cake3.cpp: In function 'void solve(long long int, long long int, long long int, long long int)':
cake3.cpp:111:13: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
111 | int mm=l+r>>1;
| ~^~
cake3.cpp: In function 'void setIO(std::string)':
cake3.cpp:42:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
42 | freopen((s + ".in").c_str(), "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
cake3.cpp:43:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
43 | freopen((s + ".out").c_str(), "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |