数据结构专题

地址 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=73154#overview

密码 acmore

基础数据结构的专题, 单调队列,单调栈,优先队列,堆,并查集,RMQ,线段树,树状数组

 

A:HDU 3530 单调队列

不错的单调队列的题目 题解

 1 //#pragma comment(linker, "/STACK:1677721600")
 2 #include <map>
 3 #include <set>
 4 #include <stack>
 5 #include <queue>
 6 #include <cmath>
 7 #include <ctime>
 8 #include <vector>
 9 #include <cstdio>
10 #include <cctype>
11 #include <cstring>
12 #include <cstdlib>
13 #include <iostream>
14 #include <algorithm>
15 using namespace std;
16 #define INF 0x3f3f3f3f
17 #define inf (-((LL)1<<40))
18 #define lson k<<1, L, (L + R)>>1
19 #define rson k<<1|1,  ((L + R)>>1) + 1, R
20 #define mem0(a) memset(a,0,sizeof(a))
21 #define mem1(a) memset(a,-1,sizeof(a))
22 #define mem(a, b) memset(a, b, sizeof(a))
23 #define FIN freopen("in.txt", "r", stdin)
24 #define FOUT freopen("out.txt", "w", stdout)
25 #define rep(i, a, b) for(int i = a; i <= b; i ++)
26 #define dec(i, a, b) for(int i = a; i >= b; i --)
27 
28 template<class T> T MAX(T a, T b) { return a > b ? a : b; }
29 template<class T> T MIN(T a, T b) { return a < b ? a : b; }
30 template<class T> T GCD(T a, T b) { return b ? GCD(b, a%b) : a; }
31 template<class T> T LCM(T a, T b) { return a / GCD(a,b) * b;    }
32 
33 //typedef __int64 LL;
34 typedef long long LL;
35 const int MAXN = 100000 + 100;
36 const int MAXM = 110000;
37 const double eps = 1e-8;
38 LL MOD = 1000000007;
39 
40 int a[MAXN], L, H, n;
41 int q1[MAXN], q2[MAXN];
42 
43 int find_ans() {
44     int f1, f2, t1, t2, l1 = -1, l2 = -1, ans = 0;
45     f1 = f2 = t1 = t2 = 0;
46     rep (i, 0, n - 1) {
47         while(f1 < t1 && a[q1[t1 - 1]] <= a[i]) t1 --;//维护最大值队列(递减)
48         q1[t1++] = i;
49         while(f2 < t2 && a[q2[t2 - 1]] >= a[i]) t2 --;//维护最小值队列(递增)
50         q2[t2++] = i;
51         while(a[q1[f1]] - a[q2[f2]] > H) {//差值过大
52             q1[f1] < q2[f2] ? l1 = q1[f1 ++] : l2 = q2[f2 ++];
53         }
54         if(a[q1[f1]] - a[q2[f2]] >= L) {//差值满足条件
55             ans = max(ans, i - max(l1, l2));
56         }
57     }
58     return ans;
59 }
60 
61 int main()
62 {
63     while(~scanf("%d %d %d", &n, &L, &H)) {
64         rep (i, 0, n - 1) scanf("%d", a + i);
65         printf("%d\n", find_ans());
66     }
67     return 0;
68 }
View Code

 

B:HDU 1242 优先队列

