hihocoder 1665
http://hihocoder.com/problemset/problem/1665
思路:线段树区间更新
1 #include <string.h> 2 #include <algorithm> 3 #include <stdio.h> 4 #include <math.h> 5 #include <queue> 6 #include <iostream> 7 #define MAXN 100010 8 #define inf 0x3f3f3f3f 9 10 using namespace std; 11 12 struct node{ 13 int l,r;//区间[l,r] 14 int add;//区间的延时标记 15 int sum;//区间和 16 int mx; //区间最大值 17 int mn; //区间最小值 18 }tree[MAXN<<2];//一定要开到4倍多的空间 19 20 void pushup(int index){ 21 //tree[index].sum = tree[index<<1].sum+tree[index<<1|1].sum; 22 tree[index].mx = max(tree[index<<1].mx,tree[index<<1|1].mx); 23 //tree[index].mn = min(tree[index<<1].mn,tree[index<<1|1].mn); 24 } 25 void pushdown(int index){ 26 //说明该区间之前更新过 27 //要想更新该区间下面的子区间,就要把上次更新该区间的值向下更新 28 if(tree[index].add){ 29 //替换原来的值 30 31 //tree[index<<1].sum = (tree[index<<1].r-tree[index<<1].l+1)*tree[index].add; 32 //tree[index<<1|1].sum = (tree[index<<1|1].r-tree[index<<1|1].l+1)*tree[index].add; 33 tree[index<<1].mx = tree[index].add; 34 tree[index<<1|1].mx = tree[index].add; 35 //tree[index<<1].mn = tree[index].add; 36 //tree[index<<1|1].mn = tree[index].add; 37 tree[index<<1].add = tree[index].add; 38 tree[index<<1|1].add = tree[index].add; 39 tree[index].add = 0; 40 //在原来的值的基础上加上val 41 42 //tree[index<<1].sum += (tree[index<<1].r-tree[index<<1].l+1)*tree[index].add; 43 //tree[index<<1|1].sum +=(tree[index<<1|1].r-tree[index<<1|1].l+1)*tree[index].add; 44 //tree[index<<1].mx += tree[index].add; 45 //tree[index<<1|1].mx += tree[index].add; 46 //tree[index<<1].mn += tree[index].add; 47 //tree[index<<1|1].mn += tree[index].add; 48 //tree[index<<1].add += tree[index].add; 49 //tree[index<<1|1].add += tree[index].add; 50 //tree[index].add = 0; 51 52 } 53 } 54 void build(int l,int r,int index){ 55 tree[index].l = l; 56 tree[index].r = r; 57 tree[index].add = 0;//刚开始一定要清0 58 if(l == r){ 59 tree[index].sum= 0; 60 tree[index].mn = tree[index].mx = tree[index].sum; 61 return ; 62 } 63 int mid = (l+r)>>1; 64 build(l,mid,index<<1); 65 build(mid+1,r,index<<1|1); 66 pushup(index); 67 } 68 void updata(int l,int r,int index,int val){ 69 if(l <= tree[index].l && r >= tree[index].r){ 70 /*把原来的值替换成val,因为该区间有tree[index].r-tree[index].l+1 71 个数,所以区间和 以及 最值为: 72 */ 73 //tree[index].sum = (tree[index].r-tree[index].l+1)*val; 74 //tree[index].mn = val; 75 tree[index].mx = val; 76 tree[index].add = val;//延时标记*/ 77 //在原来的值的基础上加上val,因为该区间有tree[index].r-tree[index].l+1 78 //个数,所以区间和 以及 最值为: 79 //tree[index].sum += (tree[index].r-tree[index].l+1)*val; 80 //tree[index].mn += val; 81 // tree[index].mx += val; 82 //tree[index].add += val;//延时标记 83 return ; 84 } 85 pushdown(index); 86 int mid = (tree[index].l+tree[index].r)>>1; 87 if(l <= mid){ 88 updata(l,r,index<<1,val); 89 } 90 if(r > mid){ 91 updata(l,r,index<<1|1,val); 92 } 93 pushup(index); 94 } 95 int query(int l,int r,int index){ 96 if(l <= tree[index].l && r >= tree[index].r){ 97 return tree[index].mx; 98 } 99 pushdown(index); 100 int mid = (tree[index].l+tree[index].r)>>1; 101 int Max = 0; 102 if(l <= mid){ 103 Max = max(query(l,r,index<<1),Max); 104 } 105 if(r > mid){ 106 Max = max(query(l,r,index<<1|1),Max); 107 } 108 //return ans; 109 return Max; 110 //return Min; 111 } 112 int main() 113 { 114 int n,m,q,x,y,z,t; 115 build(1,100005,1); 116 scanf("%d",&t); 117 while(t--){ 118 scanf("%d %d",&x,&y); 119 z = query(x,y,1); 120 cout<<z+1<<endl; 121 updata(x,y,1,z+1); 122 //cout<<query(x,y+1,1)<<endl; 123 } 124 return 0; 125 }