DSAhardArrays~40m
chmod -R
Problem
You are building a Windows file system permission manager. Files are indexed 0 to n-1, each with a permission value. The system must support two operations efficiently:
["update", l, r, val]: Set the permission of all files in range[l, r]toval(bulk chmod).["query", l, r]: Return the sum of permissions of all files in range[l, r](audit report).
Both operations must run in O(log n) time. A naive O(n) scan per operation will time out on large file systems.
Given the initial permission array and a list of operations, return the results. Update operations return null.
Examples
Example 1
Input:
(5, [1,2,3,4,5], [["update",0,4,7],["query",0,4]])Output:
[null, 35]Set all 5 files to permission 7. Sum = 7×5 = 35.
Example 2
Input:
(5, [5,5,5,5,5], [["update",1,3,0],["query",0,4]])Output:
[null, 10]Set files 1-3 to permission 0. Remaining: files 0 and 4 still have permission 5. Sum = 5+0+0+0+5 = 10. Critical: lazy=0 must not be treated as no-op.
Example 3
Input:
(5, [1,2,3,4,5], [["update",0,4,10],["update",1,3,20],["update",2,2,30],["query",0,4]])Output:
[null, null, null, 90]Stacked updates: final array is [10,20,30,20,10]. Sum = 90. Tests propagation order.
Constraints
- ›1 <= n <= 10^5
- ›0 <= initial[i] <= 100
- ›0 <= val <= 100
- ›0 <= l <= r < n
- ›At most 10^4 operations
Loading question…
Related DSA questions
← Back homemockbit.io/q/116