hdu5032 树状数组

题意:

对于一个1000*1000的Mushroom,

起点在(1,1)给定一个斜率和一个x,求由斜率和x所对应的直线构成的三角形内蘑菇的总值。

每个点的对应的值为(x+A)(y+B)

每个点都有一个相对于(1,1)的一个斜率我们就按照这个斜率的大小进行排序 大的放在后面

然后我们对于每个要查询的点的斜率的进行排序

采用离线的 方法去计算我们要求的东西这样我们对于每次查询都把小于他斜率的点扔进树状数组对应的x中就可以了

然后求和

#include <iostream>
#include <algorithm>
#include <string.h>
#include <cstdio>
using namespace std;
const int maxn =1000000+10;
typedef long long LL;

struct point
{
    int x,y;
    LL ans;
    double p;
    point(int cx=0,int cy=0,LL cans=0,double cp=0)
    {
          x=cx; y=cy; ans=cans; p=cp;
    }
}s[maxn],sc[maxn];
bool cmpp(point a, point b)
{
     return a.p<b.p;
}
bool cmpi(point a, point b)
{
     return a.y<b.y;
}
LL C[1005];
int lowbit(int x)
{
     return x&-x;
}
void add(int x,LL d)
{
    while(x<=1000)
        {
             C[x]+=d; x+=lowbit(x);
        }
}
LL sum(int x)
{
    LL ret=0;
    while(x>0)
    {
       ret+=C[x]; x-=lowbit(x);
    }
    return ret;
}

int main()
{
    int num=0;
    for(int i=1; i<=1000;i++)
         for(int j=1; j<=1000; j++)
           s[num++]=point(i,j,0,1.0*j/i);
    int cas;
    sort(s,s+num,cmpp);
    scanf("%d",&cas);
    for(int cc =1; cc<=cas; cc++)
    {
         LL A,B;
         scanf("%I64d%I64d",&A,&B);
         int m;
         scanf("%d",&m);
        for(int i=0; i<m; i++){
             int a,b,x;
             scanf("%d%d%d",&a,&b,&x);
             sc[i]=point( x,i,0,1.0*b/a );
        }
        sort(sc,sc+m,cmpp);
        int now=0;
        memset(C,0,sizeof(C));
        for(int i=0; i<m; i++)
        {
            while(now<1000000&&s[now].p<=sc[i].p){
                 add(s[now].x,1LL*(s[now].x+A)*(s[now].y+B));now++;
            }
            sc[i].ans=sum(sc[i].x);
        }
        sort(sc,sc+m,cmpi);
        printf("Case #%d:\n",cc);
        for(int i=0; i<m; i++)
        {
            printf("%I64d\n",sc[i].ans);
        }
    }

    return 0;
}

时间: 2024-08-30 04:14:27

hdu5032 树状数组的相关文章

Hdu5032 极角排序+树状数组

题目链接 思路:参考了题解.对询问进行极角排序,然后用树状数组维护一下前缀和即可. /* ID: onlyazh1 LANG: C++ TASK: test */ #include<bits/stdc++.h> using namespace std; #define lson l,m,rt<<1 #define rson m+1,r,rt<<1|1 typedef long long ll; const int maxn=1010; const int maxm=10

HDU5032 Always Cook Mushroom(树状数组&amp;&amp;离线)

树状数组+询问离线.一个优化是需要的,就是先对1000*1000个点先排序,而不是每次都生成这1000*1000个点然后和询问一起排序,那样会tle. #include <iostream> #include <cstring> #include <string> #include <vector> #include <cstdio> #include <algorithm> #include <cmath> using

HDU5032 -- Always Cook Mushroom 树状数组

题意:1000*1000的格子, 坐标为(1, 1) ~ (1000, 1000), 常数 A, B, 点(x,  y)权值为 (x + A) * (y + B), q次询问, 每次询问(0, 0) (p,  0), (p, q)的直角三角形内的权值和. 作法: 离线处理, 把所有点和询问放到一起, 按斜率从小到大排序, 考虑每个询问, 那么对当前询问有贡献的点的斜率肯定小于等于该直角三角形斜边的斜率, 所有遇到一个点加入到树状数组, 然后每次询问就是取 区间和了.

HDU 5542 The Battle of Chibi dp+树状数组

题目:http://acm.hdu.edu.cn/showproblem.php?pid=5542 题意:给你n个数,求其中上升子序列长度为m的个数 可以考虑用dp[i][j]表示以a[i]结尾的长度为j的上升子序列有多少 裸的dp是o(n2m) 所以需要优化 我们可以发现dp的第3维是找比它小的数,那么就可以用树状数组来找 这样就可以降低复杂度 #include<iostream> #include<cstdio> #include<cstring> #include

(POJ 3067) Japan (慢慢熟悉的树状数组)

Japan Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 29295   Accepted: 7902 Description Japan plans to welcome the ACM ICPC World Finals and a lot of roads must be built for the venue. Japan is tall island with N cities on the East coas

【二维树状数组】See you~

https://www.bnuoj.com/v3/contest_show.php?cid=9148#problem/F [题意] 给定一个矩阵,每个格子的初始值为1.现在可以对矩阵有四种操作: A x y n1 :给格点(x,y)的值加n1 D x y n1: 给格点(x,y)的值减n1,如果现在格点的值不够n1,把格点置0 M x1 y1 x2 y2:(x1,y1)移动给(x2,y2)n1个 S x1 y1 x2 y2 查询子矩阵的和 [思路] 当然是二维树状数组 但是一定要注意:lowbi

Vijos P1066 弱弱的战壕【多解,线段树,暴力,树状数组】

弱弱的战壕 描述 永恒和mx正在玩一个即时战略游戏,名字嘛~~~~~~恕本人记性不好,忘了-_-b. mx在他的基地附近建立了n个战壕,每个战壕都是一个独立的作战单位,射程可以达到无限(“mx不赢定了?!?”永恒[email protected][email protected]). 但是,战壕有一个弱点,就是只能攻击它的左下方,说白了就是横纵坐标都不大于它的点(mx:“我的战壕为什么这么菜”ToT).这样,永恒就可以从别的地方进攻摧毁战壕,从而消灭mx的部队. 战壕都有一个保护范围,同它的攻击

CF 313 DIV2 B 树状数组

http://codeforces.com/contest/313/problem/B 题目大意 给一个区间,问你这个区间里面有几个连续相同的字符. 思路: 表示个人用树状数组来写的...了解了树状数组的本质就行了. 当然用sum[r]-sum[l]也是可以的

Curious Robin Hood(树状数组+线段树)

1112 - Curious Robin Hood    PDF (English) Statistics Forum Time Limit: 1 second(s) Memory Limit: 64 MB Robin Hood likes to loot rich people since he helps the poor people with this money. Instead of keeping all the money together he does another tri