Cattle-off practice match continuous section 14 B range (multiplication)

Links: https://ac.nowcoder.com/acm/contest/82/B
Source: Cattle-off network

Continuous segment interval
Time limit: C / C ++ 7 seconds and 14 seconds in other languages
to space constraints: C / C ++ 262144K, other languages 524288K
64bit IO the Format:% LLD

Title Description

To give you a sequence of length n, a and a constant k

There are m times query, each query within an interval [l, r] all data into the minimum number of consecutive segments, and each segment that are <= k

If a query no solution, output "Chtholly"

Enter a description:

The first row of three numbers n, m, K 
the number of the second row and n represents a sequence
after m rows, each row represents a given query lr two numbers

Output Description:

M output lines, each an integer that represents the answer
Example 1

Entry

copy
5 5 7
2 3 2 3 4
3 3
4 4
5 5
1 5
2 4

Export

copy
1
1
1
2
2

Remarks:

To 100% of the data,. 1 <= n-, m <= 1000000,. 1 <= A I , K <= 1000000000

 

Meaning of the questions:

To give you an array, and an integer k, q times and ask, every time you ask for a l, r

You ask the minimum number of array is divided into contiguous portions, so that each successive sum is less than equal to k.

 

Ideas:

Two 229-fold thought,

Since n <= 1e6 so we only need to open the array multiplier 20, because 20 is << 1 1048576> 1e6

We open the array multiplier st [i] [j] denotes the i-th number farthest away from the right to the 1 << j,

So we safeguard this information for each inquiry, we only need to solve the greedy.

lg [i] representative of log2 (i) can be proved from l to r, we can (1) is applied to select some number of log2 (rl + 1), log2 (rl), log2 (rl-1) ... log2 on arrival.

So we only need to make current pos greedy right log2 (r-l + 1) is far less if R, is moved if less, then finer (smaller) moves one step. Can, and finally moved to the R is determined whether to determine what can be output.

See details Code:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <vector>
#include <iomanip>
#define ALL(x) (x).begin(), (x).end()
#define rt return
#define dll(x) scanf("%I64d",&x)
#define xll(x) printf("%I64d\n",x)
#define sz(a) int(a.size())
#define all(a) a.begin(), a.end()
#define rep(i,x,n) for(int i=x;i<n;i++)
#define repd(i,x,n) for(int i=x;i<=n;i++)
#define pii pair<int,int>
#define pll pair<long long ,long long>
#define gbtb ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define MS0(X) memset((X), 0, sizeof((X)))
#define MSC0(X) memset((X), '\0', sizeof((X)))
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define eps 1e-6
#define gg(x) getInt(&x)
#define db(x) cout<<"== [ "<<x<<" ] =="<<endl;
using namespace std;
typedef long long ll;
ll gcd(ll a, ll b) {return b ? gcd(b, a % b) : a;}
ll lcm(ll a, ll b) {return a / gcd(a, b) * b;}
ll powmod(ll a, ll b, ll MOD) {ll ans = 1; while (b) {if (b % 2)ans = ans * a % MOD; a = a * a % MOD; b /= 2;} return ans;}
inline void getInt(int* p);
const int maxn = 1000010;
const int inf = 0x3f3f3f3f;
/*** TEMPLATE CODE * * STARTS HERE ***/
ll n, m, k;
ll a[maxn];
ll lg[maxn];
int st[maxn][21];

int main()
{
    //freopen("D:\\common_text\\code_stream\\in.txt","r",stdin);
    //freopen("D:\\common_text\\code_stream\\out.txt","w",stdout);
    gbtb;
    cout << (1 << 20) << endl;
    cin >> n >> m >> k;
    repd(i, 1, n)
    {
        cin >> a[i];
    }
    repd(i, 1, n)
    {
        lg[i] = lg[i / 2] + 1;
    }

    repd(i, 1, n)
    {
        a[i] += a[i - 1];
    }
    int pos;
    repd(i, 0, 20)
    {
        st[n + 1][i] = n + 1;
    }
    for (int i = n; i >= 1; i--)
    {
        pos = upper_bound(a + 1, a + 1 + n, a[i - 1] + k) - a;
        st[i][0] = pos;
        repd(j, 1, 20)
        {
            st[i][j] = st[st[i][j - 1]][j - 1];
        }
    }
    int l, r;
    repd(t, 1, m)
    {
        cin >> l >> r;
        pos = l;
        int ans = 0;
        for (int j = lg[r - l + 1]; j >= 0; j--)
        {
            if (st[pos][j] <= r)
            {
                pos = st[pos][j];
                ans += (1 << j);
            }
        }
        if (st[pos][0] <= r)
        {
            cout << "Chtholly" << endl;
        } else
        {
            cout << ans + 1 << endl;
        }
    }


    return 0;
}

inline void getInt(int* p) {
    char ch;
    do {
        ch = getchar();
    } while (ch == ' ' || ch == '\n');
    if (ch == '-') {
        *p = -(getchar() - '0');
        while ((ch = getchar()) >= '0' && ch <= '9') {
            *p = *p * 10 - ch + '0';
        }
    }
    else {
        *p = ch - '0';
        while ((ch = getchar()) >= '0' && ch <= '9') {
            *p = *p * 10 + ch - '0';
        }
    }
}

 

 

 

Guess you like

Origin www.cnblogs.com/qieqiemin/p/10960912.html