生活随笔
收集整理的這篇文章主要介紹了
bzoj2743
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
其實和bzoj1878類似
只不過要求的是區間內數量多于1個的數字種數
其實還是按照bzoj1878做
只不過我們是把每一種數字下一個出現的位置+1,并把這個位置置為0
1 var x,y,ans,p,last,a,c,next:
array[
0..
1000010]
of longint;
2 max,i,n,m,j:longint;
3
4 function lowbit(x:longint):longint;
5 begin
6 exit(x
and(-
x));
7 end;
8
9 function sum(x:longint):longint;
10 begin
11 sum:=
0;
12 while x<>
0 do
13 begin
14 sum:=sum+
c[x];
15 x:=x-
lowbit(x);
16 end;
17 end;
18
19 procedure add(x,t:longint);
20 begin
21 while x<=n
do
22 begin
23 inc(c[x],t);
24 x:=x+
lowbit(x);
25 end;
26 end;
27
28 procedure swap(
var a,b:longint);
29 var c:longint;
30 begin
31 c:=
a;
32 a:=
b;
33 b:=
c;
34 end;
35
36 procedure sort(l,r: longint);
37 var i,j,z: longint;
38 begin
39 i:=
l;
40 j:=
r;
41 z:=x[(l+r)
div 2];
42 repeat
43 while x[i]<z
do inc(i);
44 while z<x[j]
do dec(j);
45 if not(i>j)
then
46 begin
47 swap(x[i],x[j]);
48 swap(y[i],y[j]);
49 swap(p[i],p[j]);
50 inc(i);
51 dec(j);
52 end;
53 until i>
j;
54 if l<j
then sort(l,j);
55 if i<r
then sort(i,r);
56 end;
57
58 begin
59 readln(n,max,m);
60 for i:=
1 to n
do
61 read(a[i]);
62
63 for i:=
1 to m
do
64 begin
65 readln(x[i],y[i]);
66 p[i]:=
i;
67 end;
68 sort(
1,m);
69 fillchar(last,sizeof(last),
0);
70 for i:=n
downto 1 do
71 begin
72 next[i]:=
last[a[i]];
73 last[a[i]]:=
i;
74 end;
75 for i:=
1 to max
do
76 if next[last[i]]<>
0 then add(next[last[i]],
1);
77
78 j:=
1;
79 for i:=
1 to n
do
80 begin
81 while x[j]=i
do
82 begin
83 ans[p[j]]:=sum(y[j])-sum(x[j]-
1);
84 inc(j);
85 end;
86 if next[i]<>
0 then add(next[i],-
1);
87 if next[next[i]]<>
0 then add(next[next[i]],
1);
88 end;
89 for i:=
1 to m
do
90 writeln(ans[i]);
91 end.
View Code ?
轉載于:https://www.cnblogs.com/phile/p/4473139.html
總結
以上是生活随笔為你收集整理的bzoj2743的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。