#3:调试疯了——3
UVA11992,自己板子真心难调bugwoc
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include <bits/stdc++.h> 2 #define maxn 50005 3 using namespace std; 4 5 struct Seg { 6 int l, r; 7 int sum, maxx, minn; 8 }; 9 10 struct tree { 11 Seg t[maxn<<2]; 12 int addv[maxn<<2], setv[maxn<<2]; 13 14 void init(int c) { 15 for (int i = 1; i <= (c<<2); i++) { 16 addv[i] = 0; 17 setv[i] = -1; 18 } 19 } 20 21 void push_up(int p) { 22 int ls = p << 1, rs = p << 1 | 1; 23 t[p].sum = t[ls].sum + t[rs].sum; 24 t[p].maxx = max(t[ls].maxx, t[rs].maxx); 25 t[p].minn = min(t[ls].minn, t[rs].minn); 26 } 27 28 void build(int l, int r, int p) { 29 t[p].l = l, t[p].r = r; 30 if (l == r) { 31 t[p].sum = t[p].maxx = t[p].minn = 0; 32 return; 33 } 34 35 int mid = (l + r) >> 1; 36 int ls = p << 1, rs = p << 1 | 1; 37 build(l, mid, ls); 38 build(mid+1, r, rs); 39 push_up(p); 40 } 41 42 void push_down(int p) { 43 int ls = p << 1, rs = p << 1 | 1; 44 if (setv[p] >= 0) { 45 t[ls].sum = setv[p]*(t[ls].r-t[ls].l+1); 46 t[rs].sum = setv[p]*(t[rs].r-t[rs].l+1); 47 t[ls].maxx = t[rs].maxx = setv[p]; 48 t[ls].minn = t[rs].minn = setv[p]; 49 setv[ls] = setv[rs] = setv[p]; 50 addv[ls] = addv[rs] = 0; 51 setv[p] = -1; 52 } 53 if (addv[p]) { 54 t[ls].maxx += addv[p]; 55 t[rs].maxx += addv[p]; 56 t[ls].minn += addv[p]; 57 t[rs].minn += addv[p]; 58 t[ls].sum += addv[p]*(t[ls].r-t[ls].l+1); 59 t[rs].sum += addv[p]*(t[rs].r-t[rs].l+1); 60 addv[ls] += addv[p]; 61 addv[rs] += addv[p]; 62 addv[p] = 0; 63 } 64 } 65 66 void update(int l, int r, int k, int p, int flag) { 67 if (l <= t[p].l && t[p].r <= r) { 68 if (flag) { 69 t[p].maxx = t[p].minn = k; 70 t[p].sum = k*(t[p].r - t[p].l + 1); 71 setv[p] = k; 72 addv[p] = 0; 73 } else { 74 t[p].maxx += k; 75 t[p].minn += k; 76 t[p].sum += k*(t[p].r - t[p].l + 1); 77 addv[p] += k; 78 } 79 return; 80 } 81 82 int mid = (t[p].l + t[p].r) >> 1; 83 int ls = p << 1, rs = p << 1 | 1; 84 push_down(p); 85 if (l <= mid) update(l, r, k, ls, flag); 86 if (mid < r) update(l, r, k, rs, flag); 87 push_up(p); 88 } 89 90 Seg query(int l, int r, int p) { 91 if (l <= t[p].l && t[p].r <= r) return t[p]; 92 93 int mid = (t[p].l + t[p].r) >> 1; 94 int ls = p << 1, rs = p << 1 | 1; 95 push_down(p); 96 if (l > mid) return query(l, r, rs); 97 if (r <= mid) return query(l, r, ls); 98 99 Seg a, b, ans; 100 a = query(l, r, ls); 101 b = query(l, r, rs); 102 ans.sum = a.sum + b.sum; 103 ans.maxx = max(a.maxx, b.maxx); 104 ans.minn = min(a.minn, b.minn); 105 return ans; 106 } 107 }T[21]; 108 109 int r, c, m, v; 110 111 int main() { 112 while (~scanf("%d%d%d", &r, &c, &m)) { 113 for (int i = 1; i <= r; i++) { 114 T[i].init(c); 115 T[i].build(1, c, 1); 116 } 117 while (m--) { 118 int o, x1, y1, x2, y2; 119 scanf("%d%d%d%d%d", &o, &x1, &y1, &x2, &y2); 120 121 if (o == 3) { 122 int sum = 0, minn = 1e9+1, maxx = -1e9-1; 123 for (int i = x1; i <= x2; i++) { 124 Seg a = T[i].query(y1, y2, 1); 125 sum += a.sum; 126 minn = min(minn, a.minn); 127 maxx = max(maxx, a.maxx); 128 } 129 printf("%d %d %d\n", sum, minn, maxx); 130 } else { 131 scanf("%d", &v); 132 for (int i = x1; i <= x2; i++) 133 T[i].update(y1, y2, v, 1, o-1); 134 } 135 } 136 } 137 return 0; 138 }
Contest hunter最长公共上升子序列。
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include <cstdio> 2 #define R(a) scanf("%d", &a); 3 #define rep(i, a, b) for (int i = a; i <= b; i++) 4 #define max(a, b) a > b ? a : b 5 6 int n, ans; 7 int a[3001], b[3001], f[3001][3001]; 8 9 int main() { 10 R(n); 11 rep(i, 1, n) R(a[i]); 12 rep(i, 1, n) R(b[i]); 13 rep(i, 1, n) { 14 int val = 0; 15 rep(j, 1, n) { 16 if (a[i] == b[j]) { 17 f[i][j] = val + 1; 18 } else { 19 f[i][j] = f[i-1][j]; 20 if (b[j] < a[i]) val = max(val, f[i][j]); 21 } 22 } 23 } 24 25 rep(i, 1, n) ans = max(ans, f[n][i]); 26 printf("%d\n", ans); 27 return 0; 28 }
POJ3666,数据比较水可以只写递增不写递减就AC……
可以滚动数组,也可nlogn优先队列奇妙过法,跟cf#371div1的C是同一道题,那个题的过法网友们也是千奇百怪,神奇的优先队列看了半天解释也没看懂,目前水平太低悟不透这些,先放坑里。
假A代码:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include <cstdio> 2 #include <cstring> 3 #include <cmath> 4 #include <algorithm> 5 using namespace std; 6 #define maxn 2001 7 #define init(a, b) memset(a, b, sizeof(a)) 8 #define R(a) scanf("%d", &a) 9 #define REP(i, a, b) for (int i = a; i <= b; i++) 10 11 int n, a[maxn]; 12 int num[maxn], f[maxn][maxn]; 13 14 int main() { 15 R(n); 16 REP(i, 1, n) { 17 R(a[i]); 18 num[i] = a[i]; 19 } 20 21 sort(num+1, num+1+n); 22 int m = unique(num+1, num+1+n) - (num+1); 23 24 init(f, 0x3f); 25 f[0][0] = 0; 26 27 REP(i, 1, n) { 28 int temp = f[i-1][0]; 29 REP(j, 1, m) { 30 temp = min(temp, f[i-1][j]); 31 f[i][j] = temp + abs(a[i] - num[j]); 32 } 33 } 34 35 int ans = 0x3f3f3f3f; 36 REP(i, 1, m) ans = min(ans, f[n][i]); 37 printf("%d", ans); 38 39 return 0; 40 }