QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#93793 | #6192. Interval Problem | ksunhokim | WA | 148ms | 14028kb | C++17 | 2.9kb | 2023-04-02 15:41:24 | 2023-04-02 15:41:26 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int INF = 1e9;
#define dbg(x) "(" << #x <<": " << (x) << ") "
template<typename Ostream, typename Cont>
enable_if_t<is_same_v<Ostream,ostream>, Ostream&>
operator<<(Ostream& os, const Cont& v){
os<<"[";
for(auto& x:v){os<<x<<", ";}
return os<<"]";
}
template<typename ...Ts>
ostream& operator<<(ostream& os, const pair<Ts...>& p){
return os<<"{"<<p.first<<", "<<p.second<<"}";
}
// 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+1);
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);
}
auto calc = [&](vector<int>& order, bool inv) {
map<int, int> stack;
set<int> indices;
map<int, int> zombies;
ll kill_sum = 0;
int kill = 0;
for (int i : order) {
// cout << dbg(i) dbg(A[i]) dbg(kill_sum) dbg(kill) dbg(zombies.size())<< "\n";
if (!stack.count(A[i])) {
stack.insert({A[i],i});
indices.insert(inv ? -i : i);
ans[A[i]] += 2*zombies.size() + kill + kill_sum;
} else {
indices.erase(inv ? -stack[A[i]] : stack[A[i]]);
zombies.insert({inv ? -i : i, A[i]});
stack.erase(A[i]);
ans[A[i]] += stack.size();
if (stack.empty()) {
kill = 0;
kill_sum = 0;
} else {
for (auto it = zombies.begin(); it != zombies.end();) {
if (it->first < *indices.begin()) {
it = zombies.erase(it);
kill++;
kill_sum += 2;
} else {
break;
}
}
}
}
}
};
vector<int> order(2*n);
iota(begin(order), end(order), 0);
calc(order, false);
reverse(begin(order), end(order));
calc(order, true);
for (int i=0;i<n;i++){
cout << ans[i] << "\n";
}
}
詳細信息
Test #1:
score: 100
Accepted
time: 0ms
memory: 3380kb
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: 148ms
memory: 14028kb
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:
30 54 30 29 26 40 18 17 19 36 31 14 43 58 32 29 12 32 38 24 16 50 12 17 18 12 48 47 38 22 22 8 46 31 44 31 37 23 44 33 15 28 35 35 23 9 12 52 51 35 45 37 25 33 16 38 18 14 63 37 12 45 19 31 30 40 37 32 10 30 16 52 27 18 15 19 79 23 13 32 18 27 15 47 17 44 34 37 41 52 21 25 41 53 9 10 41 14 8 15 21 2...
result:
wrong answer 1st numbers differ - expected: '12', found: '30'