#include <iostream>
#include <vector>
#include <string>
#include <fstream>
#include <algorithm>
#include <climits>
#include <cstdlib>
#include <cstdio>
#include <set>
#include <unordered_set>
#include <map>
#include <unordered_map>
#include <bitset>
#include <deque>
#include <queue>
#include <tuple>
#include <cmath>
#include <cctype>
#include <stack>
#include <cassert>
using namespace std;
using ll = long long;
int N;
pair<int, int> inp[100001];
ll bit[100002]={};
set<int> pos; // where all the jumps in flags are located
void add(int k, ll x) {
while (k <= 1e5) {
bit[k] += x;
k += k&-k;
}
}
void inc(int a, int b, int amt) {
add(a, amt); add(b+1, -amt);
}
ll sum(int k) {
ll s = 0;
while (k >= 1) {
s += bit[k];
k -= k&-k;
}
return s;
}
ll get(int k) {
return sum(k);
}
int main() {
cin >> N;
pos.insert(1);
for (int i = 1; i <= N; i++) cin >> inp[i].first >> inp[i].second;
sort(inp+1, inp+1+N);
// idx = 1
inc(1, inp[1].second, 1);
pos.insert(inp[1].second+1);
//cout << "pos, bit" << endl;
//for (auto pp : pos) cout << pp << " "; cout << endl;
//for (int i = 1; i <= 20; i++) cout << get(i) << " "; cout << endl;
for (int i = 2; i <= N; i++) {
int h = inp[i].first, k = inp[i].second;
inc(h-k+1, h, 1);
if (get(h) == 1) pos.insert(h+1);
if (h-k+1 > 1) {
if (pos.find(h-k+1) != pos.end()) pos.erase(h-k+1);
else {
auto it = pos.upper_bound(h-k+1);
int ed = *it;
it--;
int fp = *it;
int size = ed-(h-k+1);
inc(h-k+1, ed-1, -1); inc(fp, fp+size-1, 1);
if (get(ed) == get(ed-1)) pos.erase(ed);
if (fp != 1) pos.erase(fp);
pos.insert(fp+size);
}
}
//cout << "debugging pos,bit" << endl;
//for (auto pp : pos) cout << pp << " "; cout << endl;
//for (int i = 1; i <= 20; i++) cout << get(i) << " "; cout << endl;
}
ll ans = 0;
for (int i = 1; i <= 1e5; i++) {
ll s = get(i);
ans += s*(s-1LL)/2LL;
}
cout << ans << endl;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
332 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
2 ms |
332 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
332 KB |
Output is correct |
2 |
Correct |
2 ms |
972 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
8 ms |
652 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
38 ms |
2064 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
68 ms |
1348 KB |
Output isn't correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
68 ms |
1804 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
88 ms |
4028 KB |
Output is correct |
2 |
Correct |
62 ms |
1612 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
99 ms |
2340 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |