NOIP 20 analog test "Zhou Ren-fly"

liu_runda out of the question once again $ \% \% \% \% \% \% \% \% \% \% \% \% \% \% \% \% \% \% $

week

answer

Do not ~

Any

answer

Why the title repeatedly emphasized simple path, no ring

FIG no ring units - = number of edges in the number of blocks Unicom

Prefix and prefix and prefix and maintenance side, and points,

Maintenance and poor maintenance of the prefix is ​​transformed side edge sideways and prefixes, prefixes and bristling edge

Note that side of the border issue

Look at how to maintain edge

Take my example

You and the current side is the left side is set to 1 when the current

So when you answer statistics

        BIA bianheng = LL [X2] [Y2] -bianheng [X1- . 1 ] [Y2] -bianheng [X2] [Y1] + bianheng [X1- . 1 ] [Y1];

Cutting edge thinking we should answer when statistics x2 connected

Similarly, when we have to maintain or vertical edges similar operations

Code

#include<bits/stdc++.h>
using namespace std;
#define ll int
#define A 2101
char s[A][A];
ll vis[A][A],stax[4200000],stay[4200000],dian[A][A],bianheng[A][A],bianshu[A][A];
ll cnt=0,n,m,q;
const ll nowx[5]={0,0,0,1,-1};
const ll nowy[5]={0,1,-1,0,0};
void dfs(ll x,ll y,ll x1,ll y1,ll x2,ll y2){
    vis[x][y]=cnt;
//    printf("x=%lld y=%lld vis=%lld\n",x,y,vis[x][y]);
    for(ll i=1;i<=4;i++){
        ll xnow=x+nowx[i],ynow=y+nowy[i];
        if(xnow>x2||xnow<x1) continue;
        if(ynow>y2||ynow<y1) continue;
        if(s[xnow][ynow]-'0'==0) continue;
        if(vis[xnow][ynow]) continue;
        dfs(xnow,ynow,x1,y1,x2,y2);
    }
}
void bfs(ll x1,ll y1,ll x2,ll y2){
    cnt=0;
    for(ll i=x1;i<=x2;i++)
        for(ll j=y1;j<=y2;j++){
            if(s[i][j]=='1'&&!vis[i][j]){
                ++cnt;
                dfs(i,j,x1,y1,x2,y2);
            }
        }
    for(ll i=x1;i<=x2;i++)
        for(ll j=y1;j<=y2;j++){
            vis[i][j]=0;
        }
    printf("%d\n",cnt);
}
int main(){
    scanf("%d%d%d",&n,&m,&q);
    for(ll i=1;i<=n;i++){
        scanf("%s",s[i]+1);
    }
//    memset()
    for(ll i=1;i<=n;i++)
        for(ll j=1;j<=m;j++){
            if(s[i][j]=='1'&&s[i][j-1]=='1'){
                bianheng[i][j]++;
            }
            if(s[i][j]=='1'&&s[i-1][j]=='1'){
                bianshu[i][j]++;
            }
            if(s[i][j]=='1'){
                dian[i][j]++;
            }
        }
    for(ll i=1;i<=n;i++)
        for(ll j=1;j<=m;j++){
        bianheng[i][j]=bianheng[i][j]+bianheng[i-1][j]+bianheng[i][j-1]-bianheng[i-1][j-1];
        bianshu[i][j]=bianshu[i][j]+bianshu[i-1][j]+bianshu[i][j-1]-bianshu[i-1][j-1];
        dian[i][j]=dian[i][j]+dian[i-1][j]+dian[i][j-1]-dian[i-1][j-1];
    }
//    for(ll i=1;i<=n;i++,puts(""))
//        for(ll j=1;j<=m;j++){
//            printf("bian=%d ",bianheng[i][j]);
//        }
    for (LL I = . 1 , X1, X2, Y1, Y2; I <= Q; I ++ ) { 
        Scanf ( " % D% D% D% D " , & X1, & Y1, & X2, & Y2); 
        LL BIA = bianheng [X2] [Y2] -bianheng [X1- . 1 ] [Y2] -bianheng [X2] [Y1] + bianheng [X1- . 1 ] [Y1];
 //         the printf ( "% D%% D D D% = BIA D% \ n-", bianheng [X2] [Y2], bianheng [X1-1] [Y2], bianheng [X2] [Y1], bianheng [X1-1] [y1 - 1], BIA); 
        BIA + = bianshu [X2] [Y2] -bianshu [X1] [Y2] -bianshu [X2] [Y1- . 1 ] + bianshu [X1] [Y1- . 1 ];
 //         the printf ( "% D%% D D D% = BIA % d \ n ", bianshu [ x2] [y2], bianshu [x1] [y2], bianshu [x2] [y1-1], bianshu [x1-1] [y1-1], bia);
        ll dia=dian[x2][y2]-dian[x1-1][y2]-dian[x2][y1-1]+dian[x1-1][y1-1];
        printf("%d\n",dia-bia);
    }
}

fly

answer

In fact seeking reverse order, but in reverse order pair is $ n * log $ is not over, thinking optimization

In fact, we found it to multi-segment arithmetic sequence,

Look at all the arithmetic sequence is complete situation

Us on the same segment may be formed from the arithmetic series $ x [i-1] $ push $ x [i] $ Consideration we are looking for is larger than the current number, $ x [i] = x [i-1] + before a $ contribution should all arithmetic sequence minus one

So you get the current contribution of $ tmp $ contribute $ tmp-cnt (the number of columns the number of arithmetic) before setting $

So we think $ <a $ (that is, the arithmetic sequence the first term) how maintenance, we take a tree-like array maintenance $ <a $ value of all

Suppose the current value of $ X $ is then reverse logarithmic $ i-sum (x) -1 $

Then we consider the first paragraph is not complete

Suppose the current $ x $ transferred to $ x2 $ in $ x, x2 $ smaller than $ begin $ arithmetic sequence so on all have to contribute less than $ begin $ minus one (not in the first period of $ x $ - $ x2 $ number can not be transferred)

If the current has been larger than the $ begin $ regard it as a normal arithmetic sequence

Code

#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define A 111111
ll c[A];
ll tus,n,a,mod,now,cnt=0,ans=0,last,tmp;
void add(ll x,ll u){
    for(ll i=x;i<=a;i+=i&-i)
        c[i]+=u;
}
ll sum(ll x){
    ll ans=0;
    for(ll i=x;i>=1;i-=i&-i)
        ans+=c[i];
    return ans;
}
int main(){
    scanf("%lld%lld%lld%lld",&n,&tus,&a,&mod);
    ll now=tus;
    if(tus<=a){
        add(tus+1,1);
    }
    for(ll i=2;i<=n;i++){
        now=(a+now)%mod;
        if(now<a){
            tmp=i-sum(now+1)-1;
            cnt++;
            add(now+1,1);
        }
        else{
            tmp-=cnt;
            if(now<tus) tmp++;
        }
        ans+=tmp;
    }
    printf("%lld\n",ans);
}

 

Guess you like

Origin www.cnblogs.com/znsbc-13/p/11366198.html