2025 牛客五一集训派对 day2

· · 个人记录

Problem A

#include<bits/stdc++.h>
using namespace std;
#define ll long long
const int N=1e6+5,mod=998244353;
ll n,ans,las;
struct line{
    ll l,r;
}b[N];
bool cmp(line x,line y){
    return x.l<y.l;
}
signed main(){
    scanf("%lld",&n);
    for(ll i=1,x,y;i<=n;i++){
        scanf("%lld%lld",&x,&y);
        b[i]=line{x-y,x+y};
    }
    sort(b+1,b+n+1,cmp);
    for(ll i=1,las=b[1].r;i<=n;i++){
        if(las<=b[i].l){
            ans+=b[i].l-las;
            las=b[i].r;
        }
        else
            las=max(las,b[i].r);
    }
    printf("%lld\n",ans);
    return 0;
}

Problem C

#include <bits/stdc++.h>
using namespace std;
#define int long long
typedef pair<int,int> pint;
const int N=2e5+114,I=1ll<<40;
int n,m,k,q;
struct Node{
    int l,r,lv,rv;
    Node(){}
    Node(int x,int y):l(I*y),r(I*y),lv((I*(m-y)-1)/x+1),rv((I*(y-1)-1)/x+1){}
    Node(int l_,int r_,int lv_,int rv_):l(max(I*1,l_)),r(min(I*m,r_)),lv(lv_),rv(rv_){}
    bool operator <(Node t) const{return l<t.l;}
};
vector<Node> vec[N];
int x[N],y[N];
void ins(vector<Node>& vec,Node t){
    vec.insert(lower_bound(vec.begin(),vec.end(),t),t);
}
void era(vector<Node>& vec,Node t){
    vec.erase(lower_bound(vec.begin(),vec.end(),t));
}
Node A[N*2],A_[N*2],A__[N*2];
int Asz,A_sz,A__sz;
signed main(){
//  freopen("awa.in","r",stdin);
//  freopen("awa.out","w",stdout);
    std::ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    cin >> n >> m >> k >> q;
    for(int i=1;i<=k;++i) cin >> x[i] >> y[i];
    for(int i=1;i<=k;++i) vec[x[i]].push_back(Node(x[i],y[i]));
    for(int i=1;i<=n;++i) sort(vec[i].begin(),vec[i].end());
    while(q--){
        int p_,x_,y_;
        cin >> p_ >> x_ >> y_;
        era(vec[x[p_]],Node(x[p_],y[p_]));
        x[p_]=x_,y[p_]=y_;
        ins(vec[x[p_]],Node(x[p_],y[p_]));
        Asz=A_sz=A__sz=0;
        int ans=0;
        for(int iwi=1;iwi<=n;++iwi){
            A_sz=0;
            for(int j=1;j<=Asz;++j) A_[++A_sz]=(Node(A[j].l-A[j].lv,A[j].r+A[j].rv,A[j].lv,A[j].rv));
            A__sz=0;
            {
                int i=1;
//              cout << vec[i].size() << " ";
                for(Node k:vec[iwi]){
//                  cout << "awa";
                    while(i<=A_sz && A_[i].l<=k.l){
                        A__[++A__sz]=A_[i];
                        ++i;
                    }
                    A__[++A__sz]=k;
                }
                while(i<=A_sz){
                    A__[++A__sz]=A_[i];
                    ++i;
                }
            }
            Asz=0;
            for(int j=1;j<=A__sz;++j){
                Node& k=A__[j];
                if(Asz==0 || k.l>A[Asz].r) A[++Asz]=k;
                else if(k.r>A[Asz].r) A[Asz].r=k.r,A[Asz].rv=k.rv; 
            }
            ans+=m;
            for(int j=1;j<=Asz;++j){
                Node& k=A[j];
                ans-=(k.r>>40)-((k.l-1)>>40);
            }
//          if(q==199) cout << Asz << "\n";
//          cout << "\n";
        }
        cout << ans << "\n";
    }
    return 0;
}

Problem D

#include<bits/stdc++.h>
#include<math.h>
using namespace std;
#define ll long long
const int N=1e6+5,mod=998244353;
int t;
double r,x,y,d,l1,l2;
double p,P=3.1415926535897932384626;
double v1,v2,v3,eps=1e-8;
inline int cmp(double x,double y){
    if(x-y>eps) return 1;
    if(y-x>eps) return -1;
    return 0;
}
signed main(){
    scanf("%d",&t);
    while(t--){
        scanf("%lf%lf%lf%lf",&r,&x,&y,&d);
        if(cmp(d,0)==0){
            puts("0");
            continue;
        }
        p=sqrt(x*x+y*y);
        if(cmp(p,d)==0) v1=P/2;
        else{
            l1=abs(p-d),l2=sqrt(r*r-l1*l1);
            v1=atan2(l2,l1);
            if(cmp(p,d)==1) v1=P-v1;
        }
        l1=abs(p+d),l2=sqrt(r*r-l1*l1);
        v2=atan2(l2,l1);
        v3=P-v1-v2;
        printf("%.12lf\n",v3*r);
    }
    return 0;
}

