HDU5032 always Cook Mushroom (tree-like array && offline)

Source: Internet
Author: User

Tree-like array + ask offline. An optimization is needed to sort the 1000*1000 points first, instead of generating the 1000*1000 points each time and then sorting with the query, which will be tle.

#include <iostream> #include <cstring> #include <string> #include <vector> #include <cstdio > #include <algorithm> #include <cmath>using namespace std; #define MAXN 1200#define ll Long longll A, b;    struct node{ll A, B;    int t;    ll v;    int id;        BOOL operator < (const node& y) const{if (b*y.a!=a*y.b) return b*y.a<a*y.b;    else return t<y.t; }}x[120000],y[1005000];int tot,tot2;ll ans[200000];ll bit[maxn];void Add (int i,ll v) {while (i<=1000) {bit[i]+        =v;    i+=i& (-i);    }}ll sum (int i) {ll ret=0;        while (i>0) {ret+=bit[i];    i-=i& (-i); } return ret;}    int main () {int t;cin>>t;int ca=0;    tot2=0;            for (int i=1;i<=1000;++i) {for (int j=1;j<=1000;++j) {y[tot2].a=i;y[tot2].b=j;        Y[tot2++].t=1;    }} sort (Y,y+tot2);        while (t--) {scanf ("%i64d%i64d", &a,&b);        tot=0;      int m;scanf ("%d", &m);  int ai,bi,xi;            for (int i=0;i<m;++i) {scanf ("%d%d%d", &ai,&bi,&xi);            X[tot].a=ai;            X[tot].b=bi;            X[tot].id=i;            x[tot].t=2;        X[tot++].v=xi;        } sort (X,x+tot);        memset (bit,0,sizeof (bit));        int id=0; for (int i=0;i<tot;++i) {while (Id<tot2&&y[id]<x[i]) {Add (y[id].a, (y[id].a+a) * (y[                ID].B+B));            ++id;        } ans[x[i].id]=sum (X[I].V);        } printf ("Case #%d:\n", ++CA);        for (int i=0;i<m;++i) {printf ("%i64d\n", Ans[i]); }} return 0;}

HDU5032 always Cook Mushroom (tree-like array && offline)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.