zoukankan      html  css  js  c++  java
  • [SHOI2007]园丁的烦恼「树状数组处理偏序」

    题目描述

    这不是个链接

    思路分析

    • 二维偏序题,常用方法为树状数组(然而常数有亿点大需要吸口氧)
    • 为什么是二维偏序呢,因为显然对于一个点 ((x,y)),其它点 ((i,j)) ,只有同时满足 (i<x,j<y) 才能产生贡献。对于每个 ((x,y)) 我们只需查前面有多少个纵坐标小于 (y) 的了,然后再更新一下,都可以用树状数组实现
    • 但这题要求是一个矩阵范围内,可以类似于矩阵前缀和的处理,将矩阵拆成 (4) 个点:((c,d),(c,b-1),(a-1,d),(a-1,b-1))。考虑有重复加减,最后的答案就是

    [query(c,d)-query(c,b-1)-query(a-1,d)+query(a-1,b-1) ]

    • 还有一些细节就是,我们并不关心纵坐标的具体值,所以先将其离散化;查询拆成的坐标和树的坐标一起存储,一起排序,用一个 (flag) 区分开是否是查询就好;因为每次查询拆成了四个点,所以加上不是查询的一共要开五倍空间,我因为这里一直 (RE)

    (Code)

    #include<cstdio>
    #include<cstring>
    #include<cmath>
    #include<algorithm>
    #define N 2500005
    #define R register
    using namespace std;
    inline int read(){
    	int x = 0,f = 1;
    	char ch = getchar();
    	while(ch>'9'||ch<'0'){if(ch=='-')f=-1;ch=getchar();}
    	while(ch>='0'&&ch<='9'){x=(x<<1)+(x<<3)+(ch^48);ch=getchar();}
    	return x*f;
    }
    int n,m,ed,b[N],c[N];
    int ans[N][5],cnt[N];
    inline void update(int x,int val){
    	for(;x<=n;x+=x&(-x))c[x] += val;
    }
    inline int query(int x){
    	int res = 0;
    	for(;x;x-=x&(-x))res += c[x];
    	return res;
    }
    struct data{
    	int x,y,flag;
    	data(){}
    	data(int _x,int _y,int _flag){x = _x,y = _y,flag = _flag;}
    	inline bool operator <(const data &a)const{
    		if(x!=a.x)return x<a.x;
    	    return y==a.y ? flag < a.flag : y < a.y;
    	}
    }tr[N<<1];
    int main(){
    	n = read(),m = read();
    	for(R int i = 1;i <= n;i++){
    		tr[i].x = read(),tr[i].y = read(),tr[i].flag = 0;
    	}
    	for(R int i = 1;i <= m;i++){
    		int a = read(),b = read(),c = read(),d = read();
    		tr[++n] = data(a-1,b-1,i);
    		tr[++n] = data(c,d,i);
    		tr[++n] = data(c,b-1,i);
    		tr[++n] = data(a-1,d,i);
    	}
    	sort(tr+1,tr+1+n);
    	for(R int i = 1;i <= n;i++)b[i] = tr[i].y;
    	sort(b+1,b+1+n);
    	ed = unique(b+1,b+1+n)-(b+1);
    	for(R int i = 1;i <= n;i++){
    		int tmp = lower_bound(b+1,b+1+ed,tr[i].y)-b;
    		if(tr[i].flag)ans[tr[i].flag][++cnt[tr[i].flag]] = query(tmp);//用一个cnt方便记录是拆成的第几个点
    		else update(tmp,1);
    	}
    	for(R int i = 1;i <= m;i++)printf("%d
    ",ans[i][4]-ans[i][3]-ans[i][2]+ans[i][1]);
    	return 0;
    }
    
  • 相关阅读:
    二、静、动态代理(9~10)~~~~
    luogu P3572 [POI2014]PTA-Little Bird 单调队列优化dp
    luogu P6113 【模板】一般图最大匹配 带花树
    Codeforces Round #646 (Div. 2) C——Game On Leaves 思维
    Codeforces Round #646 (Div. 2) E——Tree Shuffling 思维
    luogu P2979 [USACO10JAN]Cheese Towers S 变形dp背包
    luogu P6577 【模板】二分图最大权完美匹配
    Educational Codeforces Round 88 (Rated for Div. 2) D
    Codeforces Round #645 (Div. 2) E
    Codeforces Round #645 (Div. 2) D
  • 原文地址:https://www.cnblogs.com/hhhhalo/p/13811571.html
Copyright © 2011-2022 走看看