hoangi19
Senior Member
đúng là debug lòi trĩ thật.
C#:
public class CustomStack {
public int[] Stack;
public int Index;
public int[] Lazy;
public int MaxSize;
public CustomStack(int maxSize) {
Stack = new int[maxSize];
Index = -1;
Lazy = new int[maxSize];
MaxSize = maxSize;
}
public void Push(int x) {
// Console.WriteLine("xx{0} , {1} , {2}", Index, MaxSize, x);
if (Index+1 < MaxSize) Stack[++Index] = x;
// Console.WriteLine("xxx{0} , {1} , {2}", Index, MaxSize, x);
}
public int Pop() {
if (Index < 0) return -1;
// Console.WriteLine("yy{0} , {1}", Index, MaxSize);
int result = Stack[Index] + Lazy[Index];
// Console.WriteLine("yyy{0} , {1} , {2}", Index, MaxSize, result);
if (Index > 0) {
Lazy[Index - 1] += Lazy[Index];
}
Lazy[Index] = 0;
// Console.WriteLine("yyyy{0} , {1} , {2}", Index, MaxSize, result);
Index--;
return result;
}
public void Increment(int k, int val) {
int maxK = Math.Min(k, Index+1);
// Console.WriteLine("kkk{0}, {1}, {2}, {3}", k, Index+1, maxK, val);
if (maxK > 0) Lazy[--maxK] += val;
// Console.WriteLine("kkkk{0}, {1}, {2}, {3}", k, Index+1, maxK, Lazy[maxK]);
}
}
/**
* Your CustomStack object will be instantiated and called as such:
* CustomStack obj = new CustomStack(maxSize);
* obj.Push(x);
* int param_2 = obj.Pop();
* obj.Increment(k,val);
*/

