QOJ.ac

QOJ

IDSubmission IDProblemHackerOwnerResultSubmit timeJudge time
#1200#763412#6990. Maximum Element In A Stackhejinming983282hejinming983282Failed.2024-11-19 20:08:552024-11-19 20:08:55

Details

Extra Test:

Invalid Input

input:

1
0 0 0 0 0

output:


result:

FAIL Integer parameter [name=n] equals to 0, violates the range [1, 5000000] (stdin, line 2)

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#763412#6990. Maximum Element In A Stackhejinming983282#AC ✓1848ms23496kbC++232.2kb2024-11-19 20:08:232024-11-19 20:08:23

answer

#include <bits/stdc++.h>
using namespace std;

typedef unsigned int uint;
typedef unsigned long long ull;

// Define the maximum possible n per test case
const int MAX_N = 5000000;

// Global array for stack_max
uint stack_max_arr[5000000];

int main(){
    int T;
    scanf("%d", &T);
    for(int test_case=1; test_case <= T; test_case++){
        // Read n, p, q, m, SA, SB, SC
        int n;
        uint p, q, m;
        uint SA, SB, SC;
        scanf("%d %u %u %u %u %u %u", &n, &p, &q, &m, &SA, &SB, &SC);
        
        int stack_size =0;
        ull y =0;
        
        for(int i=1; i<=n; i++){
            // First rng61() for operation decision
            SA ^= (SA << 16);
            SA ^= (SA >> 5);
            SA ^= (SA << 1);
            uint t = SA;
            SA = SB;
            SB = SC;
            SC ^= t ^ SA;
            uint op_rng = SC;
            uint op_decision = op_rng % (p + q);
            
            if(op_decision < p){
                // PUSH operation, need to generate value
                SA ^= (SA << 16);
                SA ^= (SA >> 5);
                SA ^= (SA << 1);
                t = SA;
                SA = SB;
                SB = SC;
                SC ^= t ^ SA;
                uint val_rng = SC;
                uint v = (val_rng % m) +1;
                uint new_max;
                if(stack_size >0){
                    if(v > stack_max_arr[stack_size -1]){
                        new_max = v;
                    }
                    else{
                        new_max = stack_max_arr[stack_size -1];
                    }
                }
                else{
                    new_max = v;
                }
                stack_max_arr[stack_size] = new_max;
                stack_size++;
            }
            else{
                // POP operation
                if(stack_size >0){
                    stack_size--;
                }
                // else do nothing
            }
            // After operation, get a_i
            uint a_i = (stack_size >0) ? stack_max_arr[stack_size -1] : 0;
            y ^= ((ull)i) * ((ull)a_i);
        }
        printf("Case #%d: %llu\n", test_case, y);
    }
    return 0;
}