#include <bits/stdc++.h>
using namespace std;
#define ll long long
const int N=100050;
ll sum[N],cnt[N];
ll suf[N];
const int M=2*N;
int ls[M],rs[M],tsz,root;
pair<int,int> mn[M];
pair<int,int> Get(int c, int ss, int se, int qs, int qe)
{
if(qs<=ss && qe>=se) return mn[c];
int mid=ss+se>>1;
if(qe<=mid) return Get(ls[c],ss,mid,qs,qe);
if(qs>mid) return Get(rs[c],mid+1,se,qs,qe);
return min(Get(ls[c],ss,mid,qs,qe),Get(rs[c],mid+1,se,qs,qe));
}
void Set(int &c, int ss, int se, int qi, int f)
{
if(!c) c=++tsz;
if(ss==se){ mn[c]={f,ss};return;}
int mid=ss+se>>1;
if(qi<=mid) Set(ls[c],ss,mid,qi,f);
else Set(rs[c],mid+1,se,qi,f);
mn[c]=min(mn[ls[c]],mn[rs[c]]);
}
int main()
{
int n,h,k,mxh=0;
scanf("%i",&n);
for(int i=1;i<=n;i++)
{
scanf("%i %i",&h,&k);
cnt[h]++;
sum[h-k+1]++;
sum[h+1]--;
mxh=max(mxh,h);
}
for(int i=1;i<=mxh;i++) sum[i]+=sum[i-1];
stack<pair<int,int>> stk;
auto push_stk=[&](int x, int sz)
{
if(stk.empty() || stk.top().first!=x) stk.push({x,sz});
else
{
pair<int,int> p=stk.top();
stk.pop();
sz+=p.second;
stk.push({x,sz});
}
};
for(int i=1;i<=mxh;i++)
{
while(stk.size() && stk.top().first<sum[i])
{
pair<int,int> p=stk.top();
stk.pop();
ll dif=1e9,nh=1e9;
if(stk.size()) dif=stk.top().first-p.first,nh=stk.top().first;
dif*=p.second;
if(sum[i]-dif<nh)
{
int sz=p.second;
ll lo=0,hi=1e5,mi=lo+hi+1>>1;
for(;lo<hi;mi=lo+hi+1>>1)
{
if(sum[i]-mi*sz>=p.first+mi) lo=mi;
else hi=mi-1;
}
sum[i]-=lo*sz;
p.first+=lo;
int ost=sum[i]-p.first;
if(ost>0) push_stk(p.first+1,ost);
push_stk(p.first,sz-ost);
}
else
{
sum[i]-=dif;
push_stk(nh,p.second);
}
}
push_stk(sum[i],1);
}
ll ans=0;
for(int i=1;i<=mxh;i++) ans+=(ll)sum[i]*(sum[i]-1)/2;
printf("%lld\n",ans);
return 0;
}
Compilation message
sails.cpp: In function 'std::pair<int, int> Get(int, int, int, int, int)':
sails.cpp:13:12: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
int mid=ss+se>>1;
~~^~~
sails.cpp: In function 'void Set(int&, int, int, int, int)':
sails.cpp:22:12: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
int mid=ss+se>>1;
~~^~~
sails.cpp: In function 'int main()':
sails.cpp:64:28: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
ll lo=0,hi=1e5,mi=lo+hi+1>>1;
~~~~~^~
sails.cpp:65:24: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
for(;lo<hi;mi=lo+hi+1>>1)
~~~~~^~
sails.cpp:30:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%i",&n);
~~~~~^~~~~~~~~
sails.cpp:33:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%i %i",&h,&k);
~~~~~^~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
2 ms |
384 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
2 ms |
384 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
384 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
2 ms |
384 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
28 ms |
384 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
1041 ms |
640 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
10 ms |
1024 KB |
Output isn't correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
1073 ms |
1072 KB |
Time limit exceeded |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
1062 ms |
1152 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
503 ms |
2424 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
1047 ms |
1792 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |