"Codeforces 675E" "dp+ Line Tree Maintenance" gives n stations, and tells you that the first station has a direct ticket to the station from I+1 to A[i, asking you all stations to the rest of the stations to the minimum ticket costs and "_online

Source: Internet
Author: User
Tags ticket

Transmission door: E. Trains and Statistic Description:

E. Trains and statistic time limit/test 2 seconds memory limit per test 256 megabytes input standard input output Stan Dard output

Vasya commutes by train every day. There are n train stations in the city, and at the i-th station it's possible to buy only tickets to stations from I + 1 t o ai inclusive. No tickets are sold at the last station.

Letρi, J is the minimum number of tickets one needs to buy in order to get from stations I to station J. As Vasya is fond of different useless statistic him asks you to compute the sum of all valuesρi, J among all pairs 1≤i & Lt J≤n. Input

The ' the ' of the ' input contains a single integer n (2≤n≤100)-the number of stations.

The second line contains n-1 integer ai (i + 1≤ai≤n), the i-th of them means this at the I-th station one could buy TI Ckets to all station from i + 1 to AI inclusive. Output

Print the sum ofρi, J among all pairs of 1≤i < j≤n. Examples Input

4
4 4 4
Output
6
Input
5
2 3 5 5
Output
17
Note

In the ' the ', it ' s possible to-get-from-any-station to, any, with greater index, using only one ticket. The total number of pairs are 6, so the answer is also 6.

Consider the second sample:ρ1, 2 = 1ρ1, 3 = 2ρ1, 4 = 3ρ1, 5 = 3ρ2, 3 = 1ρ2, 4 = 2ρ2, 5 = 2ρ3, 4 = 1ρ3, 5 = 1ρ4, 5 = 1

Thus the answer equals 1 + 2 + 3 + 3 + 1 + 2 + 2 + 1 + 1 + 1 = 17.

Meaning

Give n stations and tell you that there is a direct ticket from I+1 to A[i station at the station, asking you for the minimum ticket cost of all stations to the rest of the stations. Ideas:

We use Dp[i] to represent the total of the first station from the i+1 to the N minimum ticket cost. So dp[n]=0, so you can extrapolate from N to 1 and calculate the answer.

Let's see how we get dp[i]. For stations from I+1 to A[i], only one ticket from station I is required. But for stations larger than A[i], we can find the largest station m from I+1 to A[i. Because A[m] is the largest, so can reach a larger range, so from this m station to the rest of the station to spend the ticket must be the least. This is the basis of our greed.

When you look for this m, you can use the line-tree to find the maximum value of a in the interval and then use the set two to query for M.

At this time we found that from m+1 to A[i] in dp[m] has been calculated, each one more, so the result is to subtract a[i]-m,

Calculate dp[i]=dp[m]+ (N-i)-(A[I]-M),

Just add up all the dp[i.


Line Tree Special Reference here


Code:

#include <bits/stdc++.h> #define Lson l, M, rt<<1 #define Rson m+1, R, rt<<1|1 #define PR (x) cout
;< #x << "=" << x << "" #define PL (x) cout << #x << "=" << x << Endl;

#define LL __int64 using namespace std;
#define MST (SS,B) memset (ss,b,sizeof (ss));
    #define for (i,j,n) for (int i=j;i<=n;i++) template<class t> void Read (t&num) {char CH; bool F=false; For (Ch=getchar (); ch< ' 0 ' | | Ch> ' 9 ';
    f= ch== '-', Ch=getchar ()); for (num=0;
    ch>= ' 0 ' &&ch<= ' 9 '; num=num*10+ch-' 0 ', Ch=getchar ());
F && (Num=-num);
int stk[70], TP;
    Template<class t> inline void print (T p) {if (!p) {puts ("0");
    while (p) stk[++ TP] = p%10, p/=10;
    while (TP) Putchar (stk[tp--] + ' 0 ');
Putchar (' \ n ');

const int N=1E5+10;
ll Dp[n],ans=0;
int max[n<<2],a[n],n; 

set<int>s[n];  
void pushup (int rt) {Max[rt]=max (max[rt<<1],max[rt<<1|1]); } void Build (int l,int r,int RT) {//Line segment tree to find interval maximum if (l==r) {max[rt]=a[l];
  Return
  int m= (R+L) >>1;
  Build (Lson);
  Build (Rson);
Pushup (RT);
  int query (int l,int R, int l,int r,int RT) {if (l<=l && r<=r) return MAX[RT];  
  int m= (L+R) >>1;  
  int ret=0;  
  if (l<=m) Ret=max (ret, query (L, R, Lson));  
  if (r>m) Ret=max (Ret,query (L, R, Rson));  
return ret;
  int main () {read (n);
  S[0].insert (n);
    for (int i=1; i<n; i++) {read (a[i));
  S[a[i]].insert (i);
  Build (1, N, 1);
  dp[n]=0; for (int i=n-1; i>=1; i--) {int m=query (i+1, A[i], 1, N, 1);/find I+1--a[i] The maximum a[m of a station m between stations (M=*s[m].lower_bound
    )//Returns the subscript dp[i]=dp[m]+n-i-(A[I]-M) that is greater than or equal to the first occurrence of a[m in i+1;
  Ans+=dp[i];
  } print (ANS);
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.