#include<iostream>#include<cstdio>#include<cstring>#include<cmath>#defineexp1e-8#defineREregister#defineILinlineusingnamespace std;double f[110],a[110][110];bool flag[110],light[110];structformula{
double s[110];}s[110];
IL intGAUSS(RE int n,formula* s,double* ans,bool* flag,bool* sign){
RE int i,j,k,l;
RE double tmp;
RE bool res;memset(flag,0,sizeof(flag));memset(sign,0,sizeof(sign));for(i=l=1;i<=n&&l<=n;++i,++l){
for(j=i+1,k=i;j<=n;++j)if(fabs(s[k].s[l])<fabs(s[j].s[l]))k=j;if(i^k)swap(s[i],s[k]);if(fabs(s[i].s[l])<exp){
flag[l]=1,--i;continue;}for(j=1;j<=n;++j)if(i^j&&fabs(s[j].s[l])>=exp)for(tmp=s[j].s[l]/s[i].s[l],s[j].s[0]-=s[i].s[0]*tmp,k=l;k<=n;++k)
s[j].s[k]-=s[i].s[k]*tmp;}for(res=0,i=1;i<=n;sign[i]=bool(j>n),res|=sign[i],++i)if(fabs(s[i].s[0])>=exp)for(j=1;j<=n&&fabs(s[i].s[j])<exp;++j);if(res)return-1;for(res=0,i=1;i<=n;res|=flag[i],++i)if(!flag[i])ans[i]=s[i].s[0]/s[i].s[i];if(res)return0;return1;}intmain(){
int n,i,j,ans;scanf("%d",&n);for(i=1;i<=n;scanf("%lf",&s[i].s[0]),++i)for(j=1;j<=n;++j)scanf("%lf",&s[i].s[j]);
ans=GAUSS(n,s,f,flag,light);/* for(i=1;i<=n;printf("%.2lf\n",s[i].s[0]),++i)
for(j=1;j<=n;++j)
printf("%.2lf ",s[i].s[j]);*/if(ans==-1||ans==0)printf("No Solution");elsefor(i=1;i<=n;++i)printf("%.2lf\n",f[i]);/* if(ans==-1)
{
printf("\nNo solution\n");
for(i=1;i<=n;++i)
if(light[i])
printf("%d\n",i);
}
if(ans==0)
{
printf("\nFree\n");
for(i=1;i<=n;++i)
if(flag[i])
printf("%d\n",i);
}
if(ans==1)printf("\nSolution\n");*/return0;}
树剖
#include<cstdio>#defineILinline#defineREregister#defineN30010#defineE60010#defineINF2147483647#defineswap(x, y) x ^= y, y ^= x, x ^= yusingnamespace std;int head[N], e[E], nxt[E], tot;int tree[N], mx[N <<2], sum[N <<2];int dep[N], top[N], dfn[N], fa[N], siz[N], big[N], cnt;int s[N], n;
IL intmax(RE int x, RE int y){
return x > y ? x : y;}
IL voidadd(RE int x, RE int y){
e[++tot]= y, nxt[tot]= head[x], head[x]= tot;
e[++tot]= x, nxt[tot]= head[y], head[y]= tot;return;}
IL voidbuild(RE int x, RE int l, RE int r){
if(l == r){
mx[x]= sum[x]= tree[l];return;}
RE int mid =(l + r)>>1;build(x <<1, l, mid),build(x <<1|1, mid +1, r);
mx[x]=max(mx[x <<1], mx[x <<1|1]);
sum[x]= sum[x <<1]+ sum[x <<1|1];return;}
IL voidchange(RE int x, RE int l, RE int r, RE int w, RE int v){
if(l == r){
mx[x]= sum[x]= v;return;}
RE int mid =(l + r)>>1;if(w <= mid)change(x <<1, l, mid, w, v);elsechange(x <<1|1, mid +1, r, w, v);
mx[x]=max(mx[x <<1], mx[x <<1|1]);
sum[x]= sum[x <<1]+ sum[x <<1|1];return;}
IL intquery_max(RE int x, RE int l, RE int r, RE int left, RE int right){
if(left <= l && r <= right)return mx[x];
RE int mid =(l + r)>>1, ans =-INF;if(left <= mid)
ans =max(ans,query_max(x <<1, l, mid, left, right));if(right > mid)
ans =max(ans,query_max(x <<1|1, mid +1, r, left, right));return ans;}
IL intquery_sum(RE int x, RE int l, RE int r, RE int left, RE int right){
if(left <= l && r <= right)return sum[x];
RE int mid =(l + r)>>1, ans =0;if(left <= mid)
ans +=query_sum(x <<1, l, mid, left, right);if(right > mid)
ans +=query_sum(x <<1|1, mid +1, r, left, right);return ans;}
IL intquerymax(RE int x, RE int y){
RE int ans;for(ans =-INF; top[x]!= top[y]; x = fa[top[x]]){
if(dep[top[x]]< dep[top[y]])swap(x, y);
ans =max(ans,query_max(1,1, n, dfn[top[x]], dfn[x]));}if(dep[x]> dep[y])swap(x, y);
ans =max(ans,query_max(1,1, n, dfn[x], dfn[y]));return ans;}
IL intquerysum(RE int x, RE int y){
RE int ans;for(ans =0; top[x]!= top[y]; x = fa[top[x]]){
if(dep[top[x]]< dep[top[y]])swap(x, y);
ans +=query_sum(1,1, n, dfn[top[x]], dfn[x]);}if(dep[x]> dep[y])swap(x, y);
ans +=query_sum(1,1, n, dfn[x], dfn[y]);return ans;}
IL voiddfs(RE int x, RE int y){
dep[x]= dep[y]+1, fa[x]= y, siz[x]=1;for(RE int i = head[x]; i; i = nxt[i])if(e[i]!= y){
dfs(e[i], x), siz[x]+= siz[e[i]];if(siz[e[i]]> siz[big[x]])
big[x]= e[i];}return;}
IL voidDFS(RE int x, RE int y){
dfn[x]=++cnt, tree[cnt]= s[x], top[x]= y;if(big[x])DFS(big[x], y);for(int i = head[x]; i; i = nxt[i])if(e[i]!= fa[x]&& e[i]!= big[x])DFS(e[i], e[i]);return;}intmain(){
// freopen("data1.in","r",stdin);// freopen("tree.out","w",stdout);char str[10];
RE int i;int x, y, q;scanf("%d",&n), cnt = tot =0;for(i =1; i < n;++i)scanf("%d%d",&x,&y),add(x, y);for(i =1; i <= n;++i)scanf("%d",&s[i]);
fa[1]=1,dfs(1,0),DFS(1,1),build(1,1, n);for(scanf("%d",&q); q;--q){
scanf("%s%d%d", str,&x,&y);switch(str[1]){
case'H':change(1,1, n, dfn[x], y);break;case'M':printf("%d\n",querymax(x, y));break;case'S':printf("%d\n",querysum(x, y));break;}}// fclose(stdin);// fclose(stdout);return0;}