이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
//####################
//TwoCurrencies
//####################
#include<bits/stdc++.h>
using ll = long long;
using namespace std;
const int maxiN = 1000042;
const int LOG = 18;
int n, m, q;
vector < int > links[maxiN];
int depth[maxiN];
int up[maxiN][LOG];
void root(int node, int d = 0, int parent = -1) {
up[node][0] = parent;
depth[node] = d;
for (int v: links[node])
if (v != parent) {
root(v, d + 1, node);
}
}
int getK(int node, int K) {
for (int i = LOG - 1; i >= 0; i--) {
if ((K >> i) & 1) {
node = up[node][i];
}
}
return node;
}
int LCA(int a, int b) {
if (depth[b] > depth[a]) swap(a, b);
a = getK(a, depth[a] - depth[b]);
if (a == b) {
return a;
}
for (int l = LOG - 1; l >= 0; l--) {
if (up[a][l] != up[b][l]) {
a = up[a][l];
b = up[b][l];
}
}
return up[a][0];
}
pair<int,int> eulerseg[maxiN];
int actEuleur=-1;
void computeEuleurTour(int node,int parent = -1){
actEuleur++;
eulerseg[node].first = actEuleur;
for(int v:links[node])if(parent!=v){
computeEuleurTour(v,node);
}
actEuleur++;
eulerseg[node].second = actEuleur;
}
struct segtree{
vector<int> vals;
int leaf;
segtree(int n){
leaf = (1<<((int)ceil(log2(n))));
vals.resize(2*leaf);
fill(vals.begin(),vals.end(),0);
}
segtree()=default;
void init(int n){
leaf = (1<<((int)ceil(log2(n))));
vals.resize(2*leaf);
fill(vals.begin(),vals.end(),0);
}
void reset(){
fill(vals.begin(),vals.end(),0);
}
void _addRange(int a,int b,int val){
if(a==b){
vals[a]+=val;
}else if(a&1){
vals[a]+=val;
_addRange(a+1,b,val);
}else if(b%2==0){
vals[b]+=val;
_addRange(a,b-1,val);
}else{
_addRange(a/2,b/2,val);
}
}
void addRange(int a,int b,int val){
_addRange(a+leaf,b+leaf,val);
}
int getVal(int pos){
pos+=leaf;
int re = 0;
for(int p=pos;p>=1;p>>=1){
re+=vals[p];
}
return re;
}
};
struct req{
int a,b;
ll x,y;
int lca;
ll totalSum;
};
vector<pair<ll,int>> W_child;
vector<req> queries;
vector<ll> ans;
struct node{
int deb,fin;
vector<int> id_req;
};
int middle(node&node_){
int m = (node_.deb+node_.fin)/2;
if(m==node_.fin)m=node_.deb;
return m;
}
segtree pathsum,occurence;
pair<int,int> computePath(int a,int b,int lca){
ll nbocc=0;
ll sumGold=0;
if(a==lca && b==lca){
nbocc=0;
}else if(a==lca){
nbocc=occurence.getVal(eulerseg[b].first)-occurence.getVal(eulerseg[a].first);
sumGold=pathsum.getVal(eulerseg[b].first)-pathsum.getVal(eulerseg[a].first);
}else if(b==lca){
nbocc=occurence.getVal(eulerseg[a].first)-occurence.getVal(eulerseg[b].first);
sumGold=pathsum.getVal(eulerseg[a].first)-pathsum.getVal(eulerseg[b].first);
}else{
nbocc=occurence.getVal(eulerseg[b].first)+occurence.getVal(eulerseg[a].first)-2*occurence.getVal(eulerseg[lca].first);;
sumGold=pathsum.getVal(eulerseg[b].first)+pathsum.getVal(eulerseg[a].first)-2*pathsum.getVal(eulerseg[lca].first);;
}
return {sumGold,nbocc};
}
vector<bool> oracle(vector<int> reqs,int p){
vector<bool> ans(reqs.size());
for(int i:reqs){
pair<int,int> rep = computePath(queries[i].a,queries[i].b,queries[i].lca);
if(queries[i].x>=rep.second && queries[i].y<=(queries[i].totalSum-rep.first)){
ans[i]=true;
}else{
ans[i]=false;
}
}
return ans;
}
void parallel_binary_search(){
//usagé donc on nettoie
pathsum.reset();
occurence.reset();
vector<int> ids;
for(int i = 0;i<n;i++){
ids.push_back(i);
}
node first = {0,n-1,ids};
vector<node> nodes = {first};
for(int lvl=0;lvl<LOG;lvl++){
cout<<"#0 :"<<lvl<<endl;
pathsum.reset();
occurence.reset();
int actPush = n;
vector<node> next_lvl;
cout<<"#1"<<endl;
for(node &node_:nodes){
if(node_.deb==node_.fin){
//singleton
for(int i:node_.id_req){
ans[i] = node_.deb;
}
}else{
int mid = middle(node_);
cout<<"here"<<endl;
//On met à jour les segtree pour l'oracle
while(actPush!=mid){
actPush--;
pathsum.addRange(eulerseg[W_child[actPush].second].first,eulerseg[W_child[actPush].second].second,W_child[actPush].first);
occurence.addRange(eulerseg[W_child[actPush].second].first,eulerseg[W_child[actPush].second].second,1);
}
cout<<"here"<<endl;
vector<bool> dir = oracle(node_.id_req,mid);
cout<<"here"<<endl;
node left,right;
left.deb = node_.deb;
left.fin = mid;
right.deb = mid+1;
right.fin = node_.fin;
for(int i:node_.id_req){
if(dir[i]){
right.id_req.push_back(i);
}else{
left.id_req.push_back(i);
}
}
next_lvl.push_back(right);
next_lvl.push_back(left);
}
}
cout<<"#2"<<lvl<<endl;
nodes.clear();
swap(nodes,next_lvl);
}
}
signed main() {
fill(depth, depth + maxiN, -1);
cin >> n >> m >> q;
queries.resize(q);
occurence.init(2*n);
pathsum.init(2*n);
ans.resize(q);
vector < pair < int, int >> edges;
for (int i = 0; i < n - 1; i++) {
int a, b;
cin >> a >> b;
a--;
b--;
edges.push_back({a,b});
links[a].push_back(b);
links[b].push_back(a);
}
root(0, 0, -1);
int C=-1;
computeEuleurTour(0);
for (int i = 0; i < m; i++) {
int p, c;
cin >> p >> c;
C = c;
p--;
auto a = edges[p].first;
auto b = edges[p].second;
if (depth[a] < depth[b]) swap(a, b);
occurence.addRange(eulerseg[a].first,eulerseg[a].second,1);
pathsum.addRange(eulerseg[a].first,eulerseg[a].second,C);
W_child.push_back({c,a});
}
//On calcule la tab de LCA
for (int l = 1; l < LOG; l++) {
for (int i = 0; i < n; i++) {
if (up[i][l - 1] == -1) {
up[i][l] = -1;
}
up[i][l] = up[up[i][l - 1]][l - 1];
}
}
for (int i = 0; i < q; i++) {
int a,b;
ll x, y;
cin >> a >> b >> x >> y;
a--;
b--;
int lca = LCA(a, b);
queries[i] = {a,b,x,y,lca,computePath(a,b,lca).first};
}
parallel_binary_search();
return 0;
};
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |