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

BZOJ2120: 数颜色

时间:2014-08-30 00:00:58      阅读:227      评论:0      收藏:0      [点我收藏+]

标签:des   style   blog   http   color   os   io   ar   for   

2120: 数颜色

Time Limit: 6 Sec  Memory Limit: 259 MB
Submit: 1394  Solved: 516
[Submit][Status]

Description

墨墨购买了一套N支彩色画笔(其中有些颜色可能相同),摆成一排,你需要回答墨墨的提问。墨墨会像你发布如下指令: 1、 Q L R代表询问你从第L支画笔到第R支画笔中共有几种不同颜色的画笔。 2、 R P Col 把第P支画笔替换为颜色Col。为了满足墨墨的要求,你知道你需要干什么了吗?

Input

第1行两个整数N,M,分别代表初始画笔的数量以及墨墨会做的事情的个数。第2行N个整数,分别代表初始画笔排中第i支画笔的颜色。第3行到第2+M行,每行分别代表墨墨会做的一件事情,格式见题干部分。

Output

对于每一个Query的询问,你需要在对应的行中给出一个数字,代表第L支画笔到第R支画笔中共有几种不同颜色的画笔。

Sample Input

6 5
1 2 3 4 5 5
Q 1 4
Q 2 6
R 1 2
Q 1 4
Q 2 6

Sample Output

4
4
3
4

HINT

 

对于100%的数据,N≤10000,M≤10000,修改操作不多于1000次,所有的输入数据中出现的所有整数均大于等于1且不超过10^6。

 

Source

题解:

同维护队列

代码:

bubuko.com,布布扣
 1 #include<cstdio>
 2 #include<cstdlib>
 3 #include<cmath>
 4 #include<cstring>
 5 #include<algorithm>
 6 #include<iostream>
 7 #include<vector>
 8 #include<map>
 9 #include<set>
10 #include<queue>
11 #include<string>
12 #define inf 1000000000
13 #define maxn 10000+1000
14 #define maxm 1000000+1000
15 #define eps 1e-10
16 #define ll long long
17 #define pa pair<int,int>
18 using namespace std;
19 inline int read()
20 {
21     int x=0,f=1;char ch=getchar();
22     while(ch<0||ch>9){if(ch==-)f=-1;ch=getchar();}
23     while(ch>=0&&ch<=9){x=10*x+ch-0;ch=getchar();}
24     return x*f;
25 }
26 int n,m,block,b[maxn],c[maxn],pre[maxn],pos[maxn],last[maxm];
27 void reset(int x)
28 {
29     int l=(x-1)*block+1,r=min(x*block,n);
30     for(int i=l;i<=r;i++)pre[i]=b[i];
31     sort(pre+l,pre+r+1);
32 }
33 int find(int x,int y)
34 {
35     int l=(x-1)*block+1,r=x*block,mid;
36     while(l<=r)
37     {
38         mid=(l+r)>>1;
39         if(pre[mid]>=y)r=mid-1;else l=mid+1;
40     }
41     return l-(x-1)*block-1;
42 }
43 int query(int x,int y)
44 {
45     int sum=0,bx=pos[x],by=pos[y];
46     if(by-bx<=1)
47     {
48         for(int i=x;i<=y;i++)if(b[i]<x)sum++;
49     }
50     else
51     {
52         for(int i=x;i<=bx*block;i++)if(b[i]<x)sum++;
53         for(int i=(by-1)*block+1;i<=y;i++)if(b[i]<x)sum++;
54     }
55     for(int i=bx+1;i<by;i++)sum+=find(i,x);
56     return sum;
57 }
58 void change(int x,int y)
59 {
60     for(int i=1;i<=n;i++)last[c[i]]=0;
61     c[x]=y;
62     for(int i=1;i<=n;i++)
63     {
64         int t=b[i];
65         b[i]=last[c[i]];
66         last[c[i]]=i;
67         if(t!=b[i])reset(pos[i]);
68     }
69 }
70 int main()
71 {
72     freopen("input.txt","r",stdin);
73     freopen("output.txt","w",stdout);
74     n=read();m=read();
75     block=floor(sqrt(n));
76     for(int i=1;i<=n;i++)
77      {
78          c[i]=read();
79          pos[i]=(i-1)/block+1;
80          b[i]=last[c[i]];
81          last[c[i]]=i;
82      }
83     for(int i=1;i<=pos[n];i++)reset(i);
84     char ch;int x,y;
85     while(m--)
86     {
87         ch= ;
88         while(ch!=Q&&ch!=R)ch=getchar();x=read();y=read();
89         if(ch==R)change(x,y);else printf("%d\n",query(x,y));
90     } 
91     return 0;
92 }
View Code

 

BZOJ2120: 数颜色

标签:des   style   blog   http   color   os   io   ar   for   

原文地址:http://www.cnblogs.com/zyfzyf/p/3945856.html

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