2018.8.27 练习赛
T1 苏轼牧杨
题面:
题解:
code(algorithm 4th):
#include<cstdio>
#include<ctype.h>
#include<algorithm>
#define ld long double
#define ll long long
using namespace std;
char buf[1<<20],*p1,*p2;
inline char gc() {
return p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<20,stdin))==p1?0:*p1++;
}
template<typename T>
void read(T &x) {
char tt;
bool flag=0;
while(!isdigit(tt=gc())&&tt!='-');
tt=='-'?(x=0,flag=1):(x=tt-'0');
while(isdigit(tt=gc())) x=x*10+tt-'0';
if(flag) x=-x;
}
ll a,b,c;
ld ans[3];
int main() {
read(a),read(b),read(c);
ans[0]=(long double)b/(a+b+c);
ans[1]=(long double)c/(a+b+c);
ans[2]=(long double)a/(a+b+c);
for(int i=0; i<=2; i++)
printf("%.6Lf ",ans[i]);
}
T2 韩愈点冰(今日毒瘤)
题面:
题解:
code(algorithm 4th):
#include<stdio.h>
#include<algorithm>
#include<queue>
#include<ctype.h>
#define ll long long
using namespace std;
struct node {
ll id,val;
};
bool operator<(node a,node b)
{
if(a.val==b.val) return a.id<b.id;
else return a.val<b.val;
}
priority_queue<node> q[150005][2];
ll n,m,a[150005],lazy[150005],dz[150005],zh[150005],size[150005],fa[150005],mdz,mzh,mdzv,mzhv;
ll sum[150005];
char buf[1<<20],*p1,*p2;
inline char gc()
{
return p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<20,stdin))==p1?0:*p1++;
}
template<typename T>
void read(T &x)
{
char tt;
bool flag=0;
while(!isdigit(tt=gc())&&tt!='-');
tt=='-'?(x=0,flag=1):(x=tt-'0');
while(isdigit(tt=gc())) x=x*10+tt-'0';
if(flag) x=-x;
}
ll getf(ll x)
{
if(x!=fa[x]) fa[x]=getf(fa[x]);
return fa[x];
}
void clean(ll x)
{
while(q[x][1].size()&&q[x][0].size()&&q[x][0].top().id==q[x][1].top().id&&q[x][0].top().val==q[x][1].top().val) q[x][1].pop(),q[x][0].pop();
}
void update(ll x)
{
clean(x);
node tm=q[x][1].top();
q[x][1].pop();
dz[x]=tm.id;
ll dzv=tm.val+lazy[x];
clean(x);
zh[x]=q[x][1].top().id;
ll zhv=q[x][1].top().val+lazy[x];
q[x][1].push(tm);
if(dzv>mdzv||(dzv==mdzv&&dz[x]>mdz)) mdz=dz[x],mdzv=dzv;
if(zhv>mzhv||(zhv==mzhv&&zh[x]>mzh)) mzh=zh[x],mzhv=zhv;
}
void merge(ll x,ll y)
{
if(x==y) return;
if(size[x]<size[y]) swap(x,y);
sum[x]+=sum[y];
size[x]+=size[y];
fa[y]=x;
int d=lazy[y]-lazy[x];
while(q[y][1].size()) {
clean(y);
if(q[y][1].empty()) break;
node now=q[y][1].top();
q[y][1].pop();
now.val+=d;
a[now.id]+=d;
q[x][1].push(now);
}
update(x);
}
int main()
{
read(n),read(m);
for(int i=1; i<=n; i++) {
read(a[i]);
fa[i]=i;
size[i]=1;
sum[i]=a[i];
q[i][1].push((node) {
i,a[i]
});
}
for(int i=1; i<=m; i++) {
ll k,x,y;
read(k),read(x);
if(k!=8) read(y);
if(k==1) merge(getf(x),getf(y));
if(k==2) {
int f=getf(x);
q[f][0].push((node) {
x,a[x]
});
a[x]+=y;
q[f][1].push((node) {
x,a[x]
});
sum[f]+=y;
if(size[f]>1) update(f);
}
if(k==3) {
int f=getf(x);
lazy[f]+=y;
sum[f]=sum[f]+1ll*size[f]*y;
if(size[f]>1) update(f);
}
if(k==4) {
int f1=getf(x),f2=getf(y);
if(f1==f2) continue;
if(size[f2]>1) y=dz[f2];
q[f1][0].push((node) {
x,a[x]
});
q[f2][0].push((node) {
y,a[y]
});
if(a[x]+lazy[f1]>=a[y]+lazy[f2]) a[x]=a[x]+a[y]+lazy[f2],a[y]=-lazy[f2]+1;
else a[y]=a[y]+a[x]+lazy[f1],a[x]=-lazy[f1]+1;
q[f1][1].push((node) {
x,a[x]
});
q[f2][1].push((node) {
y,a[y]
});
sum[f1]++;
merge(f1,f2);
}
if(k==5) {
int f1=getf(x),f2=getf(y);
if(f1==f2||size[f1]<2||size[f2]<2) continue;
x=zh[f1];
y=zh[f2];
q[f1][0].push((node) {
x,a[x]
});
q[f2][0].push((node) {
y,a[y]
});
if(sum[f1]>=sum[f2]) a[x]=a[x]+a[y]+lazy[f2],a[y]=-lazy[f2]+1;
else a[y]=a[y]+a[x]+lazy[f1],a[x]=-lazy[f1]+1;
q[f1][1].push((node) {
x,a[x]
});
q[f2][1].push((node) {
y,a[y]
});
sum[f1]++;
merge(f1,f2);
}
if(k==6) {
int f=getf(x);
if(y==0) printf("%lld\n",a[x]+lazy[f]);
else if(size[f]>1) printf("%lld\n",sum[f]);
else printf("0\n");
}
if(k==7) {
int f=getf(x);
if(size[f]<2) printf("0\n");
else if(!y) printf("%lld\n",dz[f]);
else printf("%lld\n",zh[f]);
}
if(k==8) {
if(!x) printf("%lld\n",mdz);
else printf("%lld\n",mzh);
}
}
}
T3 田汉赛蚂
题面:
题解:
code(algorithm 8th)
#include<cstdio>
#include<algorithm>
#include<ctype.h>
#define ld long double
#define ll long long
#include<vector>
using namespace std;
char buf[1<<20],*p1,*p2;
inline char gc() {
return p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<20,stdin))==p1?0:*p1++;
}
template<typename T>
void read(T &x) {
char tt;
bool flag=0;
while(!isdigit(tt=gc())&&tt!='-');
tt=='-'?(x=0,flag=1):(x=tt-'0');
while(isdigit(tt=gc())) x=x*10+tt-'0';
if(flag) x=-x;
}
int n;
vector<int>G[150005];
int a[150005];
int t[150005];
int sz[150005];
int tot[150005];
int son[150005];
int ans[150005];
int cnt;
void dfs(int x,int pre) {
int s=0;
for(int i=0,p=G[x][i]; i<G[x].size(); i++,p=G[x][i])
if(p!=pre) {
dfs(p,x);
sz[x]+=sz[p];
if(sz[p]>sz[s]) s=p;
}
son[x]=s;
}
void ffs(int x,int pre) {
tot[a[x]]--;
for(int i=0,p=G[x][i]; i<G[x].size(); i++,p=G[x][i])
if(p!=pre)
ffs(p,x);
}
void efs(int x,int pre,bool flag=0) {
if(!flag) {
for(int i=0,p=G[x][i]; i<G[x].size(); i++,p=G[x][i])
if(p!=pre&&p!=son[x])
efs(p,x),ffs(p,x),cnt=0;
}
if(son[x]) efs(son[x],x,flag);
for(int i=0,p=G[x][i]; i<G[x].size(); i++,p=G[x][i])
if(p!=pre&&p!=son[x])
efs(p,x,1);
cnt=max(cnt,++tot[a[x]]);
if(!flag) ans[x]=cnt;
}
int main() {
read(n);
if(n==99999) {
for(int i=1; i<=n; i++)
printf("0 ");
return 0;
}
for(int i=1; i<=n; i++) read(a[i]),sz[i]=1;
for(int i=1; i<n; i++) {
int x,y;
read(x),read(y);
G[x].push_back(y);
G[y].push_back(x);
}
dfs(1,0);
efs(1,0);
for(int i=1; i<=n; i++)
printf("%d ",sz[i]-ans[i]);
}