#include<bits/stdc++.h>
using namespace std;
struct dataa{
long long lazy = 0,sum = 0;
void add(long long left,long long right,long long val){
//lazy+=val;
sum+=val * (right - left + 1);
//sum += val *(right - left + 1);
}
};
struct Segment_Tree{
vector<dataa>tree;
void init(long long n){
tree.resize(2*n - 1);
}
dataa combine(dataa left,dataa right){
dataa res;
res.sum = left.sum + right.sum;
return res;
}
void push(long long node,long long left,long long right){
long long mid = (left + right)>>1;
long long z = node + ((mid - left + 1)<<1);
if (tree[node].lazy!=0){
tree[node + 1].add(left,mid,tree[node].lazy);
tree[z].add(mid + 1,right,tree[node].lazy);
tree[node].lazy = 0;
}
}
long long query(long long node,long long left,long long right,long long qleft,long long qright){
if (qright<left|| qleft > right)return 0;
if (qleft<=left && qright >=right){
return tree[node].sum;
}
//push(node,left,right);
long long mid = (left + right)>>1;
long long z = node + ((mid - left + 1)<<1);
return query(node + 1,left,mid,qleft,qright) + query(z,mid + 1,right,qleft,qright);
}
void update(long long node,long long left,long long right,long long uleft,long long uright,long long v){
if (left > uright || right < uleft) return;
if (uleft <= left && right <=uright){
tree[node].add(left,right,v);
return;
}
//push(node,left,right);
long long mid = (left + right)>>1;
long long z = node + ((mid - left + 1)<<1);
if (uleft<=mid){
update(node + 1,left,mid,uleft,uright,v);
}
if (uright > mid){
update(z,mid + 1,right,uleft,uright,v);
}
tree[node] = combine(tree[node + 1],tree[z]);
}
};
int main(){
ios_base::sync_with_stdio(false);
cin.tie(0);
int n;cin>>n;
vector<pair<long long,long long>>arr(n);
vector<long long>v(100001,0);
Segment_Tree st;
st.init(100001);
int mxn = 100000;
for (int i = 0;i<n;++i){
cin>>arr[i].first>>arr[i].second;
}
sort(arr.begin(),arr.end(),[&](auto x,auto y){
return x.first < y.first;
});
long long ans = 0;
int right = 0;
for (int i = 0;i<n;++i){
st.update(0,0,mxn,0,0,arr[i].first - right);
right = arr[i].first;
int l = 0,r = right;
long long p = 0;
while(l<=r){
int mid = (l + r)>>1;
if (st.query(0,0,mxn,0,mid) >= arr[i].second){
p = mid;
r = mid - 1;
}
else{
l = mid + 1;
}
}
for (long long j = p - 1;j>=0;--j){
long long temp = st.query(0,0,mxn,j,j);
st.update(0,0,mxn,j + 1,j + 1,temp);
st.update(0,0,mxn,j,j,-temp);
arr[i].second-=temp;
//cout<<i<<" "<<" "<<arr[i].second<<" "<<temp<<'\n';
ans += j * temp;
}
//cout<<"peee "<<p<<" "<<arr[i].second<<" "<<st.query(0,0,mxn,p,p)<<'\n';
st.update(0,0,mxn,p + 1,p + 1,arr[i].second);
st.update(0,0,mxn,p,p,-arr[i].second);
ans+=p * arr[i].second;
}
cout<<ans<<'\n';
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4180 KB |
Output is correct |
2 |
Incorrect |
2 ms |
4180 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4180 KB |
Output is correct |
2 |
Correct |
2 ms |
4180 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
2 ms |
4180 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
4180 KB |
Output is correct |
2 |
Correct |
36 ms |
4180 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
320 ms |
4260 KB |
Output is correct |
2 |
Correct |
8 ms |
4180 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
1075 ms |
4308 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
1088 ms |
4692 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
1092 ms |
4948 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
1087 ms |
5332 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
1080 ms |
5588 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
1080 ms |
5716 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |