牛客网暑期ACM多校训练营day2
A
#include<bits/stdc++.h> #define inc(i,l,r) for(int i=l;i<=r;i++) #define dec(i,l,r) for(int i=l;i>=r;i--) #define mem(a) memset(a,0,sizeof(a)) #define sqr(x) ((x)*(x)) #define ll long long #define lowbit(x) ((x)&(-x)) #define link(x) for(edge*j=h[x];j;j=j->next) #define NM 100005 ll read(){ ll x=0,f=1;char ch=getchar(); while(!isdigit(ch)){if(ch=='-')f=-1;ch=getchar();} while(isdigit(ch))x=x*10+ch-'0',ch=getchar(); return x*f; } const ll inf=1e9+7; int n,k,T,_x,_y; ll d[NM][2],c[NM]; int main(){ T=read();k=read();n=1e5; d[0][0]=1; inc(i,1,n){ d[i][0]+=d[i-1][0]+d[i-1][1]; if(i>=k)d[i][1]=d[i-k][0]; d[i][0]%=inf;d[i][1]%=inf; c[i]=c[i-1]+d[i][0]+d[i][1]; c[i]%=inf; } // inc(i,1,5)printf("%d ",d[i][0]+d[i][1]);putchar('\n'); while(T--){ _x=read();_y=read();printf("%lld\n",(c[_y]-c[_x-1]+inf)%inf); } return 0; }
B贴上队友博客
https://blog.csdn.net/qkoqhh/article/details/81193435
/** * ┏┓ ┏┓ * ┏┛┗━━━━━━━┛┗━━━┓ * ┃ ┃ * ┃ ━ ┃ * ┃ > < ┃ * ┃ ┃ * ┃... ⌒ ... ┃ * ┃ ┃ * ┗━┓ ┏━┛ * ┃ ┃ Code is far away from bug with the animal protecting * ┃ ┃ 神兽保佑,代码无bug * ┃ ┃ * ┃ ┃ * ┃ ┃ * ┃ ┃ * ┃ ┗━━━┓ * ┃ ┣┓ * ┃ ┏┛ * ┗┓┓┏━━━━━━━━┳┓┏┛ * ┃┫┫ ┃┫┫ * ┗┻┛ ┗┻┛ */ #include<cstdio> #include<cstring> #include<algorithm> #include<iostream> #include<queue> #include<cmath> #include<map> #include<stack> #include<set> #define inc(i,l,r) for(int i=l;i<=r;i++) #define dec(i,l,r) for(int i=l;i>=r;i--) #define link(x) for(edge *j=h[x];j;j=j->next) #define mem(a) memset(a,0,sizeof(a)) #define ll long long #define eps 1e-12 #define succ(x) (1LL<<x) #define lowbit(x) (x&(-x)) #define sqr(x) ((x)*(x)) #define mid (x+y>>1) #define NM 100005 #define nm 200010 #define N 1000005 #define M(x,y) x=max(x,y) const double pi=acos(-1); const ll inf=1e16+7; using namespace std; ll read(){ ll x=0,f=1;char ch=getchar(); while(!isdigit(ch)){if(ch=='-')f=-1;ch=getchar();} while(isdigit(ch))x=x*10+ch-'0',ch=getchar(); return f*x; } struct edge{int t;edge*next;}e[nm],*h[NM],*o=e; void add(int x,int y){o->t=y;o->next=h[x];h[x]=o++;} int n,a[NM],b[NM],_t,_x,_y,f[NM],ls[NM],rs[NM],tot; ll d[NM][2],ans,_ans; bool v[NM]; int find(int x){return f[x]==x?x:f[x]=find(f[x]);} void dfs(int x){ v[x]++;d[x][1]=a[x]; link(x)if(j->t!=_x&&f[x]!=j->t){ f[j->t]=x; dfs(j->t); d[x][1]+=min(d[j->t][0],d[j->t][1]); } ll t=inf; link(x)if(f[x]!=j->t&&j->t!=_x)t=min(t,d[j->t][1]-min(d[j->t][0],d[j->t][1])); d[x][0]=min(d[x][1]+t-a[x],d[x][1]-b[x]); } int main(){ n=read(); inc(i,1,n)a[i]=read();inc(i,1,n)b[i]=read(); inc(i,1,n)f[i]=i; inc(i,1,n){ _t=read(); int x=find(_t),y=find(i); if(x==y){rs[++tot]=_t;ls[tot]=i;continue;} add(_t,i);f[x]=y; } // inc(k,1,tot)printf("::%d %d\n",ls[k],rs[k]); inc(k,1,tot){ f[ls[k]]=0;_x=0; dfs(ls[k]); // inc(i,1,n)printf("%d %d\n",d[i][0],d[i][1]); ans=min(d[ls[k]][1],d[ls[k]][0]); add(rs[k],ls[k]);f[rs[k]]=0;_x=rs[k]; dfs(rs[k]); // inc(i,1,n)printf("%d %d\n",d[i][0],d[i][1]); ans=min(ans,d[rs[k]][1]); ans=min(ans,d[rs[k]][0]); _ans+=ans; } return 0*printf("%lld\n",_ans); }
C 待补
D签到
#include<bits/stdc++.h> #define inc(i,l,r) for(int i=l;i<=r;i++) #define dec(i,l,r) for(int i=l;i>=r;i--) #define mem(a) memset(a,0,sizeof(a)) #define sqr(x) ((x)*(x)) #define ll long long #define lowbit(x) ((x)&(-x)) #define link(x) for(edge*j=h[x];j;j=j->next) #define NM 100005 const ll inf=1e18; ll read(){ ll x=0,f=1;char ch=getchar(); while(!isdigit(ch)){if(ch=='-')f=-1;ch=getchar();} while(isdigit(ch))x=x*10+ch-'0',ch=getchar(); return x*f; } int n; ll s1,s2,a[NM],d[NM][2],c[NM][2]; int main(){ int _=read(); while(_--){ mem(a);mem(d);mem(c); n=read(); inc(i,1,n)a[i]=read(); c[0][1]=-inf; inc(i,1,n){ c[i][0]=c[i-1][0];d[i][0]=d[i-1][0]; if(c[i][0]<c[i-1][1]+a[i]||(c[i][0]==c[i-1][1]+a[i]&&d[i][0]>d[i-1][1])){ c[i][0]=c[i-1][1]+a[i];d[i][0]=d[i-1][1]; } c[i][1]=c[i-1][1];d[i][1]=d[i-1][1]; if(c[i][1]<c[i-1][0]-a[i]||(c[i][1]==c[i-1][0]-a[i]&&d[i][1]>d[i-1][0]+1)){ c[i][1]=c[i-1][0]-a[i];d[i][1]=d[i-1][0]+1; } } s1=-inf;s2=inf; inc(i,1,n)inc(j,0,1)if(c[i][j]>s1||(c[i][j]==s1&&d[i][j]<s2)){s1=c[i][j];s2=d[i][j];} printf("%lld %lld\n",s1,s2*2); } return 0; }
G 二分答案
#include<bits/stdc++.h> using namespace std; typedef long long ll; const ll N = 5e5+5; ll n,T; ll x[N],a[N],ps_a[N],ps_ax[N]; ll check(ll X) { ll now = lower_bound(ps_a+1,ps_a+n+1,X/2)-ps_a; ll lll = 1,rr = upper_bound(ps_a+1,ps_a+n+1,X)-ps_a,l,r; if(lll == rr) l = X,r = a[rr]-X; else l = a[lll],r = a[rr] - (X - ps_a[rr-1]); ll res = x[now]*ps_a[now-1] - ps_ax[now-1]; if(rr > now) { res+=(ps_ax[rr-1]-ps_ax[now])-x[now]*(ps_a[rr-1] - ps_a[now]); res+=(a[rr]-r)*(x[rr] - x[now]); } while(x[rr]-x[now]<=x[now]-x[lll] && lll<now && rr<=n ) { if(l>r){ res+=(x[rr] - x[now])*r; res-=(x[now] - x[lll])*r; l-=r; rr++;r = a[rr]; } else if(l<r){ res+=(x[rr]-x[now])*l; res-=(x[now] - x[lll])*l; r-=l; lll++;l = a[lll]; } else{ res+=(x[rr]-x[now])*l; res-=(x[now] - x[lll])*l; lll++;l = a[lll]; rr++;r = a[rr]; } } // cout<<X<<"::"<<endl; // cout<<lll<<"("<<l<<") "<<now<<" "<<rr<<"("<<r<<")"<<res<<endl; if(res <=T) return 1; while(now < n) { now++; res += (ps_a[now-1]-ps_a[lll]+l)*(x[now]-x[now-1]); if(now<=rr) res -= (ps_a[rr-1]-ps_a[now-1]+(a[rr]-r))*(x[now] - x[now-1]); while(x[rr]-x[now]<=x[now]-x[lll] && lll<now && rr<=n ) { if(l>r){ res+=(x[rr] - x[now])*r; res-=(x[now] - x[lll])*r; l-=r; rr++;r = a[rr]; } else if(l<r){ res+=(x[rr]-x[now])*l; res-=(x[now] - x[lll])*l; r-=l; lll++;l = a[lll]; } else{ res+=(x[rr]-x[now])*l; res-=(x[now] - x[lll])*l; lll++;l = a[lll]; rr++;r = a[rr]; } } // cout<<lll<<"("<<l<<") "<<now<<" "<<rr<<"("<<r<<")"<<res<<endl; if(res<=T) return 1; } return 0; } int main() { ios::sync_with_stdio(false); cin>>n>>T; T>>=1; for(ll i = 1;i<=n;i++) cin>>x[i]; for(ll i = 1;i<=n;i++) { cin>>a[i]; ps_a[i] = ps_a[i-1]+a[i]; ps_ax[i] = ps_ax[i-1]+a[i]*x[i]; } ll l = 0,r = ps_a[n],ans = 0; while(l<=r) { // cout<<l<<" "<<r<<endl; ll mid = (l+r)>>1; if(check(mid)) ans = mid,l = mid+1; else r = mid-1; } cout<<ans<<endl; }
H
#include<cstdio> #include<cstring> #include<algorithm> #include<iostream> #include<queue> #include<cmath> #include<map> #include<stack> #include<set> #define inc(i,l,r) for(int i=l;i<=r;i++) #define dec(i,l,r) for(int i=l;i>=r;i--) #define link(x) for(edge *j=h[x];j;j=j->next) #define mem(a) memset(a,0,sizeof(a)) #define ll long long #define eps 1e-12 #define succ(x) (1<<x) #define lowbit(x) (x&(-x)) #define sqr(x) ((x)*(x)) #define mid (x+y>>1) #define NM 400005 #define nm 800005 #define N 1000005 #define M(x,y) x=max(x,y) const double pi=acos(-1); const ll inf=998244353; using namespace std; ll read(){ ll x=0,f=1;char ch=getchar(); while(!isdigit(ch)){if(ch=='-')f=-1;ch=getchar();} while(isdigit(ch))x=x*10+ch-'0',ch=getchar(); return f*x; } struct edge{int t;edge*next;}e[nm],*h[NM],*o=e; void add(int x,int y){o->t=y;o->next=h[x];h[x]=o++;} int n,_x,_y,a[NM],f[NM]; ll _g[5],_d[5],_c[5],d[NM][5],c[NM][5],g[NM][5]; void dfs(int x){ d[x][0]=a[x]; c[x][1]=a[x]; link(x)if(j->t!=f[x]){ f[j->t]=x; dfs(j->t); mem(_c);mem(_d);mem(_g); inc(i,0,2)inc(k,1,i)_d[i]=max(_d[i],c[j->t][k]+d[x][i-k]); inc(i,0,2)inc(k,0,i-1)_d[i]=max(_d[i],d[j->t][k]+a[x]+g[x][i-k]); inc(i,0,2)_d[i]=max(_d[i],d[j->t][i]+a[x]); inc(i,1,3)_c[i]=max(_c[i],c[j->t][i]); inc(i,1,3)_g[i]=max(_g[i],c[j->t][i]); inc(i,1,3)inc(k,1,i-1)_c[i]=max(_c[i],c[j->t][k]+c[x][i-k]); inc(i,1,3)inc(k,1,i-1)_g[i]=max(_g[i],c[j->t][k]+g[x][i-k]); inc(i,1,3)inc(k,0,i-1)_c[i]=max(_c[i],d[j->t][k]+d[x][i-1-k]); inc(i,1,3)c[x][i]=max(c[x][i],_c[i]);inc(i,0,2)d[x][i]=max(d[x][i],_d[i]); inc(i,1,3)g[x][i]=max(g[x][i],_g[i]); } } int main(){ n=read(); inc(i,1,n)a[i]=read(); inc(i,1,n-1){_x=read();_y=read();add(_x,_y);add(_y,_x);} dfs(1); return 0*printf("%lld\n",c[1][3]); }
I 签到
#include<bits/stdc++.h> using namespace std; int n,m; int up[100005],down[100005],lleft[100005],ight[100005]; int main() { scanf("%d%d",&n,&m); int x,y; for(int i = 1;i<=n;i++) up[i] = down[i] = lleft[i] = ight[i] = 1; for(int i =1 ;i<=m;i++) { scanf("%d%d",&x,&y); up[y] = down[y] = lleft[x] = ight[x] = 0; } int ans = 0; for(int i = 1;i<=n/2;i++) { int c1 = (lleft[i]+ight[n-i+1]+up[n-i+1]+down[i]); int c2 = (up[i]+ight[i]+lleft[n-i+1]+down[n-i+1]); ans+=max(c1,c2); } if(n%2) ans+=(up[n/2+1]|down[n/2+1]|lleft[n/2+1]|ight[n/2+1]); printf("%d\n",ans); }
J
考虑分治贡献 对于每个位置除了被本身的值贡献以外 看是否还有其他的值作用于这个点 来判断这个点是否对答案产生贡献
#include <bits/stdc++.h> const int MAXN=2e6+10; #define ll long long using namespace std; typedef struct node{ int x,y,vul;int flag; friend bool operator<(node aa,node bb){ if(aa.x==bb.x&&aa.y==bb.y)return aa.flag>bb.flag; else if(aa.x==bb.x) return aa.y>bb.y; return aa.x>bb.x; } }node; node d[MAXN*5],d1[MAXN*5]; ll read(){ ll x=0,f=1;char ch=getchar(); while(!isdigit(ch)){if(ch=='-')f=-1;ch=getchar();} while(isdigit(ch))x=x*10+ch-'0',ch=getchar(); return x*f; } int n,m,q,sz; int ans[MAXN];int p,num[MAXN]; int cnt1=0; void cdq(int l,int mid,int r){ int i=l;int j=mid+1;cnt1=0;p=0; while(i<=mid&&j<=r){ while(i<=mid&&d[i].y>=d[j].y){ d1[++cnt1]=d[i]; if(d[i].flag==2){p--;num[d[i].vul]--;} if(d[i].flag==1){p++;num[d[i].vul]++;} i++; } if(!d[j].flag)ans[(d[j].x-1)*m+d[j].y]+=p-num[d[j].vul]; d1[++cnt1]=d[j];j++; } if(i<=mid){ for(;i<=mid;i++)d1[++cnt1]=d[i]; } if(j<=r){ for(;j<=r;j++){ if(!d[j].flag)ans[(d[j].x-1)*m+d[j].y]+=p-num[d[j].vul]; d1[++cnt1]=d[j]; } } for(int i=1;i<=cnt1;i++)d[l+i-1]=d1[i],num[d1[i].vul]=0; } void guibin(int l,int r){ if(l>=r)return ; int mid=(l+r)>>1; guibin(l,mid); guibin(mid+1,r); cdq(l,mid,r); } int main(){ n=read();m=read();q=read(); int cnt=0;sz=(n)*(m); for(int i=1;i<=n;i++){ for(int j=1;j<=m;j++){ d[++cnt].x=i;d[cnt].y=j;d[cnt].vul=read();d[cnt].flag=0; } } int x1,y1,x2,y2,vul; for(int i=1;i<=q;i++){ x1=read();y1=read();x2=read();y2=read();vul=read(); d[++cnt].x=x2;d[cnt].y=y2;d[cnt].vul=vul;d[cnt].flag=1; d[++cnt].x=x1-1;d[cnt].y=y2;d[cnt].vul=vul;d[cnt].flag=2; d[++cnt].x=x2;d[cnt].y=y1-1;d[cnt].vul=vul;d[cnt].flag=2; d[++cnt].x=x1-1;d[cnt].y=y1-1;d[cnt].vul=vul;d[cnt].flag=1; } sort(d+1,d+cnt+1); // cout<<"====="<<endl; // for(int i=1;i<=cnt;i++)cout<<d[i].x<<" "<<d[i].y<<" "<<d[i].flag<<endl; // cout<<"====="<<endl; guibin(1,cnt); // for(int i=1;i<=sz;i++)cout<<ans[i]<<" "; // cout<<endl; int ans1=0; for(int i=1;i<=n;i++){ for(int j=1;j<=m;j++){ if(ans[(i-1)*m+j]>=1)ans1++; } } printf("%d\n",ans1); return 0; }