好像不是优先队列,只是一个搜索

  1 #pragma comment(linker, "/STACK:1677721600")
  2 #include <map>
  3 #include <set>
  4 #include <stack>
  5 #include <queue>
  6 #include <cmath>
  7 #include <ctime>
  8 #include <vector>
  9 #include <cstdio>
 10 #include <cctype>
 11 #include <cstring>
 12 #include <cstdlib>
 13 #include <iostream>
 14 #include <algorithm>
 15 using namespace std;
 16 #define INF 0x3f3f3f3f
 17 #define inf (-((LL)1<<40))
 18 #define lson k<<1, L, (L + R)>>1
 19 #define rson k<<1|1,  ((L + R)>>1) + 1, R
 20 #define mem0(a) memset(a,0,sizeof(a))
 21 #define mem1(a) memset(a,-1,sizeof(a))
 22 #define mem(a, b) memset(a, b, sizeof(a))
 23 #define FIN freopen("in.txt", "r", stdin)
 24 #define FOUT freopen("out.txt", "w", stdout)
 25 #define rep(i, a, b) for(int i = a; i <= b; i ++)
 26 #define dec(i, a, b) for(int i = a; i >= b; i --)
 27 
 28 template<class T> T MAX(T a, T b) { return a > b ? a : b; }
 29 template<class T> T MIN(T a, T b) { return a < b ? a : b; }
 30 template<class T> T GCD(T a, T b) { return b ? GCD(b, a%b) : a; }
 31 template<class T> T LCM(T a, T b) { return a / GCD(a,b) * b;    }
 32 
 33 //typedef __int64 LL;
 34 typedef long long LL;
 35 const int MAXN = 110 + 100;
 36 const int MAXM = 110000;
 37 const double eps = 1e-8;
 38 LL MOD = 1000000007;
 39 const int dx[4] = {0, 1, 0, -1};
 40 const int dy[4] = {1, 0, -1, 0};
 41 
 42 template <class T>
 43 class MyQueue
 44 {
 45 private:
 46     T* que;
 47     int si, fr, re;
 48     void setValue(int _size) {
 49         fr = 0; re = 0;
 50         si = _size;
 51         que = (T*)malloc(si * sizeof(T));
 52     }
 53 public:
 54     MyQueue() {
 55         this->setValue(100005);
 56     }
 57     MyQueue(int _size) {
 58         this->setValue(_size);
 59     }
 60     T front() {
 61         if(fr != re)
 62             return que[fr];
 63         return NULL;
 64     }
 65     void pop() {
 66         if(fr != re)
 67             fr = (fr + 1) % si;
 68     }
 69     void push(T e) {
 70         if((re + 1) % si == fr) return ;
 71         que[re] = e;
 72         re = (re + 1) % si;
 73     }
 74     bool empty() {
 75         if(fr == re) return 1;
 76         return 0;
 77     }
 78     void clear() {
 79         fr = 0;
 80         re = 0;
 81     }
 82 };
 83 
 84 struct Node {
 85     int x, y, s;
 86     Node(int _x = 0, int _y = 0, int _s = 0) {
 87         x = _x; y = _y; s = _s;
 88     }
 89 };
 90 bool vis[MAXN][MAXN];
 91 MyQueue<Node> q(41000);
 92 int m, n;
 93 char ma[MAXN][MAXN];
 94 
 95 bool inside(int x, int N) {
 96     return x >= 0 && x < N;
 97 }
 98 
 99 int bfs(int x, int y) {
100     mem0(vis);
101     q.clear();
102     vis[x][y] = 1;
103     q.push(Node(x, y, 0));
104     while(!q.empty()) {
105         Node fr = q.front(); q.pop();
106         int px = fr.x, py = fr.y, ps = fr.s;
107         rep (i, 0, 3) {
108             int nx = px + dx[i], ny = py + dy[i];
109             if(inside(nx, n) && inside(ny, m)
110                && ma[nx][ny] != '#' && !vis[nx][ny]) {
111                 int ns = ps + 1 + (ma[nx][ny] == 'x');
112                 if(ma[nx][ny] == 'a')
113                     return ns;
114                 vis[nx][ny] = 1;
115                 q.push(Node(nx, ny, ns));
116             }
117         }
118     }
119     return -1;
120 }
121 
122 int main()
123 {
124     while(~scanf("%d %d%*c", &n, &m)) {
125         int st_x = -1, st_y;
126         rep (i, 0, n - 1) {
127             scanf("%s", ma[i]);
128             if(st_x == -1) rep (j, 0, m - 1) {
129                 if(ma[i][j] == 'r') st_x = i, st_y = j;
130             }
131         }
132         int ans = bfs(st_x, st_y);
133         if(ans == -1) {
134             puts("Poor ANGEL has to stay in the prison all his life.");
135         }
136         else cout << ans << endl;
137     }
138     return 0;
139 }
View Code

 

