hihocoder 1665

http://hihocoder.com/problemset/problem/1665node

思路:線段樹區間更新ios

  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 }
相關文章
相關標籤/搜索