7.16 T3 surprise

Meaning of the questions: n * n of a map, each row of each column to ensure that there is one and only one army, if there is a k × k region inside of exactly k army, the degree of risk to +1, find this picture of danger degree. Data for 30%, N ≤ 100; data for 60%, N ≤ 5000; 100% of the data, N ≤ 50000

Solution: Because guarantee per row and only one army, the entire map can be "shot flat", a [x] [y] = 1 -> a [x] = y; n complexity side this question can not withstand, can be heard and reduction sticks to card 91, the card after it is no more. .

Correct answer is partition or tree line, I hit a partition.

For an interval, can contribute to the answer condition is that the sequence be continuous (but the order may be arbitrary), i.e. max-min = k-1. Contribution to answer this interval can be divided into three parts, mid left, right, mid, across the mid. About mid direct partition on the line, the key in that part of the mid across.

Mid span can be divided into the case where max. 4 min in the left mid, mid max min in the right, min mid max of the left and right mid in mid min left in the right mid, max. It found 1 2,3 4 is symmetrical, so only speak one.

max min in mid Left: lmax [l] -lmin [l] + 1 = r-l + 1 found enumeration left point may get the right end, the right end point is determined about the legality of it;

min mid max of the left and right mid: rmax [R] -lmin [L] = RL transposing obtained rmax [R] -R = lmin [L] -L-compliance with the bucket kept rmax [R] -R, with the left out on the line; somewhat similar to the determination whether the queue is thought monotonous compliance thoughts, rmax like array is stored prefix max or min, monotonic. Note that judgment R min card can only be used when, because if rmax [R]> lmax [L], then a [R] <lmax [l] in this case is not embodied in rmax, but he is not legal; using rmin [R]> lmin [l], then, once a [R] <lmin [l], rmin [R] are also embodied to be stuck. L empathy.

Enumerate the left point, the greater the more right away lmin, the smaller lmax, requires rmin R increases has led to continuing to meet lmin <rmin so let R left while excluding non - compliance, and restrictions on the rmax of L somewhat relaxed, can be slightly smaller, it also left L, while the matching bag. Finally ans + = t [lmin [L] -L]. It is worth noting that the number of the bucket may be negative, that is not consistent with, so when the need to add ans judgment about whether> 0. The negative has no effect on future updates, and leave him on the line.

Subscript barrel may be a negative number, unified + n can avoid this situation.

Empty buckets when not to use memset, because too slow, or hand it clear. .

I then place a card is the next time out to determine the legality of a barrel, so the range should be an open interval up to the penultimate, or a how penultimate sentence

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int n,a[50010],lmax[50010],lmin[50010],rmax[50010],rmin[50010],t[100010];
int search(int l,int r,int mid)
{
    int ans=0;
    lmax[mid]=lmin[mid]=a[mid];
    rmax[mid+1]=rmin[mid+1]=a[mid+1];
    for(int i=mid-1;i>=l;i--){
        lmax[i]=max(lmax[i+1],a[i]);
        lmin[i]=min(lmin[i+1],a[i]);
    }
    for(int i=mid+2;i<=r;i++){
        rmax[i]=max(rmax[i-1],a[i]);
        rmin[i]=min(rmin[i-1],a[i]);
    }
    for(int i=l;i<=mid;i++){
        int j=lmax[i]-lmin[i]+i;
        if(j<=r&&j>=mid+1&&lmin[i]<rmin[j]&&lmax[i]>rmax[j]) ans++;
    }
    for(int i=mid+1;i<=r;i++){
        int j=i-rmax[i]+rmin[i];
        if(j>=l&&j<=mid&&lmin[j]>rmin[i]&&lmax[j]<rmax[i]) ans++;
    }
    int L=mid+1,R=mid+1;
    while(R<=r&&rmin[R]>lmin[l]){
        t[rmax[R]-R+n]++;
        R++;
    }
    while(L<=r&&rmax[L]<lmax[l]){
        t[rmax[L]-L+n]--;
        L++;
    }
    for(int i=l;i<=mid;i++){
        while(R>mid+1&&rmin[R-1]<lmin[i]){
            R--;
            t[rmax[R]-R+n]--;
        }
        while(L>mid+1&&rmax[L-1]>lmax[i]){
            L--;
            t[rmax[L]-L+n]++;
        }
        if(t[lmin[i]-i+n]>0) ans+=t[lmin[i]-i+n];
    }
    for(int i=mid+1;i<=r;i++) t[rmax[i]-i+n]=0;
    L=mid,R=mid;
    while(L>=l&&lmin[L]>rmin[r]){
        t[lmax[L]+L]++;
        L--;
    }
    while(R>=l&&lmax[R]<rmax[r]){
        t[lmax[R]+R]--;
        R--;
    }
    for(int i=r;i>=mid+1;i--){
        while(L<mid&&lmin[L+1]<rmin[i]){
            L++;
            t[lmax[L]+L]--;
        }
        while(R<mid&&lmax[R+1]>rmax[i]){
            R++;
            t[lmax[R]+R]++;
        }
        if(t[rmin[i]+i]>0) ans+=t[rmin[i]+i];
    }
    for(int i=l;i<=mid;i++) t[i+lmax[i]]=0;
    return ans;
}
int work(int l,int r)
{
    if(l==r) return 1;
    int mid=(l+r)>>1;
    return work(l,mid)+work(mid+1,r)+search(l,r,mid);
}
int main()
{
    scanf("%d",&n);
    int u,v;
    for(int i=1;i<=n;i++){
        scanf("%d%d",&u,&v);
        a[u]=v;
    }
    printf("%d\n",work(1,n));
}
Do not decadent

 

Guess you like

Origin www.cnblogs.com/jrf123/p/11199197.html