C:HDU 4123 RMQ

一个不错的RMQ的应用 题解

#pragma comment(linker, "/STACK:1677721600")
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <cmath>
#include <ctime>
#include <vector>
#include <cstdio>
#include <cctype>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
using namespace std;
#define INF 0x3f3f3f3f
#define inf (-((LL)1<<40))
#define lson k<<1, L, (L + R)>>1
#define rson k<<1|1,  ((L + R)>>1) + 1, R
#define mem0(a) memset(a,0,sizeof(a))
#define mem1(a) memset(a,-1,sizeof(a))
#define mem(a, b) memset(a, b, sizeof(a))
#define FIN freopen("in.txt", "r", stdin)
#define FOUT freopen("out.txt", "w", stdout)
#define rep(i, a, b) for(int i = a; i <= b; i ++)
#define dec(i, a, b) for(int i = a; i >= b; i --)

template<class T> T MAX(T a, T b) { return a > b ? a : b; }
template<class T> T MIN(T a, T b) { return a < b ? a : b; }
template<class T> T GCD(T a, T b) { return b ? GCD(b, a%b) : a; }
template<class T> T LCM(T a, T b) { return a / GCD(a,b) * b;    }

//typedef __int64 LL;
typedef long long LL;
const int MAXN = 50000 + 100;
const int MAXM = 110000;
const double eps = 1e-8;
LL MOD = 1000000007;

struct Node {
    int v, s;
    Node(int _v = 0, int _s = 0) {
        v = _v; s = _s;
    }
};

vector<Node>G[MAXN];
int mx[MAXN], se[MAXN], d[MAXN], idx[MAXN];
int ma[MAXN][20], mi[MAXN][20];
int n, u, v, w;
int m, Q;

void init(int n) {
    rep (i, 0, n) G[i].clear();
    mem0(mx); mem0(se);
}

void dfs1(int u, int fa) {
    mx[u] = se[u] = 0;
    int sz = G[u].size();
    rep (i, 0, sz - 1) {
        int v = G[u][i].v;
        if(v == fa) continue;
        dfs1(v, u);
        int len = G[u][i].s + mx[v];
        if(len > mx[u]) se[u] = mx[u], mx[u] = len;
        else if(len > se[u]) se[u] = len;
    }
}

void dfs2(int u, int fa, int dis) {
    d[u] = max(dis, max(mx[u], se[u]));
    int sz = G[u].size();
    rep (i, 0, sz - 1) {
        int v = G[u][i].v, len = G[u][i].s;
        if(v == fa) continue;
        if(len + mx[v] == mx[u])
            dfs2(v, u, max(dis, se[u]) + len);
        else
            dfs2(v, u, max(dis, mx[u]) + len);
    }
}

void rmq_init(int n) {
    rep (i, 1, n) ma[i][0] = mi[i][0] = d[i];
    for(int j = 1; (1<<j) <= n; j ++) {
        for(int i = 1; i + (1<<j) - 1 <= n; i ++) {
            ma[i][j] = max(ma[i][j - 1], ma[i + (1 << (j - 1))][j - 1]);
            mi[i][j] = min(mi[i][j - 1], mi[i + (1 << (j - 1))][j - 1]);
        }
    }
    rep (len, 1, n) {
        idx[len] = 0;
        while((1 << (idx[len] + 1)) <= len) idx[len] ++;
    }
}

int rmq(int l, int r) {
    int k = idx[r - l + 1];
    return max(ma[l][k], ma[r - (1 << k) + 1][k]) - min(mi[l][k], mi[r - (1 << k) + 1][k]);
}

