/ SeriousOJ /

Record Detail

Accepted


  
# Status Time Cost Memory Cost
#1 Accepted 1ms 532.0 KiB
#2 Accepted 1ms 324.0 KiB
#3 Accepted 1ms 532.0 KiB
#4 Accepted 1ms 328.0 KiB
#5 Accepted 3ms 536.0 KiB
#6 Accepted 4ms 532.0 KiB
#7 Accepted 4ms 320.0 KiB
#8 Accepted 4ms 324.0 KiB
#9 Accepted 4ms 532.0 KiB
#10 Accepted 4ms 532.0 KiB
#11 Accepted 4ms 532.0 KiB
#12 Accepted 4ms 324.0 KiB
#13 Accepted 4ms 532.0 KiB
#14 Accepted 5ms 532.0 KiB
#15 Accepted 4ms 532.0 KiB
#16 Accepted 4ms 532.0 KiB
#17 Accepted 4ms 320.0 KiB
#18 Accepted 4ms 532.0 KiB
#19 Accepted 4ms 532.0 KiB
#20 Accepted 4ms 532.0 KiB
#21 Accepted 4ms 532.0 KiB
#22 Accepted 4ms 532.0 KiB
#23 Accepted 4ms 532.0 KiB
#24 Accepted 2ms 532.0 KiB
#25 Accepted 2ms 532.0 KiB
#26 Accepted 2ms 544.0 KiB
#27 Accepted 2ms 532.0 KiB
#28 Accepted 2ms 532.0 KiB
#29 Accepted 2ms 484.0 KiB
#30 Accepted 2ms 532.0 KiB
#31 Accepted 2ms 532.0 KiB
#32 Accepted 3ms 532.0 KiB
#33 Accepted 3ms 532.0 KiB
#34 Accepted 4ms 532.0 KiB
#35 Accepted 5ms 524.0 KiB
#36 Accepted 4ms 532.0 KiB
#37 Accepted 4ms 532.0 KiB
#38 Accepted 4ms 452.0 KiB
#39 Accepted 5ms 532.0 KiB
#40 Accepted 4ms 324.0 KiB
#41 Accepted 4ms 324.0 KiB
#42 Accepted 4ms 532.0 KiB
#43 Accepted 5ms 324.0 KiB
#44 Accepted 4ms 532.0 KiB
#45 Accepted 5ms 392.0 KiB
#46 Accepted 4ms 532.0 KiB
#47 Accepted 4ms 324.0 KiB
#48 Accepted 4ms 532.0 KiB
#49 Accepted 5ms 484.0 KiB
#50 Accepted 5ms 532.0 KiB
#51 Accepted 4ms 324.0 KiB
#52 Accepted 4ms 532.0 KiB
#53 Accepted 2ms 532.0 KiB
#54 Accepted 2ms 532.0 KiB
#55 Accepted 3ms 532.0 KiB
#56 Accepted 5ms 380.0 KiB
#57 Accepted 4ms 320.0 KiB
#58 Accepted 4ms 532.0 KiB
#59 Accepted 4ms 532.0 KiB
#60 Accepted 4ms 536.0 KiB
#61 Accepted 4ms 532.0 KiB
#62 Accepted 4ms 532.0 KiB
#63 Accepted 4ms 324.0 KiB
#64 Accepted 4ms 532.0 KiB
#65 Accepted 4ms 532.0 KiB
#66 Accepted 4ms 532.0 KiB
#67 Accepted 4ms 532.0 KiB
#68 Accepted 4ms 532.0 KiB
#69 Accepted 4ms 436.0 KiB
#70 Accepted 4ms 544.0 KiB
#71 Accepted 4ms 324.0 KiB
#72 Accepted 4ms 532.0 KiB
#73 Accepted 4ms 532.0 KiB
#74 Accepted 4ms 532.0 KiB
#75 Accepted 4ms 532.0 KiB
#76 Accepted 4ms 532.0 KiB
#77 Accepted 4ms 532.0 KiB
#78 Accepted 4ms 564.0 KiB
#79 Accepted 5ms 532.0 KiB
#80 Accepted 4ms 532.0 KiB
#81 Accepted 4ms 324.0 KiB
#82 Accepted 4ms 532.0 KiB
#83 Accepted 4ms 532.0 KiB
#84 Accepted 5ms 764.0 KiB
#85 Accepted 4ms 532.0 KiB
#86 Accepted 4ms 484.0 KiB
#87 Accepted 4ms 532.0 KiB
#88 Accepted 4ms 320.0 KiB
#89 Accepted 4ms 320.0 KiB
#90 Accepted 4ms 532.0 KiB
#91 Accepted 4ms 532.0 KiB
#92 Accepted 24ms 996.0 KiB
#93 Accepted 13ms 1.066 MiB
#94 Accepted 13ms 1.086 MiB
#95 Accepted 13ms 1.023 MiB
#96 Accepted 13ms 1.02 MiB
#97 Accepted 178ms 12.262 MiB
#98 Accepted 176ms 12.316 MiB
#99 Accepted 187ms 12.246 MiB
#100 Accepted 176ms 12.312 MiB

Code

#include <bits/stdc++.h>
using namespace std;
 
struct Node {
    int L, R;       // segment range (0-indexed)
    int left, right; // indices of children in the tree vector (-1 if leaf)
    int dp[2];      // dp[0] if starting with minimizer, dp[1] if starting with maximizer
};
 
int N; // size of the array
vector<int> A; // the array (0-indexed)
vector<Node> tree; // our game tree

// Build the tree for segment [l, r] (0-indexed)
// Returns the index of the node in the tree vector.
int buildTree(int l, int r) {
    int idx = tree.size();
    tree.push_back({l, r, -1, -1, {0, 0}});
    if(l == r) {
        tree[idx].dp[0] = A[l];
        tree[idx].dp[1] = A[l];
        return idx;
    }
    int len = r - l + 1;
    int leftSize = len / 2;  // floor division
    int leftL = l, leftR = l + leftSize - 1;
    int rightL = l + leftSize, rightR = r;
    int leftIdx = buildTree(leftL, leftR);
    int rightIdx = buildTree(rightL, rightR);
    tree[idx].left = leftIdx;
    tree[idx].right = rightIdx;
    // Combine children: note the turn switches for the remaining segment.
    tree[idx].dp[0] = min(tree[leftIdx].dp[1], tree[rightIdx].dp[1]);
    tree[idx].dp[1] = max(tree[leftIdx].dp[0], tree[rightIdx].dp[0]);
    return idx;
}
 
// Update the tree: update position pos (0-indexed) to new value 'val'
void updateTree(int idx, int pos, int val) {
    Node &node = tree[idx];
    if(pos < node.L || pos > node.R) return;
    if(node.L == node.R) {
        node.dp[0] = val;
        node.dp[1] = val;
        return;
    }
    // Recurse to children.
    updateTree(tree[idx].left, pos, val);
    updateTree(tree[idx].right, pos, val);
    // Recompute current node's dp values.
    node.dp[0] = min(tree[node.left].dp[1], tree[node.right].dp[1]);
    node.dp[1] = max(tree[node.left].dp[0], tree[node.right].dp[0]);
}
 
int main(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    
    int Q;
    cin >> N >> Q;
    A.resize(N);
    for (int i = 0; i < N; i++){
        cin >> A[i];
    }
    
    // Build the game tree over the whole array [0, N-1].
    tree.reserve(2 * N);  // worst-case size is O(N)
    int root = buildTree(0, N-1);
    
    // Process queries.
    // Each query is given as: i v p 
    // where 1<=i<=N; update A[i-1] to v and then, if p==0 then Thakur (minimizer) starts, 
    // if p==1 then Roy (maximizer) starts.
    while(Q--){
        int i, v, p;
        cin >> i >> v >> p;
        i--; // convert to 0-indexed
        // Update the array and the game tree.
        A[i] = v;
        updateTree(root, i, v);
        // Answer is stored in tree[root].dp[p]
        cout << tree[root].dp[p] << "\n";
    }
    
    return 0;
}

Information

Submit By
Type
Submission
Problem
P1169 Thakur vs Roy again
Language
C++17 (G++ 13.2.0)
Submit At
2025-03-27 20:36:15
Judged At
2025-03-27 20:36:15
Judged By
Score
100
Total Time
187ms
Peak Memory
12.316 MiB