QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#126750 | #6737. Neighbourhood | csy2005 | TL | 3ms | 50524kb | C++14 | 7.5kb | 2023-07-18 22:28:24 | 2023-07-18 22:29:08 |
Judging History
answer
#include <iostream>
#include <algorithm>
#include<cmath>
#include<cstring>
#include<cstdio>
#include<cstdlib>
#include<vector>
#include<iomanip>
#include<ctime>
#include<set>
#include<map>
#include<queue>
#include<stack>
#include<bitset>
#include<cassert>
#define sqr(x) ((x)*(x))
#define fz1(i,n) for ((i)=1;(i)<=(n);(i)++)
#define fd1(i,n) for ((i)=(n);(i)>=1;(i)--)
#define fz0g(i,n) for ((i)=0;(i)<=(n);(i)++)
#define fd0g(i,n) for ((i)=(n);(i)>=0;(i)--)
#define fz0k(i,n) for ((i)=0;(i)<(n);(i)++)
#define fd0k(i,n) for ((i)=(((long long)(n))-1);(i)>=0;(i)--)
#define fz(i,x,y) for ((i)=(x);(i)<=(y);(i)++)
#define fd(i,y,x) for ((i)=(y);(i)>=(x);(i)--)
#define fzin fz1(i,n)
#define fzim fz1(i,m)
#define fzjn fz1(j,n)
#define fzjm fz1(j,m)
#define ff(c,itr) for (__typeof((c).begin()) itr=(c).begin();itr!=(c).end();++itr)
#define pb push_back
#define mk make_pair
#define rdst(st,len){static char ss[len];scanf(" %s",ss);(st)=ss;}
#define spln(i,n) (i==n?'\n':' ')
#define fac_init(n){fac[0]=fac[1]=inv[1]=fi[0]=fi[1]=1;fz(i,2,n){fac[i]=1ll*fac[i-1]*i%mod;inv[i]=1ll*(mod-mod/i)*inv[mod%i]%mod;fi[i]=1ll*fi[i-1]*inv[i]%mod;}}
using namespace std;
typedef long long i64;
typedef long double f80;
typedef unsigned int u32;
typedef unsigned long long u64;
inline void read(int &x)
{
char c;int f=1;
while(!isdigit(c=getchar()))if(c=='-')f=-1;
x=(c&15);while(isdigit(c=getchar()))x=(x<<1)+(x<<3)+(c&15);
x*=f;
}
inline void read(i64 &x)
{
char c;int f=1;
while(!isdigit(c=getchar()))if(c=='-')f=-1;
x=(c&15);while(isdigit(c=getchar()))x=(x<<1)+(x<<3)+(c&15);
x*=f;
}
const int B=500,C=200000/B*5;
int blk,n,q,i,j,fid[200005],px[200005],py[200005],pz[200005],vis[200005],sz[200005],vbel[200005],cnt,imp[200005],tp[C+5],dw[C+5];
vector<int> bi[200005];
void dfs1(int x,int fa)
{
bool flg=0;sz[x]=1;fid[x]=fa;
ff(bi[x],it)if(*it!=fa){
int y=px[*it]^py[*it]^x;
dfs1(y,*it);sz[x]+=sz[y];
if(vbel[x]&&vbel[y]) flg=1; else vbel[x]|=vbel[y];
}
if(flg||sz[x]>blk){
sz[x]=1;imp[x]=vbel[x]=++cnt;
dw[cnt]=x;
ff(bi[x],it)if(*it!=fa){
int y=px[*it]^py[*it]^x;
if(vbel[y]) tp[vbel[y]]=x;
}
}
}
void dfs3(int x,int fa,int col)
{
vbel[x]=col;
ff(bi[x],it)if(*it!=fa){
int y=px[*it]^py[*it]^x;
dfs3(y,*it,col);
}
if(!dw[col]) dw[col]=x;
}
void dfs2(int x,int fa)
{
if(!vbel[x]) return;
if(!imp[x]){
ff(bi[x],it)if(*it!=fa){
int y=px[*it]^py[*it]^x;
if(vbel[y]) dfs2(y,*it);
else dfs3(y,*it,vbel[x]);
}
}
else{
int cur=0x3f3f3f3f,tmp=-1;
vector<pair<int,int> > v;
ff(bi[x],it)if(*it!=fa){
int y=px[*it]^py[*it]^x;
if(vbel[y]) dfs2(y,*it),v.push_back(make_pair(vbel[y],sz[y]));
}
ff(bi[x],it)if(*it!=fa){
int y=px[*it]^py[*it]^x;
if(!vbel[y]){
while(cur+sz[y]>blk){
if(!v.empty()){
tmp=v.back().first;cur=v.back().second;
v.pop_back();
}
else tp[tmp=++cnt]=x,cur=0;
}
cur+=sz[y];
dfs3(y,*it,tmp);
}
}
}
}
int impe[200005];
int bid[200005],bc[C+5],tbc[C+5],seq[C+5][B+5],iid[200005],tot;
int ti,dfnu[C+5][B+5],dfeu[C+5][B+5],mpu[C+5][B+5],dfnd[C+5][B+5],dfed[C+5][B+5],mpd[C+5][B+5];
i64 dia[C+5];
vector<pair<i64,int> > sequ[C+5],seqd[C+5];
vector<pair<int,int> > bi2[C+5][B+5];
vector<pair<int,int> > bi3[C*2+5];
#define calcid(x,b) ((tp[b]==(x))?1:bid[x])
bool dfsu(int x,int fa,int b,i64 dis)
{
bool flg=(x==dw[b]);
int t=++bc[b];seq[b][t]=x;if(tp[b]!=x) bid[x]=t;
mpu[b][dfnu[b][t]=++ti]=t;
ff(bi[x],it)if(*it!=fa){
int y=px[*it]^py[*it]^x;
if(y==tp[b]||vbel[y]==b){
impe[*it]=dfsu(y,*it,b,dis+pz[*it]);
flg|=impe[*it];
bi2[b][t].push_back(make_pair(calcid(y,b),*it));
bi2[b][calcid(y,b)].push_back(make_pair(t,*it));
}
}
dfeu[b][t]=ti;
if(!iid[x]) sequ[b].push_back(make_pair(dis,dfnu[b][t]));
if(x==dw[b]) dia[b]=dis;
return flg;
}
void dfsd(int x,int fa,int b,i64 dis)
{
int t=calcid(x,b);
mpd[b][dfnd[b][t]=++ti]=t;
ff(bi[x],it)if(*it!=fa){
int y=px[*it]^py[*it]^x;
if(y==tp[b]||vbel[y]==b){
dfsd(y,*it,b,dis+pz[*it]);
}
}
dfed[b][t]=ti;
if(!iid[x]) seqd[b].push_back(make_pair(dis,dfnd[b][t]));
}
i64 dis0[C*2+5],dis1[B+5];
int calc(const vector<pair<i64,int> > &v,i64 d)
{
// for(const auto u:v) cerr<<u.first<<' '<<u.second<<endl;
if(d<0) return 0;
return upper_bound(v.begin(),v.end(),make_pair(d,0x1064822E))-v.begin();
}
vector<pair<i64,int> > merge(const vector<pair<i64,int> > &a,const vector<pair<i64,int> > &b)
{
vector<pair<i64,int> > res(a.size()+b.size());int i=0,j=0,k=0;
while(i<a.size()||j<b.size()){
if(i<a.size()&&(j==b.size()||a[i]<b[j])){
res[k++]=a[i++];
}
else{
res[k++]=b[j++];
}
}
return res;
}
void flushu(int b,int l,int r,int dlt)
{
int i;
vector<pair<i64,int> > v0,v1;
ff(sequ[b],it)if(l<=it->second&&it->second<=r) v1.push_back(make_pair(it->first+dlt,it->second));else v0.push_back(*it);
sequ[b]=merge(v0,v1);
}
void flushd(int b,int l,int r,int dlt)
{
int i;
vector<pair<i64,int> > v0,v1;
ff(seqd[b],it)if(l<=it->second&&it->second<=r) v1.push_back(make_pair(it->first+dlt,it->second));else v0.push_back(*it);
seqd[b]=merge(v0,v1);
}
int main()
{
read(n);read(q);fz1(i,n-1) read(px[i]),read(py[i]),read(pz[i]),bi[px[i]].push_back(i),bi[py[i]].push_back(i);
blk=min(n-1,B);dfs1(1,0);cerr<<cnt<<endl;dfs2(1,0);cerr<<cnt<<endl;fz1(i,cnt)if(tp[i]==0)tp[i]=1;
fz1(i,cnt){
if(!iid[dw[i]]) iid[dw[i]]=++tot;
if(!iid[tp[i]]) iid[tp[i]]=++tot;
}
fz1(i,n) bid[i]=1;
fz1(i,n)if(fid[px[i]]==i) swap(px[i],py[i]);
fz1(i,cnt){
ti=0;dfsu(tp[i],0,i,0);
ti=0;dfsd(dw[i],0,i,0);
bi3[iid[tp[i]]].push_back(make_pair(iid[dw[i]],i));
bi3[iid[dw[i]]].push_back(make_pair(iid[tp[i]],i));
sort(sequ[i].begin(),sequ[i].end());
sort(seqd[i].begin(),seqd[i].end());
}
/* fz1(i,cnt) tbc[i]=1;
fz(i,2,n) tbc[vbel[i]]++;
fz1(i,cnt) assert(bc[i]==tbc[i]);*/
// fz1(i,cnt) cerr<<bc[i]<<' ';cerr<<endl;
double s1=0,s2=0,s3=0,s4=0;
while(q--){
int op;read(op);
if(op==2){
int x,b=0,ans=0;i64 lim;read(x);read(lim);
memset(dis0,-0x3f,sizeof(dis0));
if(iid[x]) dis0[iid[x]]=lim;
else{
b=vbel[x];memset(dis1,-0x3f,sizeof(dis1));
s1-=clock();
dis1[calcid(x,b)]=lim;queue<int> qx;qx.push(calcid(x,b));
while(!qx.empty()){
int x=qx.front();qx.pop();
if(dis1[x]<0) continue;
if(!iid[seq[b][x]]) ans++;
ff(bi2[b][x],it){
int y=it->first;i64 z=dis1[x]-pz[it->second];
if(dis1[y]==dis1[0]) dis1[y]=z,qx.push(y);
}
}
dis0[iid[tp[b]]]=dis1[calcid(tp[b],b)];
dis0[iid[dw[b]]]=dis1[calcid(dw[b],b)];
s1+=clock();
}
s2-=clock();
queue<int> qx;fz1(i,tot)if(dis0[i]>=0) qx.push(i);
while(!qx.empty()){
int x=qx.front();qx.pop();
if(dis0[x]<0) continue;ans++;
ff(bi3[x],it){
int y=it->first;i64 z=dis0[x]-dia[it->second];
if(dis0[y]==dis0[0]) dis0[y]=z,qx.push(y);
}
}
s2+=clock();
s3-=clock();
fz1(i,cnt)if(i!=b){
if(dis0[iid[tp[i]]]>dis0[iid[dw[i]]]) ans+=calc(sequ[i],dis0[iid[tp[i]]]);
else ans+=calc(seqd[i],dis0[iid[dw[i]]]);
}
s3+=clock();
printf("%d\n",ans);
}
else{
s4-=clock();
int id,c;read(id);read(c);int dlt=c-pz[id];pz[id]=c;
int b=vbel[py[id]],x=calcid(px[id],b),y=calcid(py[id],b);
flushu(b,dfnu[b][y],dfeu[b][y],dlt);
flushd(b,dfnd[b][impe[id]?x:y],dfed[b][impe[id]?x:y],dlt);
dia[b]+=impe[id]*dlt;
s4+=clock();
}
}
cerr<<s1/CLOCKS_PER_SEC<<endl;
cerr<<s2/CLOCKS_PER_SEC<<endl;
cerr<<s3/CLOCKS_PER_SEC<<endl;
cerr<<s4/CLOCKS_PER_SEC<<endl;
return 0;
}
詳細信息
Test #1:
score: 100
Accepted
time: 3ms
memory: 50524kb
input:
3 7 1 2 3 2 3 1 2 2 1 2 1 3 2 3 4 1 1 1 2 2 1 2 1 0 2 3 1
output:
2 2 3 3 1 2
result:
ok 6 numbers
Test #2:
score: -100
Time Limit Exceeded
input:
200000 200000 1 2 146181238 2 3 45037818 3 4 176924060 4 5 969365276 5 6 683948619 6 7 356268194 7 8 871634797 8 9 630254480 9 10 283061123 10 11 204904965 11 12 838381393 12 13 516373812 13 14 253862710 14 15 223572474 15 16 114452997 16 17 145251056 17 18 905638436 18 19 375445402 19 20 549829545 ...
output:
219 62303 1358 5532 65345 682 11856 120285 4980 5689 2998 2314 18102 8014 20512 2827 113022 74534 159775 14517 17961 21855 8138 265 3336 3251 7023 35187 4932 151611 14338 101 899 117 64441 888 10380 1833 29381 1014 4806 10770 23734 236 37258 2280 14550 2196 38205 80950 80839 4517 74570 13972 95914 7...