int main()
{
//    FIN;
    while(~scanf("%d %d", &n, &m) && n) {
        init(n);
        rep (i, 1, n - 1) {
            scanf("%d %d %d", &u, &v, &w);
            G[u].push_back(Node(v, w));
            G[v].push_back(Node(u, w));
        }

        //计算每个点到叶子节点的最远距离
        dfs1(1, -1);
        dfs2(1, -1, 0);

        //计算答案
        rmq_init(n);
        while(m --) {
            scanf("%d", &Q);
            int l = 1, ans = 0;
            rep (i, 1, n) {
                while(l < i && rmq(l, i) > Q) l ++;
                ans = max(ans, i - l + 1);
            }
            cout << ans << endl;
        }
    }
    return 0;
}
View Code

 

D:HDU 2888 RMQ

一个二维RMQ

 1 #pragma comment(linker, "/STACK:1677721600")
 2 #include <map>
 3 #include <set>
 4 #include <stack>
 5 #include <queue>
 6 #include <cmath>
 7 #include <ctime>
 8 #include <vector>
 9 #include <cstdio>
10 #include <cctype>
11 #include <cstring>
12 #include <cstdlib>
13 #include <iostream>
14 #include <algorithm>
15 using namespace std;
16 #define INF 0x3f3f3f3f
17 #define inf (-((LL)1<<40))
18 #define lson k<<1, L, (L + R)>>1
19 #define rson k<<1|1,  ((L + R)>>1) + 1, R
20 #define mem0(a) memset(a,0,sizeof(a))
21 #define mem1(a) memset(a,-1,sizeof(a))
22 #define mem(a, b) memset(a, b, sizeof(a))
23 #define FIN freopen("in.txt", "r", stdin)
24 #define FOUT freopen("out.txt", "w", stdout)
25 #define rep(i, a, b) for(int i = a; i <= b; i ++)
26 #define dec(i, a, b) for(int i = a; i >= b; i --)
27 
28 template<class T> T MAX(T a, T b) { return a > b ? a : b; }
29 template<class T> T MIN(T a, T b) { return a < b ? a : b; }
30 template<class T> T GCD(T a, T b) { return b ? GCD(b, a%b) : a; }
31 template<class T> T LCM(T a, T b) { return a / GCD(a,b) * b;    }
32 
33 //typedef __int64 LL;
34 typedef long long LL;
35 const int MAXN = 50000 + 100;
36 const int MAXM = 110000;
37 const double eps = 1e-8;
38 LL MOD = 1000000007;
39 
40 int m, n;
41 int mx[302][9][302][9];
42 int idx[302], q, lx, ly, rx, ry;
43 
44 void rmq_init(int m, int n) {
45     for(int i = 0; (1<<i) <= m; i ++) {
46         for(int j = 0; (1<<j) <= n; j ++) {
47             if(i == 0 && j == 0) continue;
48             int len2 = (1 << j), len1 = (1 << i);
49             for(int x = 1; x + len1 - 1 <= m; x ++) {
50                 for(int y = 1; y + len2 - 1 <= n; y ++) {
51                     if(i == 0) mx[x][i][y][j] = max(mx[x][i][y][j - 1], mx[x][i][y + (len2 >> 1)][j - 1]);
52                     else mx[x][i][y][j] = max(mx[x][i - 1][y][j], mx[x + (len1 >> 1)][i - 1][y][j]);
53                 }
54             }
55         }
56     }
57     for(int i = 1; i <= m || i <= n; i ++) {
58         idx[i] = 0;
59         while((1 << (idx[i] + 1)) <= i) idx[i] ++;
60     }
61 }
62 
63 int rmq(int lx, int rx, int ly, int ry) {
64     int a = idx[rx - lx + 1], la = (1 << a);
65     int b = idx[ry - ly + 1], lb = (1 << b);
66     return max(max(max(mx[lx][a][ly][b],
67                        mx[rx - la + 1][a][ly][b]),
68                        mx[lx][a][ry - lb + 1][b]),
69                        mx[rx - la + 1][a][ry - lb + 1][b]);
70 }
71 
72 int main()
73 {
74     //FIN;
75     while(~scanf("%d %d", &m, &n)) {
76         mem0(mx);
77         rep (i, 1, m) rep (j, 1, n)
78             scanf("%d", &mx[i][0][j][0]);
79         rmq_init(m, n);
80         scanf("%d", &q);
81         while(q--) {
82             scanf("%d %d %d %d", &lx, &ly, &rx, &ry);
83             int ma = rmq(lx, rx, ly, ry);
84             printf("%d %s\n", ma, ma == mx[lx][0][ly][0] || ma == mx[lx][0][ry][0]
85                                || ma == mx[rx][0][ly][0] || ma == mx[rx][0][ry][0]
86                                ? "yes" : "no");
87         }
88     }
89     return 0;
90 }
View Code

 

