HDU 5439 Aggregated counting

Source: Internet
Author: User
Tags add numbers

Main topic:

Add numbers to the array starting from 1

is to add this number by the number represented in the current location.

1 2 2 3 3 since the 4 is required, and the 4th position is 3, the following 3 4 are required

The problem is that given a n, find the last position n appears p, and then find the last position p appears to

Here you can consider finding g[i] to show how many times I have appeared

Think about it here, because G[i] is equivalent to the number of the I position appears

So g[i] also satisfies this sequence

F[i] Indicates the last position where I appears, i.e. the total number of 1~i

Back to calculate g[i] When you can consider is to find the first position in the f[] interval, if f[k-1]< i <= f[k]

So that means g[i] = k at this time

Then we can calculate the complexity of Logn g[n].

To calculate the final answer, consider that given N, find the last occurrence of P, median length p = g[1]+g[2]....+g[p]

Then find the corresponding ans, then each increase of g[i], will let the length of the whole sequence increase i*g[i]

I*g[i] can be understood so that the length of I is the number of g[i], so the total length is i*g[i]

So ans = Sigma (i*g[i]) i<=n

So for n <= 1e9

Then a rough calculation will find F[500000]>1e9

So g[n]<500000 as long as the violence to the top 500000 g[], f[]

So before the answer is calculated, find G[n] how much

g[n]= Lower_bound (f+1, f+n+1, N)-F

Then explain [1, g[n]-1] all the lengths in this section have been used

So before preprocessing this length prefix and sum[]

For each length I, the number of times he appears is f[i]-f[i-1]

Sigma (N*g[n]) f[i-1]<n<=f[i]-g[n] = i

Then the answer is i* arithmetic progression, remember to take the mold (⊙o⊙) OH

Then (G[n]-1, G[n]] this paragraph as long as the enumeration (G[N]-1, n] is ok

1#include <iostream>2#include <cstdio>3#include <cstring>4#include <vector>5#include <queue>6 using namespacestd;7 #defineM 268 #defineN 5000009 #defineull unsigned long LongTen #definell Long Long One Const intMOD =1000000007; A intf[n+2], g[n+2], cnt[n+2]; -ll sum[n+2]; -  the intHash (intv) - { -     returnLower_bound (f+1, f+n+1, v)-F; - } +  - voidInit () + { Ag[1] =1, f[1] =1; atg[2] =2, f[2] =3; -      for(intI=3; I<=n; i++){ -G[i] = Lower_bound (f+1, F+i, I)-F; -F[i] = f[i-1]+G[i]; -     } - //for (int i=1; i<=100; i++) in //cout<<i<< "" <<g[i]<< "" <<f[i]<<endl; - //cout<<f[n]<<endl; to  +sum[1] =1; -      for(intI=2; I<=n; i++){ theSum[i] = sum[i-1]+ (LL) (f[i-1]+1+f[i]) * (f[i]-f[i-1])/2% MOD * (LL) I%MOD; *       //if (i<=10) cout<< "sum:" <<i<< "<<sum[i]<<endl; $     }Panax Notoginseng } - intMain () { the //freopen ("a.in", "R", stdin); + //freopen ("OUT.txt", "w", stdout); A  the init (); +     intT, N; -scanf"%d", &T); $      while(t--){ $scanf"%d", &n); -         intpos =Hash (n); -LL ret = sum[pos-1]; the          for(inti=f[pos-1]+1; I<=n; i++)//this interval is pos for each length -         {Wuyiret = (ret+ (ll) i*pos)%MOD; the         } -printf"%i64d\n", ret); Wu     } -     return 0; About}

HDU 5439 Aggregated counting

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.