dp 选讲
这个题让我们算 ,考虑这个式子的组合意义,可以看作我们有两个相同的容器,最后取出来的方案是一样的的情况。 这时两个人的取球的方案数都是 ,所以方案总数就是 ,这时,对于dp,我们设 两个装置表示一共拿了i次,从第一个装置的上方拿了j次,从第二个装置的上方拿了k次的方案数 dp就有4种情况,分别是:
- 都从上面取
- 第一个从上面取,第二个从下面取
- 第一个从下面取,第二个从上面取
- 第一个从下面取,第二个从下面取
这四种情况都要求取出来的球一样 启示: 对于难以直接维护的东西,看有无组合意义等优化转移
#include<bits/stdc++.h>
namespace fast_IO {
#define IOSIZE 5000000
char ibuf[IOSIZE], obuf[IOSIZE], *p1 = ibuf, *p2 = ibuf, *p3 = obuf;
#define getchar() ((p1==p2)and(p2=(p1=ibuf)+fread(ibuf,1,IOSIZE,stdin),p1==p2)?(EOF):(*p1++))
#define putchar(x) ((p3==obuf+IOSIZE)&&(fwrite(obuf,p3-obuf,1,stdout),p3=obuf),*p3++=x)
#define isdigit(ch) (ch>47&&ch<58)
#define isspace(ch) (ch<33)
template<typename T> inline T read() { T s = 0; int w = 1; char ch; while (ch = getchar(), !isdigit(ch) and (ch != EOF)) if (ch == '-') w = -1; if (ch == EOF) return false; while (isdigit(ch)) s = s * 10 + ch - 48, ch = getchar(); return s * w; }
template<typename T> inline bool read(T &s) { s = 0; int w = 1; char ch; while (ch = getchar(), !isdigit(ch) and (ch != EOF)) if (ch == '-') w = -1; if (ch == EOF) return false; while (isdigit(ch)) s = s * 10 + ch - 48, ch = getchar(); return s *= w, true; }
template<typename T> inline void print(T x) { if (x < 0) putchar('-'), x = -x; if (x > 9) print(x / 10); putchar(x % 10 + 48); }
inline bool read(char &s) { while (s = getchar(), isspace(s)); return true; }
inline bool read(char *s) { char ch; while (ch = getchar(), isspace(ch)); if (ch == EOF) return false; while (!isspace(ch)) *s++ = ch, ch = getchar(); *s = '\000'; return true; }
inline void print(char x) { putchar(x); }
inline void print(char *x) { while (*x) putchar(*x++); }
inline void print(const char *x) { for (int i = 0; x[i]; i++) putchar(x[i]); }
inline bool read(std::string& s) { s = ""; char ch; while (ch = getchar(), isspace(ch)); if (ch == EOF) return false; while (!isspace(ch)) s += ch, ch = getchar(); return true; }
inline void print(std::string x) { for (int i = 0, n = x.size(); i < n; i++) putchar(x[i]); }
inline bool read(bool &b) { char ch; while(ch=getchar(), isspace(ch)); b=ch^48; return true; }
inline void print(bool b) { putchar(b+48); }
template<typename T, typename... T1> inline int read(T& a, T1&... other) { return read(a) + read(other...); }
template<typename T, typename... T1> inline void print(T a, T1... other) { print(a), print(other...); }
struct Fast_IO { ~Fast_IO() { fwrite(obuf, p3 - obuf, 1, stdout); } } io;
template<typename T> Fast_IO& operator >> (Fast_IO &io, T &b) { return read(b), io; }
template<typename T> Fast_IO& operator << (Fast_IO &io, T b) { return print(b), io; }
#define cout io
#define cin io
#define endl '\n'
} using namespace fast_IO;
using namespace std;
const int N=500+10,mod=1024523;
int dp[2][N][N];//一共选i次,
int n,m;
string s1,s2;
signed main()
{
cin>>n>>m;
cin>>s1>>s2;
s1=" "+s1;
s2=" "+s2;
dp[0][0][0]=1;
for(int i=1,o=1;i<=n+m;i++,o^=1)
{
for(int j=0;j<=n;j++)
for(int k=0;k<=n;k++)
dp[o][j][k]=0;
for(int j=0;j<=min(i,n);j++)
{
for(int k=0;k<=min(i,n);k++)
{
if(j&&k&&s1[j]==s1[k])
dp[o][j][k]+=dp[o^1][j-1][k-1];
if(j&&(i-k)&&s1[j]==s2[i-k])
dp[o][j][k]+=dp[o^1][j-1][k];
if((i-j)&&k&&s2[i-j]==s1[k])
dp[o][j][k]+=dp[o^1][j][k-1];
if((i-j)&&(i-k)&&s2[i-j]==s2[i-k])
dp[o][j][k]+=dp[o^1][j][k];
dp[o][j][k]%=mod;
}
}
}
cout<<dp[(n+m)&1][n][n]<<endl;
return 0;
}P2943 [USACO09MAR] Cleaning Up G
这个题首先注意到最优代价一定是小于 的,因为如果分段的代价大于n,我可以直接一个人一段,总代价是
然后每一个段内的代价不能超过 ,即总代价不能超过 ,所以我们的dp只能从不超过 的地方转移过来,这样就可以动态维护桶
表示在i这个位置的最小代价
启示: 对于dp式,看可否去掉无用的状态转移,如这道题就是有前i个优化到了前 个
#include<bits/stdc++.h>
namespace fast_IO {
#define IOSIZE 5000000
char ibuf[IOSIZE], obuf[IOSIZE], *p1 = ibuf, *p2 = ibuf, *p3 = obuf;
#define getchar() ((p1==p2)and(p2=(p1=ibuf)+fread(ibuf,1,IOSIZE,stdin),p1==p2)?(EOF):(*p1++))
#define putchar(x) ((p3==obuf+IOSIZE)&&(fwrite(obuf,p3-obuf,1,stdout),p3=obuf),*p3++=x)
#define isdigit(ch) (ch>47&&ch<58)
#define isspace(ch) (ch<33)
template<typename T> inline T read() { T s = 0; int w = 1; char ch; while (ch = getchar(), !isdigit(ch) and (ch != EOF)) if (ch == '-') w = -1; if (ch == EOF) return false; while (isdigit(ch)) s = s * 10 + ch - 48, ch = getchar(); return s * w; }
template<typename T> inline bool read(T &s) { s = 0; int w = 1; char ch; while (ch = getchar(), !isdigit(ch) and (ch != EOF)) if (ch == '-') w = -1; if (ch == EOF) return false; while (isdigit(ch)) s = s * 10 + ch - 48, ch = getchar(); return s *= w, true; }
template<typename T> inline void print(T x) { if (x < 0) putchar('-'), x = -x; if (x > 9) print(x / 10); putchar(x % 10 + 48); }
inline bool read(char &s) { while (s = getchar(), isspace(s)); return true; }
inline bool read(char *s) { char ch; while (ch = getchar(), isspace(ch)); if (ch == EOF) return false; while (!isspace(ch)) *s++ = ch, ch = getchar(); *s = '\000'; return true; }
inline void print(char x) { putchar(x); }
inline void print(char *x) { while (*x) putchar(*x++); }
inline void print(const char *x) { for (int i = 0; x[i]; i++) putchar(x[i]); }
inline bool read(std::string& s) { s = ""; char ch; while (ch = getchar(), isspace(ch)); if (ch == EOF) return false; while (!isspace(ch)) s += ch, ch = getchar(); return true; }
inline void print(std::string x) { for (int i = 0, n = x.size(); i < n; i++) putchar(x[i]); }
inline bool read(bool &b) { char ch; while(ch=getchar(), isspace(ch)); b=ch^48; return true; }
inline void print(bool b) { putchar(b+48); }
template<typename T, typename... T1> inline int read(T& a, T1&... other) { return read(a) + read(other...); }
template<typename T, typename... T1> inline void print(T a, T1... other) { print(a), print(other...); }
struct Fast_IO { ~Fast_IO() { fwrite(obuf, p3 - obuf, 1, stdout); } } io;
template<typename T> Fast_IO& operator >> (Fast_IO &io, T &b) { return read(b), io; }
template<typename T> Fast_IO& operator << (Fast_IO &io, T b) { return print(b), io; }
#define cout io
#define cin io
#define endl '\n'
} using namespace fast_IO;
using namespace std;
const int N=4e4+10,M=700+10;
int n,m;
int p[N];
int mp[M][N];
int dp[N];
int pos[M],cnt[M];
signed main()
{
cin>>n>>m;
for(int i=1;i<=n;i++)
cin>>p[i];
for(int i=1;i<=n;i++)
dp[i]=1e9;
dp[0]=0;
int m=sqrt(n)+10;
for(int i=1;i<=n;i++)
{
for(int j=1;j<=m;j++)//每个段内的数
{
mp[j][p[i]]++;
if(mp[j][p[i]]==1)
{
cnt[j]++;
if(cnt[j]>j)
{
while(--mp[j][p[pos[j]]]!=0)
pos[j]++;
pos[j]++;
cnt[j]--;
}
}
if(cnt[j]==j)
dp[i]=min(dp[i],dp[pos[j]-1]+j*j);
}
}
cout<<dp[n]<<endl;
return 0;
}AT_abc134_f [ABC134F] Permutation Oddness
这个题考虑先拆除绝对值
首先可以将位置和数分开考虑,然后发现位置和数造成的贡献是不变的,这个贡献不变可以这样考虑:
对于一个数,当将它放在了前面,就是绝对值种相对大的那个数,这样在不考虑小数相减的情况下,这个数使答案增加了这个数的大小这么多
如果将它放在了后面,那么它就是小数,使绝对值内被减的那个数,会使原来的答案减小这个数的大小
对于一个数,这时它的贡献无论时加上还是减去,都是这个数的大小这么多,位置也是一样的,将这两个拆开考虑即可
表示已经处理了i个数,前面待匹配的数/位置有j个,目前的总答案(就是题目中的怪异度)为k的方案数
这里我们可以通过待匹配的数来达到打乱匹配的目的,所以在 是我们默认排列时有序的
有四种转移:
- 当前这个数与自己这个位值匹配,新产生的贡献为
- 当前这个数和位置都向前匹配,新产生 的贡献,并多一个待匹配的数,意味着后面的数可以匹配这个位置
- 当前这个数和位置都向后匹配,新产生 的贡献,并少一个待匹配的数,这时由于之前有 个待匹配的数,所以方案数要乘一个系数 ,因为有两个数
- 这个数/位置一个向前匹配,一个向后匹配,待匹配的数不变,但由于之前有 个待匹配的数,所以一个数的系数是 ,有位置和数两种情况,所以乘二
这两篇题解也写的不错: 1
#include<bits/stdc++.h>
namespace fast_IO {
#define IOSIZE 5000000
char ibuf[IOSIZE], obuf[IOSIZE], *p1 = ibuf, *p2 = ibuf, *p3 = obuf;
#define getchar() ((p1==p2)and(p2=(p1=ibuf)+fread(ibuf,1,IOSIZE,stdin),p1==p2)?(EOF):(*p1++))
#define putchar(x) ((p3==obuf+IOSIZE)&&(fwrite(obuf,p3-obuf,1,stdout),p3=obuf),*p3++=x)
#define isdigit(ch) (ch>47&&ch<58)
#define isspace(ch) (ch<33)
template<typename T> inline T read() { T s = 0; int w = 1; char ch; while (ch = getchar(), !isdigit(ch) and (ch != EOF)) if (ch == '-') w = -1; if (ch == EOF) return false; while (isdigit(ch)) s = s * 10 + ch - 48, ch = getchar(); return s * w; }
template<typename T> inline bool read(T &s) { s = 0; int w = 1; char ch; while (ch = getchar(), !isdigit(ch) and (ch != EOF)) if (ch == '-') w = -1; if (ch == EOF) return false; while (isdigit(ch)) s = s * 10 + ch - 48, ch = getchar(); return s *= w, true; }
template<typename T> inline void print(T x) { if (x < 0) putchar('-'), x = -x; if (x > 9) print(x / 10); putchar(x % 10 + 48); }
inline bool read(char &s) { while (s = getchar(), isspace(s)); return true; }
inline bool read(char *s) { char ch; while (ch = getchar(), isspace(ch)); if (ch == EOF) return false; while (!isspace(ch)) *s++ = ch, ch = getchar(); *s = '\000'; return true; }
inline void print(char x) { putchar(x); }
inline void print(char *x) { while (*x) putchar(*x++); }
inline void print(const char *x) { for (int i = 0; x[i]; i++) putchar(x[i]); }
inline bool read(std::string& s) { s = ""; char ch; while (ch = getchar(), isspace(ch)); if (ch == EOF) return false; while (!isspace(ch)) s += ch, ch = getchar(); return true; }
inline void print(std::string x) { for (int i = 0, n = x.size(); i < n; i++) putchar(x[i]); }
inline bool read(bool &b) { char ch; while(ch=getchar(), isspace(ch)); b=ch^48; return true; }
inline void print(bool b) { putchar(b+48); }
template<typename T, typename... T1> inline int read(T& a, T1&... other) { return read(a) + read(other...); }
template<typename T, typename... T1> inline void print(T a, T1... other) { print(a), print(other...); }
struct Fast_IO { ~Fast_IO() { fwrite(obuf, p3 - obuf, 1, stdout); } } io;
template<typename T> Fast_IO& operator >> (Fast_IO &io, T &b) { return read(b), io; }
template<typename T> Fast_IO& operator << (Fast_IO &io, T b) { return print(b), io; }
#define cout io
#define cin io
#define endl '\n'
} using namespace fast_IO;
using namespace std;
#define int long long
const int N=50+10,mod=1e9+7;
int f[N][N][N*N*2];
int n,K;
signed main()
{
cin>>n>>K;
int pos=n*n;
f[0][0][0+pos]=1;
for(int i=1;i<=n;i++)
{
for(int j=0;j<=i;j++)
{
for(int k=-n*n;k<=n*n;k++)
{
f[i][j][k+pos]=(f[i][j][k+pos]+f[i-1][j][k+pos])%mod;
if(j&&k+i*2<=n*n)
f[i][j][k+pos]=(f[i][j][k+pos]+f[i-1][j-1][k+i*2+pos])%mod;
if(j+1<=i-1&&k-i*2>=-n*n)
f[i][j][k+pos]=(f[i][j][k+pos]+f[i-1][j+1][k-i*2+pos]*(j+1)%mod*(j+1)%mod)%mod;
if(j)
f[i][j][k+pos]=(f[i][j][k+pos]+f[i-1][j][k+pos]*2ll*j%mod)%mod;
}
}
}
cout<<f[n][0][K+pos]<<endl;
return 0;
}
/*
https://www.luogu.com.cn/article/6z6q2hzi
https://www.luogu.com.cn/article/tu8737be
*/CF1967E1 Again Counting Arrays (Easy Version)
这是一个反射容斥板子,具体实现见代码,推导过程见ppt
#include<bits/stdc++.h>
namespace fast_IO {
#define IOSIZE 5000000
char ibuf[IOSIZE], obuf[IOSIZE], *p1 = ibuf, *p2 = ibuf, *p3 = obuf;
#define getchar() ((p1==p2)and(p2=(p1=ibuf)+fread(ibuf,1,IOSIZE,stdin),p1==p2)?(EOF):(*p1++))
#define putchar(x) ((p3==obuf+IOSIZE)&&(fwrite(obuf,p3-obuf,1,stdout),p3=obuf),*p3++=x)
#define isdigit(ch) (ch>47&&ch<58)
#define isspace(ch) (ch<33)
template<typename T> inline T read() { T s = 0; int w = 1; char ch; while (ch = getchar(), !isdigit(ch) and (ch != EOF)) if (ch == '-') w = -1; if (ch == EOF) return false; while (isdigit(ch)) s = s * 10 + ch - 48, ch = getchar(); return s * w; }
template<typename T> inline bool read(T &s) { s = 0; int w = 1; char ch; while (ch = getchar(), !isdigit(ch) and (ch != EOF)) if (ch == '-') w = -1; if (ch == EOF) return false; while (isdigit(ch)) s = s * 10 + ch - 48, ch = getchar(); return s *= w, true; }
template<typename T> inline void print(T x) { if (x < 0) putchar('-'), x = -x; if (x > 9) print(x / 10); putchar(x % 10 + 48); }
inline bool read(char &s) { while (s = getchar(), isspace(s)); return true; }
inline bool read(char *s) { char ch; while (ch = getchar(), isspace(ch)); if (ch == EOF) return false; while (!isspace(ch)) *s++ = ch, ch = getchar(); *s = '\000'; return true; }
inline void print(char x) { putchar(x); }
inline void print(char *x) { while (*x) putchar(*x++); }
inline void print(const char *x) { for (int i = 0; x[i]; i++) putchar(x[i]); }
inline bool read(std::string& s) { s = ""; char ch; while (ch = getchar(), isspace(ch)); if (ch == EOF) return false; while (!isspace(ch)) s += ch, ch = getchar(); return true; }
inline void print(std::string x) { for (int i = 0, n = x.size(); i < n; i++) putchar(x[i]); }
inline bool read(bool &b) { char ch; while(ch=getchar(), isspace(ch)); b=ch^48; return true; }
inline void print(bool b) { putchar(b+48); }
template<typename T, typename... T1> inline int read(T& a, T1&... other) { return read(a) + read(other...); }
template<typename T, typename... T1> inline void print(T a, T1... other) { print(a), print(other...); }
struct Fast_IO { ~Fast_IO() { fwrite(obuf, p3 - obuf, 1, stdout); } } io;
template<typename T> Fast_IO& operator >> (Fast_IO &io, T &b) { return read(b), io; }
template<typename T> Fast_IO& operator << (Fast_IO &io, T b) { return print(b), io; }
#define cout io
#define cin io
#define endl '\n'
} using namespace fast_IO;
using namespace std;
#define int long long
const int N=4e5+10,mod=998244353;
int n,m,b0;
int fac[N],infac[N];
int M[N],M_[N];
int qpow(int a,int b)
{
int res=1;
while(b)
{
if(b&1)
res=res*a%mod;
a=a*a%mod;
b>>=1;
}
return res%mod;
}
void init()
{
fac[0]=1;
for(int i=1;i<N;i++)
fac[i]=fac[i-1]*i%mod;
infac[N-1]=qpow(fac[N-1],mod-2);
for(int i=N-2;i>=0;i--)
infac[i]=infac[i+1]*(i+1)%mod;
}
int C(int x,int y)
{
if(x<y||x<0||y<0)
return 0;
return fac[x]*infac[y]%mod*infac[x-y]%mod;
}
namespace m_small
{
int dp[2][N];//dp[i][j]表示在走到(i,j)这个点,有多少种给a数组赋值的合法的方案
void init()
{
M[0]=1;
for(int i=1;i<=n;i++)
M[i]=M[i-1]*m%mod;
for(int i=0;i<=m+2;i++)
dp[0][i]=dp[1][i]=0;
}
void solve()
{
init();
dp[0][b0+1]=1;//本来是不能走到0以下,也就是不能碰到y=x-1,但这样下标会成负数,所以统一加一
int ans=0;
for(int i=1,o=1;i<=n;i++,o^=1)
{
for(int j=0;j<=m;j++)
dp[o][j]=0;
for(int j=0;j<=m;j++)
{
if(j)
dp[o][j]=(dp[o][j]+dp[o^1][j-1]*(m-1)%mod)%mod;//因为贪心地保留能走到的最高的点,所以向上走是a有m-1种放法(除了这个点都可以)
dp[o][j]=(dp[o][j]+dp[o^1][j+1])%mod;//方案数,累加,因为贪心,向下走了一定是因为不向上走,所以此时a的放置方案只有一种
}
ans=(ans+dp[o][0]*M[n-i])%mod;//这是不合法的方案数,因为已经碰到了y=x-1即平移后的直线y=x,所以后面的a就可以无视限制随便放
dp[o][0]=0;//现在这种方案是不合法方案,所以要赋值为0,不能进入下一次更新
}
cout<<((qpow(m,n)-ans)%mod+mod)%mod<<endl;//从所有点的方案总数中减去不合法的答案
}
}
namespace m_big//反射容斥
{
int calc(int x,int y,int b1,int b2)//钦定先碰直线y=x+b1再碰或不碰直线y=x+b2的方案数
{
int res=0;
int o=1;//容斥系数
while(1)
{
if(x<0||y<0)
break;//翻到其他象限了,不用继续考虑
o=mod-o;
swap(x,y);
x-=b1;y+=b1;//根据公式将点按直线翻折
res=(res+C(x+y,y)*o%mod)%mod;//这一点的路径条数就是翻折前对应点的路径方案数,所以乘上容斥系数累加到答案里
int tmp=b2;
b2=b1;
b1=b1*2-tmp;//将y=x+b1按y=x+b2翻折
}
return res;
}
void init()
{
M[0]=M_[0]=1;
for(int i=1;i<=n;i++)//纵坐标的上限是n
{
M[i]=M[i-1]*m%mod;
M_[i]=M_[i-1]*(m-1)%mod;
}
}
void solve()
{
init();
int sum=0;
for(int i=0;i<n;i++)//枚举y=x即平移后y=x-b0上的点
{
if((i+b0)&1)
continue;
int x=(i+b0)/2,y=(i-b0)/2;
if(y<0)
continue;
int tmp=(C(x+y,y)+calc(x,y,m-b0,-1-b0)+calc(x,y,-1-b0,m-b0))%mod;//算出到这个点有多少合法的路径
sum=(sum+tmp*M_[y]%mod*M[n-i-1]%mod)%mod;
/*
这句话的意思是首先先走到y=x(未翻折)这条直线上,
这时将a的一个点正好放在b的上面,让b无法向上走,这样b就是一个不合法的方案,
当b不合法后,任意摆放a,所以乘上m^(n-i-1)
因为我们原dp式是有系数的,每向上走一次,我们会有一个(m-1)的系数,所以我们现在向上走了y次,所以乘上(m-1)^y
*/
}
cout<<((qpow(m,n)-sum)%mod+mod)%mod<<endl;
}
}
signed main()
{
init();
int T;
cin>>T;
while(T--)
{
cin>>n>>m>>b0;
if(b0>=m)
cout<<qpow(m,n)<<endl;
else if(m>sqrt(n))
m_big::solve();
else
m_small::solve();
}
return 0;
}每次分治,选长或宽来劈,每次只更新经过我劈这条线的点对,这样做是对的,因为两个点的最短路一定经过我某一次劈的轴,每次选长的那一边就可以了
#include<bits/stdc++.h>
namespace fast_IO {
#define IOSIZE 5000000
char ibuf[IOSIZE], obuf[IOSIZE], *p1 = ibuf, *p2 = ibuf, *p3 = obuf;
#define getchar() ((p1==p2)and(p2=(p1=ibuf)+fread(ibuf,1,IOSIZE,stdin),p1==p2)?(EOF):(*p1++))
#define putchar(x) ((p3==obuf+IOSIZE)&&(fwrite(obuf,p3-obuf,1,stdout),p3=obuf),*p3++=x)
#define isdigit(ch) (ch>47&&ch<58)
#define isspace(ch) (ch<33)
template<typename T> inline T read() { T s = 0; int w = 1; char ch; while (ch = getchar(), !isdigit(ch) and (ch != EOF)) if (ch == '-') w = -1; if (ch == EOF) return false; while (isdigit(ch)) s = s * 10 + ch - 48, ch = getchar(); return s * w; }
template<typename T> inline bool read(T &s) { s = 0; int w = 1; char ch; while (ch = getchar(), !isdigit(ch) and (ch != EOF)) if (ch == '-') w = -1; if (ch == EOF) return false; while (isdigit(ch)) s = s * 10 + ch - 48, ch = getchar(); return s *= w, true; }
template<typename T> inline void print(T x) { if (x < 0) putchar('-'), x = -x; if (x > 9) print(x / 10); putchar(x % 10 + 48); }
inline bool read(char &s) { while (s = getchar(), isspace(s)); return true; }
inline bool read(char *s) { char ch; while (ch = getchar(), isspace(ch)); if (ch == EOF) return false; while (!isspace(ch)) *s++ = ch, ch = getchar(); *s = '\000'; return true; }
inline void print(char x) { putchar(x); }
inline void print(char *x) { while (*x) putchar(*x++); }
inline void print(const char *x) { for (int i = 0; x[i]; i++) putchar(x[i]); }
inline bool read(std::string& s) { s = ""; char ch; while (ch = getchar(), isspace(ch)); if (ch == EOF) return false; while (!isspace(ch)) s += ch, ch = getchar(); return true; }
inline void print(std::string x) { for (int i = 0, n = x.size(); i < n; i++) putchar(x[i]); }
inline bool read(bool &b) { char ch; while(ch=getchar(), isspace(ch)); b=ch^48; return true; }
inline void print(bool b) { putchar(b+48); }
template<typename T, typename... T1> inline int read(T& a, T1&... other) { return read(a) + read(other...); }
template<typename T, typename... T1> inline void print(T a, T1... other) { print(a), print(other...); }
struct Fast_IO { ~Fast_IO() { fwrite(obuf, p3 - obuf, 1, stdout); } } io;
template<typename T> Fast_IO& operator >> (Fast_IO &io, T &b) { return read(b), io; }
template<typename T> Fast_IO& operator << (Fast_IO &io, T b) { return print(b), io; }
#define cout io
#define cin io
#define endl '\n'
} using namespace fast_IO;
using namespace std;
const int N=2e4+10,M=1e5+10;
int h[N],to[(N)<<2],ne[(N)<<2],w[(N)<<2],idx;
void add(int u,int v,int c)
{
to[++idx]=v;
w[idx]=c;
ne[idx]=h[u];
h[u]=idx;
}
int n,m;
struct Que
{
int x1,y1,x2,y2,id;
};
int id(int x,int y)
{
return (x-1)*(m)+y;
}
vector<Que> Q;
int q;
int ans[M];
int dis[N];
bool vst[N];
pair<int,int> pos[N];
bool check(int id,int x1,int y1,int x2,int y2)
{
if(!(x1<=pos[id].first&&pos[id].first<=x2))
return 0;
if(!(y1<=pos[id].second&&pos[id].second<=y2))
return 0;
return 1;
}
void dijkstra(int s,int x1,int y1,int x2,int y2)
{
for(int i=x1;i<=x2;i++)
{
for(int j=y1;j<=y2;j++)
{
dis[id(i,j)]=1e9;
vst[id(i,j)]=0;
}
}
dis[s]=0;
priority_queue<pair<int,int>,vector<pair<int,int> > ,greater<pair<int,int> > > q;
q.emplace(make_pair(dis[s],s));
while(!q.empty())
{
int u=q.top().second;
q.pop();
if(vst[u])
continue;
vst[u]=1;
for(int i=h[u];i;i=ne[i])
{
int v=to[i];
if(!check(v,x1,y1,x2,y2))
continue;
if(dis[v]>dis[u]+w[i])
{
dis[v]=dis[u]+w[i];
q.emplace(make_pair(dis[v],v));
}
}
}
}
void solve(int x1,int y1,int x2,int y2,vector<Que> que)
{
if(que.empty())
return ;
if(x1==x2&&y1==y2)
{
for(auto i:que)
ans[i.id]=0;
return ;
}
if(x2-x1+1>y2-y1+1)//横着
{
int mid=(x2+x1)>>1;
for(int i=y1;i<=y2;i++)
{
dijkstra(id(mid,i),x1,y1,x2,y2);
for(auto j:que)
{
ans[j.id]=min(ans[j.id],dis[id(j.x1,j.y1)]+dis[id(j.x2,j.y2)]);
}
}
vector<Que> l,r;
for(auto i:que)
{
if(i.x1<=mid&&i.x2<=mid)
l.emplace_back(i);
if(i.x1>mid&&i.x2>mid)
r.emplace_back(i);
}
solve(x1,y1,mid,y2,l);
solve(mid+1,y1,x2,y2,r);
}
else
{
int mid=(y2+y1)>>1;
for(int i=x1;i<=x2;i++)
{
dijkstra(id(i,mid),x1,y1,x2,y2);
for(auto j:que)
ans[j.id]=min(ans[j.id],dis[id(j.x1,j.y1)]+dis[id(j.x2,j.y2)]);
}
vector<Que> l,r;
for(auto i:que)
{
if(i.y1<=mid&&i.y2<=mid)
l.emplace_back(i);
if(i.y1>mid&&i.y2>mid)
r.emplace_back(i);
}
solve(x1,y1,x2,mid,l);
solve(x1,mid+1,x2,y2,r);
}
}
signed main()
{
cin>>n>>m;
int x;
for(int i=1;i<=n;i++)
{
for(int j=1;j<=(m-1);j++)
{
cin>>x;
add(id(i,j),id(i,j+1),x);
add(id(i,j+1),id(i,j),x);
}
}
for(int i=1;i<=(n-1);i++)
{
for(int j=1;j<=m;j++)
{
cin>>x;
add(id(i,j),id(i+1,j),x);
add(id(i+1,j),id(i,j),x);
}
}
cin>>q;
int x1,x2,y1,y2;
for(int i=1;i<=q;i++)
{
cin>>x1>>y1>>x2>>y2;
Q.push_back((Que){x1,y1,x2,y2,i});
ans[i]=1e9;
}
for(int i=1;i<=n;i++)
{
for(int j=1;j<=m;j++)
{
pos[id(i,j)]=make_pair(i,j);
}
}
solve(1,1,n,m,Q);
for(int i=1;i<=q;i++)
cout<<ans[i]<<endl;
return 0;
}这道题官方题解是 的,但连续段dp可以做到
连续段dp不考虑连续区间的具体位置关系,而是默认所有情况都是存在的,这样中间就有很多不确定的状态,而只有一首一尾是确定的状态(开头是一个连续段都没有,结尾是只有一个连续段),只有这时答案时有意义的,其余时候只有计算意义,没有实际意义,也不能用来算答案
设 表示已经开了i台电脑,有j个连续段的方案数,转移有:
- 在离已有的连续段很远的地方开一台电脑,每一个空前后都可以,共有 个空,所以乘上 ,这样就是新开了一台电脑
- 在已有的连续段前后紧挨着开一个电脑,这样连续段不会变,且每一个连续段前后都可以,所以乘上
- 在已有的连续段隔一个开一个电脑,且不会合并已有连续段,这样开的电脑会多自动开的那一个和手动开的哪一个,在每一个连续段前后都可以,乘上
- 在已有的两个只隔了两个的连续段之间开一个,这样另一个就会自动开,这样就合并了一个连续段,每两个连续段都可以发生,且可以选择是开前面的那一个还是开后面的那一个,乘上
- 在已有的两个间隔为3的连续段的中间开一个,这样两边的两个都会自动开,连续段会减少一个,每两个连续段都可以发生,乘上
#include<bits/stdc++.h>
namespace fast_IO {
#define IOSIZE 5000000
char ibuf[IOSIZE], obuf[IOSIZE], *p1 = ibuf, *p2 = ibuf, *p3 = obuf;
#define getchar() ((p1==p2)and(p2=(p1=ibuf)+fread(ibuf,1,IOSIZE,stdin),p1==p2)?(EOF):(*p1++))
#define putchar(x) ((p3==obuf+IOSIZE)&&(fwrite(obuf,p3-obuf,1,stdout),p3=obuf),*p3++=x)
#define isdigit(ch) (ch>47&&ch<58)
#define isspace(ch) (ch<33)
template<typename T> inline T read() { T s = 0; int w = 1; char ch; while (ch = getchar(), !isdigit(ch) and (ch != EOF)) if (ch == '-') w = -1; if (ch == EOF) return false; while (isdigit(ch)) s = s * 10 + ch - 48, ch = getchar(); return s * w; }
template<typename T> inline bool read(T &s) { s = 0; int w = 1; char ch; while (ch = getchar(), !isdigit(ch) and (ch != EOF)) if (ch == '-') w = -1; if (ch == EOF) return false; while (isdigit(ch)) s = s * 10 + ch - 48, ch = getchar(); return s *= w, true; }
template<typename T> inline void print(T x) { if (x < 0) putchar('-'), x = -x; if (x > 9) print(x / 10); putchar(x % 10 + 48); }
inline bool read(char &s) { while (s = getchar(), isspace(s)); return true; }
inline bool read(char *s) { char ch; while (ch = getchar(), isspace(ch)); if (ch == EOF) return false; while (!isspace(ch)) *s++ = ch, ch = getchar(); *s = '\000'; return true; }
inline void print(char x) { putchar(x); }
inline void print(char *x) { while (*x) putchar(*x++); }
inline void print(const char *x) { for (int i = 0; x[i]; i++) putchar(x[i]); }
inline bool read(std::string& s) { s = ""; char ch; while (ch = getchar(), isspace(ch)); if (ch == EOF) return false; while (!isspace(ch)) s += ch, ch = getchar(); return true; }
inline void print(std::string x) { for (int i = 0, n = x.size(); i < n; i++) putchar(x[i]); }
inline bool read(bool &b) { char ch; while(ch=getchar(), isspace(ch)); b=ch^48; return true; }
inline void print(bool b) { putchar(b+48); }
template<typename T, typename... T1> inline int read(T& a, T1&... other) { return read(a) + read(other...); }
template<typename T, typename... T1> inline void print(T a, T1... other) { print(a), print(other...); }
struct Fast_IO { ~Fast_IO() { fwrite(obuf, p3 - obuf, 1, stdout); } } io;
template<typename T> Fast_IO& operator >> (Fast_IO &io, T &b) { return read(b), io; }
template<typename T> Fast_IO& operator << (Fast_IO &io, T b) { return print(b), io; }
#define cout io
#define cin io
#define endl '\n'
} using namespace fast_IO;
using namespace std;
#define int long long
const int N=400+10;
int n,p;
int dp[N][N];
signed main()
{
cin>>n>>p;
dp[0][0]=1;
for(int i=0;i<n;i++)
{
for(int j=0;j<=i;j++)
{
dp[i+1][j+1]=(dp[i+1][j+1]+dp[i][j]*(j+1))%p;
dp[i+1][j]=(dp[i+1][j]+dp[i][j]*(j*2))%p;
dp[i+2][j]=(dp[i+2][j]+dp[i][j]*(j*2))%p;
if(j>=2)
{
dp[i+3][j-1]=(dp[i+3][j-1]+dp[i][j]*(j-1))%p;
dp[i+2][j-1]=(dp[i+2][j-1]+dp[i][j]*(j-1)*2)%p;
}
}
}
cout<<dp[n][1]<<endl;
return 0;
}具体讲解见ppt,这里主要讲一下g数组的另一种定义方式
我们可以将g数组看作已经处理了[i,n]的范围,且前缀和的最大值的期望,这样就可以理解了
#include<bits/stdc++.h>
namespace fast_IO {
#define IOSIZE 5000000
char ibuf[IOSIZE], obuf[IOSIZE], *p1 = ibuf, *p2 = ibuf, *p3 = obuf;
#define getchar() ((p1==p2)and(p2=(p1=ibuf)+fread(ibuf,1,IOSIZE,stdin),p1==p2)?(EOF):(*p1++))
#define putchar(x) ((p3==obuf+IOSIZE)&&(fwrite(obuf,p3-obuf,1,stdout),p3=obuf),*p3++=x)
#define isdigit(ch) (ch>47&&ch<58)
#define isspace(ch) (ch<33)
template<typename T> inline T read() { T s = 0; int w = 1; char ch; while (ch = getchar(), !isdigit(ch) and (ch != EOF)) if (ch == '-') w = -1; if (ch == EOF) return false; while (isdigit(ch)) s = s * 10 + ch - 48, ch = getchar(); return s * w; }
template<typename T> inline bool read(T &s) { s = 0; int w = 1; char ch; while (ch = getchar(), !isdigit(ch) and (ch != EOF)) if (ch == '-') w = -1; if (ch == EOF) return false; while (isdigit(ch)) s = s * 10 + ch - 48, ch = getchar(); return s *= w, true; }
template<typename T> inline void print(T x) { if (x < 0) putchar('-'), x = -x; if (x > 9) print(x / 10); putchar(x % 10 + 48); }
inline bool read(char &s) { while (s = getchar(), isspace(s)); return true; }
inline bool read(char *s) { char ch; while (ch = getchar(), isspace(ch)); if (ch == EOF) return false; while (!isspace(ch)) *s++ = ch, ch = getchar(); *s = '\000'; return true; }
inline void print(char x) { putchar(x); }
inline void print(char *x) { while (*x) putchar(*x++); }
inline void print(const char *x) { for (int i = 0; x[i]; i++) putchar(x[i]); }
inline bool read(std::string& s) { s = ""; char ch; while (ch = getchar(), isspace(ch)); if (ch == EOF) return false; while (!isspace(ch)) s += ch, ch = getchar(); return true; }
inline void print(std::string x) { for (int i = 0, n = x.size(); i < n; i++) putchar(x[i]); }
inline bool read(bool &b) { char ch; while(ch=getchar(), isspace(ch)); b=ch^48; return true; }
inline void print(bool b) { putchar(b+48); }
template<typename T, typename... T1> inline int read(T& a, T1&... other) { return read(a) + read(other...); }
template<typename T, typename... T1> inline void print(T a, T1... other) { print(a), print(other...); }
struct Fast_IO { ~Fast_IO() { fwrite(obuf, p3 - obuf, 1, stdout); } } io;
template<typename T> Fast_IO& operator >> (Fast_IO &io, T &b) { return read(b), io; }
template<typename T> Fast_IO& operator << (Fast_IO &io, T b) { return print(b), io; }
#define cout io
#define cin io
#define endl '\n'
} using namespace fast_IO;
using namespace std;
#define int long long
const int N=5000+10,mod=1e9+7;
int n;
int p[N];
int g[N][N];
int qpow(int a,int b)
{
int res=1;
while(b)
{
if(b&1)
res=res*a%mod;
a=a*a%mod;
b>>=1;
}
return res%mod;
}
void init()
{
for(int i=0;i<=n+1;i++)
{
p[i]=0;
for(int j=0;j<=n+1;j++)
g[i][j]=0;
}
}
signed main()
{
int T;
cin>>T;
while(T--)
{
cin>>n;
init();
for(int i=1,x,y;i<=n;i++)
{
cin>>x>>y;
y=qpow(y,mod-2);
p[i]=x*y%mod;
}
for(int i=0;i<=n;i++)
cin>>g[1][i];
for(int i=1;i<=n+1;i++)
for(int j=0;j<=n;j++)
g[i+1][j]=(g[i+1][j]+g[i][max(0ll,j-1)]*(mod+1-p[i])%mod+((j+1<=n)?(g[i][j+1]):(0))*p[i]%mod)%mod;
for(int i=1;i<=n;i++)
cout<<g[i+1][0]<<" ";
cout<<endl;
}
return 0;
}思路很简单,先枚举子集更新,再向外扩展更新
但关于转移顺序,将dijkstra放在最外很显然是错的,放在最里面是对的但是会T,所以放再更新完子集时,这时感性理解一下,每更新完内部就更新外部,因为这样会加入新的点,所以需要再枚举子集,如此反复
#include<bits/stdc++.h>
namespace fast_IO {
#define IOSIZE 5000000
char ibuf[IOSIZE], obuf[IOSIZE], *p1 = ibuf, *p2 = ibuf, *p3 = obuf;
#define getchar() ((p1==p2)and(p2=(p1=ibuf)+fread(ibuf,1,IOSIZE,stdin),p1==p2)?(EOF):(*p1++))
#define putchar(x) ((p3==obuf+IOSIZE)&&(fwrite(obuf,p3-obuf,1,stdout),p3=obuf),*p3++=x)
#define isdigit(ch) (ch>47&&ch<58)
#define isspace(ch) (ch<33)
template<typename T> inline T read() { T s = 0; int w = 1; char ch; while (ch = getchar(), !isdigit(ch) and (ch != EOF)) if (ch == '-') w = -1; if (ch == EOF) return false; while (isdigit(ch)) s = s * 10 + ch - 48, ch = getchar(); return s * w; }
template<typename T> inline bool read(T &s) { s = 0; int w = 1; char ch; while (ch = getchar(), !isdigit(ch) and (ch != EOF)) if (ch == '-') w = -1; if (ch == EOF) return false; while (isdigit(ch)) s = s * 10 + ch - 48, ch = getchar(); return s *= w, true; }
template<typename T> inline void print(T x) { if (x < 0) putchar('-'), x = -x; if (x > 9) print(x / 10); putchar(x % 10 + 48); }
inline bool read(char &s) { while (s = getchar(), isspace(s)); return true; }
inline bool read(char *s) { char ch; while (ch = getchar(), isspace(ch)); if (ch == EOF) return false; while (!isspace(ch)) *s++ = ch, ch = getchar(); *s = '\000'; return true; }
inline void print(char x) { putchar(x); }
inline void print(char *x) { while (*x) putchar(*x++); }
inline void print(const char *x) { for (int i = 0; x[i]; i++) putchar(x[i]); }
inline bool read(std::string& s) { s = ""; char ch; while (ch = getchar(), isspace(ch)); if (ch == EOF) return false; while (!isspace(ch)) s += ch, ch = getchar(); return true; }
inline void print(std::string x) { for (int i = 0, n = x.size(); i < n; i++) putchar(x[i]); }
inline bool read(bool &b) { char ch; while(ch=getchar(), isspace(ch)); b=ch^48; return true; }
inline void print(bool b) { putchar(b+48); }
template<typename T, typename... T1> inline int read(T& a, T1&... other) { return read(a) + read(other...); }
template<typename T, typename... T1> inline void print(T a, T1... other) { print(a), print(other...); }
struct Fast_IO { ~Fast_IO() { fwrite(obuf, p3 - obuf, 1, stdout); } } io;
template<typename T> Fast_IO& operator >> (Fast_IO &io, T &b) { return read(b), io; }
template<typename T> Fast_IO& operator << (Fast_IO &io, T b) { return print(b), io; }
#define cout io
#define cin io
#define endl '\n'
} using namespace fast_IO;
using namespace std;
const int N=1000+10;
int n,m,k;
int h[N],to[N],ne[N],w[N],idx;
int dp[N][(1<<10)+10];
void add(int u,int v,int c)
{
to[++idx]=v;
w[idx]=c;
ne[idx]=h[u];
h[u]=idx;
}
int dis[N];
bool vst[N];
void dijkstra(int S)
{
priority_queue<pair<int,int> ,vector<pair<int,int> >,greater<pair<int,int> > >q;
for(int i=1;i<=n;i++)
{
dis[i]=dp[i][S];
vst[i]=0;
q.emplace(make_pair(dis[i],i));
}
while(!q.empty())
{
int u=q.top().second;
q.pop();
if(vst[u])
continue;
vst[u]=1;
for(int i=h[u];i;i=ne[i])
{
int v=to[i];
if(dis[v]>dis[u]+w[i])
{
dis[v]=dis[u]+w[i];
q.emplace(make_pair(dis[v],v));
}
}
}
for(int i=1;i<=n;i++)
dp[i][S]=dis[i];
}
signed main()
{
cin>>n>>m>>k;
for(int i=1,u,v,w;i<=m;i++)
{
cin>>u>>v>>w;
add(u,v,w);
add(v,u,w);
}
memset(dp,63,sizeof(dp));
for(int i=1,u;i<=k;i++)
{
cin>>u;
dp[u][1<<(i-1)]=0;
}
for(int S=1;S<(1<<k);S++)
{
for(int i=1;i<=n;i++)
{
for(int T=S;T;T=(T-1)&S)
{
dp[i][S]=min(dp[i][S],dp[i][T]+dp[i][S^T]);
}
dijkstra(S);
}
}
int ans=1e9;
for(int i=1;i<=n;i++)
ans=min(ans,dp[i][(1<<k)-1]);
cout<<ans;
return 0;
}基本就是一个树链剖分的动态维护矩阵,每次查询直接线段树乘起来
#include<bits/stdc++.h>
namespace fast_IO {
#define IOSIZE 5000000
char ibuf[IOSIZE], obuf[IOSIZE], *p1 = ibuf, *p2 = ibuf, *p3 = obuf;
#define getchar() ((p1==p2)and(p2=(p1=ibuf)+fread(ibuf,1,IOSIZE,stdin),p1==p2)?(EOF):(*p1++))
#define putchar(x) ((p3==obuf+IOSIZE)&&(fwrite(obuf,p3-obuf,1,stdout),p3=obuf),*p3++=x)
#define isdigit(ch) (ch>47&&ch<58)
#define isspace(ch) (ch<33)
template<typename T> inline T read() { T s = 0; int w = 1; char ch; while (ch = getchar(), !isdigit(ch) and (ch != EOF)) if (ch == '-') w = -1; if (ch == EOF) return false; while (isdigit(ch)) s = s * 10 + ch - 48, ch = getchar(); return s * w; }
template<typename T> inline bool read(T &s) { s = 0; int w = 1; char ch; while (ch = getchar(), !isdigit(ch) and (ch != EOF)) if (ch == '-') w = -1; if (ch == EOF) return false; while (isdigit(ch)) s = s * 10 + ch - 48, ch = getchar(); return s *= w, true; }
template<typename T> inline void print(T x) { if (x < 0) putchar('-'), x = -x; if (x > 9) print(x / 10); putchar(x % 10 + 48); }
inline bool read(char &s) { while (s = getchar(), isspace(s)); return true; }
inline bool read(char *s) { char ch; while (ch = getchar(), isspace(ch)); if (ch == EOF) return false; while (!isspace(ch)) *s++ = ch, ch = getchar(); *s = '\000'; return true; }
inline void print(char x) { putchar(x); }
inline void print(char *x) { while (*x) putchar(*x++); }
inline void print(const char *x) { for (int i = 0; x[i]; i++) putchar(x[i]); }
inline bool read(std::string& s) { s = ""; char ch; while (ch = getchar(), isspace(ch)); if (ch == EOF) return false; while (!isspace(ch)) s += ch, ch = getchar(); return true; }
inline void print(std::string x) { for (int i = 0, n = x.size(); i < n; i++) putchar(x[i]); }
inline bool read(bool &b) { char ch; while(ch=getchar(), isspace(ch)); b=ch^48; return true; }
inline void print(bool b) { putchar(b+48); }
template<typename T, typename... T1> inline int read(T& a, T1&... other) { return read(a) + read(other...); }
template<typename T, typename... T1> inline void print(T a, T1... other) { print(a), print(other...); }
struct Fast_IO { ~Fast_IO() { fwrite(obuf, p3 - obuf, 1, stdout); } } io;
template<typename T> Fast_IO& operator >> (Fast_IO &io, T &b) { return read(b), io; }
template<typename T> Fast_IO& operator << (Fast_IO &io, T b) { return print(b), io; }
#define cout io
#define cin io
#define endl '\n'
} using namespace fast_IO;
using namespace std;
#define int long long
const int N=1e5+10;
int h[N],to[N<<1],ne[N<<1],idx;
void add(int u,int v)
{
to[++idx]=v;
ne[idx]=h[u];
h[u]=idx;
}
struct Matrix
{
int a[2][2];
Matrix()
{
memset(a,0,sizeof(a));
}
friend Matrix operator *(Matrix a,Matrix b)
{
Matrix c;
for(int i=0;i<2;i++)
for(int j=0;j<2;j++)
c.a[i][j]=-2e9;
for(int i=0;i<2;i++)
for(int j=0;j<2;j++)
for(int k=0;k<2;k++)
c.a[i][j]=max(c.a[i][j],a.a[i][k]+b.a[k][j]);
return c;
}
}num[N];
int n,m;
int val[N];
struct segment_tree
{
Matrix val;
int l,r;
}t[N<<2];
void push_up(int u)
{
t[u].val=t[u<<1].val*t[u<<1|1].val;
}
void build(int u,int l,int r)
{
t[u].l=l;
t[u].r=r;
if(l==r)
{
t[u].val=num[l];
return ;
}
int mid=(t[u].l+t[u].r)>>1;
build(u<<1,l,mid);
build(u<<1|1,mid+1,r);
push_up(u);
}
void update(int u,int pos)
{
if(t[u].l==t[u].r&&t[u].l==pos)
{
t[u].val=num[pos];
return ;
}
int mid=(t[u].l+t[u].r)>>1;
if(pos<=mid)
update(u<<1,pos);
else
update(u<<1|1,pos);
push_up(u);
}
Matrix query(int u,int l,int r)
{
if(t[u].l==l&&t[u].r==r)
return t[u].val;
int mid=(t[u].l+t[u].r)>>1;
if(r<=mid)
return query(u<<1,l,r);
if(l>mid)
return query(u<<1|1,l,r);
return query(u<<1,l,mid)*query(u<<1|1,mid+1,r);
}
int siz[N],son[N],fa[N],dep[N];
int f[N][2];
void dfs1(int u,int F)
{
dep[u]=dep[F]+1;
fa[u]=F;
siz[u]=1;
f[u][0]=0;
f[u][1]=val[u];
for(int i=h[u];i;i=ne[i])
{
int v=to[i];
if(v==F)
continue;
dfs1(v,u);
siz[u]+=siz[v];
f[u][0]+=max(f[v][0],f[v][1]);
f[u][1]+=f[v][0];
if(siz[v]>siz[son[u]])
son[u]=v;
}
}
int top[N];
int dfn[N],en[N],time_stamp;
int g[N][2];
void dfs2(int u,int ttop)
{
top[u]=ttop;
dfn[u]=++time_stamp;
en[ttop]=max(en[ttop],time_stamp);
g[u][0]=0;
g[u][1]=val[u];
if(!son[u])
return ;
dfs2(son[u],ttop);
for(int i=h[u];i;i=ne[i])
{
int v=to[i];
if(v==son[u]||v==fa[u])
continue;
dfs2(v,v);
g[u][0]+=max(f[v][0],f[v][1]);
g[u][1]+=f[v][0];
}
}
void change(int u,int y)
{
num[dfn[u]].a[1][0]+=y-val[u];
val[u]=y;
while(u)
{
Matrix old=query(1,dfn[top[u]],en[top[u]]);
update(1,dfn[u]);
Matrix newone=query(1,dfn[top[u]],en[top[u]]);
u=fa[top[u]];
num[dfn[u]].a[0][0]+=max(newone.a[0][0],newone.a[1][0])-max(old.a[0][0],old.a[1][0]);
num[dfn[u]].a[0][1]=num[dfn[u]].a[0][0];
num[dfn[u]].a[1][0]+=newone.a[0][0]-old.a[0][0];
}
}
int get_ans()
{
Matrix res=query(1,1,en[1]);
return max(res.a[0][0],res.a[1][0]);
}
signed main()
{
cin>>n>>m;
for(int i=1;i<=n;i++)
cin>>val[i];
for(int i=1,u,v;i<n;i++)
{
cin>>u>>v;
add(u,v);
add(v,u);
}
dfs1(1,0);
dfs2(1,1);
for(int i=1;i<=n;i++)
{
num[dfn[i]].a[0][0]=g[i][0];
num[dfn[i]].a[0][1]=g[i][0];
num[dfn[i]].a[1][0]=g[i][1];
num[dfn[i]].a[1][1]=-1e9;
}
build(1,1,n);
for(int i=1,x,y;i<=m;i++)
{
cin>>x>>y;
change(x,y);
cout<<get_ans()<<endl;
}
return 0;
}详细的说明见ppt
简而言之,就是先dp,然后有一个式子,满足这式子的方案数就可以累加到答案里,但想到这个式子很困难
同时代码里的 表示每一列/行的1的数量
#include<bits/stdc++.h>
namespace fast_IO {
#define IOSIZE 5000000
char ibuf[IOSIZE], obuf[IOSIZE], *p1 = ibuf, *p2 = ibuf, *p3 = obuf;
#define getchar() ((p1==p2)and(p2=(p1=ibuf)+fread(ibuf,1,IOSIZE,stdin),p1==p2)?(EOF):(*p1++))
#define putchar(x) ((p3==obuf+IOSIZE)&&(fwrite(obuf,p3-obuf,1,stdout),p3=obuf),*p3++=x)
#define isdigit(ch) (ch>47&&ch<58)
#define isspace(ch) (ch<33)
template<typename T> inline T read() { T s = 0; int w = 1; char ch; while (ch = getchar(), !isdigit(ch) and (ch != EOF)) if (ch == '-') w = -1; if (ch == EOF) return false; while (isdigit(ch)) s = s * 10 + ch - 48, ch = getchar(); return s * w; }
template<typename T> inline bool read(T &s) { s = 0; int w = 1; char ch; while (ch = getchar(), !isdigit(ch) and (ch != EOF)) if (ch == '-') w = -1; if (ch == EOF) return false; while (isdigit(ch)) s = s * 10 + ch - 48, ch = getchar(); return s *= w, true; }
template<typename T> inline void print(T x) { if (x < 0) putchar('-'), x = -x; if (x > 9) print(x / 10); putchar(x % 10 + 48); }
inline bool read(char &s) { while (s = getchar(), isspace(s)); return true; }
inline bool read(char *s) { char ch; while (ch = getchar(), isspace(ch)); if (ch == EOF) return false; while (!isspace(ch)) *s++ = ch, ch = getchar(); *s = '\000'; return true; }
inline void print(char x) { putchar(x); }
inline void print(char *x) { while (*x) putchar(*x++); }
inline void print(const char *x) { for (int i = 0; x[i]; i++) putchar(x[i]); }
inline bool read(std::string& s) { s = ""; char ch; while (ch = getchar(), isspace(ch)); if (ch == EOF) return false; while (!isspace(ch)) s += ch, ch = getchar(); return true; }
inline void print(std::string x) { for (int i = 0, n = x.size(); i < n; i++) putchar(x[i]); }
inline bool read(bool &b) { char ch; while(ch=getchar(), isspace(ch)); b=ch^48; return true; }
inline void print(bool b) { putchar(b+48); }
template<typename T, typename... T1> inline int read(T& a, T1&... other) { return read(a) + read(other...); }
template<typename T, typename... T1> inline void print(T a, T1... other) { print(a), print(other...); }
struct Fast_IO { ~Fast_IO() { fwrite(obuf, p3 - obuf, 1, stdout); } } io;
template<typename T> Fast_IO& operator >> (Fast_IO &io, T &b) { return read(b), io; }
template<typename T> Fast_IO& operator << (Fast_IO &io, T b) { return print(b), io; }
#define cout io
#define cin io
#define endl '\n'
} using namespace fast_IO;
using namespace std;
const int N=5000+10,mod=998244353;
int n;
pair<int,int> dph[2][N],dpl[2][N];
/*
dph表示考虑a数组
dpl表示考虑b数组
两个dp的意义是一样的
dp[i][j]表示处理了前i行/列,目前有的1的个数为j的最大匹配数的方案
*/
string s;
int h[N],l[N];
pair<int,int> add(pair<int,int> a,pair<int,int> b)
{
if(!b.second)
return a;
if(a.first<b.first)
return b;
else if(a.first==b.first)
return make_pair(a.first,(a.second+b.second)%mod);
else
return a;
}
signed main()
{
cin>>n;
for(int i=1;i<=n;i++)
{
cin>>s;
for(int j=0;j<(signed)s.size();j++)
{
h[i]+=(s[j]=='1');
l[j+1]+=(s[j]=='1');
}
}
dph[0][0].second=dpl[0][0].second=1;
for(int i=1,o=1;i<=n;i++,o^=1)
{
for(int j=0;j<=n;j++)
dpl[o][j]=dph[o][j]=make_pair(-1e9,0);
for(int j=0;j<=n;j++)
{
if(j)
{
dph[o][j]=add(dph[o][j],make_pair(dph[o^1][j-1].first+h[i],dph[o^1][j-1].second));
dpl[o][j]=add(dpl[o][j],make_pair(dpl[o^1][j-1].first+l[i],dpl[o^1][j-1].second));
}
dph[o][j]=add(dph[o][j],make_pair(dph[o^1][j].first+(n-h[i]),dph[o^1][j].second));
dpl[o][j]=add(dpl[o][j],make_pair(dpl[o^1][j].first+(n-l[i]),dpl[o^1][j].second));
}
}
int ans=0;
for(int i=0;i<=n;i++)
{
for(int j=0;j<=n;j++)
{
if(dph[n&1][i].first+dpl[n&1][j].first-i*j-(n-i)*(n-j)==n*n)
{
ans=(ans+dph[n&1][i].second*dpl[n&1][j].second%mod)%mod;
}
}
}
cout<<ans<<endl;
return 0;
}这个主要是对ppt的解释
-
为什么只选 的物品是,能获得的总价值是不超过 因为这时,当每一个物品价值最小为1时,所能获得的最小价值就是这个
-
为什么在前面预处理时我们循环的下限是,而后面加东西时下限就是 呢 因为我们在预处理时保证了的有序性,所以只用更新在它之前出现过的数,但在后面时,我们不能保证 值的有序性,所以需要更新整个范围
#include<bits/stdc++.h>
namespace fast_IO {
#define IOSIZE 5000000
char ibuf[IOSIZE], obuf[IOSIZE], *p1 = ibuf, *p2 = ibuf, *p3 = obuf;
#define getchar() ((p1==p2)and(p2=(p1=ibuf)+fread(ibuf,1,IOSIZE,stdin),p1==p2)?(EOF):(*p1++))
#define putchar(x) ((p3==obuf+IOSIZE)&&(fwrite(obuf,p3-obuf,1,stdout),p3=obuf),*p3++=x)
#define isdigit(ch) (ch>47&&ch<58)
#define isspace(ch) (ch<33)
template<typename T> inline T read() { T s = 0; int w = 1; char ch; while (ch = getchar(), !isdigit(ch) and (ch != EOF)) if (ch == '-') w = -1; if (ch == EOF) return false; while (isdigit(ch)) s = s * 10 + ch - 48, ch = getchar(); return s * w; }
template<typename T> inline bool read(T &s) { s = 0; int w = 1; char ch; while (ch = getchar(), !isdigit(ch) and (ch != EOF)) if (ch == '-') w = -1; if (ch == EOF) return false; while (isdigit(ch)) s = s * 10 + ch - 48, ch = getchar(); return s *= w, true; }
template<typename T> inline void print(T x) { if (x < 0) putchar('-'), x = -x; if (x > 9) print(x / 10); putchar(x % 10 + 48); }
inline bool read(char &s) { while (s = getchar(), isspace(s)); return true; }
inline bool read(char *s) { char ch; while (ch = getchar(), isspace(ch)); if (ch == EOF) return false; while (!isspace(ch)) *s++ = ch, ch = getchar(); *s = '\000'; return true; }
inline void print(char x) { putchar(x); }
inline void print(char *x) { while (*x) putchar(*x++); }
inline void print(const char *x) { for (int i = 0; x[i]; i++) putchar(x[i]); }
inline bool read(std::string& s) { s = ""; char ch; while (ch = getchar(), isspace(ch)); if (ch == EOF) return false; while (!isspace(ch)) s += ch, ch = getchar(); return true; }
inline void print(std::string x) { for (int i = 0, n = x.size(); i < n; i++) putchar(x[i]); }
inline bool read(bool &b) { char ch; while(ch=getchar(), isspace(ch)); b=ch^48; return true; }
inline void print(bool b) { putchar(b+48); }
template<typename T, typename... T1> inline int read(T& a, T1&... other) { return read(a) + read(other...); }
template<typename T, typename... T1> inline void print(T a, T1... other) { print(a), print(other...); }
struct Fast_IO { ~Fast_IO() { fwrite(obuf, p3 - obuf, 1, stdout); } } io;
template<typename T> Fast_IO& operator >> (Fast_IO &io, T &b) { return read(b), io; }
template<typename T> Fast_IO& operator << (Fast_IO &io, T b) { return print(b), io; }
#define cout io
#define cin io
#define endl '\n'
} using namespace fast_IO;
using namespace std;
const int N=2e6+10;
int n,m,k;
int w[N],v[N];
bool can_use[N];
int dp_val[N];
int dp_wei[N];
struct Que
{
int opt,num;
}que[N];
int ans[N];
signed main()
{
cin>>n>>m>>k;
for(int i=1;i<=n;i++)
cin>>w[i]>>v[i];
for(int i=1;i<=m;i++)
{
cin>>que[i].opt>>que[i].num;
if(que[i].opt==1)
can_use[que[i].num]=1;
}
int B=sqrt(m);
for(int i=1;i<=n;i++)
{
if(w[i]>B)
break;
if(can_use[i])
continue;
int W=w[i]*v[i],V=v[i];
for(int j=k;j>=W;j--)
dp_val[j]=max(dp_val[j],dp_val[j-W]+V);
}
for(int i=1;i<=k;i++)
dp_wei[i]=1e9;
for(int i=n;i>=1;i--)
{
if(w[i]<=B)
break;
if(can_use[i])
continue;
int W=w[i]*v[i],V=v[i];
for(int j=(int)(k/w[i]);j>=V;j--)
dp_wei[j]=min(dp_wei[j],dp_wei[j-V]+W);
}
for(int i=m;i>=1;i--)
{
if(que[i].opt==1)
{
ans[i]=-1;
int id=que[i].num;
int W=w[id]*v[id],V=v[id];
if(w[id]<=B)
for(int j=k;j>=W;j--)
dp_val[j]=max(dp_val[j],dp_val[j-W]+V);
else
for(int j=(int)(k/B);j>=V;j--)
dp_wei[j]=min(dp_wei[j],dp_wei[j-V]+W);
}
else
{
for(int j=0;j<=k/B;j++)
if(dp_wei[j]<=que[i].num)
ans[i]=max(ans[i],j+dp_val[que[i].num-dp_wei[j]]);
}
}
for(int i=1;i<=m;i++)
if(ans[i]!=-1)
cout<<ans[i]<<endl;
return 0;
}是序列的min/max的计数问题,考虑转化为一个序列上的配对问题,然后是dp部分,设 表示在c数组的前i个数有j个配对的最大价值,因为从大到小,这一个数向前匹配的一定是以ta为min的数,所以有: 其中 表示在c数组的前i-1个数都比它大,所以贡献固定
启示:
序列 min/max 计数尝试排序后解决。 序列匹配计数问题的 dp 套路。
#include<bits/stdc++.h>
namespace fast_IO {
#define IOSIZE 5000000
char ibuf[IOSIZE], obuf[IOSIZE], *p1 = ibuf, *p2 = ibuf, *p3 = obuf;
#define getchar() ((p1==p2)and(p2=(p1=ibuf)+fread(ibuf,1,IOSIZE,stdin),p1==p2)?(EOF):(*p1++))
#define putchar(x) ((p3==obuf+IOSIZE)&&(fwrite(obuf,p3-obuf,1,stdout),p3=obuf),*p3++=x)
#define isdigit(ch) (ch>47&&ch<58)
#define isspace(ch) (ch<33)
template<typename T> inline T read() { T s = 0; int w = 1; char ch; while (ch = getchar(), !isdigit(ch) and (ch != EOF)) if (ch == '-') w = -1; if (ch == EOF) return false; while (isdigit(ch)) s = s * 10 + ch - 48, ch = getchar(); return s * w; }
template<typename T> inline bool read(T &s) { s = 0; int w = 1; char ch; while (ch = getchar(), !isdigit(ch) and (ch != EOF)) if (ch == '-') w = -1; if (ch == EOF) return false; while (isdigit(ch)) s = s * 10 + ch - 48, ch = getchar(); return s *= w, true; }
template<typename T> inline void print(T x) { if (x < 0) putchar('-'), x = -x; if (x > 9) print(x / 10); putchar(x % 10 + 48); }
inline bool read(char &s) { while (s = getchar(), isspace(s)); return true; }
inline bool read(char *s) { char ch; while (ch = getchar(), isspace(ch)); if (ch == EOF) return false; while (!isspace(ch)) *s++ = ch, ch = getchar(); *s = '\000'; return true; }
inline void print(char x) { putchar(x); }
inline void print(char *x) { while (*x) putchar(*x++); }
inline void print(const char *x) { for (int i = 0; x[i]; i++) putchar(x[i]); }
inline bool read(std::string& s) { s = ""; char ch; while (ch = getchar(), isspace(ch)); if (ch == EOF) return false; while (!isspace(ch)) s += ch, ch = getchar(); return true; }
inline void print(std::string x) { for (int i = 0, n = x.size(); i < n; i++) putchar(x[i]); }
inline bool read(bool &b) { char ch; while(ch=getchar(), isspace(ch)); b=ch^48; return true; }
inline void print(bool b) { putchar(b+48); }
template<typename T, typename... T1> inline int read(T& a, T1&... other) { return read(a) + read(other...); }
template<typename T, typename... T1> inline void print(T a, T1... other) { print(a), print(other...); }
struct Fast_IO { ~Fast_IO() { fwrite(obuf, p3 - obuf, 1, stdout); } } io;
template<typename T> Fast_IO& operator >> (Fast_IO &io, T &b) { return read(b), io; }
template<typename T> Fast_IO& operator << (Fast_IO &io, T b) { return print(b), io; }
#define cout io
#define cin io
#define endl '\n'
} using namespace fast_IO;
using namespace std;
#define int long long
const int N=5e3+10,mod=998244353;
int n;
int a[N];
int b[N];
struct dataa
{
bool flg;
int num;
}c[N<<1];
int dp[2][N];//dp[i][j]表示c数组的前i个匹配了j个的总价值
//dp[i][j]=dp[i-1][j-1]*c[i]*(dif-(j-1))+dp[i-1][j]
int qpow(int a,int b)
{
int res=1;
while(b)
{
if(b&1)
res=res*a%mod;
a=a*a%mod;
b>>=1;
}
return res%mod;
}
signed main()
{
cin>>n;
for(int i=1;i<=n;i++)
cin>>a[i],c[i].num=a[i],c[i].flg=0;
for(int i=1;i<=n;i++)
cin>>b[i],c[i+n].num=b[i],c[i+n].flg=1;
sort(c+1,c+1+(n<<1),[](dataa t1,dataa t2){return t1.num>t2.num;});
dp[0][0]=1;
int cnt_0=0,cnt_1=0;
for(int i=1,o=1;i<=(n<<1);i++,o^=1)
{
for(int j=0;j<=(i/2);j++)
{
if(!j)
{
dp[o][j]=dp[o^1][j];
continue;
}
if(c[i].flg)
dp[o][j]=(dp[o^1][j-1]*c[i].num%mod*(cnt_0-(j-1))%mod+dp[o^1][j]%mod)%mod;
else
dp[o][j]=(dp[o^1][j-1]*c[i].num%mod*(cnt_1-(j-1))%mod+dp[o^1][j]%mod)%mod;
}
if(c[i].flg)
cnt_1++;
else
cnt_0++;
}
int res=1;
for(int i=2;i<=n;i++)
res=res*i%mod;
cout<<(dp[((n<<1)&1)][n]*qpow(res,mod-2))%mod<<endl;
return 0;
}AT_abc328_g [ABC328G] Cut and Reorder
范围很小,考虑状压dp
可以发现,最优操作是做一次改变顺序的操作,再一个一个地改值
则可以枚举怎样填数,数都是一段一段填的,分别枚举,注意位运算的小标对应关系以及区间的关系
#include<bits/stdc++.h>
#pragma GCC optimize(2)
namespace fast_IO {
#define IOSIZE 5000000
char ibuf[IOSIZE], obuf[IOSIZE], *p1 = ibuf, *p2 = ibuf, *p3 = obuf;
#define getchar() ((p1==p2)and(p2=(p1=ibuf)+fread(ibuf,1,IOSIZE,stdin),p1==p2)?(EOF):(*p1++))
#define putchar(x) ((p3==obuf+IOSIZE)&&(fwrite(obuf,p3-obuf,1,stdout),p3=obuf),*p3++=x)
#define isdigit(ch) (ch>47&&ch<58)
#define isspace(ch) (ch<33)
template<typename T> inline T read() { T s = 0; int w = 1; char ch; while (ch = getchar(), !isdigit(ch) and (ch != EOF)) if (ch == '-') w = -1; if (ch == EOF) return false; while (isdigit(ch)) s = s * 10 + ch - 48, ch = getchar(); return s * w; }
template<typename T> inline bool read(T &s) { s = 0; int w = 1; char ch; while (ch = getchar(), !isdigit(ch) and (ch != EOF)) if (ch == '-') w = -1; if (ch == EOF) return false; while (isdigit(ch)) s = s * 10 + ch - 48, ch = getchar(); return s *= w, true; }
template<typename T> inline void print(T x) { if (x < 0) putchar('-'), x = -x; if (x > 9) print(x / 10); putchar(x % 10 + 48); }
inline bool read(char &s) { while (s = getchar(), isspace(s)); return true; }
inline bool read(char *s) { char ch; while (ch = getchar(), isspace(ch)); if (ch == EOF) return false; while (!isspace(ch)) *s++ = ch, ch = getchar(); *s = '\000'; return true; }
inline void print(char x) { putchar(x); }
inline void print(char *x) { while (*x) putchar(*x++); }
inline void print(const char *x) { for (int i = 0; x[i]; i++) putchar(x[i]); }
inline bool read(std::string& s) { s = ""; char ch; while (ch = getchar(), isspace(ch)); if (ch == EOF) return false; while (!isspace(ch)) s += ch, ch = getchar(); return true; }
inline void print(std::string x) { for (int i = 0, n = x.size(); i < n; i++) putchar(x[i]); }
inline bool read(bool &b) { char ch; while(ch=getchar(), isspace(ch)); b=ch^48; return true; }
inline void print(bool b) { putchar(b+48); }
template<typename T, typename... T1> inline int read(T& a, T1&... other) { return read(a) + read(other...); }
template<typename T, typename... T1> inline void print(T a, T1... other) { print(a), print(other...); }
struct Fast_IO { ~Fast_IO() { fwrite(obuf, p3 - obuf, 1, stdout); } } io;
template<typename T> Fast_IO& operator >> (Fast_IO &io, T &b) { return read(b), io; }
template<typename T> Fast_IO& operator << (Fast_IO &io, T b) { return print(b), io; }
#define cout io
#define cin io
#define endl '\n'
} using namespace fast_IO;
using namespace std;
#define int long long
/*
所有区间左闭右开
*/
const int N=22+2;
int dp[(1<<N)];
int n,c;
int a[N],b[N];
int w[N][N][N];//w[i][j][k]表示a数组从i开始到j结束,b数组从k开始,有(i-j)这么长的区间的代价的代价
signed main()
{
cin>>n>>c;
for(int i=1;i<=n;i++)
cin>>a[i];
for(int i=1;i<=n;i++)
cin>>b[i];
for(int l1=1;l1<=n;l1++)
{
for(int r1=l1+1;r1<=(n+1);r1++)
{
int len=(r1-l1);
for(int l2=1;l2<=n;l2++)
{
if(l2+len>(n+1))
break;
for(int i=0;i<=len-1;i++)
w[l1][r1][l2]+=abs(a[i+l1]-b[i+l2]);
}
}
}
memset(dp,63,sizeof(dp));
dp[0]=0;
for(int S=0;S<=((1<<n)-1);S++)//将a数组的1~i填到b数组中的位置集
{
int i=__builtin_popcount(S)+1;
for(int j=i+1;j<=(n+1);j++)
{
int len=(j-i);
for(int k=1;k+len<=(n+1);k++)
{
if(((S>>(k-1))&((1<<len)-1))!=0)
continue;//两部分不能有交集
int T=S|(((1<<len)-1)<<(k-1));
dp[T]=min(dp[T],dp[S]+w[i][j][k]+c);
}
}
}
cout<<dp[(1<<n)-1]-c<<endl;
return 0;
}题目中要求的是最小权覆盖集
最小权覆盖集=全集-最大权独立集
所以就是P4719 【模板】动态 DP
注意代码略有不同,再修改时应该直接加,因为如按照原来的那种写法,会在减的时候减两个1e9,这是矩阵乘法中的-inf就不够大了,所以应该直接加,同时再强制不选时,我们将值赋为-inf,再算答案时因该加上,因为负数会使答案的其他部分变小
#include<bits/stdc++.h>
namespace fast_IO {
#define IOSIZE 5000000
char ibuf[IOSIZE], obuf[IOSIZE], *p1 = ibuf, *p2 = ibuf, *p3 = obuf;
#define getchar() ((p1==p2)and(p2=(p1=ibuf)+fread(ibuf,1,IOSIZE,stdin),p1==p2)?(EOF):(*p1++))
#define putchar(x) ((p3==obuf+IOSIZE)&&(fwrite(obuf,p3-obuf,1,stdout),p3=obuf),*p3++=x)
#define isdigit(ch) (ch>47&&ch<58)
#define isspace(ch) (ch<33)
template<typename T> inline T read() { T s = 0; int w = 1; char ch; while (ch = getchar(), !isdigit(ch) and (ch != EOF)) if (ch == '-') w = -1; if (ch == EOF) return false; while (isdigit(ch)) s = s * 10 + ch - 48, ch = getchar(); return s * w; }
template<typename T> inline bool read(T &s) { s = 0; int w = 1; char ch; while (ch = getchar(), !isdigit(ch) and (ch != EOF)) if (ch == '-') w = -1; if (ch == EOF) return false; while (isdigit(ch)) s = s * 10 + ch - 48, ch = getchar(); return s *= w, true; }
template<typename T> inline void print(T x) { if (x < 0) putchar('-'), x = -x; if (x > 9) print(x / 10); putchar(x % 10 + 48); }
inline bool read(char &s) { while (s = getchar(), isspace(s)); return true; }
inline bool read(char *s) { char ch; while (ch = getchar(), isspace(ch)); if (ch == EOF) return false; while (!isspace(ch)) *s++ = ch, ch = getchar(); *s = '\000'; return true; }
inline void print(char x) { putchar(x); }
inline void print(char *x) { while (*x) putchar(*x++); }
inline void print(const char *x) { for (int i = 0; x[i]; i++) putchar(x[i]); }
inline bool read(std::string& s) { s = ""; char ch; while (ch = getchar(), isspace(ch)); if (ch == EOF) return false; while (!isspace(ch)) s += ch, ch = getchar(); return true; }
inline void print(std::string x) { for (int i = 0, n = x.size(); i < n; i++) putchar(x[i]); }
inline bool read(bool &b) { char ch; while(ch=getchar(), isspace(ch)); b=ch^48; return true; }
inline void print(bool b) { putchar(b+48); }
template<typename T, typename... T1> inline int read(T& a, T1&... other) { return read(a) + read(other...); }
template<typename T, typename... T1> inline void print(T a, T1... other) { print(a), print(other...); }
struct Fast_IO { ~Fast_IO() { fwrite(obuf, p3 - obuf, 1, stdout); } } io;
template<typename T> Fast_IO& operator >> (Fast_IO &io, T &b) { return read(b), io; }
template<typename T> Fast_IO& operator << (Fast_IO &io, T b) { return print(b), io; }
#define cout io
#define cin io
#define endl '\n'
} using namespace fast_IO;
using namespace std;
#define int long long
const int N=1e5+10;
int h[N],to[N<<1],ne[N<<1],idx;
void add(int u,int v)
{
to[++idx]=v;
ne[idx]=h[u];
h[u]=idx;
}
struct Matrix
{
int a[2][2];
Matrix()
{
memset(a,0,sizeof(a));
}
friend Matrix operator *(Matrix a,Matrix b)
{
Matrix c;
for(int i=0;i<2;i++)
for(int j=0;j<2;j++)
c.a[i][j]=-2e9;
for(int i=0;i<2;i++)
for(int j=0;j<2;j++)
for(int k=0;k<2;k++)
c.a[i][j]=max(c.a[i][j],a.a[i][k]+b.a[k][j]);
return c;
}
}num[N];
int n,m;
int val[N];
struct segment_tree
{
Matrix val;
int l,r;
}t[N<<2];
void push_up(int u)
{
t[u].val=t[u<<1].val*t[u<<1|1].val;
}
void build(int u,int l,int r)
{
t[u].l=l;
t[u].r=r;
if(l==r)
{
t[u].val=num[l];
return ;
}
int mid=(t[u].l+t[u].r)>>1;
build(u<<1,l,mid);
build(u<<1|1,mid+1,r);
push_up(u);
}
void update(int u,int pos)
{
if(t[u].l==t[u].r&&t[u].l==pos)
{
t[u].val=num[pos];
return ;
}
int mid=(t[u].l+t[u].r)>>1;
if(pos<=mid)
update(u<<1,pos);
else
update(u<<1|1,pos);
push_up(u);
}
Matrix query(int u,int l,int r)
{
if(t[u].l==l&&t[u].r==r)
return t[u].val;
int mid=(t[u].l+t[u].r)>>1;
if(r<=mid)
return query(u<<1,l,r);
if(l>mid)
return query(u<<1|1,l,r);
return query(u<<1,l,mid)*query(u<<1|1,mid+1,r);
}
int siz[N],son[N],fa[N],dep[N];
int f[N][2];
void dfs1(int u,int F)
{
dep[u]=dep[F]+1;
fa[u]=F;
siz[u]=1;
f[u][0]=0;
f[u][1]=val[u];
for(int i=h[u];i;i=ne[i])
{
int v=to[i];
if(v==F)
continue;
dfs1(v,u);
siz[u]+=siz[v];
f[u][0]+=max(f[v][0],f[v][1]);
f[u][1]+=f[v][0];
if(siz[v]>siz[son[u]])
son[u]=v;
}
}
int top[N];
int dfn[N],en[N],time_stamp;
int g[N][2];
void dfs2(int u,int ttop)
{
top[u]=ttop;
dfn[u]=++time_stamp;
en[ttop]=max(en[ttop],time_stamp);
g[u][0]=0;
g[u][1]=val[u];
if(!son[u])
return ;
dfs2(son[u],ttop);
for(int i=h[u];i;i=ne[i])
{
int v=to[i];
if(v==son[u]||v==fa[u])
continue;
dfs2(v,v);
g[u][0]+=max(f[v][0],f[v][1]);
g[u][1]+=f[v][0];
}
}
void change(int u,int y)
{
num[dfn[u]].a[1][0]+=y;
val[u]+=y;
while(u)
{
Matrix old=query(1,dfn[top[u]],en[top[u]]);
update(1,dfn[u]);
Matrix newone=query(1,dfn[top[u]],en[top[u]]);
u=fa[top[u]];
num[dfn[u]].a[0][0]+=max(newone.a[0][0],newone.a[1][0])-max(old.a[0][0],old.a[1][0]);
num[dfn[u]].a[0][1]=num[dfn[u]].a[0][0];
num[dfn[u]].a[1][0]+=newone.a[0][0]-old.a[0][0];
}
}
int get_ans()
{
Matrix res=query(1,1,en[1]);
return max(res.a[0][0],res.a[1][0]);
}
string A;
signed main()
{
cin>>n>>m>>A;
int sum=0;
for(int i=1;i<=n;i++)
cin>>val[i],sum+=val[i];
for(int i=1,u,v;i<n;i++)
{
cin>>u>>v;
add(u,v);
add(v,u);
}
dfs1(1,0);
dfs2(1,1);
for(int i=1;i<=n;i++)
{
num[dfn[i]].a[0][0]=g[i][0];
num[dfn[i]].a[0][1]=g[i][0];
num[dfn[i]].a[1][0]=g[i][1];
num[dfn[i]].a[1][1]=-1e9;
}
build(1,1,n);
for(int i=1,a,x,b,y;i<=m;i++)
{
cin>>a>>x>>b>>y;
if((fa[a]==b||fa[b]==a)&&!x&&!y)
{
cout<<-1<<endl;
continue;
}
change(a,x?-1e9:1e9);
change(b,y?-1e9:1e9);
cout<<(int)(sum-get_ans()+(x?0:1e9)+(y?0:1e9))<<endl;
change(a,x?1e9:-1e9);
change(b,y?1e9:-1e9);
}
return 0;
}