1018(离线树状数组)
来源:互联网 发布:冰箱怎么选 知乎 编辑:程序博客网 时间:2024/06/06 03:51
Wintokk has collected a huge amount of coins at THU. One day he had all his coins fallen on to the ground. Unfortunately, WangDong came by and decided to rob Wintokk of the coins.
They agreed to distribute the coins according to the following rules:
Consider the ground as a plane. Wintokk draws a horizontal line on the plane and then WangDong draws a vertical one so that the plane is divided into 4 parts, as shown below.
Wintokk will save the coins in I and III while those fit in II and IV will be taken away by the robber WangDong.
For fixed locations of the coins owned by Wintokk, they drew several pairs of lines. For each pair, Wintokk wants to know the difference between the number of the saved coins and that of the lost coins.
It's guaranteed that all the coins will lie on neither of the lines drew by that two guys.
210 329 2217 1418 233 156 2830 274 126 78 011 212 255 1019 2410 528 182 296 513 1220 2715 2611 923 2510 022 2416 3014 317 218 17 4
64422444
思路:
变成离线操作后,二维的就可以变为一维,数组的大小就够用了。
代码:
#include <iostream>#include <cstdio>#include <algorithm>#include <cstring>#include <cmath>using namespace std;long long c[500005][2];int pp=500001;long long ans[50005];struct poin{ int x,y;}a[50005];struct poin1{ int x,y,id;}b[50005];bool cmp(const poin a,const poin b){ return a.x<b.x;}bool cmp1(const poin1 a,const poin1 b){ return a.x<b.x;}int lowbit(int x){ return x&(-x);}void add(int x,int v,int k){ while(x<=500004) { c[x][k]+=v; x+=lowbit(x); }}long long sum(int x,int k){ long long su=0; while(x>0) { su+=c[x][k]; x-=lowbit(x); } return su;}int main(){ int t; scanf("%d",&t); while(t--) { int n,m; memset(c,0,sizeof(c)); memset(ans,0,sizeof(ans)); scanf("%d%d",&n,&m); for(int i=1;i<=n;i++) { scanf("%d%d",&a[i].x,&a[i].y); a[i].x++;a[i].y++; } for(int i=1;i<=m;i++) { scanf("%d%d",&b[i].x,&b[i].y); b[i].id=i; b[i].x++;b[i].y++; } sort(a+1,a+n+1,cmp); sort(b+1,b+m+1,cmp1); for(int i=1;i<=n;i++) { add(a[i].y,1,1); } int st=1,ed; for(int i=1;i<=m;i++) { for(ed=st;ed<=n;ed++) { if(a[ed].x>b[i].x)break; } for(int j=st;j<ed;j++) { add(a[j].y,1,0); add(a[j].y,-1,1); } long long sum1=sum(b[i].y,0)+sum(pp,1)-sum(b[i].y,1); long long sum2=sum(b[i].y,1)+sum(pp,0)-sum(b[i].y,0); ans[b[i].id]=fabs(sum1-sum2); st=ed; } for(int i=1;i<=m;i++) { printf("%I64d\n",ans[i]); } if(t)printf("\n"); } return 0;}
- 1018(离线树状数组)
- hdu4630(树状数组+离线处理)
- hdu 4638 Group (树状数组+离线)
- HDU 4638 Group(离线 + 树状数组)
- hdu 5057(树状数组+离线处理)
- hdu-5057(离线的树状数组)
- HDU--3333(树状数组+离线处理)
- hdu 3874(树状数组+离线算法)
- [BZOJ 2743]采花(树状数组+离线)
- HDU3874 (树状数组+离线操作)
- 2743: [HEOI2012]采花(离线树状数组)
- 1006(离线操作+树状数组)
- HDU4638【离线树状数组】
- 树状数组+离线+hdu4605
- Hduoj3874 【离线+树状数组】
- hdu3874 离线树状数组
- HDU4630(树状数组,离线)
- hdu 4638 (树状数组+离线)
- 百度、腾讯和阿里巴巴 12大隐藏良心级实用功能
- 安装elasticsearch5.4.0的中文分词ik5.4.0
- 屏幕适配:dp、px、ppi、sp、手机尺寸和分辨率之间的关系
- Java反射机制应用实践
- Java中char和String的转换
- 1018(离线树状数组)
- Solr 环境搭建 6.6.0(一)
- C++模板中的typename的两种用法
- 关于停止线程和唤起线程的方法
- 华为2017秋招测试工程师笔试试卷
- WinForm中给DataGridView绑定datatable
- 【读书笔记】数据挖掘导论(Introduction to Data Mining) 1
- java代理(静态代理和jdk动态代理以及cglib代理)
- 冒泡排序