hdu6867
题目
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int INF=1e9+5;
const int maxn=5e5+5;
struct edge
{
int v,next;
};
edge no[maxn];
int head[maxn];
int sz[maxn];
ll mm[maxn];
int cnt;
void init(int n)
{
for(int i=1;i<=n;i++)
{
head[i]=0;
sz[i]=1;
}
cnt=1;
}
void add(int x,int y)
{
no[cnt].v=y;
no[cnt].next=head[x];
head[x]=cnt;
cnt++;
}
void dfs(int u,int fa)
{
for(int i=head[u];i;i=no[i].next)
{
int v=no[i].v;
if(v==fa)
continue;
dfs(v,u);
sz[u]+=sz[v];
}
}
void Dfs(int u,int fa)
{
mm[u]=mm[fa]+sz[1]-sz[u];
for(int i=head[u];i;i=no[i].next)
{
int v=no[i].v;
if(v==fa)
continue;
Dfs(v,u);
}
}
void solve()
{
ll max_=0;
ll res=0;
int n;
cin>>n;
init(n);
for(int i=2;i<=n;i++)
{
int fai;
cin>>fai;
add(fai,i);
}
dfs(1,-1);
for(int i=1;i<=n;i++)
res+=sz[i];
Dfs(1,-1);
for(int i=1;i<=n;i++)
{
if(mm[i]>max_)
max_=mm[i];
}
cout<<res+max_<<endl;
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0),cout.tie(0);
int _=1;
cin>>_;
while(_--)
{
//init();
solve();
}
return 0;
}
hdu6869
题目
博弈 如果 先手可以把目前的状态变成之前的先手输状态那么他肯定是赢得
对于k=0来说
(0,0) 先手没得选肯定是输的 能变成0得肯定是赢得 发现 (1,2)首先不能变成0 对称得 (2,1)
也不行 再者 (3,5) (4,7)(6,10)…
(1)发现其首元素是之前得元素得mex值
(2) 其差值在逐渐递增
因此为(奇异局势)
k=1
(0,0)(1,3)(2,6)(4,10)…
对于一开始得差值也就是逐渐增加差值得 等差d (在这题中就是(k+1))
用(bb-aa)/d 求出他是第几项 对于第i项 bb=floor(i*(d+2+sqrt(d2+4))/2)
这些都是必败态判断一下就行了
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main()
{
ios::sync_with_stdio(0);
cin.tie(0),cout.tie(0);
int T;
cin>>T;
while(T--)
{
ll a,b,k;
cin>>a>>b>>k;
ll aa=min(a,b);
ll bb=max(a,b);
if((bb-aa)%(k+1)!=0)
cout<<"1"<<"\n";
else
{
ll xx=(bb-aa)/(k+1);
if(bb==floor(1.0*xx*(1.0*(k+3+sqrt(k*k+2*k+5))/2)))
{
cout<<"0"<<"\n";
}
else
cout<<"1"<<"\n";
}
}
return 0;
}
hdu6873
题目
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int INF=1e9+5;
const int N=2e5+5;
int b[N];
int lc[N];
int rc[N];
int val[N];
int sz[N];
int mn[N];
int rnd[N];
ll s[N];
int mx[N];
int root;
int tot;
int last;
void init(int n)
{
tot=0;
last=0;
root=1;
for(int i=1;i<=n;i++)
{
lc[i]=rc[i]=0;
}
sz[0]=0;mn[0]=INF;mx[0]=0;s[0]=0;
}
void push(int u)
{
sz[u]=sz[lc[u]]+sz[rc[u]]+1;
mn[u]=min(val[u],min(mn[lc[u]],mn[rc[u]]));
mx[u]=max(val[u],max(mx[lc[u]],mx[rc[u]]));
s[u]=val[u]+s[lc[u]]+s[rc[u]];
}
int build(int l,int r)
{
if(l>r)
return 0;
int mid=(l+r)/2;
tot++;
int u=tot;
rnd[u]=rand();
val[u]=b[mid];
lc[u]=build(l,mid-1);
rc[u]=build(mid+1,r);
push(u);
return u;
}
int kft(int x)
{
int u=root;
while(sz[lc[u]]+1!=x)
{
if(sz[lc[u]]+1<x)
{
x=x-(sz[lc[u]]+1);
u=rc[u];
}
else
{
u=lc[u];
}
}
return u;
}
void bfs(int u,int n)
{
if(u==0)
return ;
bfs(lc[u],n);
last++;
cout<<val[u];
if(last!=n)
cout<<" ";
bfs(rc[u],n);
}
void split(int u,int xx,int &x,int &y)
{
if(u==0)
{
x=y=0;
return;
}
if(sz[lc[u]]+1<=xx)
{
x=u;
split(rc[u],xx-sz[lc[u]]-1,rc[u],y);
}
else
{
y=u;
split(lc[u],xx,x,lc[u]);
}
push(u);
}
void split_min(int u,int yy,int &x,int &y)
{
if(u==0)
{
x=y=0;
return;
}
if(mn[rc[u]]>=yy&&val[u]>=yy)
{
y=u;
split_min(lc[u],yy,x,lc[u]);
}
else
{
x=u;
split_min(rc[u],yy,rc[u],y);
}
push(u);
}
int merge_(int x,int y)
{
if(!x||!y)
return x+y;
if(rnd[x]<rnd[y]) ///随机重构
{
rc[x]=merge_(rc[x],y), push(x);///x的右边是 rc[x] 和 y lc[x]<x<rc[x]<y
return x;
}
else
{
lc[y]=merge_(x,lc[y]), push(y);///y的左边是 x 和 lc[y] x<lc[y]<y<rc[y]
return y;
}
}
ll perform(int x,int y)
{
if(y>val[kft(x)])
return 0;
int L,R;
split(root,x,L,R);
if(mn[L]>=y)
{
root=merge_(L,R);
return 0;
}
int l,r;
ll res;
split_min(L,y,l,r);
res=s[r]-1ll*(y-1)*sz[r];
int l1,r1,l2,r2;
split(l,sz[l]-1,l1,r1);
split(r,1,l2,r2);
val[r1]+=val[l2]-(y-1);
val[l2]=y-1;
s[r1]=mn[r1]=mx[r1]=val[r1];
s[l2]=mn[l2]=mx[l2]=val[l2];
root=merge_(merge_(merge_(l1,r1),merge_(r2,l2)),R);
return res;
}
void solve()
{
int n,q;
cin>>n>>q;
init(n);
for(int i=1;i<=n;i++)
cin>>b[i];
build(1,n);
for(int i=1;i<=q;i++)
{
int op,x,y;
cin>>op>>x;
if(op==1)
{
cin>>y;
cout<<perform(x,y)<<"\n";
}
else
{
int u=kft(x);
cout<<val[u]<<"\n";
}
}
bfs(root,n);
cout<<"\n";
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0),cout.tie(0);
int _=1;
cin>>_;
while(_--)
{
solve();
}
return 0;
}