"bzoj4627" [BeiJing2016] rotary sushi discretization + tree-like array

Source: Internet
Author: User

Title Description

A sequence of length n is given, and the number of successive sub-sequences of all elements and in the [L,r] range is obtained.

Input

The first line contains three integers n,l and R, each representing the number of sushi trays, the lower and upper limits of satisfaction. The second line contains n integer AI, which represents the satisfaction of small z on sushi. n≤100000,| Ai|≤100000,0≤l, R≤10^9

Output

Only one row, which contains an integer, indicates how many choices are available to make the sum of the satisfaction of small z not less than L and not higher than R.

Sample input

5 5 9
1 2 3) 4 5

Sample output

6

Exercises

Discretization + Tree-like array

Subtract the sequence from the conversion prefix, which is the number of $x>y$ that satisfies the $l\le sum[x]-sum[y]\le R.

Then we enumerate the $x$, we can get the range of $y$, which requires the number of $y$ of the previous satisfying condition. You can maintain a tree array of 1 to the current location, query the number in the tree array, and then add that number to the tree array. Because of the large data range, discretization is required.

Time Complexity $o (n\log N) $

#include <cstdio> #include <algorithm> #define N 100010#define now v + 1, v + N + 2using namespace Std;typedef Long Long ll;ll sum[n], V[n];int f[n], n;inline void Add (int x) {    int i;    for (i = x; I <= n + 1; i + = i & i) f[i] + +;} inline int query (int x) {    int i, ans = 0;    for (i = x; i; i-= i & i) ans + = f[i];    return ans;} int main () {    int i;    ll L, r, ans = 0;    scanf ("%d%lld%lld", &n, &l, &r);    for (i = 1; I <= n; i + +) scanf ("%lld", &sum[i]), sum[i] + = Sum[i-1], v[i] = sum[i];    Sort (now);    for (i = 0; I <= n; i + +) ans + = query (Upper_bound (now, Sum[i] – L)-V-1)-Query (Lower_bound (now, Sum[i]-R)- V-1), add (Lower_bound (now, sum[i])-V);    printf ("%lld\n", ans);    return 0;}

"bzoj4627" [BeiJing2016] rotary sushi discretization + tree-like array

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.