用s[i]代表1~i的球数,每次查i~j相当于知道了s[j]-s[i-1]的奇偶性
要确切知道每个位置有没有球,就是要知道所有s[i]-s[i-1]的奇偶性
然后对应一个MST的模型= =
code:
#include<set>
#include<map>
#include<deque>
#include<queue>
#include<stack>
#include<cmath>
#include<ctime>
#include<bitset>
#include<string>
#include<vector>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<climits>
#include<complex>
#include<iostream>
#include<algorithm>
#define ll long long
using namespace std;
const int maxn = 2100;
int n;
struct edge{int x,y,c;}a[maxn*maxn]; int len;
inline bool cmp(const edge x,const edge y){return x.c<y.c;}
int fa[maxn];
int findfa(const int x){return fa[x]==x?x:fa[x]=findfa(fa[x]);}
ll re;
int main()
{
scanf("%d",&n);
for(int i=1;i<=n;i++) fa[i]=i;
for(int i=1;i<=n;i++)
{
for(int j=i;j<=n;j++)
{
int x; scanf("%d",&x);
a[++len]=(edge){i-1,j,x};
}
}
sort(a+1,a+len+1,cmp);
for(int i=1;i<=len;i++)
{
int x=a[i].x,y=a[i].y;
int f1=findfa(x),f2=findfa(y);
if(f1!=f2) fa[f2]=f1,re+=a[i].c;
}
printf("%lld\n",re);
return 0;
}