QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#93788 | #6192. Interval Problem | ksunhokim | WA | 2ms | 3460kb | C++17 | 2.2kb | 2023-04-02 14:21:25 | 2023-04-02 14:21:28 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int INF = 1e9;
// Fenwick Tree for summation
struct fenwick_tree {
int n;
vector<ll> bits;
fenwick_tree(int n) : n(n), bits(n+1) {}
// add delta to node v
void update(int v, int delta) {
for (++v; v <= n; v += v&(-v))
bits[v] += delta;
}
// get prefix sum of range [0,r]
ll query(int r) {
ll res = 0;
for (++r; r > 0; r -= r&(-r))
res += bits[r];
return res;
}
};
int main() {
cin.tie(nullptr)->sync_with_stdio(false);
int n;
cin >> n;
vector<tuple<int,int,int>> intervals(n);
vector<int> A(n*2);
for (int i=0;i<n;i++){
int l,r;
cin >> l >> r;
--l,--r;
A[l] = i, A[r] = i;
intervals[i]={l,r,i};
}
vector<ll> ans(n);
vector<int> vis(n);
fenwick_tree ft(n*2);
sort(begin(intervals), end(intervals));
for (int i=0;i<n;i++){
auto [l,r,j] = intervals[i];
ft.update(r,1);
}
for (int i=0;i<n;i++){
auto [l,r,j] = intervals[i];
ft.update(r,-1);
ans[j] += ft.query(r) - ft.query(l);
}
for (int i=0;i<n;i++){
auto [l,r,j] = intervals[i];
ft.update(r,1);
}
for (int i=n-1;i>=0;i--){
auto [l,r,j] = intervals[i];
ft.update(r,-1);
ans[j] -= ft.query(2*n) - ft.query(r);
}
int opens = 0;
int closes = 0;
ll disjoint_sum = 0;
map<int, int> stack;
for (int i=0;i<n*2;i++) {
if (!stack.count(A[i])) {
stack.insert({A[i],opens});
opens++;
ans[A[i]] += disjoint_sum + closes;
} else {
stack.erase(A[i]);
closes++;
disjoint_sum += 1;
opens--;
ans[A[i]] += opens;
if (opens == 0) {
disjoint_sum = 0;
closes = 0;
}
}
}
opens=0,closes=0,disjoint_sum=0;
for (int i=n*2-1;i>=0;i--) {
if (!stack.count(A[i])) {
stack.insert({A[i],opens});
opens++;
ans[A[i]] += disjoint_sum + closes;
} else {
stack.erase(A[i]);
closes++;
disjoint_sum += 1;
opens--;
ans[A[i]] += opens;
if (opens == 0) {
disjoint_sum = 0;
closes = 0;
}
}
}
for (int i=0;i<n;i++){
cout << ans[i] << "\n";
}
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 0
Wrong Answer
time: 2ms
memory: 3460kb
input:
5 2 3 6 7 1 9 5 10 4 8
output:
-98250 -98252 -98253 -98252 -98252
result:
wrong answer 1st numbers differ - expected: '7', found: '-98250'