@jason-fan
2021-05-01T03:14:19.000000Z
字数 9039
阅读 213
CODE
#include <bits/stdc++.h>
template<typename _Tp>
inline void read(_Tp &x) {
x=0;bool fg=0;char ch=getchar();
while(ch<'0'||ch>'9') {if(ch=='-') fg^=1;ch=getchar();}
while(ch>='0'&&ch<='9') x=(x<<1)+(x<<3)+(_Tp)(ch^48),ch=getchar();
if(fg) x=-x;
}
template<typename _Tp,typename... Args>void read(_Tp &t,Args &... args){read(t);read(args...);}
using namespace std;
typedef long long ll;
const int N = 1000010;
int n,m,a[N],b[N];
struct pp {
int ps,vl,fg;
bool operator<(const pp &a) const {
return vl<a.vl;
}
}p[N*2];
int sta[N*2];
bool check(int gap) {
for(int i=1;i<=n;++i) sta[i]=0;
int used=0,cnt=0;
int l,r=0;
for(l=1;l<=2*n;++l) {
// erase l-1
if(l>1) {
pp t=p[l-1];
sta[t.ps]-=t.fg;
if(sta[t.ps]==0) --cnt;
if(t.fg==1) {
if(sta[t.ps]) ++used;
}else {
if(sta[t.ps]==0) --used;
}
}
while(r<2*n&&p[r+1].vl-p[l].vl<=gap) {
++r;
// insert r
pp t=p[r];
if(sta[t.ps]==0) ++cnt;
if(t.fg==1) {
if(sta[t.ps]) --used;
}else {
if(sta[t.ps]==0) ++used;
}
sta[t.ps]+=t.fg;
}
if(cnt==n&&used<=m) return true;
}
return false;
}
int main() {
read(n);read(m);
for(int i=1;i<=n;++i) read(a[i]);
for(int i=1;i<=n;++i) read(b[i]);
for(int i=1;i<=n;++i) {
p[i].ps=i;p[i].vl=a[i];p[i].fg=1;
p[i+n].ps=i;p[i+n].vl=b[i];p[i+n].fg=2;
}
sort(p+1,p+2*n+1);
int L=1,R=1e9,M,B;
while(L<=R) {
M=(L+R)>>1;
if(check(M)) B=M,R=M-1;
else L=M+1;
}
cout<<B<<endl;
return 0;
}
#include <bits/stdc++.h>
#define re register
template<typename _Tp>
inline void read(_Tp &x) {
x=0;bool fg=0;char ch=getchar();
while(ch<'0'||ch>'9') {if(ch=='-') fg^=1;ch=getchar();}
while(ch>='0'&&ch<='9') x=(x<<1)+(x<<3)+(_Tp)(ch^48),ch=getchar();
if(fg) x=-x;
}
using namespace std;
typedef long long ll;
const int N = 305;
const int Max = 1000000;
ll a[N][N],b[N][N];
int n,m,T;
ll c[N][N];
namespace QAQ { // 差分约束
const int N = 610;
const int M = 370010;
ll w[N][N];
ll dis[N];
bool vis[N];
int ct[N];
// Xv-Xu<=w
void adde(int u,int v,int _w) {
w[u][v]=_w;
}
void init() { memset(w,0x3f,sizeof(w));}
bool BF() { // Bellman-Ford
memset(dis,0,sizeof(dis));
for(int ts=0;ts<=n+m;++ts) {
bool fg=0;
for(int u=1;u<=n+m;++u) {
for(int v=(u<=n)?n+1:1,ed=(u<=n)?n+m:n;v<=ed;++v) {
if(dis[v]>dis[u]+w[u][v]) {
dis[v]=dis[u]+w[u][v];
fg=1;
}
}
}
if(!fg) return true;
}
return false;
}
}
namespace checker {
void check() {
for(int i=1;i<=n;++i) {
for(int j=1;j<=n;++j) {
if(a[i][j]<0||a[i][j]>Max) {
cerr<<"out of bound"<<endl;
exit(-1);
}
}
}
for(int i=1;i<n;++i){
for(int j=1;j<m;++j) {
if(b[i][j]!=a[i][j]+a[i+1][j]+a[i][j+1]+a[i+1][j+1]) {
cerr<<"don't match"<<endl;
exit(-2);
}
}
}
}
}
void solve() {
for(int i=1;i<=n;++i) for(int j=1;j<=m;++j) a[i][j]=0;
#define idR(x) (x)
#define idC(x) (n+x)
for(re int i=n-1;i>=1;--i) {
for(re int j=m-1;j>=1;--j) {
a[i][j]=b[i][j]-a[i+1][j]-a[i][j+1]-a[i+1][j+1];
}
}
QAQ::init();
for(re int i=1;i<=n;++i) {
for(re int j=1;j<=m;++j) {
if((i+j)&1) {
// 行+ 列-
// 0 <= idR(i)-idC(j)+a[i][j] <= 1e6
QAQ::adde(idC(j),idR(i),Max-a[i][j]);
QAQ::adde(idR(i),idC(j),a[i][j]);
}else {
// 行- 列+
// 0 <= -idR(i)+idC(j)+a[i][j] <= 1e6
QAQ::adde(idR(i),idC(j),Max-a[i][j]);
QAQ::adde(idC(j),idR(i),a[i][j]);
}
}
}
bool fg=QAQ::BF();
if(!fg) {puts("NO");return ;}
for(re int i=1;i<=n;++i) for(re int j=1;j<=m;++j) {
if((i+j)&1) a[i][j]=a[i][j]-QAQ::dis[idC(j)]+QAQ::dis[idR(i)];
else a[i][j]=a[i][j]+QAQ::dis[idC(j)]-QAQ::dis[idR(i)];
}
puts("YES");
// checker::check();
for(int i=1;i<=n;++i) {
for(int j=1;j<=m;++j) printf("%lld ",a[i][j]);
printf("\n");
}
}
int rmain() {
read(n);read(m);
for(int i=1;i<n;++i) for(int j=1;j<m;++j) read(b[i][j]);
solve();
return 0;
}
int main() {
read(T);
while(T--) rmain();
return 0;
}
#include <bits/stdc++.h>
template<typename _Tp>
inline void read(_Tp &x) {
x=0;bool fg=0;char ch=getchar();
while(ch<'0'||ch>'9') {if(ch=='-') fg^=1;ch=getchar();}
while(ch>='0'&&ch<='9') x=(x<<1)+(x<<3)+(_Tp)(ch^48),ch=getchar();
if(fg) x=-x;
}
template<typename _Tp,typename... Args>void read(_Tp &t,Args &... args){read(t);read(args...);}
using namespace std;
typedef long long ll;
const int N = 1010;
const int M = 200010;
struct edge {int u,v;}e[M];
int n,m;
vector<int> g[N],rg[N];
int ans[M];
bool vis[N],rvis[N];
int cnt=0;
void bfs(int s) {
queue<int> q;
q.push(s);vis[s]=1; if(rvis[s]) ++cnt;
while(!q.empty()) {
int u=q.front();q.pop();
for(int i=0;i<g[u].size();++i) {
int v=g[u][i];
if(!vis[v]) {
vis[v]=1;
if(rvis[v]) ++cnt;
q.push(v);
}
}
}
}
void rbfs(int s) {
queue<int> q;
q.push(s);rvis[s]=1; if(vis[s]) ++cnt;
while(!q.empty()) {
int u=q.front();q.pop();
for(int i=0;i<rg[u].size();++i) {
int v=rg[u][i];
if(!rvis[v]) {
rvis[v]=1;
if(vis[v]) ++cnt;
q.push(v);
}
}
}
}
int main() {
read(n);read(m);
for(int i=1;i<=m;++i) {
read(e[i].u);read(e[i].v);
}
for(int u=1;u<=n;++u) {
memset(vis,0,sizeof(vis));
memset(rvis,0,sizeof(rvis));
for(int i=1;i<=n;++i) g[i].clear(),rg[i].clear();
vis[u]=1;rvis[u]=1; ans[m+1]++;
cnt=1;
for(int i=m;i>=1;--i) {
if(e[i].u>=u&&e[i].v>=u) {
// if() {
g[e[i].u].push_back(e[i].v);
rg[e[i].v].push_back(e[i].u);
// }
if(vis[e[i].u] && !vis[e[i].v]) bfs(e[i].v);
if(rvis[e[i].v] && !rvis[e[i].u]) rbfs(e[i].u);
}
ans[i]+=cnt;
}
}
for(int i=1;i<=m+1;++i) {
printf("%d ",ans[i]);
}
puts("");
return 0;
}
#include <bits/stdc++.h>
#define fi first
#define se second
template<typename _Tp>
inline void read(_Tp &x) {
x=0;bool fg=0;char ch=getchar();
while(ch<'0'||ch>'9') {if(ch=='-') fg^=1;ch=getchar();}
while(ch>='0'&&ch<='9') x=(x<<1)+(x<<3)+(_Tp)(ch^48),ch=getchar();
if(fg) x=-x;
}
using namespace std;
typedef long long ll;
typedef vector<int> vt;
const int N = 200010;
int head[N],pnt[N<<1],nxt[N<<1],E;
int n,m,q,c,P[N],op[N],w[N];
void adde(int u,int v) {
E++;pnt[E]=v;nxt[E]=head[u];head[u]=E;
}
namespace seg {
#define ls(p) t[p].l
#define rs(p) t[p].r
#define mid ((l+r)>>1)
struct node{
int l,r,x;
}t[N*23*2];
int root[2][N],tot;
int newnode() {
++tot;t[tot].l=t[tot].r=t[tot].x=0;return tot;
}
void modify(int p,int q,int ps,int vl,int l=1,int r=c) {
if(l==r) {
t[q].x=vl;
return;
}
if(ps<=mid) {
modify(ls(p),ls(q)=newnode(),ps,vl,l,mid);
rs(q)=rs(p);
}else {
modify(rs(p),rs(q)=newnode(),ps,vl,mid+1,r);
ls(q)=ls(p);
}
}
int ask(int p,int ps,int l=1,int r=c) {
if(ps<1||ps>c) return 0;
if(!p) return 0;
if(l==r) return t[p].x;
if(ps<=mid) return ask(ls(p),ps,l,mid);
return ask(rs(p),ps,mid+1,r);
}
#undef ls
#undef rs
#undef mid
}using seg::root;
namespace STD {
const int M = 21;
int fa[N],siz[N],son[N],dep[N],top[N];
int dnxtp[M][N],unxtp[M][N];
vt bots;
void dfs1(int u,int f,int d) {
siz[u]=1;fa[u]=f;dep[u]=d;
for(int i=head[u];i;i=nxt[i]) {
int v=pnt[i];
if(v==f) continue;
dfs1(v,u,d+1);
siz[u]+=siz[v];
if(siz[v]>siz[son[u]]) son[u]=v;
}
}
void dfs2(int u,int topf) {
top[u]=topf;
if(son[u]) {
dfs2(son[u],topf);
}else bots.push_back(u);
for(int i=head[u];i;i=nxt[i]) {
int v=pnt[i];
if(v==fa[u]||v==son[u]) continue;
dfs2(v,v);
}
}
void init() {
dfs1(1,0,0);dfs2(1,1);
for(int i=1;i<=n;++i) w[i]=op[w[i]];
root[0][0]=seg::newnode();
root[1][0]=seg::newnode();
static int t[N];
for(int _i=0;_i<(int)bots.size();++_i) {
int cnt=0,u=bots[_i];
while(true) {
t[++cnt]=u;
if(u==top[u]) break;
u=fa[u];
}
t[0]=t[cnt+1]=0;
map<int,int> ext;
for(int i=1;i<=cnt;++i) {
u=t[i];
if(ext.find(w[u]+1)!=ext.end()) {
dnxtp[0][u]=ext[w[u]+1];
}
seg::modify(root[0][t[i-1]],root[0][u]=seg::newnode(),w[u],u);
ext[w[u]]=u;
}
for(int i=1;i<M;++i) {
for(int j=1,u;j<=cnt;++j) {
u=t[j];
dnxtp[i][u]=dnxtp[i-1][dnxtp[i-1][u]];
}
}
ext.clear();
for(int i=cnt;i>=1;--i) {
u=t[i];
if(ext.find(w[u]+1)!=ext.end()) {
unxtp[0][u]=ext[w[u]+1];
}
seg::modify(root[1][t[i+1]],root[1][u]=seg::newnode(),w[u],u);
ext[w[u]]=u;
}
for(int i=1;i<M;++i) {
for(int j=1,u;j<=cnt;++j) {
u=t[j];
unxtp[i][u]=unxtp[i-1][unxtp[i-1][u]];
}
}
}
}
vector<pair<int,int> > up,dn;
void LCA(int s,int t) {
up.clear();dn.clear();
int fs=top[s],ft=top[t];
while(fs!=ft) {
if(dep[fs]>dep[ft]) {
up.push_back(make_pair(s,fs));
s=fa[fs];fs=top[s];
}else {
dn.push_back(make_pair(t,ft));
t=fa[ft];ft=top[t];
}
}
if(dep[s]>dep[t]) {
up.push_back(make_pair(s,t));
}else {
dn.push_back(make_pair(t,s));
}
}
void query(int s,int t) {
LCA(s,t);
int x=0;
for(int _i=0;_i<(int)up.size();++_i) {
pair<int,int> cha=up[_i];
int u=seg::ask(root[1][cha.fi],w[x]+1);
if(u==0||dep[u]<dep[cha.se]) continue;
x=u;
for(int i=M-1;i>=0;--i) if(unxtp[i][x]&&dep[unxtp[i][x]]>=dep[cha.se]) {
x=unxtp[i][x];
}
}
for(int _i=dn.size()-1;_i>=0;--_i) {
pair<int,int> cha=dn[_i];
int u=seg::ask(root[0][cha.se],w[x]+1);
if(u==0||dep[u]>dep[cha.fi]) continue;
x=u;
for(int i=M-1;i>=0;--i) if(dnxtp[i][x]&&dep[dnxtp[i][x]]<=dep[cha.fi]) {
x=dnxtp[i][x];
}
}
printf("%d\n",w[x]);
}
void solve() {
init();
for(int ts=1;ts<=q;++ts) {
int s,t;read(s);read(t);
query(s,t);
}
}
}
int main() {
read(n);read(m);read(c);
for(int i=1;i<=c;++i) read(P[i]),op[P[i]]=i;
for(int i=1;i<=n;++i) read(w[i]);
for(int i=1;i<n;++i) {
int u,v;read(u);read(v);
adde(u,v);adde(v,u);
}
read(q);
STD::solve();
return 0;
}
#include <bits/stdc++.h>
template<typename _Tp>
inline void read(_Tp &x) {
x=0;bool fg=0;char ch=getchar();
while(ch<'0'||ch>'9') {if(ch=='-') fg^=1;ch=getchar();}
while(ch>='0'&&ch<='9') x=(x<<1)+(x<<3)+(_Tp)(ch^48),ch=getchar();
if(fg) x=-x;
}
template<typename _Tp,typename... Args>void read(_Tp &t,Args &... args){read(t);read(args...);}
using namespace std;
typedef long long ll;
const int N = 13;
const int M = 505;
const int K = 1<<13;
int a[N],n,m,ful;
int ct[K];
ll dp[K][N][M];
int main() {
read(n);read(m);
int bst=0;
for(int i=0;i<n;++i) {
read(a[i]);
if(a[i]>a[bst]) bst=i;
}
ful=(1<<n)-1;
for(int mask=1;mask<=ful;++mask) ct[mask]=ct[mask-(mask&-mask)]+1;
for(int i=0;i<n;++i) {
int b=a[bst]-a[i];
if(i>bst) b++;
if(n*b<=m) dp[1<<i][i][n*b]=1;
}
for(int S=1;S<ful;++S) {
for(int _c=S;_c;_c-=_c&-_c) {
int c=log2(_c&-_c);
for(int r=0;r<=m;++r) if(dp[S][c][r]) {
for(int _x=(S^ful);_x;_x-=_x&-_x) {
int x=log2(_x&-_x);
int dif=max(0,a[c]-a[x]);
if(a[x]+dif==a[c]&&x>c) ++dif;
if(r+dif*(n-ct[S])<=m)
dp[S|(1<<x)][x][r+dif*(n-ct[S])]+=dp[S][c][r];
}
}
}
}
ll ans=0;
for(int c=0;c<n;++c) {
for(int r=0;r<=m;++r) {
ans+=dp[ful][c][r];
}
}
printf("%lld\n",ans);
return 0;
}