Triangular Region Updates with Differential Arrays and Game Strategy Optimization

Triangular Region Updates

Grid operations involve updating triangular regions using a combination of difference arrays. Each operation adds value s to a right triangle starting at coordinates (x,y) with leg length l. The solution employs two difference arrays:

  1. Triangular difference array to handle the primary region
  2. Rectnagular difference array to correct overcounted areas

Update procedure:

  1. Add s at (x,y) in the triangular difference array
  2. Subtract s at (x+l,y+l) in the triangular difference array
  3. Subtract s at (x+l,y) in the rcetangular difference array
  4. Add s at (x+l,y+l) in the rectangular difference array

Prefix sums are computed as follows:

#include <iostream>
using namespace std;

const int MAX_SIZE = 1005;
long long tri_diff[MAX_SIZE][MAX_SIZE];
long long rect_diff[MAX_SIZE][MAX_SIZE];
long long tri_psum[MAX_SIZE][MAX_SIZE];
long long rect_psum[MAX_SIZE][MAX_SIZE];

void apply_update(int row, int col, int len, int val) {
    tri_diff[row][col] += val;
    if (row + len <= n && col + len <= n) 
        tri_diff[row + len][col + len] -= val;
    if (row + len <= n) 
        rect_diff[row + len][col] -= val;
    if (row + len <= n && col + len <= n) 
        rect_diff[row + len][col + len] += val;
}

void compute_sums(int size) {
    for (int i = 1; i <= size; i++) {
        for (int j = 1; j <= size; j++) {
            rect_psum[i][j] = rect_diff[i][j] 
                + rect_psum[i - 1][j] 
                + rect_psum[i][j - 1] 
                - rect_psum[i - 1][j - 1];
        }
    }
    
    for (int j = 1; j <= size; j++) {
        long long col_sum = 0;
        for (int i = 1; i <= size; i++) {
            col_sum += tri_diff[i][j];
            tri_psum[i][j] = tri_psum[i - 1][j - 1] + col_sum;
        }
    }
}

Final grid values are obtained by combining both prefix sums. Time complexity is O(N²) for an N×N grid.

Game Strategy Optimization

Two players alternately remove elements from a set based on divisibility by given values. Players alternate between maximizing (Alice) and minimizing (Bob) the final sum.

Key observations:

  • Each operation splits the current set into divisible and non-divisible subsets
  • Players' strategies: Alice maximizes outcome, Bob minimizes
  • Critical threshold: When operations exceed 2·log₂(n), result must be 0

Optimized solution:

#include <iostream>
#include <vector>
using namespace std;

long long simulate(int step, vector<long long> current, 
                   const vector<long long>& divisors, int total_steps) {
    if (step >= total_steps) {
        long long total = 0;
        for (auto num : current) total += num;
        return total;
    }
    if (current.empty()) return 0;
    
    vector<long long> divisible;
    vector<long long> non_divisible;
    for (auto num : current) {
        if (num % divisors[step] == 0) 
            divisible.push_back(num);
        else 
            non_divisible.push_back(num);
    }
    
    long long result1 = simulate(step + 1, divisible, divisors, total_steps);
    long long result2 = simulate(step + 1, non_divisible, divisors, total_steps);
    
    return (step % 2 == 0) ? max(result1, result2) : min(result1, result2);
}

Complexity analysis: Each operation level processes O(N) elements total, resulting in O(N·M) time. The logarithmic threshold optimization reduces worst-case complexity.

Tags: differential_array prefix_sum game_theory depth_first_search complexity_analysis

Posted on Wed, 12 Aug 2026 16:06:54 +0000 by aveach