Solution to a problem P2163 [[SHOI2007] gardener troubles]

Topic Link

Solution [SHOI2007] gardener troubles

Title effect: given \ (n-\) tree coordinates, each query to \ ((a, b) \ ) left lower end point, \ ((C, D) \) is the lower right end of the rectangular tree quantity

Subject to the effect: normal operation and about the first two-dimensional prefix:

Note \ (S_ {i, j} \) is the lower left end point \ ((0,0) \) , the right upper end point () \ (i, j) \ number of trees in the

Then ask each group answer is

\ (Years = S_ {c, d} - S_ {a - 1, d} - S_ {c, b-1} + S_ {a-1, b-1} \)

Our point is divided into two categories: inquiry point and modify points

For each inquiry point, we ask that \ (x \ leq mx \; and \; y \ leq my \) number of points, which is a two-dimensional partial order problem, press directly \ (x \) sorting, then \ (cdq \) partition to

We should note that the sort order of two points \ (A \) , \ (b \)

If \ (AX = BX \; and \; by AY = \) , we modify the points in front
if \ (ax = bx \; and \; ay \ neq by \) we press \ (y \) sort
if \ (ax \ neq bx \) we press \ (x \) Sort

Code

#include <cstdio>
#include <cctype>
#include <algorithm>
using namespace std;
const int maxq = 3e6;
struct Ques{
    int x,y,opt,id;//opt表示点的类型,1为修改点,0为询问点
    bool operator < (const Ques &b)const{//排序
        return x == b.x ? (y == b.y ? opt : y < b.y) : x < b.x;
    }
}ques[maxq],tmp[maxq];
int ques_tot,ans_tot,ans[maxq];
void cdq(int a,int b){//二维偏序
    if(a == b)return;
    int mid = (a + b) >> 1;
    cdq(a,mid),cdq(mid + 1,b);
    int posa = a,posb = mid + 1,pos = a,tot = 0;
    while(posa <= mid && posb <= b){
        if(ques[posa].y <= ques[posb].y)tot += ques[posa].opt,tmp[pos++] = ques[posa++];
        else ans[ques[posb].id] += tot,tmp[pos++] = ques[posb++];
    }
    while(posa <= mid)tmp[pos++] = ques[posa++];
    while(posb <= b)ans[ques[posb].id] += tot,tmp[pos++] = ques[posb++];
    for(int i = a;i <= b;i++)ques[i] = tmp[i];
}
int n,m;
int main(){
    scanf("%d %d",&n,&m);
    for(int x,y,i = 1;i <= n;i++)
        scanf("%d %d",&x,&y),ques[++ques_tot] = Ques{x,y,1,0};
    for(int a,b,c,d,i = 1;i <= m;i++){//拆点
        scanf("%d %d %d %d",&a,&b,&c,&d);
        ques[++ques_tot] = Ques{c,d,0,++ans_tot};
        ques[++ques_tot] = Ques{c,b - 1,0,++ans_tot};
        ques[++ques_tot] = Ques{a - 1,d,0,++ans_tot};
        ques[++ques_tot] = Ques{a - 1,b - 1,0,++ans_tot};
    }
    sort(ques + 1,ques + 1 + ques_tot);
    cdq(1,ques_tot);
    for(int i = 1;i + 3 <= ans_tot;i += 4)
        printf("%d\n",ans[i] - ans[i + 1] - ans[i + 2] + ans[i + 3]);
    return 0;
}

Guess you like

Origin www.cnblogs.com/colazcy/p/11515107.html