• C. Sonya and Queries


    http://codeforces.com/contest/714/problem/C

    看到这题目,想想,肯定不能暴力啊,如果用map,如何快速找到满足要求的数目,然后,长度18,我想,这不是熟悉的trie树么,还犹豫什么,把所有的输入都处理成长度为18的字符串,处理就行了,然后调试了一会,就交了,然后tle了,我就怀疑了,难道不是这样的套路么?!然后就开始查答案,一看官方题解,恍然大悟,这道题目有个很好的性质,就是插入11和插入33的效果是一样的,删除11和删除33的效果是一致的,同一种模型,我们不必具体的区分该位具体是什么,比如奇数(1,3,5,7,9)都可以简单用1来表示,偶数(0,2,4,6,8)都可以用0表示,这样题目就化简很多,还要trie树做什么,同一种模式可以归为一类,总共1 << 18种方式,然后单独统计,单独输出,就ok!就完了,感觉还是太笨了!也没仔细去分析。

     1 #include<bits/stdc++.h>
     2 #define pb push_back
     3 #define FOR(i, n) for (int i = 0; i < (int)n; ++i)
     4 #define dbg(x) cout << #x << " at line " << __LINE__ << " is: " << x << endl
     5 typedef long long ll;
     6 using namespace std;
     7 typedef pair<int, int> pii;
     8 const int maxn = 1 << 19;
     9 int a[maxn];
    10 int n;
    11 char ch[25];
    12 ll x;
    13 ll d[20];
    14 void solve() {
    15     d[0] = 1;
    16     for (int i = 1; i < 18; i++) d[i] = d[i - 1] * 10;
    17     scanf("%d", &n);
    18     while(n--) {
    19         scanf("%s", ch);
    20         //cout << ch << endl;
    21         if(ch[0] == '?') {
    22             scanf("%s", ch);
    23             int l = strlen(ch);
    24             int t = 0;
    25             for (int i = 0; i < l; i++) {
    26                 if(ch[i] == '1')
    27                     t |= (1 << (l - i - 1));
    28             }
    29             //cout << "ask " << t << endl;
    30             printf("%d
    ", a[t]);
    31         } else {
    32             scanf("%I64d", &x);
    33             int t = 0;
    34             for (int i = 17; i >= 0; i--) {
    35                 int td = x / d[i];
    36                 x %= d[i];
    37                 if(td & 1) t |= (1 << i);
    38             }
    39             //cout << "test "<< t << endl;
    40             if(ch[0] == '+')
    41                 a[t]++;
    42             else a[t]--;
    43         }
    44     }
    45 }
    46 int main() {
    47 
    48     //freopen("test.in", "r", stdin);
    49     //freopen("test.out", "w", stdout);
    50     solve();
    51     return 0;
    52 }
  • 相关阅读:
    C/S模式客户端连接服务器连接不上的问题
    C#获取网络状态
    SQL2008R转SQL2005
    Centos7 调整磁盘空间
    crontab 定时任务
    nginx 简单教程
    vagrant 使用
    加快 hive 查询的 5 种方法
    编译 ambari 2.7.3
    kylin 连接 hortonworks 中的 hive 遇到的问题
  • 原文地址:https://www.cnblogs.com/y119777/p/5876534.html
Copyright © 2020-2023  润新知