1287 Cannon
Topic Source: codility
Base time limit: 1 seconds space limit: 131072 KB Score: 40 Difficulty: 4-level algorithm problem collection focus
A positive integer array of length m, which represents the height of the terrain from left to right. Test a cannon, the shells parallel to the ground from left to right to fly, height is h, if the height of a terrain is greater than the height of the projectile flying H (A[i] >= h), the shells will be blocked and fall in I-1 place, then a[i-1] + 1. If H <= a[0], then this shell is invalid, if H > All A[i], this shell is not valid. Now the array B of the given n integers represents the height of the shells and calculates the final topography.
For example: terrain height a = {1, 2, 0, 4, 3, 2, 1, 5, 7}, Shell height B = {2, 8, 0, 7, 6, 5, 3, 4, 5, 6, 5}, the resulting terrain heights are: {2, 2, 2, 4, 3, 3, 5, 6, 7}.
Input
Line 1th: 2 number m, n is separated by a space, the length of the array A and B respectively (1 <= m, n <= 50000)
2nd to M + 1 rows: 1 numbers per line representing the corresponding terrain height (0 <= a[i] <= 1000000).
M + 2 to n + M + 1 lines, 1 numbers per line, indicating the height of the projectile (0 <= b[i] <= 1000000).
Output
Outputs a total of M rows, one number per line, corresponding to the final terrain height.
Input Example
9
1
2
0
4
3
2
1
5
7
2
8
0
7
6 br> 5
3
4
5
6
5
Output Example
2
2
2
4
3
3
5
6
7
Their line of tree good dishes, the problem has an idea, but suffering from the line of trees to achieve their own lack of ability, has not written .... Yesterday when the match a line tree water problem, fortunately, teammates help change a bit, change the process, it seems to really understand a bit of line tree, and then do a bit of the previous did not do this problem, really over the ah ....
Ideas:
It is better to change the previous point of the larger node than the current one.
See the code in detail:
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> using namespace
Std
const int maxn=100000+100;
#define Lson rt<<1,l,mid #define Rson rt<<1|1,mid+1,r int DIXING[MAXN]; struct node {int l,r,val;}
TREE[MAXN*4]; void pushup (int rt) {Tree[rt].val=max (tree[rt<<1].val,tree[rt<<1|1].val);} void Build (int rt,int l,int R
) {tree[rt].l=l;
Tree[rt].r=r;
if (l==r) {tree[rt].val=dixing[l];
Return
} int mid= (L+R)/2;
Build (Lson);
Build (Rson);
Pushup (RT);
} void Xin (int rt,int l,int R) {if (tree[rt].l==l&&tree[rt].r==r) {tree[rt].val+=1;
Return
} int mid= (TREE[RT].L+TREE[RT].R)/2;
if (l>mid) {xin (rt<<1|1,l,r);
} else {xin (rt<<1,l,r);
} pushup (RT);
} void Update (int rt,int W) {if (TREE[RT].L==TREE[RT].R) {dixing[tree[rt].l-1]+=1; printf ("1One:%d\n ", TREE[RT].L-1);
Xin (1,TREE[RT].L-1,TREE[RT].L-1);
return;
} if (tree[rt<<1].val>=w) {update (RT<<1,W);
} else {update (RT<<1|1,W);
} pushup (RT);
} int main () {int n,m;
scanf ("%d%d", &n,&m);
int i,j;
for (i=1;i<=n;i++) {scanf ("%d", &dixing[i]);
} build (1,1,n);
while (m--) {int xx;
scanf ("%d", &xx); if (xx<=dixing[1]| |
Xx>tree[1].val) continue;
Update (1,XX);
printf ("%d 111\n", ans);
Xin (1,ans,ans);
} for (i=1;i<=n;i++) {printf ("%d\n", Dixing[i]); }
}