HDU 5273 (Recursive)

Source: Internet
Author: User

Dylans loves sequenceaccepts:250submissions:806Time limit:2000/1000 MS (java/others)Memory limit:131072/131072 K (java/others) Problem description
Dylans got the
    
       N    
  
 
    
 Number
   
    
        a[1]. . . a [N] 
 

    
   。 Yes
    
        Q   
  
 
    
 Problems, each of which is like a
    
        (L,R)    
  
 
    
 He needs to ask the
    
        L? R  
  
 
    
 The number of inverse pairs in these numbers. More formally, he needs to ask for a two-tuple
    
        (x,y)   
  
 
    
 The number that makes
    
        L≤x,y≤R   
  
 
    
 And
    
        x<y     
  
 
    
 And
   
    
        a[x]>a[y  ]
 

    
   
Enter a description
The first line has two numbers.
    
       N    
  
 
    
 And
    
        Q   
  
 
    
 。 The second line gives
    
       N    
  
 
    
 A number
   
    
        a[1]. . . a [N] 
 

    
   。 The next
    
        Q   
  
 
    
 Rows, given two numbers per line
    
        L,R    
  
 
    
 。
   
    N  ≤ +,Q≤100000,L≤R,1≤a[I]≤  2  ?1
   
Output description
For each query, the output is in reverse order.
Input sample
3 23 2 11 21 3
Output sample
1 3
Hint
There should be no extra space at the end of each line read in the hack data.
Statistic | Submit | Clarifications | Back


RES[I][J] (j<=i) represents the number of I in the 1 to J in this interval to the inverse of the contribution. GAO[I][J] for the inverse logarithm of the interval [i,j], how to quickly find out the inverse logarithm of the interval, considering the contribution of the number of K (I=<K<=J), the contribution of the k number should be 1 to K contribution minus 1 to i-1 contribution, then by gao[i][j-1] can O ( 1) to find out the gao[i][j]. After preprocessing, the query is O (1).

#include <bits/stdc++.h>using namespace Std;typedef long long ll;const int maxn = 1e3+; #define foreach (It,v) for ( __typeof (V.begin ()) it = V.begin (); It! = V.end (); ++it) #define REP (i,a,b) for (int i= (a);i< (b); i++) #define PB push_backint A[maxn];ll RES[MAXN][MAXN],GAO[MAXN][MAXN]    ; int main (int argc, char const *argv[]) {int n,q;        while (scanf ("%d%d", &n,&q) ==2) {for (int i = 1; I <= N; i++) {scanf ("%d", a+i);            } for (int i = 1; I <= N; i++) {res[i][0] = 0;        for (int j = 1; J <= I; j + +) Res[i][j] = Res[i][j-1] + (A[j] > A[i]);            } for (int i = 1; I <= N; i++) {gao[i][i] = 0;            for (int j = i + 1; j <= N; j + +) {Gao[i][j] =gao[i][j-1] + res[j][j]-res[j][i-1];            }} while (q--) {int l,r;scanf ("%d%d", &l,&r);        printf ("%i64d\n", Gao[l][r]); }} return 0;}


HDU 5273 (Recursive)

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.