Codeforces Round #539 (Div. 1)

A - Sasha and a Bit of Relax

code:

#include <cstdio>
#include <map> 
#include <cstring>
#include <algorithm>
#define N 300006   
#define ll long long  
#define setIO(s) freopen(s".in","r",stdin)
using namespace std;
int a[N],n;      
map<int,int>mp[2];    
int main()
{ 
    // setIO("input");   
    int i,j;  
    scanf("%d",&n);  
    ll ans=0ll;  
    mp[0][0]++;   
    for(i=1;i<=n;++i) 
    {
        scanf("%d",&a[i]),a[i]^=a[i-1];    
        ans+=(ll)mp[i&1][a[i]];        
        mp[i&1][a[i]]++;   
    }
    printf("%lld\n",ans);   
    return 0;
}

  

B - Sasha and One More Name

code:

#include <cstdio>
#include <map> 
#include <cstring>
#include <algorithm>
#define N 300006   
#define ll long long  
#define setIO(s) freopen(s".in","r",stdin)
using namespace std;
char S[N],A[N];   
int n;     
int check() 
{
    int i,j,flag=0,mid=n/2;            
    for(i=2;i<=mid;++i) if(S[i]!=S[i-1]) { flag=1;  break; }    
    return flag;    
}
int check_1() 
{
    int i,j;       
    for(i=2;i<n;++i) 
    { 
        int tmp=0; 
        for(j=i;j<=n;++j) A[++tmp]=S[j];   
        for(j=1;j<i;++j)  A[++tmp]=S[j]; 
        int flag=0;    
        int mid=n/2;    
        for(j=1;j<=mid;++j) if(A[j]!=A[n-j+1]) flag=1;     
        if(!flag) 
        {            
            int flag2=0; 
            for(j=1;j<=n;++j) if(A[j]!=S[j]) flag2=1;          
            if(flag2) return 1;  
        }       
    }
    return 0;   
}
int main() 
{ 
    // setIO("input");  
    int i,j;     
    scanf("%s",S+1),n=strlen(S+1);      
    if(!check()) printf("Impossible\n");  
    else if(check_1()) printf("1\n");     
    else printf("2\n");   
    return 0;
}

  

D - Sasha and Interesting Fact from Graph Theory

code:

#include <cstdio> 
#include <algorithm> 
#include <cstring>     
#define N 2000001     
#define ll long long 
#define mod 1000000007  
#define setIO(s) freopen(s".in","r",stdin) 
using namespace std;    
int fac[N],inv[N];     
int qpow(int x,int y) 
{
    if(y<0) return qpow(x,y+mod-1);   
    int tmp=1;    
    for(;y>0;y>>=1,x=(ll)x*x%mod) 
        if(y&1) tmp=(ll)tmp*x%mod;  
    return tmp;   
}  
int C(int x,int y) 
{ 
    if(x<0||y<0||x<y) return 0;   
    return (ll)fac[x]*inv[y]%mod*inv[x-y]%mod; 
}
int main() 
{ 
    // setIO("input");   
    int i,j,n,m,a,b,ans=0;      
    fac[0]=inv[0]=1;  
    for(i=1;i<N;++i) fac[i]=(ll)fac[i-1]*i%mod,inv[i]=qpow(fac[i],mod-2);   
    scanf("%d%d%d%d",&n,&m,&a,&b);   
    for(i=0;i<=n-2;++i) 
    {                 
        (ans+=(ll)C(m-1,i)*C(n-2,i)%mod*(i+2)%mod*qpow(n,n-i-3)%mod*qpow(m,n-2-i)%mod*fac[i]%mod)%=mod;   
    }
    printf("%d\n",ans);   
    return 0;
}

  

CF1109F Sasha and Algorithm of Silence's Sounds

code:

