QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#93856 | #6192. Interval Problem | ksunhokim | WA | 336ms | 40100kb | C++17 | 4.7kb | 2023-04-03 11:23:01 | 2023-04-03 11:23:04 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#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<<"}";
}
const int inf = 1e9;
struct seg_tree {
int n;
vector<int> t;
seg_tree(int n) : n(n), t(4*n, -inf) {}
// query the combinded value on [l,r]
int query(int l, int r, int v=1, int tl=0, int tr=-1) {
if (tr == -1) tr = n-1;
if (l > r) return -inf;
if (l == tl && r == tr) {
return t[v];
}
int tm = (tl + tr) / 2;
int x = query(l, min(r,tm), v*2, tl, tm);
int y = query(max(tm+1,l), r, v*2+1, tm+1, tr);
return max(x,y);
}
// update the node at pos
void update(int pos, int val, int v=1, int tl=0, int tr=-1) {
if (tr == -1) tr = n-1;
if (tl == tr) {
t[v] = val;
return;
}
int tm = (tl + tr)/2;
if (pos <= tm) {
update(pos, val, v*2, tl, tm);
} else {
update(pos, val, v*2+1, tm+1,tr);
}
t[v] = max(t[v*2], t[v*2+1]);
}
};
// 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;
}
ll query(int l, int r) {
return query(r) - query(l-1);
}
};
int main() {
cin.tie(nullptr)->sync_with_stdio(false);
int n;
cin >> n;
vector<pair<int,int>> intervals(n);
vector<int> A(2*n);
vector<int> l_to_id(2*n, -1);
vector<int> r_to_id(2*n);
vector<ll> ans(n);
for (int i=0;i<n;i++){
int l,r;
cin >> l >> r;
--l,--r;
intervals[i]={l,r};
l_to_id[l] = i;
r_to_id[r] = i;
A[l] = i;
A[r] = i;
}
vector<tuple<int,int,int>> worklist(n);
for (int i=0;i<n;i++){
auto [l,r] = intervals[i];
worklist[i] = {r,l,i};
}
sort(begin(worklist), end(worklist));
seg_tree st(2*n);
for (auto [r,l,__] : worklist) {
st.update(l, r);
}
vector adj_left(n, vector<int>());
vector adj_right(n, vector<int>());
vector<int> prd_left(n,-1);
vector<int> prd_right(n, -1);
vector<int> ins_cnt(n);
for (int i=0;i<n;i++){
auto [r,l,id] = worklist[i];
st.update(l, -inf);
int rr = st.query(0,r);
if (rr != -inf) {
prd_right[id] = r_to_id[rr];
adj_right[r_to_id[rr]].push_back(id);
}
}
for (int i=0;i<n;i++){
auto [l,r] = intervals[i];
worklist[i] = {l,r,i};
}
sort(begin(worklist), end(worklist));
for (auto [l,r,__] : worklist) {
st.update(r, -l);
}
fenwick_tree ft(2*n);
for (int i=0;i<n;i++){
auto [l,r,id] = worklist[i];
ans[id] -= ft.query(r, 2*n-1);
ft.update(r,1);
}
ft = fenwick_tree(2*n);
for (int i=n-1;i>=0;i--){
auto [l,r,id] = worklist[i];
ans[id] += ft.query(0, r);
ft.update(r,1);
}
ft = fenwick_tree(2*n);
for (int i=0;i<n;i++){
auto [l,r,id] = worklist[i];
ft.update(l,1);
ft.update(r,-1);
}
for (int i=0;i<n;i++){
auto [l,r,id] = worklist[i];
ans[id] += ft.query(0,r);
}
ft = fenwick_tree(2*n);
for (int i=0;i<n;i++){
auto [l,r,id] = worklist[i];
ft.update(r,1);
ft.update(l,-1);
}
for (int i=0;i<n;i++){
auto [l,r,id] = worklist[i];
ans[id] += ft.query(l, 2*n-1);
}
for (int i=n-1;i>=0;i--){
auto [l,r,id] = worklist[i];
st.update(r, -inf);
int ll = st.query(l, 2*n-1);
if (ll != -inf) {
prd_left[id] = l_to_id[-ll];
adj_left[l_to_id[-ll]].push_back(id);
}
}
map<int,int> stack;
ll sum = 0;
int cnt = 0;
for (int i=0;i<2*n;i++){
if (!stack.count(A[i])) {
stack[A[i]] = 1;
ans[A[i]] += sum + cnt;
} else {
cnt++;
if (prd_right[A[i]] != -1) {
stack[prd_right[A[i]]]+=stack[A[i]];
sum += stack[A[i]];
}
stack.erase(A[i]);
}
}
sum=0,cnt=0;
for (int i=2*n-1;i>=0;i--){
if (!stack.count(A[i])) {
stack[A[i]] = 1;
ans[A[i]] += sum + cnt;
} else {
cnt++;
if (prd_left[A[i]] != -1) {
stack[prd_left[A[i]]]+=stack[A[i]];
sum += stack[A[i]];
}
stack.erase(A[i]);
}
}
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: 100
Accepted
time: 2ms
memory: 3432kb
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: 336ms
memory: 40100kb
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:
533830 534105 534186 534082 533927 533496 534185 535534 535161 534254 535143 533897 535543 534743 535104 533775 534207 533729 534773 535499 534662 535575 534359 534127 533698 534060 534177 535227 533488 534148 535218 534011 534629 535605 533366 535132 533772 535213 535446 534734 534677 534866 533900...
result:
wrong answer 1st numbers differ - expected: '12', found: '533830'