//#pragma GCC optimize("O3")
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/rope>
#define mem(a,v) memset((a), (v), sizeof (a))
#define enl printf("\n")
#define case(t) printf("Case #%d: ", (t))
#define ni(n) scanf("%d", &(n))
#define nl(n) scanf("%lld", &(n))
#define nai(a, n) for (int i = 0; i < (n); i++) ni(a[i])
#define nal(a, n) for (int i = 0; i < (n); i++) nl(a[i])
#define pri(n) printf("%d\n", (n))
#define prl(n) printf("%lld\n", (n))
#define pii pair<int, int>
#define pil pair<int, long long>
#define pll pair<long long, long long>
#define vii vector<pii>
#define vil vector<pil>
#define vll vector<pll>
#define vi vector<int>
#define vl vector<long long>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
using namespace std;
using namespace __gnu_pbds;
typedef long long ll;
typedef cc_hash_table<int,int,hash<int>> ht;
typedef tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update> oset;
const double pi = acos(-1);
const int MOD = 1e9 + 7;
const int INF = 1e9 + 7;
const int MAXN = 1e5 + 5;
const double eps = 1e-9;
struct node {
int mi, indhi, indlo; // minimimum value + leftmost/rightmost index != 0
int len, cnt; // interval length + number of zeros
} seg[MAXN*4];
void merge(node &a, node &b, node &c) {
a.len = b.len + c.len;
a.mi = min(b.mi, c.mi);
if (b.mi != 0)
a.indlo = b.indlo;
else
a.indlo = c.indlo;
if (c.mi != 0)
a.indhi = c.indhi;
else
a.indhi = b.indhi;
a.cnt = b.cnt + c.cnt;
}
void build(int l, int r, int k) {
if (l == r) {
seg[k] = {0, -1, MAXN, 1, 1};
return;
}
int m = (l+r) / 2;
build(l, m, k*2);
build(m+1, r, k*2+1);
merge(seg[k], seg[k*2], seg[k*2+1]);
}
void upd(int l, int r, int k, int a, int v) {
if (r < a || a < l) return;
if (a <= l && r <= a) {
seg[k].mi += v;
if (seg[k].mi != 0) {
seg[k].indlo = seg[k].indhi = l;
seg[k].cnt = 0;
} else {
seg[k].indlo = seg[k].indhi = -1;
seg[k].cnt = 1;
}
return;
}
int m = (l+r) / 2;
upd(l, m, k*2, a, v);
upd(m+1, r, k*2+1, a, v);
merge(seg[k], seg[k*2], seg[k*2+1]);
}
int qry(int l, int r, int k, int a) {
if (r < a || a < l) return 0;
if (a <= l && r <= a) return seg[k].mi;
int m = (l+r) / 2;
return qry(l, m, k*2, a) + qry(m+1, r, k*2+1, a);
}
pii search(int l, int r, int k, int a, int b) {
if (r < a || b < l) return mp(MAXN, -1);
if (a <= l && r <= b) return mp(seg[k].indlo, seg[k].indhi);
int m = (l+r) / 2;
pii ql = search(l, m, k*2, a, b);
pii qr = search(m+1, r, k*2+1, a, b);
ql.fi = min(ql.fi, qr.fi);
ql.se = max(ql.se, qr.se);
return ql;
}
pii a[MAXN];
int main() {
int n;
ni(n);
for (int i = 0; i < n; i++) {
int h, k;
scanf("%d %d", &h, &k);
a[i] = mp(h, k);
}
sort(a, a + n);
build(1, MAXN-1, 1);
for (int i = 0; i < n; i++) {
int h, k;
tie(h, k) = a[i];
//cerr << "insert: " << h << " " << k << "\n";
int lo = h - k + 1, hi = h, ind = h - k;
int len = k;
while (lo <= hi) {
int mi = (lo+hi) / 2;
pii tmp = search(1, MAXN-1, 1, mi, h);
if (tmp.fi == MAXN) tmp.fi = -1;
if (tmp.fi < h - k + 1)
lo = mi + 1;
else
hi = mi - 1, ind = mi;
}
if (ind > h - k) {
//cerr << "upd: " << ind << " +1\n";
//cerr << "upd: " << h+1 << " -1\n";
upd(1, MAXN-1, 1, ind, 1);
upd(1, MAXN-1, 1, h+1, -1);
len -= h - ind + 1;
ind--;
}
if (len != 0) {
pii tmp = search(1, MAXN-1, 1, 1, ind);
if (tmp.se == -1) tmp.se = 1;
//cerr << "upd2: " << tmp.se << " +1\n";
//cerr << "upd2: " << tmp.se+len << " -1\n";
upd(1, MAXN-1, 1, tmp.se, 1);
upd(1, MAXN-1, 1, tmp.se+len, -1);
}
}
ll ans = 0, cur = 0;
for (int i = 1; i < MAXN; i++) {
ll val = qry(1,MAXN-1,1,i);
cur += val;
ans += ((cur-1)*cur) / 2ll;
}
cout << ans << "\n";
//prl(ans);
return 0;
}
Compilation message
sails.cpp: In function 'int main()':
sails.cpp:9:20: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
#define ni(n) scanf("%d", &(n))
~~~~~^~~~~~~~~~~~
sails.cpp:108:2: note: in expansion of macro 'ni'
ni(n);
^~
sails.cpp:111:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d", &h, &k);
~~~~~^~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
18 ms |
5504 KB |
Output is correct |
2 |
Incorrect |
18 ms |
5504 KB |
Output isn't correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
18 ms |
5504 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
18 ms |
5504 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
18 ms |
5504 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
23 ms |
5504 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
48 ms |
5616 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
141 ms |
6052 KB |
Output isn't correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
252 ms |
6364 KB |
Output isn't correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
463 ms |
6832 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
488 ms |
7032 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
560 ms |
7040 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |