Codeforces 1197E Count The Rectangles (+ dendritic scan line array)

Meaning of the questions:

N you parallel to the axis line, ask how many can be composed of rectangles, the absolute value of the coordinates is less than 5000

Do not overlap or reduced between the guarantee line

Ideas:

Scan from bottom to top each y is the ordinate of the horizontal line and the vertical scanning lines and all the marking line intersects with it, to ensure that the upper end of the at least one extra

And maintain them with Fenwick tree

Then the number of scan line y + 1 of the ordinate of the horizontal line yy, yy queries simultaneously to the vertical y intersecting with their contribution to the line count

Remove each query a yy, you want to delete the endpoint vertical line yy in Fenwick tree, because after yy and it will not be intersected

Code:

Almost shining official solution to the problem. .

#include<iostream>
#include<cstdio>
#include<algorithm>
//#include<cmath>
#include<cstring>
#include<string>
#include<stack>
#include<queue>
#include<deque>
#include<set>
#include<vector>
#include<map>
    
#define fst first
#define sc second
#define pb push_back
#define mem(a,b) memset(a,b,sizeof(a))
#define lson l,mid,root<<1
#define rson mid+1,r,root<<1|1
#define lc root<<1
#define rc root<<1|1

using namespace std;

typedef double db;
typedef long double ldb;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> PI;
typedef pair<ll,ll> PLL;

const db eps = 1e-6;
const int mod = 1e9+7;
const int maxn = 1e4+100;
const int maxm = 2e6+100;
const int inf = 0x3f3f3f3f;
//const db pi = acos(-1.0);

int n;
struct node{
    int l,r;
    int x;
    node(){}
    node(int a,int b,int c):l(a),r(b),x(c){}
};
vector<node>vt[maxn],hr[maxn];
vector<int>tmp[maxn];
int tree[maxn*10];
int lowbit(int x){
    return x&-x;
}
void add(int x,int C){
    for(int i=x;i<maxn-20;i+=lowbit(i)){
        tree[i]+=C;
    }
}
int sum(int x){
    int ans=0;
    for(int i=x;i;i-=lowbit(i)){
        ans+=tree[i];
    }
    return ans;
}

int main(){
    scanf("%d", &n);
    for(int i = 1; i <= n; i++){
        int x1,y1,x2,y2;
        scanf("%d %d %d %d" ,&x1, &y1, &x2, &y2);
        x1+=5001;x2+=5001;
        y1+=5001;y2+=5001;
        if(x1==x2){
            vt[x1].pb(node(min(y1,y2),max(y1,y2),x1));
        }
        else{
            hr[y1].pb(node(min(x1,x2),max(x1,x2),y1));
            //printf("===%d\n",y1-5000);
        }
    }  
    ll ans = 0;
    for(int y = 0; y < maxn-10; y++){
        for(int i = 0; i < hr[y].size(); i++){
            //for(int j = 0; j < maxn-10; j++)tmp[j].clear();
            //mem(tree,0);
            node lne = hr[y][i];
            int l = lne.l, r = lne.r;
            for(int x = l; x <= r; x++){
                for(int j = 0; j < (int)vt[x].size(); j++){
                    if(vt[x][j].l<=y&&vt[x][j].r>=y+1){
                        add(x,1);
                        tmp[vt[x][j].r].pb(x);
                    }
                    
                }
            }
            for(int yy = y+1; yy < maxn-10; yy++){
                for(int j = 0; j < (int)hr[yy].size(); j++){
                    ll res = sum(hr[yy][j].r)-sum(hr[yy][j].l-1);
                    ans+=res*(res-1)/2;
                }
                for(int j = 0; j < (int)tmp[yy].size(); j++){
                    add(tmp[yy][j],-1);
                }
                tmp[yy].clear();
            }

        }
    }
    printf("%lld",ans);
    return 0;
}
/*

 */

 

Guess you like

Origin www.cnblogs.com/wrjlinkkkkkk/p/11209892.html