"Bestcoder #45"

Source: Internet
Author: User

Using the home computer to program, all kinds of not counting

First 15 minutes to brush off T1 and T2, and then forever stuck in the T3 ...

Later read the puzzle, silly eyes ...

It emphasizes that "there is only one answer" I thought it was a clue to my two-point answer, so I went straight to the weight line tree that way.

It also represents a meaning: path XOR value is the answer ...

All right, I'm having a funny head show.

Code:

T1

#include <cstdio> #include <cstring> #include <cctype> #include <algorithm> #define REP (i, L, R) for (int i=l; i<=r; i++) #define MAXN 10009#define ll long longusing namespace Std;ll read () {    ll x=0, f=1; Char Ch=ge TCHAR ();    while (!isdigit (CH)) {if (ch== '-') f=-1; Ch=getchar ();}    while (IsDigit (CH)) x=x*10+ch-' 0 ', Ch=getchar ();    return x*f;} ll N;int Main () {    int t=read (); while (t--)    {        n=read (); int a=0, b=0;        while (n)        {            if (a==0 && (n&1) ==1) b++;            a=n&1; n>>=1;        }        printf ("%d\n", b);    }    return 0;}

T2

#include <cstdio> #include <cstring> #include <cctype> #include <algorithm> #define REP (i, L, R) for (int i=l; i<=r; i++) #define MAXN 1009#define maxq 100009#define lowbit (x) (x&-x) #define LL Long longusing Namesp    Ace Std;ll Read () {ll x=0, f=1; char Ch=getchar ();    while (!isdigit (CH)) {if (ch== '-') f=-1; Ch=getchar ();}    while (IsDigit (CH)) x=x*10+ch-' 0 ', Ch=getchar (); return x*f;} struct Node{int x,y,z,w;} t[maxq];int N, Q, K[MAXN], NUM[MAXN], S[maxn];bool CMP (Node A, Node B) {return a.x<b.x;}; BOOL CMP2 (Node A, Node B) {return a.w<b.w;}; void Add (int x, int v) {while (x<=n) s[x]+=v, X+=lowbit (x);}    int Que (int x) {int now=0;    while (x>0) now+=s[x], x-=lowbit (x); return now;}    int main () {N=read (), Q=read ();    Rep (i, 1, N) k[i]=read (); Rep (i, 1, q) t[i].x=read (), T[i].y=read (), t[i].w=i;    Sort (t+1, T+1+Q, CMP);    Rep (i, 1, N) Rep (J, 1, i-1) if (k[j]>k[i]) num[i]++;    Rep (i, 1, N) Add (i, num[i]);    int now=1; Rep (o, 1, q)   {while (now<t[o].x) {Rep (i, now+1, N) if (K[now]>k[i]) Add (i,-1);        now++;    } t[o].z=que (T[O].Y);    } sort (t+1, t+1+q, CMP2);    Rep (i, 1, q) printf ("%d\n", t[i].z); return 0;}

Exercises

1001 Dylans loves numbers
This problem is modeled according to test instructions.
To set the number of reads to N, we first decompose n into binary form in array a. (Positive and negative order not related)
Then on the side of a array loop, if the current position is 1 and the previous one is 0 then the counter is + +. Pay attention to some small details.
Time Complexity of O (T∗log (N))

1002 Dylans loves sequence
N is only 1000, so I want to how to come.
The easiest thing to think about is the start of the enumeration, and then the Nlog (N) time to calculate the inverse pairs, maintained with a tree array.
(unfortunately BC does not give card ...) Woo Woo)
think carefully about how easy it is to do N2.
set Ans[l][r] to the number of l∼r in reverse order. First of all, we have a violent first count of ANS[1][1..N].
then I from the 2∼n enumeration, each time computes the reverse order starting from I to.
so what ans[i][j] less than ans[i−1][j]? Yes, less a[i−1] The contribution of this number.
We'll open another accumulator, CNT. Enumeration J from I∼n, if a[i−1] and a[j] produce reverse pairs on the cnt[j]=−1
then we add CNT from right to left (because contributions are prefixes and properties)
last Ans[i][j]=ans[i−1][j]+cnt[j].
after preprocessing all the answers you can ask O (1).

1003 Dylans loves tree
There is a magical nature in the title: there is a maximum of one number on the path that appears odd several times.
this should immediately think of XOR. Because XOR is two times and is not different or equivalent. In addition, it satisfies the interval reduction property.
because of the changes, we naturally think of using data structure maintenance.
The most brain-free is directly on the tree chain or splay maintenance interval XOR value can be.
think about it and find that you can use LCA to eliminate the "tree path" and convert it to the root to the X path for XOR values.
we can use a line tree or a tree-like array to maintain the DFS sequence very classically.
(BC does not give me card log2 ...) Woo Woo)
There is a strong trick is the weight can be 0!
So for example, there are 3 0 on the path, although they have an XOR value of 0, but they appear odd several times.
I deliberately put a[i] as ∈ Natural number set instead of [0,100000], is to try not to be found.
How to avoid it? 0 of the cases are maintained separately?
There is a very simple solution: directly to the read-in ownership value +1, the output time and then 1!
time complexity of O (N∗log (n) 2) or O (N∗log (n) )

1004 Dylans loves polynomial
The range of n is 3000. Consider the method of polynomial interpolation.
Because it is interpolated on a substring, it is natural to think of Newton interpolation. (If not please Baidu by yourself).
Then the problem is solved by preprocessing a 3000∗3000 table, and then each query will be able to read the difference quotient on the diagonal of O (N).
Time Complexity of O (N2+NQ)
Of course, after the completion of the problem, I also made up a n^2 Lagrange method.
But there are a lot of details, to do some prefix product, suffix product to maintain, here no longer described.

Spit Groove: The question person said the topic is a bit difficult, this issue 1002 idea changed several times, and then the basic is 1002 1003,1003 replaced by 1004. (That is, there is an interesting topic not to come out)
Then everyone in the clarification a large number of questions C whether there are negative and 0 points right, this gives some students to wake up ~

"Bestcoder #45"

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.