QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#114742 | #114. Construction of Highway | minhcool | 0 | 3ms | 40344kb | C++17 | 3.4kb | 2023-06-23 12:18:24 | 2023-06-23 12:18:26 |
Judging History
answer
#define local
#ifndef local
#include ""
#endif
#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
using namespace std;
#define int long long
#define fi first
#define se second
#define pb push_back
#define mp make_pair
typedef pair<int, int> ii;
typedef pair<ii, int> iii;
typedef pair<ii, ii> iiii;
const int N = 3e5 + 5;
const int oo = 1e18 + 7, mod = 1e9 + 7;
mt19937 rng(1);
int rnd(int l, int r){
int temp = rng() % (r - l + 1);
return abs(temp) + l;
}
int n, c[N];
vector<int> Adj[N];
bool ck[N];
int ind[N];
int sz[N];
void dfs(int u){
// cout << u << "\n";
sz[u] = 1;
for(auto v : Adj[u]){
dfs(v);
sz[u] += sz[v];
}
}
int cnt;
int num[N], pos[N], len[N], head[N], par[N];
void hld(int u, int nw){
//cout << u << " " << nw << "\n";
if(nw){
cnt++;
num[u] = cnt; pos[u] = 1;
head[cnt] = u;
}
ii mx = {-1, -1};
for(auto v : Adj[u]) mx = max(mx, {sz[v], v});
if(mx.fi < 0) return;
num[mx.se] = num[u]; pos[mx.se] = pos[u] + 1;
hld(mx.se, 0);
for(auto v : Adj[u]) if(v != mx.se) hld(v, 1);
}
set<ii> segs[N];// start_pos & c[i]
int bit[N], posi[N];
vector<int> used;
void upd(int id, int val){
used.pb(id);
for(; id <= n; id += id & -id){
bit[id] += val;
// cout << id << " " << n << " " << "OK\n";
}
}
int get(int id){
int ans = 0;
for(; id; id -= id & -id) ans += bit[id];
return ans;
}
int answer = 0;
void upd(int id){
int savee = posi[id];
while(id){
set<ii>::iterator it = segs[num[id]].upper_bound({pos[id], oo});
//int tempo = (it == segs[num[id]].end() ? len[num[id]] : (*it).fi - 1);
it--;
// id = par[head[num[id]]];
//cout << id << "\n";
// continue;
int lst = (*it).se, lstt = pos[id] + 1;
vector<ii> v;
for(; ; it--){
v.pb((*it));
//assert((*it).se != 0);
// cout << "HEHEHE " << (*it).se << "\n";
// cout << lstt << " " << (*it).fi << "\n";
// cout << (*it).fi << " " << (*it).se << " " << (lstt - (*it).fi) << " " << get((*it).se - 1) << "\n";
answer += (lstt - (*it).fi) * get((*it).se - 1);
upd((*it).se, (lstt - (*it).fi));
if(it == segs[num[id]].begin()) break;
lstt = (*it).fi;
}
// id = par[head[num[id]]];
// continue;
for(auto it : v) segs[num[id]].erase(it);
segs[num[id]].insert({1, savee});
segs[num[id]].insert({pos[id] + 1, lst});
id = par[head[num[id]]];
}
}
#ifdef local
void process(){
cin >> n;
vector<int> diff;
for(int i = 1; i <= n; i++){
cin >> c[i];
diff.pb(c[i]);
}
diff.pb(-1);
sort(diff.begin(), diff.end());
diff.resize(unique(diff.begin(), diff.end()) - diff.begin());
for(int i = 1; i <= n; i++) posi[i] = lower_bound(diff.begin(), diff.end(), c[i]) - diff.begin();
ck[1] = 1;
for(int i = 1; i < n; i++){
int x, y;
cin >> x >> y;
if(!ck[x]){
//cout << x << "\n";
Adj[y].pb(x);
par[x] = y;
ck[x] = 1;
ind[i] = x;
}
else{
Adj[x].pb(y);
par[y] = x;
ck[y] = 1;
ind[i] = y;
}
}
dfs(1);
hld(1, 1);
for(int i = 1; i <= n; i++){
len[num[i]] = max(len[num[i]], pos[i]);
segs[i].insert({1, n});
}
//return;
for(int i = 1; i < n; i++){
answer = 0;
upd(ind[i]);
cout << answer << "\n";
for(auto it : used){
int temp = it;
for(; temp <= n; temp += temp & -temp) bit[temp] = 0;
}
used.clear();
}
}
signed main(){
//ios_base::sync_with_stdio(0);
//cin.tie(0);
process();
}
#endif
Details
Tip: Click on the bar to expand more detailed information
Subtask #1:
score: 0
Wrong Answer
Test #1:
score: 7
Accepted
time: 3ms
memory: 40344kb
input:
2 804289384 846930887 1 2
output:
0
result:
ok single line: '0'
Test #2:
score: 0
Accepted
time: 1ms
memory: 38300kb
input:
10 505335291 738766720 190686789 260874576 747983062 906156499 502820865 142559278 261608746 380759628 1 3 1 5 5 7 3 8 1 4 3 10 7 6 5 9 5 2
output:
0 0 0 1 0 1 0 0 0
result:
ok 9 lines
Test #3:
score: -7
Wrong Answer
time: 1ms
memory: 40312kb
input:
100 205554747 483147986 844158169 953350441 612121426 310914941 210224073 856883377 922860802 495649265 8614859 989089925 378651394 344681740 29100603 816952842 21468265 552076976 87517202 953369896 374612516 787097143 126313439 207815259 287632274 886964648 220723886 119448938 444268469 865680799 6...
output:
0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 2 0 2 0 3 4 3 0 0 1 2 1 0 2 0 2 0 5 7 1 1 2 0 0 1 1 0 3 1 2 3 0 0 1 2 2 3 0 2 0 0 0 8 0 2 2 0 1 3 2 3 2 0 0 2 0 0 1 0 4 2 0 3 0 2 0 3 0 2 1 0 0 4 6 2 2 0 0 1 6 1 2 0
result:
wrong answer 10th lines differ - expected: '1', found: '0'
Subtask #2:
score: 0
Skipped
Dependency #1:
0%
Subtask #3:
score: 0
Skipped
Dependency #1:
0%