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)