E:HDU 1506 单调栈

做了好多遍了

 1 #pragma comment(linker, "/STACK:1677721600")
 2 #include <map>
 3 #include <set>
 4 #include <stack>
 5 #include <queue>
 6 #include <cmath>
 7 #include <ctime>
 8 #include <vector>
 9 #include <cstdio>
10 #include <cctype>
11 #include <cstring>
12 #include <cstdlib>
13 #include <iostream>
14 #include <algorithm>
15 using namespace std;
16 #define INF 0x3f3f3f3f
17 #define inf (-((LL)1<<40))
18 #define lson k<<1, L, (L + R)>>1
19 #define rson k<<1|1,  ((L + R)>>1) + 1, R
20 #define mem0(a) memset(a,0,sizeof(a))
21 #define mem1(a) memset(a,-1,sizeof(a))
22 #define mem(a, b) memset(a, b, sizeof(a))
23 #define FIN freopen("in.txt", "r", stdin)
24 #define FOUT freopen("out.txt", "w", stdout)
25 #define rep(i, a, b) for(int i = a; i <= b; i ++)
26 #define dec(i, a, b) for(int i = a; i >= b; i --)
27 
28 template<class T> T MAX(T a, T b) { return a > b ? a : b; }
29 template<class T> T MIN(T a, T b) { return a < b ? a : b; }
30 template<class T> T GCD(T a, T b) { return b ? GCD(b, a%b) : a; }
31 template<class T> T LCM(T a, T b) { return a / GCD(a,b) * b;    }
32 
33 //typedef __int64 LL;
34 typedef long long LL;
35 const int MAXN = 100000 + 100;
36 const int MAXM = 110000;
37 const double eps = 1e-8;
38 LL MOD = 1000000007;
39 
40 int n, a[MAXN], ans_l[MAXN], ans_r[MAXN];
41 
42 void find_ans(int *ans, int *a, int n) {
43     stack<int>s;
44     rep (i, 1, n) {
45         while(!s.empty() && a[s.top()] >= a[i]) {
46             s.pop();
47         }
48         if(s.empty()) ans[i] = i;
49         else ans[i] = i - s.top();
50         s.push(i);
51     }
52 }
53 
54 void reverse_arr(int *arr, int n) {
55     for(int i = 1; i + i <= n; i ++) swap(a[i], a[n + 1 - i]);
56 }
57 
58 int main()
59 {
60 //    FIN;
61     while(~scanf("%d", &n) && n) {
62         rep (i, 1, n) scanf("%d", a + i);
63         find_ans(ans_l, a, n);
64         reverse_arr(a, n);
65         find_ans(ans_r, a, n);
66         LL ans = 0;
67         rep (i, 1, n) {
68             ans = max(ans, (LL)a[i] * (ans_l[n + 1 - i] + ans_r[i] - 1));
69         }
70         cout << ans << endl;
71     }
72     return 0;
73 }
View Code

 

F:HDU 5033 单调栈

题解

  1 #pragma comment(linker, "/STACK:1677721600")
  2 #include <map>
  3 #include <set>
  4 #include <stack>
  5 #include <queue>
  6 #include <cmath>
  7 #include <ctime>
  8 #include <vector>
  9 #include <cstdio>
 10 #include <cctype>
 11 #include <cstring>
 12 #include <cstdlib>
 13 #include <iostream>
 14 #include <algorithm>
 15 using namespace std;
 16 #define INF 0x3f3f3f3f
 17 #define inf (-((LL)1<<40))
 18 #define lson k<<1, L, (L + R)>>1
 19 #define rson k<<1|1,  ((L + R)>>1) + 1, R
 20 #define mem0(a) memset(a,0,sizeof(a))
 21 #define mem1(a) memset(a,-1,sizeof(a))
 22 #define mem(a, b) memset(a, b, sizeof(a))
 23 #define FIN freopen("in.txt", "r", stdin)
 24 #define FOUT freopen("out.txt", "w", stdout)
 25 #define rep(i, a, b) for(int i = a; i <= b; i ++)
 26 #define dec(i, a, b) for(int i = a; i >= b; i --)
 27 
 28 template<class T> T MAX(T a, T b) { return a > b ? a : b; }
 29 template<class T> T MIN(T a, T b) { return a < b ? a : b; }
 30 template<class T> T GCD(T a, T b) { return b ? GCD(b, a%b) : a; }
 31 template<class T> T LCM(T a, T b) { return a / GCD(a,b) * b;    }
 32 
 33 //typedef __int64 LL;
 34 typedef long long LL;
 35 const int MAXN = 200000 + 100;
 36 const int MAXM = 110000;
 37 const double eps = 1e-8;
 38 LL MOD = 1000000007;
 39 const double PI = 4.0 * atan(1.0);
 40 
 41 int T, N, Q;
 42 int x, h;
 43 struct Node {
 44     int x, h;
 45     Node(int _x = 0, int _h = -1) {
 46         x = _x; h = _h;
 47     }
 48 }node[MAXN];
 49 int sta[MAXN >> 1];
 50 double ans_l[MAXN >> 1], ans_r[MAXN >> 1];
 51 
 52 int cmp_up(Node A, Node B) { return A.x < B.x; }
 53 
 54 int cmp_down(Node A, Node B) { return A.x > B.x; }
 55 
 56 double pre(int r, int l) {
 57     return (double)(node[r].h - node[l].h) / fabs(1.0 * node[r].x - node[l].x);
 58 }
 59 
 60 double pre(Node a, int l) {
 61     return (double)(a.h - node[l].h) / fabs(1.0 * a.x - node[l].x);
 62 }
 63 
 64 void record_ans(double *angle) {
 65     int tp = 0;
 66     rep (i, 1, N + Q) {
 67         if(node[i].h < 0) {
 68             while(tp >= 2 && pre(Node(node[i].x, 0), sta[tp - 1]) - pre(sta[tp - 1], sta[tp - 2]) > eps) tp --;
 69             angle[-node[i].h] = atan(fabs(1.0 * node[i].x - node[sta[tp - 1]].x) / node[sta[tp - 1]].h);
 70         }
 71         else {
 72             while(tp >= 2 && pre(i, sta[tp - 1]) - pre(sta[tp - 1], sta[tp - 2]) > eps) tp --;
 73             sta[tp++] = i;
 74         }
 75     }
 76 }
 77 
 78 int main()
 79 {
 80 //    FIN;
 81     cin >> T;
 82     rep (cas, 1, T) {
 83         scanf("%d", &N);
 84         rep (i, 1, N) {
 85             scanf("%d %d", &x, &h);
 86             node[i] = Node(x, h);
 87         }
 88         scanf("%d", &Q);
 89         rep (i, 1, Q) {
 90             scanf("%d", &x);
 91             node[N + i] = Node(x, -i);
 92         }
 93         sort(node + 1, node + 1 + N + Q, cmp_up);
 94         record_ans(ans_l);
 95         sort(node + 1, node + 1 + N + Q, cmp_down);
 96         record_ans(ans_r);
 97         cout << "Case #" << cas <<":" << endl;
 98         rep (i, 1, Q) {
 99             printf("%.10f\n", (ans_l[i] + ans_r[i]) * 180 / PI);
100         }
101     }
102     return 0;
103 }
View Code

 

G:HDU 3308 线段树

简单线段树

  1 #pragma comment(linker, "/STACK:1677721600")
  2 #include <map>
  3 #include <set>
  4 #include <stack>
  5 #include <queue>
  6 #include <cmath>
  7 #include <ctime>
  8 #include <vector>
  9 #include <cstdio>
 10 #include <cctype>
 11 #include <cstring>
 12 #include <cstdlib>
 13 #include <iostream>
 14 #include <algorithm>
 15 using namespace std;
 16 #define INF 0x3f3f3f3f
 17 #define inf (-((LL)1<<40))
 18 #define lson k<<1, L, (L + R)>>1
 19 #define rson k<<1|1,  ((L + R)>>1) + 1, R
 20 #define mem0(a) memset(a,0,sizeof(a))
 21 #define mem1(a) memset(a,-1,sizeof(a))
 22 #define mem(a, b) memset(a, b, sizeof(a))
 23 #define FIN freopen("in.txt", "r", stdin)
 24 #define FOUT freopen("out.txt", "w", stdout)
 25 #define rep(i, a, b) for(int i = a; i <= b; i ++)
 26 #define dec(i, a, b) for(int i = a; i >= b; i --)
 27 
 28 template<class T> T MAX(T a, T b) { return a > b ? a : b; }
 29 template<class T> T MIN(T a, T b) { return a < b ? a : b; }
 30 template<class T> T GCD(T a, T b) { return b ? GCD(b, a%b) : a; }
 31 template<class T> T LCM(T a, T b) { return a / GCD(a,b) * b;    }
 32 
 33 //typedef __int64 LL;
 34 typedef long long LL;
 35 const int MAXN = 100000 + 100;
 36 const int MAXM = 110000;
 37 const double eps = 1e-8;
 38 LL MOD = 1000000007;
 39 const double PI = 4.0 * atan(1.0);
 40 
 41 int T, n, m;
 42 char ch[3];
 43 int A, B, x;
 44 
 45 struct Node { int l, r, mx; };
 46 int a[MAXN];
 47 
 48 struct SegTree {
 49     Node node[MAXN<<2];
 50 
 51     void push_up(int k, int L, int R) {
 52         int mid = (L + R) >> 1;
 53         node[k].l = node[k << 1].l;
 54         node[k].r = node[k << 1 | 1].r;
 55         node[k].mx = max(node[k << 1].mx, node[k<<1 | 1].mx);
 56         if(a[mid] < a[mid + 1]) {
 57             if(node[k << 1].l == mid - L + 1) node[k].l += node[k << 1 | 1].l;
 58             if(node[k << 1 | 1].r == R - mid) node[k].r += node[k << 1].r;
 59             node[k].mx = max(node[k].mx, node[k << 1].r + node[k << 1 | 1].l);
 60         }
 61     }
 62 
 63     void update(int k, int L, int R, int p, int v) {
 64         if(L == R) {
 65             a[L] = v;
 66             node[k].l = node[k].r = node[k].mx = 1;
 67             return ;
 68         }
 69         int mid = (L + R) >> 1;
 70         if(p <= mid) update(lson, p, v);
 71         else update(rson, p, v);
 72         push_up(k, L, R);
 73     }
 74 
 75     int query(int k, int L, int R, int l, int r) {
 76         if(L > r || R < l) return 0;
 77         if(l <= L && R <= r) return node[k].mx;
 78         int mid = (L + R) >> 1;
 79         int ans = max(query(lson, l, r), query(rson, l, r));
 80         if(a[mid] < a[mid + 1] && l <= mid && mid <= r) {
 81             ans = max(ans, min(node[k << 1].r, mid - l + 1) + min(node[k << 1 | 1].l, r - mid));
 82         }
 83         return ans;
 84     }
 85 
 86 }segTree;
 87 
 88 int main()
 89 {
 90 //    FIN;
 91     cin >> T;
 92     while(T--) {
 93         cin >> n >> m;
 94         rep (i, 1, n) {
 95             scanf("%d", &x);
 96             segTree.update(1, 1, n, i, x);
 97         }
 98         while(m --) {
 99             scanf("%s %d %d", &ch, &A, &B);
100             if(ch[0] == 'Q') {
101                 printf("%d\n", segTree.query(1, 1, n, ++A, ++B));
102             }
103             else {
104                 segTree.update(1, 1, n, ++A, B);
105                 //rep (i, 1, n) printf("%d%c", a[i], i == n ? '\n' : ' ');
106             }
107         }
108     }
109     return 0;
110 }
View Code

 

H:HDU 1272 并查集

简单并查集

 1 #pragma comment(linker, "/STACK:1677721600")
 2 #include <map>
 3 #include <set>
 4 #include <stack>
 5 #include <queue>
 6 #include <cmath>
 7 #include <ctime>
 8 #include <vector>
 9 #include <cstdio>
10 #include <cctype>
11 #include <cstring>
12 #include <cstdlib>
13 #include <iostream>
14 #include <algorithm>
15 using namespace std;
16 #define INF 0x3f3f3f3f
17 #define inf (-((LL)1<<40))
18 #define lson k<<1, L, (L + R)>>1
19 #define rson k<<1|1,  ((L + R)>>1) + 1, R
20 #define mem0(a) memset(a,0,sizeof(a))
21 #define mem1(a) memset(a,-1,sizeof(a))
22 #define mem(a, b) memset(a, b, sizeof(a))
23 #define FIN freopen("in.txt", "r", stdin)
24 #define FOUT freopen("out.txt", "w", stdout)
25 #define rep(i, a, b) for(int i = a; i <= b; i ++)
26 #define dec(i, a, b) for(int i = a; i >= b; i --)
27 
28 template<class T> T MAX(T a, T b) { return a > b ? a : b; }
29 template<class T> T MIN(T a, T b) { return a < b ? a : b; }
30 template<class T> T GCD(T a, T b) { return b ? GCD(b, a%b) : a; }
31 template<class T> T LCM(T a, T b) { return a / GCD(a,b) * b;    }
32 
33 //typedef __int64 LL;
34 typedef long long LL;
35 const int MAXN = 100000 + 100;
36 const int MAXM = 110000;
37 const double eps = 1e-8;
38 LL MOD = 1000000007;
39 const double PI = 4.0 * atan(1.0);
40 
41 int fa[100002], a, b;
42 bool vis[100002];
43 
44 int find_fa(int u) {
45     return u == fa[u] ? u : fa[u] = find_fa(fa[u]);
46 }
47 
48 int main()
49 {
50 //    FIN;
51     while(1) {
52         mem0(vis);
53         int ok = 1, cnt_v = 0, cnt_e = -1;
54         while(scanf("%d %d", &a, &b) && a) {
55             if(a == -1) return 0;
56             if(!ok) continue;
57             if(!vis[a]) fa[a] = a, vis[a] = 1, cnt_v ++;
58             if(!vis[b]) fa[b] = b, vis[b] = 1, cnt_v ++;
59             int x = find_fa(a), y = find_fa(b);
60             if(x == y) ok = 0;
61             if(cnt_e == -1) cnt_e++;
62             fa[y] = x;
63             cnt_e ++;
64         }
65         puts(ok && cnt_e + 1 == cnt_v ? "Yes" : "No");
66     }
67     return 0;
68 }
View Code

 

J,K比较简单,不放上来了。I不会= =!

posted @ 2015-07-20 21:08  再见~雨泉  阅读(228)  评论(0编辑  收藏  举报