标签:
3 0 1 3 5 1 0 2 0 1 0
5 24
1 #include <bits/stdc++.h> 2 using namespace std; 3 typedef long long LL; 4 const int maxn = 200010; 5 struct arc { 6 int pos,next; 7 arc(int x = 0,int y = -1) { 8 pos = x; 9 next = y; 10 } 11 } e[maxn<<2]; 12 struct node { 13 LL sum; 14 int lazy,minv,maxv; 15 } tree[maxn<<2]; 16 int head[maxn],a[maxn],tot,n; 17 void add(int u,int pos) { 18 e[tot] = arc(pos,head[u]); 19 head[u] = tot++; 20 } 21 inline void pushup(int v) { 22 tree[v].minv = min(tree[v<<1].minv,tree[v<<1|1].minv); 23 tree[v].maxv = max(tree[v<<1].maxv,tree[v<<1|1].maxv); 24 tree[v].sum = tree[v<<1].sum + tree[v<<1|1].sum; 25 } 26 inline void pushdown(int L,int R,int v) { 27 if(tree[v].lazy != -1) { 28 int mid = (L + R)>>1; 29 tree[v<<1].maxv = tree[v<<1].minv = tree[v].lazy; 30 tree[v<<1].sum = (LL)tree[v].lazy*(mid - L + 1); 31 tree[v<<1].lazy = tree[v].lazy; 32 tree[v<<1|1].maxv = tree[v<<1|1].minv = tree[v].lazy; 33 tree[v<<1|1].lazy = tree[v].lazy; 34 tree[v<<1|1].sum = (LL)tree[v].lazy*(R - mid); 35 tree[v].lazy = -1; 36 } 37 } 38 void build(int lt,int rt,int v) { 39 tree[v].lazy = -1; 40 if(lt == rt) { 41 tree[v].maxv = tree[v].minv = tree[v].sum = n - lt + 1; 42 return; 43 } 44 int mid = (lt + rt)>>1; 45 build(lt,mid,v<<1); 46 build(mid+1,rt,v<<1|1); 47 pushup(v); 48 } 49 void update(int L,int R,int lt,int rt,int val,int v) { 50 if(tree[v].maxv <= val) return; 51 if(lt <= L && rt >= R && tree[v].minv >= val) { 52 tree[v].maxv = tree[v].minv = val; 53 tree[v].sum = LL(val)*(R - L + 1); 54 tree[v].lazy = val; 55 return; 56 } 57 pushdown(L,R,v); 58 int mid = (L + R)>>1; 59 if(lt <= mid) update(L,mid,lt,rt,val,v<<1); 60 if(rt > mid) update(mid+1,R,lt,rt,val,v<<1|1); 61 pushup(v); 62 } 63 int main() { 64 while(scanf("%d",&n),n) { 65 memset(head,-1,sizeof head); 66 tot = 0; 67 for(int i = 1; i <= n; ++i) scanf("%d",a+i); 68 for(int i = n; i > 0; --i) if(a[i] <= n) add(a[i],i); 69 build(1,n,1); 70 LL ret = 0; 71 for(int i = 0; i <= n && tree[1].sum; ++i){ 72 int last = 0; 73 for(int j = head[i]; ~j; j = e[j].next){ 74 update(1,n,last+1,e[j].pos, n - e[j].pos + 1,1); 75 last = e[j].pos; 76 } 77 if(last < n) update(1,n,last + 1,n,0,1); 78 ret += tree[1].sum; 79 } 80 printf("%I64d\n",ret); 81 } 82 return 0; 83 }
标签:
原文地址:http://www.cnblogs.com/crackpotisback/p/4787167.html