BZOJ3714 [PA2014]Kuglarz

传送门

这里记\(s_i\)为前\(i\)个位置下面的球个数之奇偶性(\(s_0=0\))

我们只要把\(s_1\)\(s_n\)求出来救星了

考虑一个区间询问\((l,r)\),其实求的是\(s_{l-1}\ xor\ s_r\),也就是知道其中一个可以推出另一个

我们要知道所有的\(s_1\)\(s_n\),如果看成图,也就是点\(0\)\(n\)要全部连通,每个区间就是边\((l-1,r)\),所以直接上最小生成树

注意使用的方法

#include<bits/stdc++.h>
#define LL long long
#define il inline
#define re register
#define max(a,b) ((a)>(b)?(a):(b))
#define min(a,b) ((a)<(b)?(a):(b))
 
using namespace std;
const int N=2000+10;
il LL rd()
{
    re LL x=0,w=1;re char ch=0;
    while(ch<'0'||ch>'9') {if(ch=='-') w=-1;ch=getchar();}
    while(ch>='0'&&ch<='9') {x=(x<<3)+(x<<1)+(ch^48);ch=getchar();}
    return x*w;
}
LL ans,mp[N][N],di[N];
int n;
bool v[N];
 
int main()
{
  n=rd();
  memset(mp,63,sizeof(mp));
  memset(di,63,sizeof(di));
  for(int i=1;i<=n;i++)
    for(int j=i;j<=n;j++)
      mp[i-1][j]=mp[j][i-1]=rd();
  di[0]=0;
  for(int h=0;h<=n;h++)
    {
      int x=n+1;
      for(int i=0;i<=n;i++)
        if(!v[i]&&di[i]<di[x]) x=i;
          v[x]=true,ans+=di[x];
      for(int y=1;y<=n;y++)
        if(!v[y]&&di[y]>mp[x][y]) di[y]=mp[x][y];
    }
  printf("%lld\n",ans);
  return 0;
}

posted @ 2018-09-25 22:29  ✡smy✡  阅读(63)  评论(0编辑  收藏  举报