Problem E

#pragma target("avx512f,sse2,sse3,sse4,sse4.2")
#include<bits/stdc++.h>
using namespace std;
#define ll long long
const int N=505,M=700005,inf=1000000;
int n,m,a[N*2],b[N*2];
int f[N*2][N*2];

int s,t,tot1=1,tot2,head1[N*3],to[M],nxt[M];
int w1[M],d1[N*3],c1[M],cst;
bool vis[N*3];
void add1(int u,int v,int w,int cost){
    to[++tot1]=v;
    nxt[tot1]=head1[u],
    w1[tot1]=w,
    c1[tot1]=cost,
    head1[u]=tot1;
}
void link(int u,int v,int w,int cost){
    add1(u,v,w,cost),
    add1(v,u,0,-cost);
}
bool bfs(){
    queue<int>q;
    for(int i=1;i<=tot2;i++) d1[i]=-inf,vis[i]=0;
    q.push(s),d1[s]=0,vis[s]=1;
    while(!q.empty()){
        int u=q.front();
        q.pop(),vis[u]=0;
        for(int i=head1[u];i>1;i=nxt[i])
            if((d1[to[i]]<d1[u]+c1[i])&&w1[i]){
                d1[to[i]]=d1[u]+c1[i];
                if(!vis[to[i]]) vis[to[i]]=1,q.push(to[i]);
            }
    }
    return d1[t]!=-inf;
}
int dfs(int u,int p){
    if(u==t) return p;
    vis[u]=1;
    int pos=p,h;
    for(int i=head1[u];i>1&&pos;i=nxt[i])
        if(!vis[to[i]]&&(d1[to[i]]==d1[u]+c1[i])&&w1[i]){
            h=dfs(to[i],min(w1[i],pos));
            if(h){
                cst+=h*c1[i];
                w1[i]-=h;
                w1[i^1]+=h;
                pos-=h;
            }
            else d1[to[i]]=-inf;
        }
    vis[u]=0;
    return p-pos;
}
int Dinic(){
    int res=0;
    while(bfs()) res+=dfs(s,inf);
    return res;
}
void cl(){
    for(int i=1;i<=tot2;i++) head1[i]=0;
    tot1=1,tot2=0,cst=0;
}
int s1[N],s2[N],id1[N],id2[N],p1[N],p2[N];
vector<int>v1[N],v2[N];
void dfs1(int x,int fa){s1[x]=1,p1[x]=fa;for(int to:v1[x])if(to!=fa){dfs1(to,x);s1[x]+=s1[to];}}
void dfs2(int x,int fa){s2[x]=1,p2[x]=fa;for(int to:v2[x])if(to!=fa){dfs2(to,x);s2[x]+=s2[to];}}
inline bool cmp1(int x,int y){return s1[x]<s1[y];}
inline bool cmp2(int x,int y){return s2[x]<s2[y];}
signed main(){
    scanf("%d%d",&n,&m);
    for(int i=1;i<=n;i++) scanf("%d",&a[i]);
    for(int i=1;i<=m;i++) scanf("%d",&b[i]);
    for(int i=1,x,y;i<n;i++){
        scanf("%d%d",&x,&y);
        v1[x].push_back(y),v1[y].push_back(x);
    }
    for(int i=1,x,y;i<m;i++){
        scanf("%d%d",&x,&y);
        v2[x].push_back(y),v2[y].push_back(x);
    }
    dfs1(1,0);
    dfs2(1,0);
    for(int i=1;i<=n;i++) id1[i]=i;
    for(int i=1;i<=m;i++) id2[i]=i;
    sort(id1+1,id1+n+1,cmp1);
    sort(id2+1,id2+m+1,cmp2);
    for(int i=1,x,y,res;i<=n;i++)
        for(int j=1;j<=m;j++){
            x=id1[i],y=id2[j];
//          printf("%d %d %d %d\n",x,y,res,cst);

            if(a[x]==b[y]){
                s=1,t=2,tot2=2;
                for(int t1:v1[x])
                    if(t1!=p1[x]){
                        s1[t1]=++tot2;
                        link(s,tot2,1,0);
                    }
                for(int t2:v2[y])
                    if(t2!=p2[y]){
                        s2[t2]=++tot2;
                        link(tot2,t,1,0);
                    }
                for(int t1:v1[x])
                    if(t1!=p1[x])
                        for(int t2:v2[y])
                            if(t2!=p2[y])
                                link(s1[t1],s2[t2],1,f[t1][t2]);
                res=Dinic();
                f[x][y]=cst+1;
                cl();
            }
            for(int t1:v1[x]) if(t1!=p1[x]) f[x][y]=max(f[x][y],f[t1][y]);
            for(int t2:v2[y]) if(t2!=p2[y]) f[x][y]=max(f[x][y],f[x][t2]);
        }
    printf("%d\n",f[1][1]);
    return 0;
}

