#include<bits/stdc++.h> #define lim 500009 #define pot (1<<19) #define linf (1ll<<60) using namespace std; int n; long long mx[pot*2+9],d[pot*2+9],l[pot*2+9],r[pot*2+9]; void add(int i,int ll,int rr,long long x) { //printf("%d %d %d %lld %lld\n", i, ll, rr, l[i], r[i]); if((ll<=l[i])&&(r[i]<=rr)) { d[i] += x; mx[i] += x; //printf("%d\n", i); } else { if((ll<=r[i])&&(l[i]<=rr)) { add((i<<1), ll, rr, x); add(1+(i<<1), ll, rr, x); mx[i] = d[i]+max(mx[i<<1],mx[(i<<1)+1]); } } } int fnd(long long x) { int i = 1; while(i<pot) { mx[(i<<1)] += d[i]; d[(i<<1)] += d[i]; mx[(i<<1)+1] += d[i]; d[(i<<1)+1] += d[i]; d[i] = 0; if(mx[(i<<1)+1]>=x) { i = (i<<1)+1; } else { i <<= 1; } } return i-pot; } void t_set(int i,int a,int x) { if(i>=pot) { mx[i] = x; } else { mx[(i<<1)] += d[i]; d[(i<<1)] += d[i]; mx[(i<<1)+1] += d[i]; d[(i<<1)+1] += d[i]; d[i] = 0; if(a<=r[i<<1]) { t_set((i<<1),a,x); } else { t_set((i<<1)+1,a,x); } mx[i] = max(mx[(i<<1)],mx[(i<<1)+1]); } } int main() { scanf("%d", &n); for(int i = pot*2;i>=pot;--i) { mx[i] = -linf; l[i] = i-pot; r[i] = i-pot; } mx[pot] = 0; for(int i = pot-1;i;--i) { mx[i] = mx[i<<1]; l[i] = l[i<<1]; r[i] = r[1+(i<<1)]; } //printf("%lld %lld\n", l[1], r[1]); for(int i = 0;i<n;++i) { int a; scanf("%d", &a); add(1,l[1],r[1],a); //printf("X"); int b = fnd(a); //printf("%d %lld\n", b, mx[b+pot]); if(mx[b+pot]>=a) { t_set(1,b+1,a); } //printf("%lld\n", mx[b+1+pot]); } printf("%d\n", n-fnd(0)); }
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 | #include<bits/stdc++.h> #define lim 500009 #define pot (1<<19) #define linf (1ll<<60) using namespace std; int n; long long mx[pot*2+9],d[pot*2+9],l[pot*2+9],r[pot*2+9]; void add(int i,int ll,int rr,long long x) { //printf("%d %d %d %lld %lld\n", i, ll, rr, l[i], r[i]); if((ll<=l[i])&&(r[i]<=rr)) { d[i] += x; mx[i] += x; //printf("%d\n", i); } else { if((ll<=r[i])&&(l[i]<=rr)) { add((i<<1), ll, rr, x); add(1+(i<<1), ll, rr, x); mx[i] = d[i]+max(mx[i<<1],mx[(i<<1)+1]); } } } int fnd(long long x) { int i = 1; while(i<pot) { mx[(i<<1)] += d[i]; d[(i<<1)] += d[i]; mx[(i<<1)+1] += d[i]; d[(i<<1)+1] += d[i]; d[i] = 0; if(mx[(i<<1)+1]>=x) { i = (i<<1)+1; } else { i <<= 1; } } return i-pot; } void t_set(int i,int a,int x) { if(i>=pot) { mx[i] = x; } else { mx[(i<<1)] += d[i]; d[(i<<1)] += d[i]; mx[(i<<1)+1] += d[i]; d[(i<<1)+1] += d[i]; d[i] = 0; if(a<=r[i<<1]) { t_set((i<<1),a,x); } else { t_set((i<<1)+1,a,x); } mx[i] = max(mx[(i<<1)],mx[(i<<1)+1]); } } int main() { scanf("%d", &n); for(int i = pot*2;i>=pot;--i) { mx[i] = -linf; l[i] = i-pot; r[i] = i-pot; } mx[pot] = 0; for(int i = pot-1;i;--i) { mx[i] = mx[i<<1]; l[i] = l[i<<1]; r[i] = r[1+(i<<1)]; } //printf("%lld %lld\n", l[1], r[1]); for(int i = 0;i<n;++i) { int a; scanf("%d", &a); add(1,l[1],r[1],a); //printf("X"); int b = fnd(a); //printf("%d %lld\n", b, mx[b+pot]); if(mx[b+pot]>=a) { t_set(1,b+1,a); } //printf("%lld\n", mx[b+1+pot]); } printf("%d\n", n-fnd(0)); } |