ACM Learning process-snnuoj 1239 counting Star time (tree array && dynamic planning && number theory)

Source: Internet
Author: User
Tags gcd

http://219.244.176.199/JudgeOnline/problem.php?id=1239

This is the Shaanxi province.Gquestion, the main idea is aN*nDot- matrix, point coordinates from(1, 1)to the(n, N), each point is entitled to a value, and then from(x, y)citedxperpendicular to the axis, then a triangle, three vertices are(0, 0),(x, 0)and the(x, y). The weights of the inner points of the triangle are obtained, including the boundary,Nthe scope is +,mthe scope is100000, and said it was also more pit. Learn younger brothern*mthe complexity of the water has passed, visual competition data comparison of water. But I hung up on us .OJgave a set of random data, a set of limit data, and then learn the younger brotherTdrop it. (Pit-Learning Brother series.) )

No water, this topic feeling itself is a very good question.

Just got the question, my first reaction must be to find out all p (x, y), and then go to the DP transfer equation to consider. However, it is more complicated to find the simple recursive feeling problem.

But one important point to find is that a point with a small slope must be covered by a point that has a large slope behind it. Then I thought about pressingxThe axes enumerate each column, maintaining the weights and values that appear for each slope. Then each large slope must be the same as the small slope of the previous enumeration, so that the tree-like array can be maintained. The key is to solve the discretization of the slope, my first thought isMapto carry outHash. At that time had this idea, originally thought should be possible to grab a blood or something. When the results hit about one hours, it was found that several peopleAlost ... (What a ghost.) )。 My first hair.TA.. With local testing,MapthatHashIt's too slow. So I started thinking about how to optimize this .Hash, think of several ways to meety1/x1 < y2/x2, andHash (x1, y1) < hash (x2, y2)The function is really no way. And two times in the middle of the wrong.HashMethod: Finally, the teammates reminded that the discretization process can be fully pre-processed, do not need to complete each group of data online, because the data only1000*1000, then all slopes must be within this range. and finallyAdrop it.

Preprocessing complexity : O (N*n*log (n*n))

Play table : O (N*n*log (n*n))

Online Enquiry : O (m)

The final total complexity is O (N*n*log (n*n) +t* (M+n*n*log (n*n)))

If m is larger, this complexity is still relatively good.

Code:

#include <iostream>#include<cstdio>#include<cstdlib>#include<cstring>#include<cmath>#include<algorithm>#include<Set>#include<map>#include<vector>#include<string>#defineLL Long Longusing namespacestd;Const intMAXM =1005;Const intMAXN =1000005; LL D[MAXN];intLowbit (intx) {    returnx& (-x);}voidAddintIdintpls) {     while(ID <= MAXN)//ID max is MAXN{D[id]+=pls; ID+=lowbit (ID); }}ll sum (intTo ) {LL s=0;  while(To >0) {s= S +D[to]; to-=Lowbit (to); }    returns;}intgcdintAintb) {    intR;  while(b! =0) {R=b; b= a%b; A=R; }    returnA;}structnode{intx, y; voidCreateintXxintyy) {        intt =gcd (xx, yy); X= xx/T; Y= yy/T; }    BOOL operator< (node k)Const    {        returnK.x*y < k.y*x; }};intN, A[MAXM][MAXM]; LL P[maxm][maxm];map<node,int>Hash;voidinit () {node T;  for(inti =1; i < MAXM; ++i) for(intj =1; J < Maxm; ++j) {T.create (i, j); Hash[t]=233; } Map<node,int>:: Iterator it; intCNT =1;  for(it = Hash.begin (); It! = Hash.end (); + +it) it->second = cnt++;}voidinput () {memset (d,0,sizeof(d));    Node T; intto ; scanf ("%d", &N);  for(inti = n; I >=1; --i) for(intj =1; J <= N; ++j) scanf ("%d", &A[i][j]);  for(intj =1; J <= N; ++j) { for(inti =1; I <= N; ++i) {t.create (J, I); to=Hash[t];            Add (To, a[i][j]); P[j][i]=sum (to); }    }}voidWork () {intm, u, v; scanf ("%d", &m);  for(inti =1; I <= m; ++i) {scanf ("%d%d", &u, &v); printf ("%lld\n", P[u][v]); }}intMain () {//freopen ("test.in", "R", stdin); //freopen ("Test.out", "w", stdout);init (); intT; scanf ("%d", &T);  for(intTimes =1; Times <= T; ++Times ) {printf ("Case #%d:\n", times);        Input ();    Work (); }    return 0;}
View Code

ACM Learning process-snnuoj 1239 counting Star time (tree array && dynamic planning && number theory)

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.