Problem G

#include<bits/stdc++.h>
using namespace std;
#define ll long long
const int N=1e6+5,mod=998244353;
int t,n,k,flag=1;
char s[N];
signed main(){
    scanf("%s",s+1);
    n=strlen(s+1);
    if(n==1) return printf("%c",s[1]),0;
    for(int i=1;i<=n-1;i++) printf("9");
    for(int i=1;i<=n-1;i++) flag&=(s[i]=='9');
    if(flag) printf("%c",s[n]);
    return 0;
}

Problem I

#include<bits/stdc++.h>
using namespace std;
#define ll long long
const ll N=1e5+5,mod=1e9+7;
int t;
int n,m,c[N];
char s[105];
//    n 表示双色个数,m 表示单色个数 
int get(int x,char c){
    if(c=='m') return x;
    if(c=='p') return x+9;
    if(c=='s') return x+18;
    if(c=='z') return x+27;
    return 0;
}
ll power(ll x,ll y){
    ll res=1;
    while(y){
        if(y&1) res=res*x%mod;
        x=x*x%mod;
        y>>=1;
    }
    return res;
}
ll inv(ll x){return power(x,mod-2);}
ll ans[8];
ll f[136+1][8];
void solve(int n){
    memset(f,0,sizeof(f));
    f[13][n]=1;
    for(int i=14;i<=136;++i){
        for(int j=6;j>=0 && 136-i+1>=(13-j*2)*3;--j){
            f[i][j+1]=(f[i][j+1]+f[i-1][j]*((13-j*2)*3)%mod*inv(136-i+1))%mod;
            f[i][j]=(f[i][j]+f[i-1][j]*((136-i+1)-(13-j*2)*3)%mod*inv(136-i+1))%mod;
        }
    }
    for(int i=14;i<=136;++i) ans[n]=(ans[n]+f[i][7]*(i-13))%mod;
}
signed main(){
    for(int n=0;n<=7;++n) solve(n);
    scanf("%d",&t);
    for(int twt=1;twt<=t;++twt){
        scanf("%s",s+1),n=0;
        for(int i=1;i<=26;i+=2) c[i/2+1]=get(s[i]-'0',s[i+1]);
        sort(c+1,c+13+1);
        for(int i=1;i<=12;i++) n+=(c[i]==c[i+1]);
//      cout << n << " ";
        printf("Case #%d: %lld\n",twt,ans[n]);
    }
    return 0;
}

Problem J

#include<bits/stdc++.h>
using namespace std;
#define ll long long
const int N=2e5+10;
int t,n,ans;
int B[N],siz[N];
set<int>v[N],g[N];
inline int find(int x){
    if(B[x]==x) return x;
    return B[x]=find(B[x]);
}
void merge(int x,int y){
    x=find(x),y=find(y);
    if(x==y) return ;
    if(v[x].size()<v[y].size()) swap(x,y);
    B[y]=x,siz[x]+=siz[y];
    vector<pair<int,int>> b;
    for(int to:v[y]){
        v[x].insert(to);
        g[to].erase(y);
        g[to].insert(x);
        if(g[to].size()==1) b.push_back({to,x});
    }
    for(auto e:b) merge(e.first,e.second);
}
int main(){
    scanf("%d",&t);
    for(int T=1;T<=t;T++){
        scanf("%d",&n);
        for(int i=1;i<=n;i++) B[i]=i,siz[i]=1;
        for(int i=1,x,y;i<=n;i++){
            scanf("%d",&x);
            for(int j=1;j<=x;j++){
                scanf("%d",&y);
                g[i].insert(y);
                v[y].insert(i);
            }
        }
        for(int i=1;i<=n;i++)
            if(g[i].size()==1)
                merge(i,*g[i].begin());
        ans=0;
        for(int i=1;i<=n;i++) ans=max(ans,siz[i]);
        for(int i=1;i<=n;i++) v[i].clear(),g[i].clear();
        cout<<"Case #"<<T<<": "<<ans<<endl;
    }
    return 0;
}