이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
typedef long long ll;
ll pie(ll army){return (1ll<<army);}
#include <bits/stdc++.h>
#define fr first
#define sc second
#define pb push_back
#define endl '\n'
#define mid ((left+right)>>1)
const ll inf=2000000000000000005;
const int sonsuz=2000000005;
using namespace std;
ll fpow(ll x,ll y,ll m=0){if(y<0){cout<<"powError";return -1;}if(m)x%=m;ll res=1;while(y>0){if(y&1)res*=x;x*=x;if(m){x%=m;res%=m;}y>>=1;}return res;}
struct Seg{
int n;
vector<ll>mx,lazy,arr;
void build(int node=1,int left=0,int right=-1){
if(right==-1)right=n-1;
if(left==right){
mx[node]=arr[left];
return;
}
build(node+1,left,mid);build(node+((mid-left+1)<<1),mid+1,right);
mx[node]=max(mx[node+1],mx[node+((mid-left+1)<<1)]);
}
void push(int node,int left,int right){
if(lazy[node]==0)return;
mx[node]+=lazy[node];
if(left!=right){
lazy[node+1]+=lazy[node];
lazy[node+((mid-left+1)<<1)]+=lazy[node];
}
lazy[node]=0;
}
void yap(vector<ll>v){
arr=v;
n=arr.size();
mx.resize((n<<1)+2);
lazy.resize((n<<1)+2,0);
build();
}
int l,r;ll x;
void up(int node=1,int left=0,int right=-1){
if(right==-1)right=n-1;
if(left>=l&&right<=r){
lazy[node]+=x;
push(node,left,right);
return;
}
push(node,left,right);
if(left>r||right<l)return;
up(node+1,left,mid);up(node+((mid-left+1)<<1),mid+1,right);
mx[node]=max(mx[node+1],mx[node+((mid-left+1)<<1)]);
}
void update(int lef,int rig,ll val){
l=lef;r=rig;x=val;
up();
}
ll qu(int node=1,int left=0,int right=-1){
if(right==-1)right=n-1;
if(left>r||right<l)return -inf;
push(node,left,right);
if(left>=l&&right<=r)return mx[node];
return max(qu(node+1,left,mid),qu(node+((mid-left+1)<<1),mid+1,right));
}
ll query(int lef,int rig){
l=lef;r=rig;
return qu();
}
};
int n,q;
ll w,K[100001];
vector<pair<int,ll>>adj[100001];
map<int,pair<int,int>>in_out[100001];
map<int,int>dep[100001];
multiset<ll>vals[100001];
Seg seg[100001];
int level[100001],par[100001],parnum[100001];
pair<int,int>edge[100001];
multiset<ll>bests;
// temp stuff
int sub[100001];
int tim,root;
// temp stuff
void dfs1(int pos,int prev){
sub[pos]=1;
for(auto[x,y]:adj[pos]){
if(level[x]==0&&x!=prev){
dfs1(x,pos);
sub[pos]+=sub[x];
}
}
}
void dfs2(int pos,int prev,ll l,vector<ll>&v){
v[tim]+=l;
in_out[root][pos].fr=tim++;
for(auto[x,y]:adj[pos]){
if(level[x])continue;
if(x==prev)continue;
dep[root][x]=dep[root][pos]+1;
dfs2(x,pos,y,v);
}
in_out[root][pos].sc=tim;
v[tim]-=l;
}
void cal(int pos,int prev,int num){
dfs1(pos,0);
int total=sub[pos];
int paren=pos;
while(true){
int nex=0;
for(auto[x,y]:adj[pos]){
if(level[x]!=0||x==paren)continue;
if(sub[x]>(total>>1)){
nex=x;
break;
}
}
if(!nex)break;
paren=pos;
pos=nex;
}
par[pos]=prev;
parnum[pos]=num;
level[pos]=level[par[pos]]+1;
vals[pos].insert(0);
vals[pos].insert(0);
root=pos;
tim=0;
vector<ll>v(total+1,0);
dfs2(pos,pos,0,v);
for(int j=1;j<v.size();j++){
v[j]+=v[j-1];
}
seg[pos].yap(v);
for(int i=0;i<adj[pos].size();i++){
if(level[adj[pos][i].fr])continue;
vals[pos].insert(-seg[pos].query(in_out[pos][adj[pos][i].fr].fr,in_out[pos][adj[pos][i].fr].sc-1));
}
ll topla=-(*vals[pos].begin())-(*(++vals[pos].begin()));
bests.insert(topla);
for(int i=0;i<adj[pos].size();i++){
if(level[adj[pos][i].fr])continue;
cal(adj[pos][i].fr,pos,i);
}
}
void code(){
cin>>n>>q>>w;
for(int i=0;i<n-1;i++){
int a,b;ll c;
cin>>a>>b>>c;
K[i]=c;
edge[i]={a,b};
adj[a].pb({b,c});
adj[b].pb({a,c});
}
cal(1,0,0);
ll ans=0;
while(q--){
int d;ll e;
cin>>d>>e;
d=(d+ans)%(n-1);
e=(e+ans)%w;
if(level[edge[d].fr]>level[edge[d].sc])swap(edge[d].fr,edge[d].sc);
int pos=edge[d].fr;
int pos2=edge[d].sc;
int sira;
while(pos2!=pos){
sira=parnum[pos2];
pos2=par[pos2];
}
while(pos){
ll topla=-(*vals[pos].begin())-(*(++vals[pos].begin()));
bests.erase(bests.find(topla));
vals[pos].erase(vals[pos].find(-seg[pos].query(in_out[pos][adj[pos][sira].fr].fr,in_out[pos][adj[pos][sira].fr].sc-1)));
int cur=edge[d].fr;
if(dep[pos][cur]<dep[pos][edge[d].sc])cur=edge[d].sc;
seg[pos].update(in_out[pos][cur].fr,in_out[pos][cur].sc-1,e-K[d]);
vals[pos].insert(-seg[pos].query(in_out[pos][adj[pos][sira].fr].fr,in_out[pos][adj[pos][sira].fr].sc-1));
topla=-(*vals[pos].begin())-(*(++vals[pos].begin()));
bests.insert(topla);
sira=parnum[pos];
pos=par[pos];
}
K[d]=e;
ans=*(--bests.end());
cout<<ans<<endl;
}
}
int main(){
ios_base::sync_with_stdio(false);cin.tie(NULL);
bool usaco=0;if(usaco){freopen(".in","r",stdin);freopen(".out","w",stdout);}
int t=1;
if(!t)cin>>t;
while(t--){code();}
return 0;
}
컴파일 시 표준 에러 (stderr) 메시지
diameter.cpp: In function 'void cal(int, int, int)':
diameter.cpp:136:15: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
136 | for(int j=1;j<v.size();j++){
| ~^~~~~~~~~
diameter.cpp:140:15: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
140 | for(int i=0;i<adj[pos].size();i++){
| ~^~~~~~~~~~~~~~~~
diameter.cpp:146:15: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
146 | for(int i=0;i<adj[pos].size();i++){
| ~^~~~~~~~~~~~~~~~
diameter.cpp: In function 'int main()':
diameter.cpp:198:32: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
198 | bool usaco=0;if(usaco){freopen(".in","r",stdin);freopen(".out","w",stdout);}
| ~~~~~~~^~~~~~~~~~~~~~~~~
diameter.cpp:198:57: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
198 | bool usaco=0;if(usaco){freopen(".in","r",stdin);freopen(".out","w",stdout);}
| ~~~~~~~^~~~~~~~~~~~~~~~~~~
diameter.cpp: In function 'void code()':
diameter.cpp:180:110: warning: 'sira' may be used uninitialized in this function [-Wmaybe-uninitialized]
180 | vals[pos].erase(vals[pos].find(-seg[pos].query(in_out[pos][adj[pos][sira].fr].fr,in_out[pos][adj[pos][sira].fr].sc-1)));
| ^
# | 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... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |