BZOJ3747 POI2015 Kinoman

Source: Internet
Author: User

Description

A total of M films, numbered 1~m, the good-looking value of Part I movies is WI w_i. In n days (numbered from 1~n) a movie is shown every day, and the first day of the show is part fi f_i.

You can choose L,r (1<=l<=r<=n) and watch all the movies in l,l+1,..., R days. If you watch more than once in the same movie, you'll be bored and you won't be able to get a good look at the movie. So the sum of the good looking values of the movie you want to maximize and watch only once.

Input Line 1: Two integer n,m (1<=m<=n<=1000000). Line 2: contains n integers f1,f2,..., fn f_1,f_2,\ldots,f_n (1≤fi≤m) (1\le f_i\le m). Line 3: contains m integers w1,w2,..., wm w_1,w_2,\ldots,w_m (1≤wj≤106) (1\le w_j \le10^6). Data range:
There are 20% data, n≤8000, 70% data, n≤100000, 100% data, n≤1000000.

Output

Output the maximum value of the sum of the good-looking values of the movie viewed and viewed only once .

Sample Input

9 4
2 3 1 1 4 1 2 4 1
5 3 6 6

Sample Output

15

Input Details

Watch 2nd, 3, ... the movie that was shown in \ldots,7 days, with the number of movies seen and seen only once is 2,3,4.

Solution:
The focus is on the use of the following sentence: "If you watch the same movie more than once, you will not be able to get the beautiful value of the film." ”

That is, for a selected interval, the weight of the element cannot be collected if there is a repetition of the element. Generally encountered "The problem of element not repeating within interval" We only need to judge the relationship between the current element and the next identical element . If the current element is in POS and the next same element is in Nxt[pos], then for all L<pos L =nxt[pos], the interval cannot get the Val value. If Pos<=r<nxt[pos] POS, the interval can be given a Val value.

That is, for the interval [pos,nxt[pos]−1] [pos,nxt[pos]-1] This interval prefix and will add a Val value. If we define a data structure that maintains a prefix that begins with the enumeration's L l as the end of the current position , the above operation is to update the interval. Obviously, those elements that are already smaller than l continue to go backwards: [pos,nxt[pos]−1]→[nxt[pos],nxt[nxt[pos]]−1] [Pos,nxt[pos]-1]\to[nxt[pos],nxt[nxt[pos]] -1] So the subject uses a line tree to maintain this relationship, split the time Complexity O (NLOGN)

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.