HDU always Cook Mushroom (polar sort + Tree array)

Source: Internet
Author: User


Problem Descriptionmatt have a company, always Cook Mushroom (ACM), which produces high-quality mushrooms.

ACM has a large field to grow their mushrooms. The field can be considered as a-a-mushrooms-grid where the is grown in grid points numbered from (1, 1) to (1000, Because of humidity and sunshine, the productions in different grid points is not the same. Further, the production in the grid points (x, y) was (x + A) (y + B) where A, B is the constant.

Matt,the owner of ACM have some queries where he wants to know the sum of the productions in a given scope (include the MUSH The growing on the boundary). In each query, the scope Matt asks was a right angled triangle whose apexes was (0, 0), (p, 0), (p, Q) 1<=p, q<=1000.

As the employee of ACM, can you answer Matt ' s queries?
Inputthe first line contains one integer T, indicating the number of test cases.

For each test case, the first line contains Integers:a, B (0<=a, b<=1000).

The second line contains one integer M (1<=m<=10^5), denoting the number of queries.

In the following M lines, the i-th line contains three integers a, b, X (1<=a, B<=10^6, 1<=x<=1000), denoting One apex of the given right angled triangle are (x, 0) and the slope of its base are (A, b). It is guaranteed the gird points in the given right angled triangle be all in valid area, numbered from (1, 1) to (1 000, 1000).
Outputfor each test case, output M + 1 lines.

The first line contains ' case #x: ', where x is the case number (starting from 1)

In the following M lines, the i-th line contains one integer, denoting the answer of the i-th query.
Sample Input
20 033 5 82 4 71 2 31 233 5 82 4 71 2 3

Sample Output
Case #1:1842170886Case #2:29012688200

Source2014 ACM/ICPC Asia Regional Beijing Online
Test Instructions:given a 1000x1000 lattice, the M-group asks for a weighted value of a point bounded by the right triangle of a direction vector (a, a, A, b) from the origin, one at a time (0,0), (x,0).

Idea: preprocessing the polar relationship sequence of these 1e6 points, offline, and the query will also be sorted by (a, a) polar angle. Then just imagine a needle in the counter-clockwise sweep, the sweep of the point of the weights added to the tree array, for each query is only a prefix and.

#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <  Cmath>typedef Long long ll;using namespace Std;const int maxn = 1005;const int inf = 1e5+5;struct Point {ll A, b;double s;} P[maxn*maxn];struct Query {ll A, B, X, id;double s;} q[maxn*maxn];ll bit[maxn];ll Ans[inf], index[inf];int cnt;void scan (l L &x) {char c;while ((c = GetChar ()) && (C < ' 0 ' | | c > ' 9 ')); x = C-' 0 '; while ((c = GetChar ()) && Amp (c >= ' 0 ' && C <= ' 9 ')) x = x * + C-' 0 ';} void out (ll x) {if (X > 9) out (X/10);p Utchar (x%10+ ' 0 ');} inline int lowbit (int x) {return x & x;} inline void Add (int x, int val) {while (x <=) {bit[x] + = val;x + lowbit (x);}} inline ll sum (int x) {ll tmp = 0;while (x > 0) {tmp + = bit[x];x-= lowbit (x);} return TMP;} BOOL Cmp1 (point x, point y) {return x.s < Y.S;} BOOL Cmp2 (query x, query y) {if (x.s = = Y.S) return x.x < Y.x;return x.s < Y.S;} void Init () {for (int i = 1; I <=1000; i++) for (int j = 1; J <=, J + +) {p[cnt].a = i;p[cnt].b = J;P[CNT++].S = 1.0 * j/i;} Sort (p, p+cnt, CMP1);}  int main () {cnt = 0;ll A, B, M;init (), int t, CAS = 1;scanf ("%d", &t), while (t--) {memset (bit, 0, sizeof (bit)), scan (A), Scan (B), scan (m); for (int i = 0; i < m; i++) {scan (Q[I].A), scan (q[i].b), scan (q[i].x); q[i].s = 1.0 * Q[I].B/Q[I].A; Q[i].id = i;} Sort (q, q + M, CMP2); for (int i = 0; i < m; i++) {index[q[i].id] = i;} CNT = 0;printf ("Case #%d:\n", cas++), for (int i = 0; i < m; i++) {while (P[cnt].s <= q[i].s) {Add (P[CNT].A) (p[cnt). A+a) * (p[cnt].b + b)); cnt++;} Ans[i] = SUM (q[i].x);} for (int i = 0, i < m; i++) {out (Ans[index[i]]);p rintf ("\ n");}} return 0;}





HDU always Cook Mushroom (polar sort + Tree array)

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.