标签:
Description
Input
Output
Sample Input
12 10 3 1 2 3 1 9 4 3 2 2 1 3 2 9 2 3 2 3 2 3
Sample Output
12 4 4 6 10
Source
1 /* 2 唐代许浑 3 《咸阳城东楼 / 咸阳城西楼晚眺 / 西门》 4 5 一上高城万里愁,蒹葭杨柳似汀洲。 6 溪云初起日沉阁,山雨欲来风满楼。 7 鸟下绿芜秦苑夕,蝉鸣黄叶汉宫秋。 8 行人莫问当年事,故国东来渭水流。 9 */ 10 #include <iostream> 11 #include <cstdio> 12 #include <algorithm> 13 #include <cstring> 14 #include <vector> 15 #include <utility> 16 #include <iomanip> 17 #include <string> 18 #include <cmath> 19 #include <queue> 20 #include <assert.h> 21 #include <map> 22 #include <ctime> 23 #include <cstdlib> 24 #include <stack> 25 #define LOCAL 26 const int MAXN = 1600000 + 10; 27 const int MAXM = 75 + 10; 28 const int INF = 100000000; 29 const int SIZE = 450; 30 const int maxnode = 0x7fffffff + 10; 31 using namespace std; 32 int i; 33 struct SEGTREE{ 34 struct Node{ 35 int l, r; 36 int rmax, mmax, lmax;//分别代表从左边开始的最长,从右边开始的最长和中间的最长 37 int delta; 38 39 /*void Count(){ 40 rmax = lmax = mmax = 0; 41 for (int i = l; i <= r; i++) if (data[i] == 0){lmax = i - l;break;} 42 for (int i = r; i >= l; i--) if (data[i] == 0){rmax = r - i;break;} 43 int t = 0; 44 for (int i = l; i <= r; i++){ 45 46 } 47 }*/ 48 }tree[MAXN * 4]; 49 50 void pushdown(int t){ 51 if (tree[t].delta != -1){ 52 if (tree[t].delta == 1) {//全1 53 tree[(t<<1)].lmax = tree[(t<<1)].rmax = tree[(t<<1)].mmax = tree[(t<<1)].r - tree[(t<<1)].l + 1;tree[(t<<1)].delta = 1; 54 tree[(t<<1) | 1].lmax = tree[(t<<1) | 1].rmax = tree[(t<<1) | 1].mmax = tree[(t<<1) | 1].r - tree[(t<<1) | 1].l + 1;tree[(t<<1) | 1].delta = 1; 55 tree[t].delta = -1; 56 }else{ 57 tree[(t<<1)].lmax = tree[(t<<1)].rmax = tree[(t<<1)].mmax = 0;tree[(t<<1)].delta = 0; 58 tree[(t<<1) | 1].lmax = tree[(t<<1) | 1].rmax = tree[(t<<1) | 1].mmax = 0;tree[(t<<1) | 1].delta = 0; 59 tree[t].delta = -1; 60 } 61 } 62 } 63 //更新 64 void update(int t){ 65 tree[t].mmax = max(tree[t<<1].mmax, max(tree[(t<<1)|1].mmax, tree[t<<1].rmax + tree[(t<<1)|1].lmax)); 66 //更新tree[t]的lmax 67 if (tree[t<<1].lmax == tree[t<<1].r - tree[t<<1].l + 1) tree[t].lmax = tree[t<<1].lmax + tree[(t<<1)|1].lmax; 68 else tree[t].lmax = tree[t<<1].lmax; 69 70 //同理 71 if (tree[(t<<1)|1].rmax == tree[(t<<1)|1].r - tree[(t<<1)|1].l + 1) tree[t].rmax = tree[t<<1].rmax + tree[(t<<1)|1].rmax; 72 else tree[t].rmax = tree[(t<<1)|1].rmax; 73 } 74 void build(int t, int l, int r){ 75 tree[t].l = l; 76 tree[t].r = r; 77 tree[t].lmax = tree[t].mmax = tree[t].rmax = tree[t].r - tree[t].l + 1; 78 tree[t].delta = -1; 79 if (l == r) return; 80 int mid = (l + r) >> 1; 81 build(t << 1, l, mid); 82 build((t << 1)|1, mid + 1, r); 83 } 84 void insert(int t, int l, int r, int val){//t为节点编号,val为权值 85 pushdown(t); 86 if (l <= tree[t].l && tree[t].r <= r){ 87 if (val == 1) {tree[t].rmax = tree[t].lmax = tree[t].mmax = tree[t].r - tree[t].l + 1;tree[t].delta = 1;} 88 else {tree[t].rmax = tree[t].lmax = tree[t].mmax = 0;tree[t].delta = 0;} 89 return; 90 } 91 int mid = (tree[t].l + tree[t].r)>>1; 92 //if (i == 3 && tree[t].l == 10 && tree[t].r == 11) 93 //printf(""); 94 if (l <= mid) insert(t << 1, l, r , val); 95 if (r > mid) insert((t << 1) | 1, l, r, val); 96 97 update(t); 98 } 99 }A; 100 int n, p; 101 102 void init(){ 103 scanf("%d%d", &n, &p); 104 A.build(1, 1, n); 105 } 106 void work(){ 107 for (i = 1; i <= p; i++){ 108 int t; 109 scanf("%d", &t); 110 if (t == 3) printf("%d\n", A.tree[1].mmax); 111 else if (t == 2){ 112 int l, r; 113 scanf("%d%d", &l, &r); 114 A.insert(1, l, l + r - 1, 1); 115 }else if (t == 1){ 116 int l, r; 117 scanf("%d%d", &l, &r); 118 A.insert(1, l, l + r - 1, 0); 119 } 120 } 121 } 122 123 int main(){ 124 125 init(); 126 work(); 127 return 0; 128 }
标签:
原文地址:http://www.cnblogs.com/hoskey/p/4334416.html