QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#105360 | #6192. Interval Problem | Melacau | WA | 198ms | 30016kb | C++17 | 4.1kb | 2023-05-13 23:09:37 | 2023-05-13 23:09:40 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
using s64 = long long;
const int N = 2e5 + 5;
const int M = 4e5 + 5;
const int T = 1 << 20;
int n, m, cnt[M], lpos[M], rpos[M];
struct Seg{int l, r, k;}seg[N];
int siz[T], tag[T];
s64 sum[T], ans[N];
void build(int k = 1, int L = 0, int R = m) {
tag[k] = 0;
if (L + 1 == R) {
siz[k] = cnt[L];
sum[k] = siz[k] << 1;
return;
}
int mid = L + R >> 1;
build(k << 1, L, mid);
build(k << 1 | 1, mid, R);
siz[k] = siz[k << 1] + siz[k << 1 | 1];
sum[k] = sum[k << 1] + sum[k << 1 | 1];
}
void addn(int k, int v) {
tag[k] += v;
sum[k] += s64(v) * siz[k];
}
void down(int k) {
addn(k << 1, tag[k]);
addn(k << 1 | 1, tag[k]);
tag[k] = 0;
}
void add(int l, int r, int k = 1, int L = 0, int R = m) {
if (l >= r || r <= L || R <= l) return;
if (l <= L && R <= r) return addn(k, 1);
down(k); int mid = L + R >> 1;
add(l, r, k << 1, L, mid);
add(l, r, k << 1 | 1, mid, R);
sum[k] = sum[k << 1] + sum[k << 1 | 1];
// cerr << "ADDR " << l << ' ' << r << ' ' << L << ' ' << R << ' ' << sum[k] << endl;
}
s64 query(int l, int r, int k = 1, int L = 0, int R = m) {
if (l >= r || r <= L || R <= l) return 0;
if (l <= L && R <= r) {
// cerr << l << ' ' << r << ' ' << L << ' ' << R << ' ' << sum[k] << endl;
return sum[k];
}
down(k); int mid = L + R >> 1;
return query(l, r, k << 1, L, mid) + query(l, r, k << 1 | 1, mid, R);
}
void solve() {
sort(seg, seg + n, [](const Seg& a, const Seg& b){
return a.l == b.l ? a.r > b.r : a.l < b.l;
});
fill(lpos, lpos + m + 1, m + 1);
fill(rpos, rpos + m + 1, -1);
for (int i = 0, j = 0, curR = -1; i < m; ++i) {
if (j < n && seg[j].l == i && seg[j].r > curR) {
if (curR >= 0) rpos[curR] = i;
lpos[curR = seg[j].r] = i;
}
while (j < n && seg[j].l == i) ++j;
if (curR == i) {rpos[curR] = i + 1; curR = -1;}
if (curR >= 0) lpos[curR] = min(lpos[curR], lpos[i]);
}
fill(cnt, cnt + m + 1, 0);
for (int i = 0; i < n; ++i) ++cnt[seg[i].r];
build();
for (int i = 0, j = 0, l = 0; j < n && i < m; ++i) {
while (j < n && seg[j].l == i) {
ans[seg[j].k] += query(l, seg[j].l);
// cerr << j << ' ' << seg[j].l << ' ' << seg[j].r << ' ' << ans[seg[j].k] << endl;
++j;
}
// cerr << i << ' ' << lpos[i] << ' ' << rpos[i] << endl;
if (rpos[i] == i + 1) l = i + 1;
else if (rpos[i] >= 0) {
add(lpos[i], rpos[i]);
// cerr << "ADD " << lpos[i] << ' ' << rpos[i] << endl;
}
}
// cerr << endl;
}
void solveint() {
sort(seg, seg + n, [](const Seg& a, const Seg& b){
return a.r > b.r;
});
fill(cnt, cnt + m + 1, 0);
static int lcnt[M] = {0};
for (int i = 0; i < n; ++i) ++cnt[seg[i].r];
for (int i = 1; i < m; ++i) cnt[i] += cnt[i - 1];
for (int i = 0, j, now = 0, x = m; i < n; i = j) {
for (j = i; j < n && seg[j].r == seg[i].r; ++j) ++lcnt[seg[j].l];
for (; x > seg[i].r; --x) now -= lcnt[x];
for (int k = i; k < j; ++k) {
ans[seg[k].k] += cnt[seg[k].r] - (seg[k].l == 0 ? 0 : cnt[seg[k].l - 1]);
ans[seg[k].k] += now;
}
now += j - i;
}
}
void reverse() {
for (int i = 0; i < n; ++i) {
swap(seg[i].l, seg[i].r);
seg[i].l = m - seg[i].l - 1;
seg[i].r = m - seg[i].r - 1;
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr); cout.tie(nullptr);
cin >> n; m = n << 1;
for (int i = 0; i < n; ++i) {
cin >> seg[i].l >> seg[i].r;
--seg[i].l; --seg[i].r;
seg[i].k = i;
}
solve();
// for (int i = 0; i < n; ++i)
// cerr << ans[i] << " \n"[i + 1 == n];
solveint();
reverse();
// for (int i = 0; i < n; ++i)
// cerr << ans[i] << " \n"[i + 1 == n];
solve();
// for (int i = 0; i < n; ++i)
// cerr << ans[i] << " \n"[i + 1 == n];
for (int i = 0; i < n; ++i)
cout << ans[i] - 1 << '\n';
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 2ms
memory: 3556kb
input:
5 2 3 6 7 1 9 5 10 4 8
output:
7 5 4 5 5
result:
ok 5 number(s): "7 5 4 5 5"
Test #2:
score: -100
Wrong Answer
time: 198ms
memory: 30016kb
input:
200000 342504 342505 248314 248317 259328 259334 234817 234821 225726 225732 371424 371425 260562 260565 34752 34753 132098 132100 262130 262134 7254 7255 228491 228492 43488 43492 188408 188410 11691 11692 350334 350335 327350 327352 360684 360685 182051 182052 72131 72135 194666 194668 61303 61313...
output:
12 21 17 6 16 33 2 1 1 19 6 0 25 19 7 11 2 20 14 8 2 11 0 1 0 2 38 35 28 6 4 0 23 22 17 10 26 10 5 9 1 16 14 24 7 1 2 40 18 18 19 13 4 26 2 28 0 0 39 19 0 37 4 13 18 19 23 14 2 4 0 40 10 0 1 6 52 7 1 18 3 6 1 40 7 24 16 13 31 43 4 10 23 20 1 0 4 4 0 1 8 3 1 17 37 14 43 5 17 32 60 0 0 0 28 0 18 16 7 ...
result:
wrong answer 131st numbers differ - expected: '42', found: '43'