1018(离线树状数组)

来源:互联网 发布:冰箱怎么选 知乎 编辑:程序博客网 时间:2024/06/06 03:51

Problem Description

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.


Input
<div><p>The first line contains an integer <i>T</i>, indicating the number of test cases. Then <i>T</i> blocks of test cases follow. For each case, the first line contains two integers <i>N</i> and <i>M</i>, where <i>N</i> is the number of coins on the ground and <i>M</i> indicates how many times they are going to draw the lines. The 2nd to (<i>N</i>+1)-th lines contain the co-ordinates of the coins and the last <i>M</i> lines consist of the <i>M</i> pairs integers (<i>x</i>, <i>y</i>) which means that the two splitting lines intersect at point (<i>x</i>, <i>y</i>).</p><p>(<i>N</i>,<i>M </i>≤ 50000, 0 ≤<i>x</i>,<i>y </i>≤ 500000)</p>

Output
<p>For each query, output a non-negative integer, the difference described above. Output a blank line between cases.</p>

Sample Input
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

Sample Output
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;}




原创粉丝点击