zoukankan      html  css  js  c++  java
  • zzuli Camellia的难题(暴力)

    1784: Camellia的难题

    Time Limit: 2 Sec  Memory Limit: 128 MB Submit: 67  Solved: 14
    SubmitStatusWeb Board

    Description

     Camellia遇到了一个问题,她无法解决所以来求助豆子,以下是豆子所理解的问题:给定1000万个点,编号1-1000万。每个点都有一个值,初始的时候均为-1,有n个操作,操作有以下五种。

    1 x 代表将x点更新为i,i为第几次操作。

    2 x 代表将x点更新为-1。

    3   代表把所有的点变为-1。

    4 x 查询x点的值。

    5  查询1000万个点里有多少个点不是-1。

    亲爱的同学,你能帮助他们解决这个问题么?

    Input

     首先输入一个t(t<10)代表t组数组,接下来每组数据首先输入一个n(n<100万)代表n次操作,接下来n行每行一种操作。

    Output

     对于4、5操作来言,输出它们的答案。

    Sample Input

    1 8 1 20 1 15 4 20 5 2 15 5 3 5

    Sample Output

    1 2 1 0
    题解:这个题就是给一系列操作,其中4和5是询问。。。
    暴力,以前一直用memset,想怎么用就怎么用,谁知道这次就挂挂了,原来memset很慢的,运行时间是sizeof(dt);这次的时间就是n*sizeof(dt)所以会超时,所以,想着用一个数组保存不是-1的数,当操作3的时候只需要将不是-1的数改成-1就可以了。。。刚开始还用树状数组真是多此一举,那样更慢了。。。
    代码:
     1 #include<iostream>
     2 #include<cstdio>
     3 #include<cstring>
     4 #include<cmath>
     5 #include<algorithm>
     6 using namespace std;
     7 #define mem(x,y) memset(x,y,sizeof(x))
     8 typedef long long LL;
     9 const int MAXN=1e7+10;
    10 int dt[MAXN],v[MAXN];
    11 /*int lowbit(int x){return x&(-x);}
    12 void update(int x,int y){
    13     while(x<MAXN){
    14         tree[x]+=y;
    15         x+=lowbit(x);
    16     }
    17 }
    18 int Q(int x){
    19     int sum=0;
    20     while(x>0){
    21         sum+=tree[x];
    22         x-=lowbit(x);
    23     }
    24     return sum;
    25 }*/
    26 int main(){
    27     int t,n;
    28     scanf("%d",&t);
    29     while(t--){
    30         scanf("%d",&n);
    31         mem(dt,-1);
    32         int flot=0,ans=0;
    33         int gg,x,cc=0;
    34         while(n--){
    35             flot++;
    36             scanf("%d",&gg);
    37                 if(gg==1){
    38                     scanf("%d",&x);
    39                     if(dt[x]==-1)ans++;
    40                     dt[x]=flot;
    41                     v[cc++]=x;
    42                     continue;
    43                 }    
    44                  if(gg==2){
    45                     scanf("%d",&x);
    46                     if(dt[x]!=-1)ans--;
    47                     dt[x]=-1;
    48                     continue;
    49                 }
    50                 if(gg==3){
    51                     for(int kk=0;kk<cc;kk++)dt[v[kk]]=-1;
    52                     cc=0;
    53                     ans=0;
    54                     continue;
    55                 }
    56                  if(gg==4){
    57                     scanf("%d",&x);
    58                     printf("%d
    ",dt[x]);
    59                     continue;
    60                 }
    61                 if(gg==5){
    62                     printf("%d
    ",ans);
    63                     continue;
    64                 }
    65             }
    66         }
    67     return 0;
    68 } 
  • 相关阅读:
    [LintCode] Single Number 单独的数字
    [LeetCode] 444. Sequence Reconstruction 序列重建
    [LeetCode] K-th Smallest in Lexicographical Order 字典顺序的第K小数字
    [LeetCode] Arranging Coins 排列硬币
    [LeetCode] Ternary Expression Parser 三元表达式解析器
    [LeetCode] 436. Find Right Interval 找右区间
    在Mac上配置全局的Git忽略文件
    Git『Everything up-to-date』问题解决
    Android组件化框架项目详解
    Android架构设计之插件化、组件化
  • 原文地址:https://www.cnblogs.com/handsomecui/p/4985534.html
Copyright © 2011-2022 走看看