#include<bits/stdc++.h>
using namespace std;
vector<int> t, t2, L;
void prop(int i, int s, int e){
if(s != e){
L[i*2] += L[i];
L[i*2+1] += L[i];
}
t[i] += L[i];
t2[i] += L[i];
L[i] = 0;
}
void upd(int i, int s, int e, int l, int r){
prop(i, s, e);
if(l <= s && e <= r){
L[i] += 1;
prop(i, s, e);
return;
}
if(s > r || e < l || s > e){
return;
}
int m = (s + e)/2;
upd(i*2, s, m, l, r);
upd(i*2+1, m+1, e, l, r);
t[i] = max(t[i*2], t[i*2+1]);
t2[i] = min(t2[i*2], t2[i*2+1]);
}
int query(int i, int s, int e, int indx){
prop(i, s, e);
if(s == e) return t[i];
int m = (s + e)/2;
if(indx <= m) return query(i*2, s, m, indx);
else return query(i*2+1, m+1, e, indx);
}
int right(int i, int s, int e, int l, int r, int val){
prop(i, s, e);
if(s > e || s > r || e < l) return -1;
if(s == e) return s;
if(l <= s && e <= r){ //completely inside
if(t[i] < val) return -1;
}
int m = (s + e)/2;
int ri = right(i*2+1, m+1, e, l, r, val);
if(ri == -1) return right(i*2, s, m, l, r, val);
else return ri;
}
int left(int i, int s, int e, int l, int r, int val){
prop(i, s, e);
if(s > e || s > r || e < l) return -1;
if(l <= s && e <= r){
if(t2[i] > val) return -1;
if(s == e) return s;
}
int m = (s + e)/2;
int le = left(i*2, s, m, l, r, val);
if(le != -1) return le;
return left(i*2+1, m+1, e, l, r, val);
}
void print(int i, int s, int e){
prop(i, s, e);
if(s == e){
cout<<"S: "<<t[i]<<endl;
return;
}
int m = (s + e)/2;
print(i*2, s, m);
print(i*2+1, m+1, e);
}
signed main(){
ios::sync_with_stdio(false); cin.tie(0);
int n; cin>>n;
vector<pair<int, int> > a(n);
for(int i = 0; i < n; i++){
cin>>a[i].first>>a[i].second;
}
sort(a.begin(), a.end());
int maxh = 1e5+4;
t = vector<int>(4*n, 0);
t2 = vector<int>(4*n, 0);
L = vector<int>(4*n, 0);
for(int i = 0; i < n; i++){
int h = a[i].first, k = a[i].second;
//need to insert k masts in the end
//cout<<"ADDING "<<h<<" "<<k<<endl;
int v = query(1, 0, n-1, h-k);
int l = left(1, 0, n-1, 0, h-1, v);
int r = right(1, 0, n-1, 0, h-1, v);
//cout<<v<<" "<<l<<" "<<r<<endl;
if(r != h-1) upd(1, 0, n-1, r+1 ,h-1);
upd(1, 0, n-1, l, l-1 + k-(h-r-1));
//cout<<"UPDING FROM "<<l<<" "<<l-1 + (k-(h-r-1))<<" and "<<r+1<<" "<<h-1<<endl;
//print(1, 0, n-1);
}
long long ans = 0;
for(int i = 0; i < n; i++){
long long v = query(1, 0, n-1, i);
//cout<<i<<" : "<<v<<endl;
ans = (ans + v*(v-1)/2);
}
cout<<ans<<endl;
}
Compilation message
sails.cpp: In function 'int main()':
sails.cpp:88:7: warning: unused variable 'maxh' [-Wunused-variable]
int maxh = 1e5+4;
^~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
2 ms |
376 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
2 ms |
376 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
2 ms |
376 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
3 ms |
348 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
5 ms |
504 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
10 ms |
760 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
78 ms |
2380 KB |
Output isn't correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
163 ms |
3700 KB |
Output isn't correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
225 ms |
5376 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
274 ms |
6372 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
339 ms |
7020 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |