이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include<bits/stdc++.h>
#define MAXN 100007
using namespace std;
struct edge{
int ind,cost;
};
struct query{
int s,t,x,y,l,r;
};
int n,m,q,tt,dist[MAXN],costs[MAXN],ans[MAXN];
int a[MAXN],b[MAXN],fr[MAXN];
int from[MAXN],to[MAXN],ret[MAXN];
vector<int> euler,w[MAXN];
vector< pair<int,int> > v[MAXN];
edge e[MAXN];
query qs[MAXN];
bool cmp(edge fr,edge sc){
return fr.cost<sc.cost;
}
int dp[4*MAXN][20];
bool u[4*MAXN][20];
int lg[4*MAXN],power[20];
void calc(){
power[0]=1;
for(int i=1;i<20;i++)power[i]=power[i-1]*2;
lg[1]=0;
for(int i=2;i<=4*n;i++)lg[i]=lg[i/2]+1;
}
int rmq(int i,int j){
if(j==0)return euler[i];
if(u[i][j])return dp[i][j];
u[i][j]=true;
dp[i][j]=min( rmq(i,j-1) , rmq(i+power[j-1],j-1) );
return dp[i][j];
}
int getmin(int l,int r){
int len=r-l+1;
return min( rmq(l,lg[len]) , rmq(r-power[lg[len]]+1,lg[len]) );
}
int lca(int x,int y){
if(fr[x]>fr[y])swap(x,y);
return ret[ getmin(fr[x],fr[y]) ];
}
long long tree[4*MAXN];
void update(int v,int l,int r,int ll,int rr,long long val){
if(ll>rr)return;
if(l==ll and r==rr){
tree[v]+=val;
}else{
int tt=(l+r)/2;
update(2*v,l,tt,ll,min(tt,rr),val);
update(2*v+1,tt+1,r,max(tt+1,ll),rr,val);
}
}
long long getval(int v,int l,int r,int pos){
if(l==r)return tree[v];
int tt=(l+r)/2;
if(pos<=tt)return getval(2*v,l,tt,pos)+tree[v];
else return getval(2*v+1,tt+1,r,pos)+tree[v];
}
long long getdist(int x,int y){
return getval(1,1,n,from[x]) + getval(1,1,n,from[y]) - getval(1,1,n,from[lca(x,y)]);
}
void dfs(int x,int p,int dd){
tt++; from[x]=tt; ret[tt]=x;
fr[x]=int(euler.size());
euler.push_back(from[x]);
dist[x]=dd;
for(int i=0;i<v[x].size();i++){
if(v[x][i].first==p)continue;
dfs(v[x][i].first,x,dd+costs[v[x][i].second]);
euler.push_back(from[x]);
}
to[x]=tt;
}
void parallel_binary(){
for(int i=0;i<=m;i++)w[i].clear();
for(int i=1;i<=q;i++){
if(qs[i].l+1<qs[i].r){
w[(qs[i].l+qs[i].r)/2].push_back(i);
}
}
for(int i=1;i<=4*n;i++)tree[i]=0;
for(int i=0;i<=m;i++){
if(i>0)update(1,1,n,from[b[e[i].ind]],to[b[e[i].ind]],e[i].cost);
for(int f:w[i]){
if(getdist(qs[f].s,qs[f].t)<=qs[f].y){
qs[f].l=(qs[f].l+qs[f].r)/2;
}else{
qs[f].r=(qs[f].l+qs[f].r)/2;
}
}
}
}
void find_sol(){
for(int i=0;i<=m;i++)w[i].clear();
for(int i=1;i<=q;i++){
w[qs[i].l].push_back(i);
}
for(int i=1;i<=4*n;i++)tree[i]=0;
for(int i=0;i<=m;i++){
if(i>0)update(1,1,n,from[b[e[i].ind]],to[b[e[i].ind]],1);
for(int f:w[i]){
ans[f]=dist[qs[f].s]+dist[qs[f].t]-dist[lca(qs[f].s,qs[f].t)] - getdist(qs[f].s,qs[f].t);
}
}
}
int main(){
cin>>n>>m>>q;
calc();
for(int i=1;i<=n-1;i++){
cin>>a[i]>>b[i];
v[a[i]].push_back({b[i],i});
v[b[i]].push_back({a[i],i});
}
for(int i=1;i<=n-1;i++){
if(from[a[i]]>from[b[i]])swap(a[i],b[i]);
}
for(int i=1;i<=m;i++){
cin>>e[i].ind>>e[i].cost;
costs[e[i].ind]++;
}
sort(e+1,e+m+1,cmp);
dfs(1,0,0);
for(int i=1;i<=q;i++){
cin>>qs[i].s>>qs[i].t>>qs[i].x>>qs[i].y;
qs[i].l=0;
qs[i].r=m+1;
}
for(int i=1;i<=17;i++){
parallel_binary();
}
find_sol();
for(int i=1;i<=q;i++){
ans[i]=qs[i].x-ans[i];
if(ans[i]<0)cout<<"-1\n";
else cout<<ans[i]<<"\n";
}
return 0;
}
컴파일 시 표준 에러 (stderr) 메시지
currencies.cpp: In function 'void dfs(int, int, int)':
currencies.cpp:91:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
91 | for(int i=0;i<v[x].size();i++){
| ~^~~~~~~~~~~~
# | 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... |