zoukankan      html  css  js  c++  java
  • BZOJ3166 [Heoi2013]Alo

    Orz hzwer && zky

    我说怎么做过。。。以前交了个暴力,竟然8s过了。。。Σ( ° △ °|||)︴

    好吧好吧。。重新写。。。

    但是怎么感觉。。。hzwer的trie的insert有点小小的问题呢?(再研究研究)

     1 /**************************************************************
     2     Problem: 3166
     3     User: rausen
     4     Language: C++
     5     Result: Accepted
     6     Time:1224 ms
     7     Memory:22964 kb
     8 ****************************************************************/
     9  
    10 #include <cstdio>
    11 #include <algorithm>
    12 #include <set>
    13  
    14 using namespace std;
    15 const int N = 50005;
    16 const int inf = 1e9;
    17 struct data {
    18     int v, w;
    19     data() {}
    20     data(int _v, int _w) : v(_v), w(_w) {}
    21      
    22     inline bool operator < (const data &x) const {
    23         return v > x.v;
    24     }
    25 } a[N];
    26  
    27 struct trie_node {
    28     int son[2], cnt;
    29 } t[N * 35];
    30 int cnt_trie;
    31  
    32 int n, root[N];
    33 set <int> q;
    34  
    35 inline int read() {
    36     int x = 0;
    37     char ch = getchar();
    38     while (ch < '0' || '9' < ch)
    39         ch = getchar();
    40     while ('0' <= ch && ch <= '9') {
    41         x = x * 10 + ch - '0';
    42         ch = getchar();
    43     }
    44     return x;
    45 }
    46  
    47 int A[35];
    48 void insert(int &root, int v) {
    49     int R = root, now = ++cnt_trie, i;
    50     root = now;
    51     for (i = 0; i <= 30; ++i)
    52         A[i] = v & 1, v >>= 1;
    53     reverse(A, A + 31);
    54     for (i = 0; i <= 30; ++i) {
    55         t[now].son[0] = t[R].son[0], t[now].son[1] = t[R].son[1];
    56         R = t[R].son[A[i]], now = t[now].son[A[i]] = ++cnt_trie;
    57         t[now].cnt = t[R].cnt + 1;
    58     }
    59 }
    60  
    61 int query(int x, int y, int v) {
    62     int res = 0, i;
    63     for (i = 0; i <= 30; ++i)
    64         A[i] = v & 1, v >>= 1;
    65     reverse(A, A + 31);
    66     for (i = 0; i <= 30; ++i) {
    67         if (t[t[y].son[!A[i]]].cnt - t[t[x].son[!A[i]]].cnt)
    68             x = t[x].son[!A[i]], y = t[y].son[!A[i]], res += (1 << 30 - i);
    69             else x = t[x].son[A[i]], y = t[y].son[A[i]];
    70     }
    71     return res;
    72 }
    73  
    74 int main() {
    75     int i, l, r, ans = 0;
    76     n = read(), root[0] = cnt_trie = 1;
    77     for (i = 1; i <= n; ++i) {
    78         a[i] = data(read(), i), root[i] = root[i - 1];
    79         insert(root[i], a[i].v);
    80     }
    81     sort(a + 1, a + n + 1);
    82     q.insert(-1), q.insert(-2), q.insert(inf), q.insert(inf + 1);
    83     q.insert(a[i].w);
    84     for (i = 2; i <= n; ++i) {
    85         l = max(1, *--(--q.lower_bound(a[i].w)) + 1);
    86         r = min(n, *++q.upper_bound(a[i].w) - 1);
    87         if (l > r) continue;
    88         ans = max(ans, query(root[l - 1], root[r], a[i].v));
    89         q.insert(a[i].w);
    90     }
    91     printf("%d
    ", ans);
    92     return 0;
    93 }
    View Code

    (p.s.  论大神们都喜欢封装struct和class...)

    updata @ 2014/12/21 10:33

    我的第67行一开始是是这么写的: 

    if (t[y].son[!A[i]] - t[x].son[!A[i]])  

    竟然过了!!!过了!!!了、、、

    说好的加强型数据呢。。。hta。。。

    By Xs酱~ 转载请说明 博客地址:http://www.cnblogs.com/rausen
  • 相关阅读:
    this.get_element .style为空或不是对象
    在server2008R2的IIS7中调试asp程序遇到的错误
    Server.Transfer 在两个窗体之间传递参数用法
    页面事务处理 ContextUtil.SetComplete(); 没有 MTS 对象上下文
    VS2008切换设计视图卡死 停止响应
    javascript对象小结
    [JavaScript] 最低公用标准的浏览器文档对象层次
    Linq 此提供程序只支持对返回实体或投影(包含所有标识列)的有序查询使用 问题的解决
    [Design] Factory Pattern
    [Database] SqlServer: Linq to Sql THE DATACONTEXT Change Processing
  • 原文地址:https://www.cnblogs.com/rausen/p/4176201.html
Copyright © 2011-2022 走看看