D4 测试
只得了80分,我好菜啊 /ll /dk
T1 Input 1:
2 3
Output 1:
24
看到这题,我的内心os:哎这不是 P4067 储能表 吗?啊原来是或运算???那也一样吧,于是做做做,喜提30分暴力分。。错误原因:对数位DP状态存储不全,应该全存上,这样就不会超时了。
赛时写的唐氏代码
#include <bits/stdc++.h>
using namespace std;
const int P=1e9+7;
struct Data {
int w,cnt;
void operator+=(tuple<Data,bool,int> x){
auto[a,b,c]=x;
(w+=a.w)%=P,(cnt+=a.cnt)%=P;
if(b) (w+=(1<<c)*a.cnt)%=P;
}
} dp[100];
int a[100],b[100];
Data DFS(int p,bool fu1,bool fu2){
if(!p) return {0,1};
if(!fu1&&!fu2&&dp[p].cnt) return dp[p];
Data res={0,0};
int ed1=fu1?a[p]:1,ed2=fu2?b[p]:1;
for(int i=0;i<=ed1;++i)
for(int j=0;j<=ed2;++j)
res+={DFS(p-1,fu1&&i==ed1,fu2&&j==ed2),i|j,p-1};
if(!fu1&&!fu2) dp[p]=res;
return res;
}
main(){
long long c,d,l1=0,l2=0;
cin>>c>>d;
for(int x=c;x;x>>=1) a[++l1]=x&1;
for(int x=d;x;x>>=1) b[++l2]=x&1;
cout<<DFS(max(l1,l2),1,1).w;
}
赛后改的代码:
#include <bits/stdc++.h>
using namespace std;
#define int long long
const int P=1e9+7;
struct Data {
int w,cnt;
void operator+=(tuple<Data,bool,int> x){
auto[a,b,c]=x;
(w+=a.w)%=P,(cnt+=a.cnt)%=P;
if(b) (w+=(1ll<<c)%P*a.cnt%P)%=P;
}
} dp[100][2][2];
bool vis[100][2][2];
int a[100],b[100];
Data DFS(int p,bool fu1,bool fu2){
if(!p) return {0,1};
if(vis[p][fu1][fu2])
return dp[p][fu1][fu2];
vis[p][fu1][fu2]=1;
Data res={0,0};
int ed1=fu1?a[p]:1,ed2=fu2?b[p]:1;
for(int i=0;i<=ed1;++i)
for(int j=0;j<=ed2;++j)
res+={DFS(p-1,fu1&&i==ed1,fu2&&j==ed2),i|j,p-1};
return dp[p][fu1][fu2]=res;
}
main(){
long long c,d,l1=0,l2=0;
cin>>c>>d;
for(int x=c;x;x>>=1) a[++l1]=x&1;
for(int x=d;x;x>>=1) b[++l2]=x&1;
cout<<DFS(max(l1,l2),1,1).w;
}
Tips:特别注意取模要取全,可以用
老师讲的神奇做法:算出
贺一下同学的代码qwq(
T2 Input 1:
10 7
7 3 5 4 10 2 1 9 8 6
3 7
5 10
2 5
8 9
6 10
2 4
1 4
Output 1:
3 7
5 6
2 3
8 9
6 7
-1
1 2
就想到了一点:每个序列第一个不在对应位置的数,将其移到对应位置上。我就写了给后缀
正解(
#include<bits/stdc++.h>
using namespace std;
const int N=1e6+1;
int n,m,a[N];
struct node{
int l,r,len,Min,Lg,Mpos;
node operator+(node x){
x.len+=len;
if(x.Min>Min) x.Mpos=Mpos,x.Min=Min;
if(Lg==len&&a[x.l]>a[r]) x.Lg+=Lg;
else x.Lg=Lg;
x.l=l;
return x;
}
} t[N*4];
#define tree int h,int l,int r
#define ls h*2
#define rs ls|1
#define mid (l+r>>1)
#define lson ls,l,mid
#define rson rs,mid+1,r
#define pushup(h) t[h]=t[ls]+t[rs]
void build(tree){
if(l==r){
cin>>a[l];
t[h].Min=a[l];
t[h].Mpos=l;
t[h].Lg=1;
t[h].len=1;
t[h].l=t[h].r=l;
return;
}
build(lson);
build(rson);
pushup(h);
}
void modify(tree,int x,int y){
if(l==r) {
t[h].Min=y;
return;
}
if(x<=mid) modify(lson,x,y);
else modify(rson,x,y);
pushup(h);
}
node query(tree,int x,int y){
if(x<=l&&r<=y) return t[h];
if(y<=mid) return query(lson,x,y);
if(x>mid) return query(rson,x,y);
return query(lson,x,y)+query(rson,x,y);
}
main(){
freopen("swap.in","r",stdin);
freopen("swap.out","w",stdout);
cin.tie(0)->sync_with_stdio(0);
cin>>n>>m;
build(1,1,n);
for(int l,r;m--;){
cin>>l>>r;
auto[ll,rr,len,Min,lg,Mpos]=query(1,1,n,l,r);
if(lg==r-l+1) {cout<<"-1\n";continue;}
auto[ll1,rr1,len1,Min1,lg1,mpos1]=query(1,1,n,l+lg,r);
int p=lower_bound(a+l,a+l+lg+1,Min1)-a;
swap(a[p],a[mpos1]);
cout<<p<<' '<<mpos1<<'\n';
modify(1,1,n,p,a[p]);
modify(1,1,n,mpos1,a[mpos1]);
}
}
T3 步行(wtcl还不会) 题解: 标算:
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;
#define REP(i,x,y) for(int i=x;i<=y;i++)
#define rep(i,n) REP(i,1,n)
#define rep0(i,n) REP(i,0,n-1)
#define repG(i,x) for(int i=pos[x];~i;i=e[i].next)
#define ll long long
#define db double
const int N=3e5+7;
const int M=2e6+7;
const ll INF=9e18+7;
void rint(int &x){
char c=getchar(); x=0;
while(c<'0'||c>'9')c=getchar();
while(c<='9'&&c>='0')x=x*10+c-'0',c=getchar();
}
void rll(ll &x){
char c=getchar(); x=0;
while(c<'0'||c>'9')c=getchar();
while(c<='9'&&c>='0')x=x*10ll+c-'0',c=getchar();
}
int pos[N],fa[N],mn[N*4],dfn[N],ed[N],fs[N],f[N],fv[N],p[N];
ll a[N],sz[N],ds[N],dis[N],Ans[M],dep[N];
pairP[N];
struct edge{int v,next;}e[N*2];
int n,m,num=0,cnt=0;
void add(int x,int y){e[num]=(edge){y,pos[x]}; pos[x]=num++;}
pairh[22][N*2];
ll sum=0,tot=0;
void dfs1(int x,int f,int d){
fa[x]=f;
dfn[x]=ed[x]=++cnt;
dep[x]=d;
sz[x]=a[x];
h[0][cnt]=make_pair(d,x);
repG(i,x){
if(e[i].v==f)continue;
dfs1(e[i].v,x,d+1);
ed[x]=++cnt;
h[0][cnt]=h[0][dfn[x]];
sz[x]+=sz[e[i].v];
}
}
void dfs2(int x,int f){
ll w=(sz[x]+sz[x] tot+=w;
dis[x]=dis[f]+w;
ds[x]=ds[f]+sz[x];
repG(i,x)if(e[i].v!=f)dfs2(e[i].v,x);
}
bool isfa(int x,int y){return (dfn[x]<=dfn[y])&(ed[x]>=dfn[y]);}
int lca(int x,int y){
int l=dfn[x],r=dfn[y];
if(l>r)swap(l,r);
int k=mn[r-l+1];
return min(h[k][l],h[k][r-(1< }
struct qr{int x,y,id;};
vectorq1[N],q2[N],q3[N];
int ff(int x){return (f[x]==x)?x:f[x]=ff(f[x]);}
void merge(int x,int y){
x=ff(x);
y=ff(y);
if(fs[x]>=fs[y]){
fs[x]+=fs[y];
f[y]=x;
}
else{
fs[y]+=fs[x];
fv[y]=fv[x];
f[x]=y;
}
}
void work(int x,int y,int D,ll v){
if((sz[x]+v)*2ll>=sum)Ans[D]+=1ll*(dep[x]-dep[y])*(sum-v)-(ds[x]-ds[y]);
else{
int nw=fv[ff(x)];
if(dep[nw] Ans[D]+=1ll*(dep[nw]-dep[y])*(sum-v)+1ll*(dep[x]-dep[nw])*v+ds[x]+ds[y]-2ll*ds[nw];
}
}
int main(){
freopen("walk.in","r",stdin);
freopen("walk.out","w",stdout);
rint(n); rint(m);
rep(i,n){
rll(a[i]);
sum+=a[i];
}
memset(pos,-1,sizeof(pos));
rep(i,n-1){
int x,y; rint(x); rint(y);
add(x,y); add(y,x);
}
dfs1(1,0,1);
dfs2(1,0);
for(int i=1;(1< rep(j,cnt-(1< REP(j,(1< }
rep(i,n)P[i]=make_pair(sz[i],i);
sort(P+1,P+n+1);
rep(i,n)p[P[i].second]=i;
rep(i,m){
int x1,y1,x2,y2; rint(x1); rint(y1); rint(x2); rint(y2);
if(fa[y1]!=x1)swap(x1,y1);
if(fa[y1]!=x1){
Ans[i]=-1;
continue;
}
int lc=lca(x2,y2);
if(!isfa(lc,x1)){
Ans[i]=-1;
continue;
}
if(!isfa(y1,x2))swap(x2,y2);
if(!isfa(y1,x2)){
Ans[i]=-1;
continue;
}
Ans[i]=tot-dis[x2]-dis[y2]+dis[lc]+dis[lc];
if(y2!=lc)q1[y1].push_back((qr){y2,lc,i});
if(x1!=lc)q2[y1].push_back((qr){x1,lc,i});
if(x2!=y1)q3[y1].push_back((qr){x2,y1,i});
if(sz[y1]+sz[y1] else Ans[i]+=(sum-sz[y1]);
}
int nw1=n,nw2=1,nw3=1,nw4=1;
rep(i,n){
f[i]=fv[i]=i;
fs[i]=1;
}
rep(i,3*n){
ll v1=nw1?P[nw1].first:-INF,v2=(nw2<=n)?-P[nw2].first:-INF,v3=(nw3<=n)?sum-P[nw3].first:-INF;
ll mv=max(v1,max(v2,v3));
while(nw4<=n&&(P[nw4].first+mv)*2ll merge(fa[P[nw4].second],P[nw4].second);
nw4++;
}
if(v1==mv){
int id=P[nw1].second;
for(int j=0;j nw1--;
}
else if(v2==mv){
int id=P[nw2].second;
for(int j=0;j nw2++;
}
else{
int id=P[nw3].second;
for(int j=0;j nw3++;
}
}
ll as=0;
rep(i,m)if(Ans[i]>=0)Ans[i]+=Ans[i];
// rep(i,m)cerr< rep(i,m)if(Ans[i]>0)as=as^Ans[i];
printf("%lld\n",as);
return 0;
}
T4
Input 1:
5 4 4
1 2 1
3 4 2
4 5 5
5 3 8
3 1 5 2
Output 1:
8
Input 2:
6 6 4
1 2 5
2 4 7
4 6 50
6 5 3
1 5 15
3 5 6
1 5 4 6
Output 2:
15
sol:
这题还挺巧妙的。就首先找到一对最近的关键点,之后再找一对最近的,或者他们分别找距离近的点,之后答案必定在