이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
#define F first
#define S second
#define pb push_back
#define ppb pop_back
#define ep insert
#define endl '\n'
#define elif else if
#define pow pwr
#define sqrt sqrtt
#define int long long
#define ll long long
#define y1 YONE
typedef unsigned long long ull;
using namespace std;
const int N=3e5+5;
int a[N],n,dp[N],c[N],p[N],lg[N];
vector<vector<int>> ps,g,s[20];
int query(int i,int l,int r){
if (l>r) swap(l,r);
int j=lg[r-l+1];
return min(s[j][i][l],s[j][i][r-(1<<j)+1]);
}
int32_t main(){
ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
for (int i=2;i<N;i++) lg[i]=lg[i/2]+1;
cin>>n>>a[0];
int ans=a[0];
for (int i=1;i<=n;i++){
cin>>a[i]>>p[i];
c[p[i]]=i;
}
priority_queue<vector<int>> pq;
for (int i=1;i<=n;i++){
if (p[i]) continue;
vector<int> b;
b.pb(a[i]);
int x=i;
while (c[x]){
x=c[x];
b.pb(a[x]);
}
ps.pb({});g.pb({});
g.back().resize(b.size(),-1);
ps.back().pb(b[0]);
for (int i=1;i<b.size();i++){
ps.back().pb(ps.back().back()+b[i]);
}
stack<int> st;
for (int i=b.size()-1;i>=0;i--){
while (!st.empty() && ps.back()[st.top()]<=ps.back()[i]) st.pop();
if (!st.empty()) g.back()[i]=st.top();
st.push(i);
}
for (int j=0;j<20;j++) s[j].pb(ps.back());
for (int j=1;j<20;j++){
for (int k=0;k+(1<<j)<=b.size();k++) s[j].back()[k]=min(s[j-1].back()[k],s[j-1].back()[k+(1<<(j-1))]);
}
/*for (int j=0;j<20;j++){
for (int k=0;k<b.size();k++) cout<<s[j].back()[k]<<' ';cout<<endl;
}*/
for (int i=0;i<b.size();i++){
if (ps.back()[i]<=0) continue;
//cout<<query(ps.size()-1,0,i)<<endl;
pq.push({query(ps.size()-1,0,i),ps.size()-1,i,-1});
break;
}
//for (auto u:ps.back()) cout<<u<<' ';cout<<endl;
//for (auto u:g.back()) cout<<u<<' ';cout<<endl;
}
while (!pq.empty()){
int i=pq.top()[1],j=pq.top()[2],last=pq.top()[3],mn=pq.top()[0];
pq.pop();
//cout<<ans<<' '<<i<<' '<<j<<' '<<mn<<' '<<last<<endl;
if (ans+mn<0) break;
ans+=ps[i][j];
if (last>=0) ans-=ps[i][last];
if (g[i][j]==-1) continue;
int k=g[i][j];
pq.push({query(i,last+1,k)-ps[i][j],i,k,j});
}
cout<<ans-a[0]<<endl;
return 0;
}
컴파일 시 표준 에러 (stderr) 메시지
Main.cpp: In function 'int32_t main()':
Main.cpp:46:23: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
46 | for (int i=1;i<b.size();i++){
| ~^~~~~~~~~
Main.cpp:57:34: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
57 | for (int k=0;k+(1<<j)<=b.size();k++) s[j].back()[k]=min(s[j-1].back()[k],s[j-1].back()[k+(1<<(j-1))]);
| ~~~~~~~~^~~~~~~~~~
Main.cpp:62:23: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
62 | for (int i=0;i<b.size();i++){
| ~^~~~~~~~~
Main.cpp:65:54: warning: narrowing conversion of '(ps.std::vector<std::vector<long long int> >::size() - 1)' from 'std::vector<std::vector<long long int> >::size_type' {aka 'long unsigned int'} to 'long long int' [-Wnarrowing]
65 | pq.push({query(ps.size()-1,0,i),ps.size()-1,i,-1});
| ~~~~~~~~~^~
Main.cpp:65:54: warning: narrowing conversion of '(ps.std::vector<std::vector<long long int> >::size() - 1)' from 'std::vector<std::vector<long long int> >::size_type' {aka 'long unsigned int'} to 'long long int' [-Wnarrowing]
# | 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... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |