Problem Description

There are nn columns of blocks standing in a row. The ii-th column has aia_i blocks in the beginning. Each block has size 1×1×11×1×1. Define (x,y)(x,y) represent the block at column xx and is the yy-th block from bottom to top. You need to support two operations.
1 x y Push one block to the left, that means you choose one block which has no block at its right and make it move to the left. Because of the friction, the block above it will also move to the left, and because the blocks cannot intersect, the block at its left will move to the left either. This will cause a chain reaction. After every block moved, if some blocks hang in the air, then it will fall because of gravitation. Note that the blocks at column 11 can’t move to the left, so if a movement causes a block at column 11 move, you can’t perform this operation.
Formally, let bib_i be the number of blocks in the ii-th column now. If y>bxy>b_x, you will do nothing. Otherwise, you will choose block (x,y)(x,y). There are two stages of the movement of blocks. The first stage is moving. Let ll be the greatest position that satisfies 1l<x1≤l<x and bl<yb_l<y, then you can perform this operation as long as ll exists. Then for all blocks (i,j)(i,j) that satisfy l<ixl<i≤x and jyj≥y, it moves to (i1,j)(i−1,j). The second stage is falling. For blocks (i,j) (j>1)(i,j)\ (j>1) that there are no blocks in (i,j1)(i,j−1), it falls to (i,j1)(i,j−1). Repeat doing it until no blocks satisfy the condition (There is a block in (i,j)(i,j) and no block in (i,j1)(i,j−1)).
Output the number of blocks you have moved in this operation. If y>bxy>b_x or you can’t perform this problem, the answer is 00. It’s not required that y>bx+1y>b_{x+1} in this problem.
image2bb2c8b0608cef90.png imageddad2d624f7313b7.pngimage8100296534558bc3.png
This shows an operation that pushes the block at (6,4)(6,4), and the value of l is 33. The number of blocks moved is 55.
2 x Ask the height of xx-th column now.
You are also asked to output the height of all columns after all operations.

Input

The first line contains an integer T(1T5)T(1≤T≤5) - the number of test cases.
For each test case, the first line contains two integers n,q(1n,q2×105)n,q(1≤n,q≤2×10^5). The second line contains nn integers b1,b2,,bn(1bi109)b_1,b_2,\cdots,b_n(1≤b_i≤10^9). Each of the following qq lines contains an operation: 1 x y (1xn,1y109)(1≤x≤n,1≤y≤10^9), or 2 x (1xn)(1≤x≤n).

Output

For each test case, output one integer in one line for each operation. Then output n integers in one line - the height of all columns after all operations from left to right in order.

Sample Input

1
2
3
4
5
6
7
1
8 4
2 1 1 4 4 6 2 3
1 6 4
2 5
1 1 1
1 8 2

Sample Output

1
2
3
4
5
5
6
0
14
2 2 4 6 3 2 3 1

Translation

nn 列方块依次紧挨着放置,每个方块的大小都为 1×1×11\times 1\times1。定义 (x,y)(x,y) 表示第 xx 列自下而上第 yy 个方块。有以下两种操作。

x y``` 将 $(x,y)$ 向左推一个单位,设当前第 $i$ 列的方块数为 $b_i$,如果 $y>b_x$,那么就什么也不做。由于方块之间挤压产生弹力,位于 $(x,y)$ 左边的一些方块也会被带动。令 $l$ 为满足 $1\le k
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
```2 x``` 输出当前第 $x$ 列的方块个数。  

### Idea
```1 x y``` 是区间偏移操作,```2 x``` 是单点查询,可以考虑用 $\text{Splay}$ 建立一棵区间树维护区间操作。要求每次操作的应答都在 $O(\log n)$ 内完成,总的时间复杂度为 $O((n+q)\log n)$。
操作 ```1 x y```,设当前第 $i$ 列的方块数为 $b_i$,令 $r=x$,并找到 $1\le k<x$ 且 $b_k<y$ 的最大正整数 $k$,令 $l=k+1$,那么 $l\sim r$ 内高度不小于 $y$ 的所有方块都会被移动。首先考虑如何找到满足 $1\le k<x$ 且 $b_k<y$ 的最大整数 $k$。对区间树上的节点 $i$,维护以 $i$ 为根的子树每个节点方块数的最小值 $MinHt_i$,有 {%raw%}$MinHt_i=\min\{b_u|u\in\text{Subtree}(i)\}${%endraw%}。利用 $\text{Splay}$ 的性质,首先将节点 $x$ 伸展到根,自顶向下搜索;如果右儿子 $R$ 的 $MinHt_R<y$,那么 $k$ 必定在右子树中,将当前答案加上左子树大小,继续向右子树搜索;否则,若左儿子 $L$ 的 $b_L<y$,那么 $L$ 就是将当前答案加上 $L$ 左子树的大小加 $1$,就是要找的 $k$,不然的话继续向走儿子搜索。得到 $l$ 后,考虑如何将 $l\sim r$ 之间的内容偏移至 $l-1\sim r-1$。可以发现,移动一个单位后,$l\sim r-1$ 的方块,就是原本 $l+1\sim r$ 的方块;因此,可以看作将 $l$ 删除,然后修改 $l-1$ 的方块数,那么原本 $l+1\sim r$ 就接到了 $l-1$ 上,形成了 $l\sim r-1$ 的节点,接着在 $r$ 的对应节点插入移动后 $r$ 位置的方块数,就完成了区间偏移的操作。接下来考虑如何获得被移动方块的数量。已知移动的是 $l\sim r$ 中高度大于 $y$ 的方块,如果我们知道 $l\sim r$ 的总方块数 $sum(l,r)$,那么答案就为 $sum(l,r)-(r-l+1)\times(y-1)$;不妨令 $sum_i$ 表示以 $i$ 为根节点的子树权值和,那么查询 $l\sim r$ 方块数时,首先将 $l-1$ 伸展至根节点,将 $r+1$ 伸展至 $l-1$ 的右子树,那么 $r+1$ 的左子树就代表区间 $[l,r]$,$l-1$ 左儿子的 $sum$ 值即为 $l\sim r$ 的方块数量和。
对于操作 ```2 x```,直接输出表示第 $x$ 列的节点的权值即可。
### Code
```cpp
/******************************************************************
Copyright: 11D_Beyonder All Rights Reserved
Author: 11D_Beyonder
Problem ID: HDU 6873
Date: 10/13/2020
Description: Splay
*******************************************************************/
#include<iostream>
#include<algorithm>
#include<cstdio>
using namespace std;
typedef long long ll;
const int N=400006;
const ll inf=0x3f3f3f3f3f3f3f3f;
short _;
int n,q;
int father[N];
int son[N][2];
int Size[N];
ll val[N];
ll MinHt[N];
int root;
int b[N];
ll sum[N];
int sz;
inline void init();
void splay(int,int);
int Rank(int);
void rotate(int);
inline bool RightSon(int);
inline void clear(int);
inline int newnode(int,bool,ll);//父亲 儿子 权值
int build(int,int,int);
inline void pushup(int);
inline ll CurHt(int);
ll SUM(int,int);
inline void modify(int,ll);
void move(int,int,ll);
int BigestBoundary(int,ll);
int main(){
for(cin>>_;_;_--){
scanf("%d%d",&n,&q);
int i;
for(i=1;i<=n;i++){
scanf("%d",&b[i]);
}
init();
root=build(1,n,0);
for(i=1;i<=q;i++){
short opt;
ll ans;
int x;
ll y;
scanf("%hd",&opt);
switch(opt){
case 1:
scanf("%d%lld",&x,&y);
if(y>CurHt(x)){
ans=0;
}else{
int r=x;
int l=BigestBoundary(r,y)+1;
if(l==1){
//在最左边,推不动
ans=0;
}else{
ans=SUM(l,r)-1ll*(r-l+1)*(y-1);
//进行move操作
//更改l-1位置的方块数
modify(l-1,CurHt(l-1)+CurHt(l)-(y-1));
if(l==r||l+1==r){
modify(l,CurHt(r));//l位置接上
modify(r,y-1);//r位置顶上清空
}else{
move(l,r,y-1);
}
}
}
break;
case 2:
scanf("%d",&x);
ans=CurHt(x);
break;
}
printf("%lld\n",ans);
}
for(i=1;i<=n;i++){
printf("%lld",CurHt(i));
putchar(i==n?'\n':' ');
}
}
return 0;
}
inline void init(){
root=sz=0;
Size[0]=father[0]=0;
val[0]=MinHt[0]=inf;
}
inline void clear(int x){
sum[x]=son[x][0]=son[x][1]=father[x]=Size[x]=0;
val[x]=MinHt[x]=inf;
}
inline int newnode(int f,bool s,ll v){
++sz;
son[sz][1]=son[sz][0]=0;
father[sz]=f;
val[sz]=v;
son[f][s]=sz;
return sz;
}
inline bool RightSon(int x){
return son[father[x]][1]==x;
}
inline void pushup(int x){
Size[x]=Size[son[x][1]]+Size[son[x][0]]+1;
MinHt[x]=min({val[x],MinHt[son[x][0]],MinHt[son[x][1]]});
sum[x]=sum[son[x][0]]+sum[son[x][1]]+val[x];
}
int build(int l,int r,int rt){
if(l>r){
return 0;
}
int cur=l+r>>1;
int tot=++sz;
val[tot]=b[cur];
father[tot]=rt;
son[tot][0]=build(l,cur-1,tot);
son[tot][1]=build(cur+1,r,tot);
pushup(tot);
return tot;
}
void rotate(int x){
int y=father[x];
int rt=father[y];
//右儿子为x则k为1
//否则x为左儿子 k=0
int k=RightSon(x);
//ZIG 右挂左
//ZAG 左挂右
son[y][k]=son[x][k^1];
father[son[x][k^1]]=y;
son[x][k^1]=y;
//更新
father[y]=x;
father[x]=rt;
if(rt){
son[rt][y==son[rt][1]]=x;
}
pushup(x);
pushup(y);
}
inline ll CurHt(int x){
return val[Rank(x)];
}
void splay(int x,int target){
while(father[x]!=target){
int y=father[x];
int rt=father[y];
if(rt==target){
rotate(x);
}else{
if(RightSon(x)==RightSon(y)){
rotate(y);
}else{
rotate(x);
}
rotate(x);
}
}
if(!target){
root=x;
}
}
int Rank(int p){
int cur=root;
while(1){
if(p<=Size[son[cur][0]]){
cur=son[cur][0];
}else{
p-=Size[son[cur][0]]+1;
if(!p){
return cur;
}else{
cur=son[cur][1];
}
}
}
}
int BigestBoundary(int r,ll h){
int x=Rank(r);
splay(x,0);
int cur=son[x][0];//左儿子编号小于r
int res=0;
while(cur){
if(son[cur][1]&&MinHt[son[cur][1]]<h){
res+=Size[son[cur][0]]+1;
cur=son[cur][1];
}else if(val[cur]<h){
res+=Size[son[cur][0]]+1;
return res;
}else if(son[cur][0]&&MinHt[son[cur][0]]<h){
cur=son[cur][0];
}else{
return 0;
}
}
return 0;
}
ll SUM(int l,int r){
int x=Rank(l-1);
splay(x,0);
if(r==n){
//边界 l-1的右儿子就是l~r
return sum[son[x][1]];
}
int y=Rank(r+1);
//y的左子树是l~r
splay(y,x);
return sum[son[y][0]];
}
//列 高度
inline void modify(int x,ll h){
x=Rank(x);
val[x]=h;
pushup(x);
splay(x,0);
}
//区间 余下的方块
void move(int l,int r,ll left){
int x=Rank(l-1);
splay(x,0);
int y=Rank(l+1);
splay(y,x);
//l在l+1的左子树上
//清空
clear(son[y][0]);
son[y][0]=0;
pushup(y);
int cur=Rank(r-1);
splay(cur,0);
if(r==n){
son[cur][1]=newnode(cur,1,left);
pushup(son[cur][1]);
splay(sz,0);
}else{
int nxt=Rank(r);//r如今已是r-1
splay(nxt,cur);
//锁定位置r
son[nxt][0]=newnode(nxt,0,left);
pushup(son[nxt][0]);
splay(sz,0);
}
}