#include <cstdio> 
#include <map> 
#include <vector>  
#include <cmath>    
#include <cstring>
#include <algorithm>    
#define M 2006  
#define N 200009   
#define inf 1000000  
#define ll long long   
#define setIO(s) freopen(s".in","r",stdin) 
using namespace std;
int n,m,total;    
namespace LCT 
{       
    #define lson s[x].ch[0] 
    #define rson s[x].ch[1]  
    int sta[N];   
    struct data 
    {   
        int ch[2],rev,f;               
    }s[N];          
    int get(int x)  { return s[s[x].f].ch[1]==x; }  
    int isrt(int x) { return !(s[s[x].f].ch[0]==x||s[s[x].f].ch[1]==x); }     
    void rotate(int x) 
    { 
        int old=s[x].f,fold=s[old].f,which=get(x);  
        if(!isrt(old)) s[fold].ch[s[fold].ch[1]==old]=x;   
        s[old].ch[which]=s[x].ch[which^1],s[s[old].ch[which]].f=old;  
        s[x].ch[which^1]=old,s[old].f=x,s[x].f=fold;    
    }    
    void mark(int x) 
    {
        swap(lson,rson),s[x].rev^=1;    
    } 
    void pushdown(int x) 
    { 
        if(s[x].rev) 
        {
            if(lson) mark(lson); 
            if(rson) mark(rson); 
            s[x].rev=0;   
        }
    }
    void splay(int x) 
    { 
        int u=x,v=0,fa;  
        for(sta[++v]=u;!isrt(u);u=s[u].f) sta[++v]=s[u].f;        
        for(;v;--v) pushdown(sta[v]);   
        for(u=s[u].f;(fa=s[x].f)!=u;rotate(x))  
            if(s[fa].f!=u) 
                rotate(get(fa)==get(x)?fa:x);   
    }
    void Access(int x) 
    {
        for(int y=0;x;y=x,x=s[x].f) 
        {
            splay(x); 
            rson=y;    
        }
    }  
    void makert(int x) 
    {
        Access(x),splay(x),mark(x);  
    }      
    int findrt(int x) 
    {       
        Access(x),splay(x);     
        while(s[x].ch[0]) pushdown(x),x=s[x].ch[0];               
        return x;      
    }
    void link(int x,int y) 
    {        
        makert(x),makert(y),s[y].f=x;    
    }       
    void cut(int x,int y) 
    {
        makert(x),Access(y),splay(y);   
        s[y].ch[0]=s[x].f=0;         
    }
    #undef lson 
    #undef rson 
};  
namespace seg
{   
    #define lson now<<1   
    #define rson now<<1|1   
    struct node 
    {    
        int mn,num_mn,sum;           
        node() { mn=num_mn=sum=0; }         
        node operator+(const node &b) const 
        {
            node c;     
            c.sum=sum+b.sum;     
            c.mn=min(b.sum+mn,b.mn);                 
            if(b.sum+mn==c.mn) c.num_mn+=num_mn;   
            if(b.mn==c.mn) c.num_mn+=b.num_mn;   
            return c;   
        }
    }s[N<<2];    
    void build(int l,int r,int now) 
    {
        s[now].mn=inf;   
        if(l==r)  
        {              
            s[now].mn=s[now].num_mn=s[now].sum=1;  
            return;  
        }   
        int mid=(l+r)>>1;                
        build(l,mid,lson),build(mid+1,r,rson);  
        s[now]=s[lson]+s[rson];   
    }        
    void update(int l,int r,int now,int p,int v) 
    {      
        if(l==r) 
        {
            s[now].sum+=v;  
            s[now].mn+=v;    
            s[now].num_mn=1;    
            return;  
        }
        int mid=(l+r)>>1;  
        if(p<=mid)  update(l,mid,lson,p,v);  
        else update(mid+1,r,rson,p,v);  
        s[now]=s[lson]+s[rson];  
    }
    node query(int l,int r,int now,int L,int R) 
    {
        if(l>=L&&r<=R) return s[now];     
        int mid=(l+r)>>1;     
        if(L<=mid&&R>mid)  return query(l,mid,lson,L,R)+query(mid+1,r,rson,L,R);  
        else if(L<=mid) return query(l,mid,lson,L,R);   
        else return query(mid+1,r,rson,L,R);   
    }   
    #undef lson 
    #undef rson   
};   
int l,r;   
struct Pos
{
    int x,y; 
    Pos(int x=0,int y=0):x(x),y(y){}  
}pos[M*M];      
int G[M][M];          
int go(int x,int y) 
{   
    if(y>=l&&y<=r)  
    {
        if(LCT::findrt(x)==LCT::findrt(y)) return 1;  
        else { LCT::link(x,y); return 0; } 
    }
    else return 0;  
}
void recover(int x,int y) 
{ 
    if(y>=l&&y<=r) 
    {
        if(LCT::findrt(x)==LCT::findrt(y)) LCT::cut(x,y);  
    }
}
int check(int x) 
{    
    int flag=1;  
    int xx=pos[x].x,yy=pos[x].y;       
    if(xx-1>=1&&go(x,G[xx-1][yy])) flag=0;    
    if(xx+1<=n&&go(x,G[xx+1][yy])) flag=0;   
    if(yy-1>=1&&go(x,G[xx][yy-1])) flag=0;   
    if(yy+1<=m&&go(x,G[xx][yy+1])) flag=0;         
    recover(x,G[xx-1][yy]); 
    recover(x,G[xx+1][yy]); 
    recover(x,G[xx][yy-1]); 
    recover(x,G[xx][yy+1]);   
    return flag;    
}          
void update(int x,int y) 
{    
    if(y>=l&&y<=r) 
    {         
        LCT::cut(x,y);     
        seg::update(1,total,1,x,1);    
    }
}
void Del(int x) 
{
    int xx=pos[x].x,yy=pos[x].y;                 
    update(x,G[xx-1][yy]);    
    update(x,G[xx+1][yy]);    
    update(x,G[xx][yy-1]);     
    update(x,G[xx][yy+1]);   
}
void rese(int x,int y) 
{    
    if(y>=l&&y<=r) 
    {         
        LCT::link(x,y);     
        seg::update(1,total,1,y,-1);       
    }
}
void Add(int x) 
{       
    int xx=pos[x].x,yy=pos[x].y;   
    rese(x,G[xx-1][yy]);  
    rese(x,G[xx+1][yy]);   
    rese(x,G[xx][yy-1]); 
    rese(x,G[xx][yy+1]);    
}
int main() 
{ 
    // setIO("input");  
    int i,j;   
    ll ans=0;   
    scanf("%d%d",&n,&m),total=n*m;    
    seg::build(1,total,1);   
    for(i=1;i<=n;++i) 
        for(j=1;j<=m;++j) scanf("%d",&G[i][j]),pos[G[i][j]]=Pos(i,j);              
    for(l=r=1;r<=total;++r) 
    {
        while(!check(r))   
        {    
            Del(l),++l;    
        }      
        Add(r);     
        seg::node p=seg::query(1,total,1,l,r);        
        if(p.mn==1) ans+=(ll)p.num_mn;    
    }
    printf("%lld\n",ans);   
    return 0;  
}

  

posted @ 2020-02-07 21:25  EM-LGH  阅读(151)  评论(0编辑  收藏  举报