Knapsack and Queries

아직 제출이 없습니다시간 제한10초메모리 제한1024 MB

문제

First, you are given the positive integer MODMOD.

You have a knapsack, that is empty at first.

You have to perform QQ queries.

  • In each query, you have first to perform either ADD or REMOVE operation, and then perform FIND.
  • For ADD operation, you are given positive integers ww and vv. You put the cookie with weight ww and value vv to the knapsack.
  • For REMOVE operation, you take out the cookie with smallest weight from the knapsack and eat it.
  • After each ADD or REMOVE you perform FIND operation: given positive integers ll and rr, you answer the follow question: can you choose cookies from that knapsack so that the l(XmodMOD)rl \leq (X \bmod MOD) \leq r (XX is sum of the weight of the selected cookies)?. If you can't, output 1-1. Otherwise output the maximum sum of the value of the selected cookies.

입력

Input is given in the following format:

MODMOD

QQ

t_1t'\_1 w_1w'\_1 v_1v'\_1 l_1l'\_1 r_1r'\_1

t_2t'\_2 w_2w'\_2 v_2v'\_2 l_2l'\_2 r_2r'\_2

\ldots

t_Qt'\_Q w_Qw'\_Q v_Qv'\_Q l_Ql'\_Q r_Qr'\_Q

Queries are encrypted as described later. You can get t_i,w_i,v_i,l_i,r_it\_i, w\_i, v\_i, l\_i, r\_i by decryption t_i,w_i,v_i,l_i,r_it'\_i, w'\_i, v'\_i, l'\_i, r'\_i.

You may assume that 1w_i,v_i1091 \leq w\_i, v\_i \leq 10^9, 0l_ir_iMOD10 \leq l\_i \leq r\_i \leq MOD-1, t_i=1t\_i=1 for ADD+FIND query, t_i=2t\_i=2 for REMOVE+FIND query (and in this case w_i=v_i=0w\_i=v\_i=0), that the cookie, which is given by ADD, is heavier than any cookies which added by the previous ADD, and that when executing REMOVE, the knapsack isn't empty.

We prepared the decryption code with C++11 (or later), Java, D, C#. Use class Crypto for decryption. The code of class Crypto and examples of its usage can be uploaded from crypto.zip for C++11 (or later), Java, D, C#. 

Here is the example for C++:

#include <cstdint> //uint8_t, uint32_t
 
class Crypto {
public:    
    Crypto() {
        sm = cnt = 0;
        seed();
    }
    
    int decode(int z) {
        z ^= next();
        z ^= (next() << 8);
        z ^= (next() << 16);
        z ^= (next() << 22);
        return z;
    }
 
    void query(long long z) {
        const long long B = 425481007;
        const long long MD = 1000000007;
        cnt++;
        sm = ((sm * B % MD + z) % MD + MD) % MD;
        seed();
    }
private: 
    long long sm;
    int cnt;
 
    uint8_t data[256];
    int I, J;
 
    void swap_data(int i, int j) {
        uint8_t tmp = data[i];
        data[i] = data[j];
        data[j] = tmp;    
    }
 
    void seed() {
        uint8_t key[8];
        for (int i = 0; i < 4; i++) {
            key[i] = (sm >> (i * 8));
        }
        for (int i = 0; i < 4; i++) {
            key[i+4] = (cnt >> (i * 8));
        }
 
        for (int i = 0; i < 256; i++) {
            data[i] = i;
        }
        I = J = 0;
 
        int j = 0;
        for (int i = 0; i < 256; i++) {
            j = (j + data[i] + key[i%8]) % 256;
            swap_data(i, j);
        }
    }
 
    uint8_t next() {
        I = (I+1) % 256;
        J = (J + data[I]) % 256;
        swap_data(I, J);
        return data[(data[I] + data[J]) % 256];
    }
};

The decryption process works in the next way: 

  • First, you make the instance of class Crypto.
  • For each query first call the decode function in order of t,w,v,l,rt', w', v', l', r'. The return values are t,w,v,l,rt, w, v, l, r. Then perform the query and call the query function with the result of the FIND.

The sample C++ code:

#include <cstdio>
#include <cstdlib>
#include <cstdint> //uint8_t, uint32_t

class Crypto {
    ...
};

int main() {
    int MOD, Q;
    scanf("%d %d", &MOD, &Q);
    Crypto c;
    for (int i = 0; i < Q; i++) {
        int t, w, v, l, r;
        scanf("%d %d %d %d %d", &t, &w, &v, &l, &r);
        t = c.decode(t);
        w = c.decode(w);
        v = c.decode(v);
        l = c.decode(l);
        r = c.decode(r);
        if (t == 1) {
            (add candy(w, v))
        } else {
            (delete candy)
        }
        long long ans = (answer for query(l, r));
        c.query(ans);
        printf("%lld\n", ans);
    }
}

Note that class Crypto consume time about 200200 to process Q=100,000Q = 100\\,000.

출력

For each query print the result of FIND operation.

제한

0t_i,w_i,v_i,l_i,r_123010 \leq t'\_i, w'\_i, v'\_i, l'\_i, r'\_1 \leq 2^{30} - 1, 2MOD5002 \leq MOD \leq 500, 1Q100,0001 \leq Q \leq 100\\,000.

힌트

The result of decoding Sample 1:

10
7
1 5 10 5 5
2 0 0 0 9
1 7 10 2 4
1 12 11 9 9
2 0 0 1 1
1 22 10 2 3
1 32 100 4 4

The result of decoding Sample 2:

7
20
1 5 44 0 1
1 11 90 0 3
2 0 0 3 4
1 18 68 1 6
1 25 32 2 3
1 31 22 2 3
1 32 26 1 5
1 36 31 3 6
2 0 0 2 5
1 43 10 3 6
2 0 0 5 6
2 0 0 3 4
2 0 0 2 4
2 0 0 1 5
2 0 0 3 5
1 49 48 0 4
2 0 0 1 5
1 50 36 0 6
1 56 48 3 5
1 59 17 3 5