二维前缀和,cdq分治,树状数组离线询问

 

2018 蓝桥杯省赛 A 组模拟赛(一)

解析:

询问矩形内点的值的和,二维前缀和,注意ll

ac:

#include
#define ll long long
using namespace std;
ll mp[3000][3000];
ll dp[3000][3000];
ll w;

int main()
{
    memset(dp,0,sizeof(dp));
    memset(mp,0,sizeof(mp));
    int n,m,a,b,c,d;
    int x,y;
    scanf("%d",&n);
    for(int i=0;i

Monitor

Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 163840/163840 K (Java/Others)
Total Submission(s): 914    Accepted Submission(s): 292


Problem Description

Xiaoteng has a large area of land for growing crops, and the land can be seen as a rectangle of n×m. 
But recently Xiaoteng found that his crops were often stolen by a group of people, so he decided to install some monitors to find all the people and then negotiate with them.
However, Xiao Teng bought bad monitors, each monitor can only monitor the crops inside a rectangle. There are p monitors installed by Xiaoteng, and the rectangle monitored by each monitor is known. 
Xiao Teng guess that the thieves would also steal q times of crops. he also guessed the range they were going to steal, which was also a rectangle. Xiao Teng wants to know if his monitors can see all the thieves at a time.

 Input

There are mutiple test cases.
Each case starts with a line containing two integers n,m(1≤n,1≤m,n×m≤107) which represent the area of the land.
And the secend line contain a integer p(1≤p≤106) which represent the number of the monitor Xiaoteng has installed. This is followed by p lines each describing a rectangle. Each of these lines contains four intergers x1,y1,x2 and y2(1≤x1≤x2≤n,1≤y1≤y2≤m) ,meaning the lower left corner and upper right corner of the rectangle.

Next line contain a integer q(1≤q≤106) which represent the number of times that thieves will steal the crops.This is followed by q lines each describing a rectangle. Each of these lines contains four intergers x1,y1,x2 and y2(1≤x1≤x2≤n,1≤y1≤y2≤m),meaning the lower left corner and upper right corner of the rectangle.

 Output

For each case you should print q lines.
Each line containing YES or NO mean the all thieves whether can be seen.

 Sample Input

6 6 3 2 2 4 4 3 3 5 6 5 1 6 2 2 3 2 5 4 1 5 6 5

 Sample Output

YES NO
Hint
In the picture,the red solid rectangles mean the monitor Xiaoteng installed, and the blue dotted rectangles mean the area will be stolen.

二维前缀和,cdq分治,树状数组离线询问_第1张图片

Source

题意:

给n个矩形,然后问q次,每次询问一个矩形是否包含在呢n个矩形的并中

解析:

用二维前缀和解决

ac:

#include
#define ll long long
#define inf 0x3f3f3f3f
using namespace std;
const int maxn=10000010;
int n,m,k,p,q;
int a[maxn];

int getid(int i,int j)
{
    if(i==0||j==0||i>n||j>m) return 0;
    return (i-1)*m+j;
}

void add(int i,int j,int v)
{
    if(getid(i,j)==0) return;
    a[getid(i,j)]+=v;
}

int query(int i,int j)
{
    return a[getid(i,j)];
}

int main()
{
    int T,cas=1;
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        for(int i=1;i<=n;i++)
            for(int j=1;i<=m;j++)
        a[getid(i,j)]=0;
        scanf("%d",&p);
        while(p--)
        {
            int x,y,x2,y2;
            scanf("%d%d%d%d",&x,&y,&x2,&y2);
            add(x,y,1);
            add(x2+1,y2+1,1);
            add(x2+1,y,-1);
            add(x,y2+1,-1);
        }
        for(int i=1;i<=n;i++)
            for(int j=1;i<=m;j++)
                a[getid(i,j)]+=query(i-1,j)+query(i,j-1)-query(i-1,j-1);
        for(int i=1;i<=n;i++)
            for(int j=1;i<=m;j++)
                a[getid(i,j)]=(a[getid(i,j)]>0);
        for(int i=1;i<=n;i++)
            for(int j=1;i<=m;j++)
                a[getid(i,j)]+=query(i-1,j)+query(i,j-1)-query(i-1,j-1);
        scanf("%d",&q);
        while(q--)
        {
            int x,y,x2,y2;
            scanf("%d%d%d%d",&x,&y,&x2,&y2);
            int ans=query(x2,y2)+query(x-1,y-1)-query(x-1,y2)-query(x2,y-1);
            if(ans==(x2-x+1)*(y2-y+1))
                puts("YES");
            else
                puts("NO");
        }
    }
    return 0;
}

https://nanti.jisuanke.com/t/41298

题意:

给你个边长为奇数的矩阵,矩阵中间会螺旋排列数字,给你一些存在的数字的坐标,若干次询问

每次询问你一个矩形范围内存在的数的数位和

解析:

这里是点少,但是范围大,不能开二维数组,首先通过坐标得螺旋点的值很容易

对于询问,可以用树状数组离线询问或者cdq分治来降维

这里我用树状数组,将询问和点按x从小到大,y从小到大,计算

对于矩形(x,y)->(xx,yy),我们求通过求4个左上点为(1,1)的来求

S=(xx,yy)-(x-1,yy)-(xx,y-1)+(x-1,y-1);

将实点和询问点一起排序,这些点有三种性质,用sign标记,sign=1,为询问中+上去的,-1位减掉的,2为实点

如果x,y相同,呢么按sign从大到小排序,先放入实点

每次查询(1,1)->(x,y)矩形内和的时候,矩形内的所以实点都已经被add

ac:

#include
#define lowbit(x) (x)&(-x)
#define MAXN 1000005
#define ll long long
using namespace std;
ll n;
ll sum[MAXN];
ll ans[MAXN];

void init()
{
    memset(sum,0,sizeof(sum));
    memset(ans,0,sizeof(ans));
}

void add(ll x,ll v)
{
    for(ll i=x;i<=n;i+=lowbit(i))
        sum[i]+=v;
}

ll query(ll x)
{
    ll ans=0;
    for(ll i=x;i>0;i-=lowbit(i))
        ans+=sum[i];
    return ans;
}

ll getsum(ll x,ll y)//计算蛇形序号,并计算数位
{
    ll sum=0;
    ll m=n/2+1;
    ll g=max(abs(x-m),abs(y-m));
    ll d=m,h=m,l=m,r=m,bc=g*2-1;
    if(x!=m||y!=m)
        d=m-g+1,h=m+g-1,l=m-g+1,r=m+g-1;
    sum=bc*bc;
    if(x>r)
        sum+=3*bc+2+(y-(d-1)+1);
    else if(yd)
        sum+=(r-x+1);
    sum=n*n-sum+1;
    ll k=0;
    while(sum){
        k+=sum%10;
        sum=sum/10;
    }
    return k;
}

struct node
{
    ll x,y;//坐标
    ll val;//数位值
    ll id,sign;//询问的标号,是否为加点,减去,增加
    friend bool operator <(node a,node b)
    {
        if(a.x==b.x&&a.y==b.y)
            return a.sign>b.sign;
        else if(a.x==b.x)
            return a.y

 

 

你可能感兴趣的:(模拟)