Codeforces Round #433 (Div. 2) E. Boredom
来源:互联网 发布:网络运营公司简介 编辑:程序博客网 时间:2024/06/05 09:29
题目大意
问多少个两个点确定的矩形与给定区间有交。
题解
其实答案就是所有形成的矩形减没有交的矩形,那么由容斥原理,就是给定区间的4边的矩形减4个角,这个可以用主席树之类的维护。
#include<map>#include<cmath>#include<queue>#include<cstdio>#include<cstring>#include<iostream>#include<algorithm>using namespace std;inline int read(){ int x=0,f=1;char ch=getchar(); while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();} while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();} return x*f;}struct node{ int id; int x;}arr[200005];int root[2000005];int lson[4000005],rson[4000005],sum[4000005],tot=1;int n,q,x1,x2,yy1,y2;void ins(int rt,int &now,int l,int r,int pos,int num){ if(!now) { now=++tot; sum[now]=lson[now]=rson[now]=0; } if(l==r) { sum[now]+=num; return ; } int mid=l+r>>1; if(pos<=mid) { ins(lson[rt],lson[now],l,mid,pos,num); rson[now]=rson[rt]; } else { ins(rson[rt],rson[now],mid+1,r,pos,num); lson[now]=lson[rt]; } sum[now]=sum[lson[now]]+sum[rson[now]]; return;}int query(int rt,int l,int r,int L,int R){ if(rt==0) return 0; if(L>R) return 0; if(rt==0) return 0; if(L<=l&&r<=R) return sum[rt]; int mid=l+r>>1; int sum=0; if(L<=mid) sum+=query(lson[rt],l,mid,L,R); if(R>=mid+1) sum+=query(rson[rt],mid+1,r,L,R); return sum;}long long calc(long long t){ return (t-1)*t/2;}int main(){ n=read(),q=read(); root[0]=1; for(int i=1;i<=n;i++) { arr[i].x=read(); arr[i].id=i; ins(root[i-1],root[i],1,n,arr[i].x,1); } while(q--) { x1=read(),yy1=read(),x2=read(),y2=read(); long long t1=0,t2=0,t3=0,t4=0; t1=max(0,x1-1); t2=query(root[n],1,n,1,yy1-1); t3=max(0,n-x2); t4=query(root[n],1,n,y2+1,n); long long sum=calc(n); sum-=calc(t1)+calc(t2)+calc(t3)+calc(t4); t1=query(root[x1-1],1,n,1,yy1-1); t2=query(root[x1-1],1,n,y2+1,n); t3=query(root[n],1,n,1,yy1-1)-query(root[x2],1,n,1,yy1-1); t4=query(root[n],1,n,y2+1,n)-query(root[x2],1,n,y2+1,n); sum+=calc(t1)+calc(t2)+calc(t3)+calc(t4); printf("%lld\n",sum); }}
阅读全文
0 0
- Codeforces Round #433 (Div. 2) E. Boredom
- 解题报告:Codeforces Round #433 (Div. 2) E. Boredom ( 离线处理+树状数组)
- Codeforces Round #433 (Div. 2) E. Boredom (主席树 计算矩形内点的个数)
- Codeforces Round #260 (Div. 2) C. Boredom
- Codeforces Round #260 (Div. 2) C Boredom
- Codeforces Round #260 (Div. 2)C. Boredom
- Codeforces Round #260 (Div. 2)C. Boredom(dp)
- Codeforces Round #260 (Div. 2) C. Boredom【DP】
- Codeforces Round #260 (Div. 2)455A - Boredom (DP)
- dp解Codeforces Round #260 (Div. 2)C. Boredom
- Codeforces Round #260 (Div. 2)——C. Boredom
- Codeforces Round #260 (Div. 2) C. Boredom(DP)
- Codeforces Round #260 (Div. 1) A. Boredom
- Codeforces Round #260 (Div. 1) A. Boredom
- Codeforces Round #260 (Div. 1) A. Boredom
- 主席树 简要介绍 codeforces Round 433 853C/854E Boredom 题解
- Codeforces Round #433(Div.2) C,D,E题目详解
- Codeforces Round #433 (Div. 1, based on Olympiad of Metropolises) C. Boredom
- JavaScript的垃圾回收机制与内存管理
- 算法和数据结构
- Kettle执行Job报Exception while loading class org.gjt.mm.mysql.Driver
- HDU 5534 Partial Tree ACM/ICPC 2015 Changchun(完全背包)
- 修改表字段长度的操作,对业务是否有影响?
- Codeforces Round #433 (Div. 2) E. Boredom
- ElasticSearch集群搭建及插件安装
- JQuery实现对select选择框的赋值
- vueJs2.0学习笔记(三)
- windows下zabbix客户端批处理
- 从拉格朗日乘子法到SVM
- ROS回调函数的类方法实现,both in Python 和 C++
- 【path-sum】
- Pointers on C——9 Strings, Characters, and Bytes.6