# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
986734 |
2024-05-21T06:33:23 Z |
yeediot |
Cake 3 (JOI19_cake3) |
C++17 |
|
838 ms |
145208 KB |
#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=-8e18, 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=-8e18;
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:
*/
Compilation message
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 |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
344 KB |
Output is correct |
13 |
Correct |
0 ms |
344 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
344 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
1 ms |
348 KB |
Output is correct |
22 |
Correct |
0 ms |
348 KB |
Output is correct |
23 |
Correct |
0 ms |
348 KB |
Output is correct |
24 |
Correct |
0 ms |
348 KB |
Output is correct |
25 |
Correct |
1 ms |
348 KB |
Output is correct |
26 |
Correct |
0 ms |
348 KB |
Output is correct |
27 |
Correct |
0 ms |
348 KB |
Output is correct |
28 |
Correct |
1 ms |
348 KB |
Output is correct |
29 |
Correct |
0 ms |
348 KB |
Output is correct |
30 |
Correct |
1 ms |
348 KB |
Output is correct |
31 |
Correct |
0 ms |
348 KB |
Output is correct |
32 |
Correct |
0 ms |
348 KB |
Output is correct |
33 |
Correct |
1 ms |
348 KB |
Output is correct |
34 |
Correct |
1 ms |
348 KB |
Output is correct |
35 |
Correct |
1 ms |
348 KB |
Output is correct |
36 |
Correct |
0 ms |
348 KB |
Output is correct |
37 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
344 KB |
Output is correct |
13 |
Correct |
0 ms |
344 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
344 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
1 ms |
348 KB |
Output is correct |
22 |
Correct |
0 ms |
348 KB |
Output is correct |
23 |
Correct |
0 ms |
348 KB |
Output is correct |
24 |
Correct |
0 ms |
348 KB |
Output is correct |
25 |
Correct |
1 ms |
348 KB |
Output is correct |
26 |
Correct |
0 ms |
348 KB |
Output is correct |
27 |
Correct |
0 ms |
348 KB |
Output is correct |
28 |
Correct |
1 ms |
348 KB |
Output is correct |
29 |
Correct |
0 ms |
348 KB |
Output is correct |
30 |
Correct |
1 ms |
348 KB |
Output is correct |
31 |
Correct |
0 ms |
348 KB |
Output is correct |
32 |
Correct |
0 ms |
348 KB |
Output is correct |
33 |
Correct |
1 ms |
348 KB |
Output is correct |
34 |
Correct |
1 ms |
348 KB |
Output is correct |
35 |
Correct |
1 ms |
348 KB |
Output is correct |
36 |
Correct |
0 ms |
348 KB |
Output is correct |
37 |
Correct |
0 ms |
348 KB |
Output is correct |
38 |
Correct |
2 ms |
1304 KB |
Output is correct |
39 |
Correct |
3 ms |
1624 KB |
Output is correct |
40 |
Correct |
2 ms |
1372 KB |
Output is correct |
41 |
Correct |
2 ms |
1372 KB |
Output is correct |
42 |
Correct |
2 ms |
1384 KB |
Output is correct |
43 |
Correct |
2 ms |
1372 KB |
Output is correct |
44 |
Correct |
2 ms |
1372 KB |
Output is correct |
45 |
Correct |
2 ms |
1340 KB |
Output is correct |
46 |
Correct |
2 ms |
1368 KB |
Output is correct |
47 |
Correct |
3 ms |
1372 KB |
Output is correct |
48 |
Correct |
3 ms |
1372 KB |
Output is correct |
49 |
Correct |
2 ms |
1368 KB |
Output is correct |
50 |
Correct |
2 ms |
1372 KB |
Output is correct |
51 |
Correct |
2 ms |
1372 KB |
Output is correct |
52 |
Correct |
2 ms |
1372 KB |
Output is correct |
53 |
Correct |
2 ms |
1372 KB |
Output is correct |
54 |
Correct |
2 ms |
1372 KB |
Output is correct |
55 |
Correct |
2 ms |
1368 KB |
Output is correct |
56 |
Correct |
2 ms |
1372 KB |
Output is correct |
57 |
Correct |
2 ms |
1372 KB |
Output is correct |
58 |
Correct |
2 ms |
1372 KB |
Output is correct |
59 |
Correct |
1 ms |
604 KB |
Output is correct |
60 |
Correct |
2 ms |
604 KB |
Output is correct |
61 |
Correct |
1 ms |
600 KB |
Output is correct |
62 |
Correct |
1 ms |
604 KB |
Output is correct |
63 |
Correct |
1 ms |
604 KB |
Output is correct |
64 |
Correct |
1 ms |
604 KB |
Output is correct |
65 |
Correct |
2 ms |
1252 KB |
Output is correct |
66 |
Correct |
2 ms |
1252 KB |
Output is correct |
67 |
Correct |
2 ms |
1372 KB |
Output is correct |
68 |
Correct |
2 ms |
1372 KB |
Output is correct |
69 |
Correct |
2 ms |
1196 KB |
Output is correct |
70 |
Correct |
2 ms |
1372 KB |
Output is correct |
71 |
Correct |
1 ms |
808 KB |
Output is correct |
72 |
Correct |
1 ms |
856 KB |
Output is correct |
73 |
Correct |
2 ms |
1372 KB |
Output is correct |
74 |
Correct |
2 ms |
1372 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
344 KB |
Output is correct |
13 |
Correct |
0 ms |
344 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
344 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
1 ms |
348 KB |
Output is correct |
22 |
Correct |
0 ms |
348 KB |
Output is correct |
23 |
Correct |
0 ms |
348 KB |
Output is correct |
24 |
Correct |
0 ms |
348 KB |
Output is correct |
25 |
Correct |
1 ms |
348 KB |
Output is correct |
26 |
Correct |
0 ms |
348 KB |
Output is correct |
27 |
Correct |
0 ms |
348 KB |
Output is correct |
28 |
Correct |
1 ms |
348 KB |
Output is correct |
29 |
Correct |
0 ms |
348 KB |
Output is correct |
30 |
Correct |
1 ms |
348 KB |
Output is correct |
31 |
Correct |
0 ms |
348 KB |
Output is correct |
32 |
Correct |
0 ms |
348 KB |
Output is correct |
33 |
Correct |
1 ms |
348 KB |
Output is correct |
34 |
Correct |
1 ms |
348 KB |
Output is correct |
35 |
Correct |
1 ms |
348 KB |
Output is correct |
36 |
Correct |
0 ms |
348 KB |
Output is correct |
37 |
Correct |
0 ms |
348 KB |
Output is correct |
38 |
Correct |
2 ms |
1304 KB |
Output is correct |
39 |
Correct |
3 ms |
1624 KB |
Output is correct |
40 |
Correct |
2 ms |
1372 KB |
Output is correct |
41 |
Correct |
2 ms |
1372 KB |
Output is correct |
42 |
Correct |
2 ms |
1384 KB |
Output is correct |
43 |
Correct |
2 ms |
1372 KB |
Output is correct |
44 |
Correct |
2 ms |
1372 KB |
Output is correct |
45 |
Correct |
2 ms |
1340 KB |
Output is correct |
46 |
Correct |
2 ms |
1368 KB |
Output is correct |
47 |
Correct |
3 ms |
1372 KB |
Output is correct |
48 |
Correct |
3 ms |
1372 KB |
Output is correct |
49 |
Correct |
2 ms |
1368 KB |
Output is correct |
50 |
Correct |
2 ms |
1372 KB |
Output is correct |
51 |
Correct |
2 ms |
1372 KB |
Output is correct |
52 |
Correct |
2 ms |
1372 KB |
Output is correct |
53 |
Correct |
2 ms |
1372 KB |
Output is correct |
54 |
Correct |
2 ms |
1372 KB |
Output is correct |
55 |
Correct |
2 ms |
1368 KB |
Output is correct |
56 |
Correct |
2 ms |
1372 KB |
Output is correct |
57 |
Correct |
2 ms |
1372 KB |
Output is correct |
58 |
Correct |
2 ms |
1372 KB |
Output is correct |
59 |
Correct |
1 ms |
604 KB |
Output is correct |
60 |
Correct |
2 ms |
604 KB |
Output is correct |
61 |
Correct |
1 ms |
600 KB |
Output is correct |
62 |
Correct |
1 ms |
604 KB |
Output is correct |
63 |
Correct |
1 ms |
604 KB |
Output is correct |
64 |
Correct |
1 ms |
604 KB |
Output is correct |
65 |
Correct |
2 ms |
1252 KB |
Output is correct |
66 |
Correct |
2 ms |
1252 KB |
Output is correct |
67 |
Correct |
2 ms |
1372 KB |
Output is correct |
68 |
Correct |
2 ms |
1372 KB |
Output is correct |
69 |
Correct |
2 ms |
1196 KB |
Output is correct |
70 |
Correct |
2 ms |
1372 KB |
Output is correct |
71 |
Correct |
1 ms |
808 KB |
Output is correct |
72 |
Correct |
1 ms |
856 KB |
Output is correct |
73 |
Correct |
2 ms |
1372 KB |
Output is correct |
74 |
Correct |
2 ms |
1372 KB |
Output is correct |
75 |
Correct |
722 ms |
135064 KB |
Output is correct |
76 |
Correct |
838 ms |
131120 KB |
Output is correct |
77 |
Correct |
672 ms |
135116 KB |
Output is correct |
78 |
Correct |
759 ms |
137364 KB |
Output is correct |
79 |
Correct |
240 ms |
137784 KB |
Output is correct |
80 |
Correct |
256 ms |
133688 KB |
Output is correct |
81 |
Correct |
317 ms |
109192 KB |
Output is correct |
82 |
Correct |
442 ms |
116492 KB |
Output is correct |
83 |
Correct |
356 ms |
115256 KB |
Output is correct |
84 |
Correct |
391 ms |
116380 KB |
Output is correct |
85 |
Correct |
362 ms |
110104 KB |
Output is correct |
86 |
Correct |
218 ms |
102676 KB |
Output is correct |
87 |
Correct |
232 ms |
102236 KB |
Output is correct |
88 |
Correct |
290 ms |
104212 KB |
Output is correct |
89 |
Correct |
276 ms |
107884 KB |
Output is correct |
90 |
Correct |
235 ms |
109624 KB |
Output is correct |
91 |
Correct |
177 ms |
100644 KB |
Output is correct |
92 |
Correct |
178 ms |
100044 KB |
Output is correct |
93 |
Correct |
197 ms |
104548 KB |
Output is correct |
94 |
Correct |
168 ms |
106868 KB |
Output is correct |
95 |
Correct |
214 ms |
109368 KB |
Output is correct |
96 |
Correct |
57 ms |
19520 KB |
Output is correct |
97 |
Correct |
60 ms |
20940 KB |
Output is correct |
98 |
Correct |
53 ms |
20788 KB |
Output is correct |
99 |
Correct |
52 ms |
21048 KB |
Output is correct |
100 |
Correct |
57 ms |
19764 KB |
Output is correct |
101 |
Correct |
51 ms |
19760 KB |
Output is correct |
102 |
Correct |
230 ms |
89268 KB |
Output is correct |
103 |
Correct |
219 ms |
87612 KB |
Output is correct |
104 |
Correct |
263 ms |
94264 KB |
Output is correct |
105 |
Correct |
186 ms |
88208 KB |
Output is correct |
106 |
Correct |
188 ms |
90432 KB |
Output is correct |
107 |
Correct |
163 ms |
86076 KB |
Output is correct |
108 |
Correct |
664 ms |
136508 KB |
Output is correct |
109 |
Correct |
543 ms |
144816 KB |
Output is correct |
110 |
Correct |
67 ms |
39224 KB |
Output is correct |
111 |
Correct |
77 ms |
39628 KB |
Output is correct |
112 |
Correct |
372 ms |
129264 KB |
Output is correct |
113 |
Correct |
264 ms |
145208 KB |
Output is correct |