//#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, indlo, indhi; // 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);
//a.indlo = min(b.indlo, c.indlo);
//a.indhi = max(b.indhi, c.indhi);
if (b.len != b.cnt)
a.indlo = b.indlo;
else
a.indlo = c.indlo;
if (c.len != c.cnt)
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, MAXN, -1, 1, 1};
if (l == 0 || l == MAXN-1)
seg[k] = {1, l, l, 1, 0};
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 prop(int l, int r, int k) {
if (l == r) return;
if (seg[k*2+1].indlo == -1 && seg[k*2].indhi != MAXN)
seg[k*2+1].indlo = seg[k*2].indhi;
if (seg[k*2].indhi == MAXN && seg[k*2+1].indlo != -1)
seg[k*2].indhi = seg[k*2+1].indlo;
}
void upd(int l, int r, int k, int a, int v) {
//prop(l, r, k);
if (r < a || a < l) return;
if (a <= l && r <= a) {
seg[k].mi += v;
if (seg[k].mi != 0) {
seg[k].indlo = l;
seg[k].indhi = l;
seg[k].cnt = 0;
} else {
seg[k].indlo = MAXN;
seg[k].indhi = -1;
seg[k].cnt = 1;
}
//cerr << l << " " << r << " " << k << " " << a << " " << seg[k].indlo << "\n";
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]);
//prop(l, r, k);
}
int qry(int l, int r, int k, int a) {
//prop(l, r, k);
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, 0);
if (a <= l && r <= b) {
//cerr << l << " " << r << " " << k << " " << a << " " << b << " " << seg[k].indlo << "\n";
return mp(seg[k].indlo, 0);
}
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);
if (seg[k*2].cnt != seg[k*2].len && seg[k*2].indhi < a)
ql.se = max(ql.se, seg[k*2].indhi);
if (qr.se < a)
ql.se = max(ql.se, qr.se);
ql.fi = min(ql.fi, qr.fi);
if (ql.fi == a)
ql.se = a;
//cerr << l << " " << r << " " << k << " " << a << " " << b << " " << ql.fi << " " << ql.se << "\n";
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(0, 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 = 1, hi = h, ind = 0;
int len = k;
while (lo <= hi) {
int mi = (lo+hi) / 2;
pii tmp = search(0, MAXN-1, 1, mi, h);
//cerr << "qry: " << mi << "->" << h << " : " << tmp.se << "\n";
if (h - tmp.se + 1 > k)
lo = mi + 1;
else
hi = mi - 1, ind = mi;
}
if (ind != 0) {
//cerr << "upd: " << ind << " +1\n";
//cerr << "upd: " << h+1 << " -1\n";
upd(0, MAXN-1, 1, ind, 1);
upd(0, MAXN-1, 1, h+1, -1);
len -= h - ind + 1;
ind--;
} else ind = 1;
if (len != 0) {
pii tmp = search(0, MAXN-1, 1, h-k+1, h);
if (tmp.se == 0) tmp.se = 1;
//cerr << "upd2: " << tmp.se << " +1\n";
//cerr << "upd2: " << tmp.se+len << " -1\n";
upd(0, MAXN-1, 1, tmp.se, 1);
upd(0, MAXN-1, 1, tmp.se+len, -1);
}
}
ll ans = 0, cur = 0;
for (int i = 1; i < MAXN; i++) {
ll val = qry(0,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:136:2: note: in expansion of macro 'ni'
ni(n);
^~
sails.cpp:139:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d", &h, &k);
~~~~~^~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
17 ms |
5504 KB |
Output is correct |
2 |
Correct |
17 ms |
5536 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
18 ms |
5504 KB |
Output is correct |
2 |
Correct |
17 ms |
5504 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
17 ms |
5504 KB |
Output is correct |
2 |
Correct |
17 ms |
5496 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
17 ms |
5504 KB |
Output is correct |
2 |
Correct |
19 ms |
5540 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
25 ms |
5504 KB |
Output is correct |
2 |
Correct |
24 ms |
5628 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
61 ms |
5632 KB |
Output is correct |
2 |
Correct |
142 ms |
6136 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
164 ms |
5888 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
321 ms |
6136 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
539 ms |
6220 KB |
Output is correct |
2 |
Correct |
487 ms |
7040 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
586 ms |
6332 KB |
Output is correct |
2 |
Correct |
382 ms |
6904 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
693 ms |
6400 KB |
Output is correct |
2 |
Correct |
461 ms |
7316 KB |
Output is correct |