码迷,mamicode.com
首页 > 其他好文 > 详细

zzuli Camellia的难题(暴力)

时间:2015-11-22 12:32:59      阅读:130      评论:0      收藏:0      [点我收藏+]

标签:

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\n",dt[x]);
59                     continue;
60                 }
61                 if(gg==5){
62                     printf("%d\n",ans);
63                     continue;
64                 }
65             }
66         }
67     return 0;
68 } 

 

zzuli Camellia的难题(暴力)

标签:

原文地址:http://www.cnblogs.com/handsomecui/p/4985534.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!