HDU 5273 Dylans loves sequence reverse order number simple recursion

Source: Internet
Author: User

Dylans loves sequence

Time Limit:20 Sec

Memory limit:256 MB

Topic Connection

http://acm.hdu.edu.cn/showproblem.php?pid=5273

Descriptiondylans gets the N number A[1]...a[n].
There are q questions, each problem shaped like (l,r)
He needs to ask for the number of reverse l−r in these numbers.
More formally, he needs to ask for a two-tuple (x, y) number, making l≤x,y≤r and X<y and A[x]>a[y]

Input

The first line has two numbers n and Q.
The second line gives N numbers a[1]...a[n].
The next Q line, each line gives two numbers L, R.

n≤1000,q≤100000,l≤r,1≤a[i]≤231−1

Output

For each query, the output is in reverse order.

Sample Input

3 2
3 2 1
1 2
1 3

Sample Output

1
3

HINT

Test instructions

Exercises

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).

Code

#include <cstdio>#include<cmath>#include<cstring>#include<ctime>#include<iostream>#include<algorithm>#include<Set>#include<vector>#include<sstream>#include<queue>#include<typeinfo>#include<fstream>#include<map>#include<stack>typedefLong Longll;using namespacestd;//freopen ("d.in", "R", stdin);//freopen ("D.out", "w", stdout);#defineSspeed ios_base::sync_with_stdio (0); Cin.tie (0)#defineTest Freopen ("Test.txt", "R", stdin)#defineMAXN 2000001#defineMoD 10007#defineEPS 1e-9Const intinf=0x3f3f3f3f;Constll infll =0x3f3f3f3f3f3f3f3fll;inline ll Read () {ll x=0, f=1;CharCh=GetChar ();  while(ch<'0'|| Ch>'9'){if(ch=='-') f=-1; ch=GetChar ();}  while(ch>='0'&&ch<='9') {x=x*Ten+ch-'0'; ch=GetChar ();} returnx*F;}//**************************************************************************************intd[1005][1005];intsum[1005][1005];inta[1005];intMain () {intN=read (), q=read ();  for(intI=1; i<=n;i++) A[i]=read ();  for(intI=1; i<=n;i++)    {         for(intj=i+1; j<=n;j++)        {            if(a[j]<A[i]) d[i][j]=d[i][j-1]+1; Elsed[i][j]=d[i][j-1]; }         for(intj=i-1; j>=1; j--)        {            if(a[j]>A[i]) d[i][j]=d[i][j+1]+1; ElseD[i][j]=d[i][j+1]; }    }     for(intI=1; i<=n;i++)    {         for(intj=i;j<=n;j++) Sum[i][j]=sum[i][j-1]+D[j][i]; }     while(q--)    {        intX=read (), y=read (); printf ("%d\n", Sum[x][y]); }}

HDU 5273 Dylans loves sequence reverse order number simple recursion

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.