Poj 2886 who gets the most candies? (Tree array)

Source: Internet
Author: User

The tree array is more elegant than the line tree... We have done a good job in anti-prime number tabulation, that is, the formula for pushing the next position from the current position has not been developed, so we can see other people's...

If (P [now]. x> 0) NXT = (NXT-1 + P [now]. X) % leave;

Else NXT = (NXT + P [now]. X) % leave + leave) % leave;

Then, in the tree array, find the number (Binary) in the NXT value )...

View code

// # Pragma comment (linker, "/Stack: 327680000,327680000 ") # include <iostream> # include <cstdio> # include <cmath> # include <vector> # include <cstring> # include <algorithm> # include <string> # include <set> # include <functional> # include <numeric> # include <sstream> # include <stack> # include <map> # include <queue> # define Cl (ARR, val) memset (ARR, Val, sizeof (ARR) # define rep (I, n) for (I) = 0; (I) <(N ); ++ (I) # define For (I, L, H) for (I) = (l); (I) <= (h); ++ (I) # define Ford (I, h, L) for (I) = (h); (I)> = (l); -- (I) # define L (x) (X) <1 # define R (x) <1 | 1 # define mid (L, R) (L + r)> 1 # define min (X, y) (x) <(y )? (X): (y) # define max (x, y) (x) <(y )? (Y): (x) # define e (x) (1 <(x) # define iabs (x) <0? -(X): (x) # define out (x) printf ("% i64d \ n", x) # define read () freopen ("data. in "," r ", stdin) # define write () freopen (" data. out "," W ", stdout); const double EPS = 1e-8; typedef long ll; const int INF = ~ 0u> 2; using namespace STD; const int n = 500010; const int M = 1024; struct node {int A, B; node () {} node (INT X, int y): A (x), B (y) {} bool operator <(const node CMP) const {return B <CMP. B ;}} f [m]; bool vis [m]; int prime [m]; int CNT, num; void get_prime () {int I, j; CL (VIS, true); for (I = 2; I <m; ++ I) {for (j = I * I; j <m; j + = I) {vis [J] = false ;}} CNT = 0; for (I = 2; I <m; ++ I) {If (vis [I]) Prime [CNT ++] = I ;}} void solve (INT pnum, int Val, int POs, int Lim) {If (Val> N) return; f [num ++] = node (pnum, Val); LL nlim, nval, npnum; nval = val; nlim = 0; npnum = 1; while (nlim <Lim & nval <= N) {nlim ++; npnum ++; nval * = prime [POS]; If (nval <= N) solve (npnum * pnum, nval, POS + 1, nlim) ;}} void get_antiprime () {get_prime (); num = 0; solve (1, 1, 0, 32); sort (F, F + num);} // --------- all of the above Table ------------------ int C [N], n; int lowbit (int I) {return I & (-I) ;} void add (INT P, int Val) {While (P <= N) {C [p] + = val; P + = lowbit (p) ;}} int sum (INT p) {int res = 0; while (P> 0) {res + = C [p]; P-= lowbit (p);} return res;} struct pnode {char name [20]; int X;} p [N]; int BS (int K) {int L = 1, R = N, mid, TMP; while (R-l> = 1) {mid = mid (L, R); TMP = sum (MID); If (TMP> = k) r = mid; else l = mi D + 1;} // printf ("% d \ n", l, R); return r;} int main () {// read (); get_antiprime (); int K, I, G, MX; int now, CNT, NXT, NN, M; while (scanf ("% d", & N, & K )! = EOF) {Cl (C, 0); for (I = 1; I <= N; ++ I) {scanf ("% S % d ", P [I]. name, & P [I]. x); add (I, 1);} MX = 0; G = 0; for (I = 0; I <num & F [I]. B <= N; ++ I) {If (F [I]. a> MX) {MX = f [I]. a; G = f [I]. B ;}} now = NXT = K; CNT = 1; while (CNT <G) {nn = N-CNT; M = P [now]. x; If (M> 0) NXT = (NXT-1 + M % NN) % nn; else NXT = (NXT + M) % Nn + nn) % nn; if (NXT = 0) NXT = nn; add (now,-1); // printf ("% d % s \ n", now, P [now]. name); now = BS (NXT); // printf ("% d \ n", now, NXT, NN); CNT ++ ;} printf ("% S % d \ n", P [now]. name, MX) ;}return 0 ;}

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.