-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDQUERY.c
146 lines (138 loc) · 2.32 KB
/
DQUERY.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#include<bits/stdc++.h>
using namespace std;
#define MAX 100005
#define optimizar_io ios_base::sync_with_stdio(0);cin.tie(0);
#define gc getchar_unlocked
inline void write_int(int x)
{
#ifdef USE_SIGN
if (x < 0)
{
putchar('-');
x = -x;
}
#endif
char buf[10], *p = buf;
do
{
*p++ = '0' + x % 10;
x /= 10;
}
while (x);
do
{
putchar(*--p);
}
while (p > buf);
}
inline int read_int()
{
char c;
while (c = getchar(), c <= ' ');
#ifdef USE_SIGN
bool sign = c == '-';
if (sign)
{
c = getchar();
}
#endif
int res = c - '0';
while (c = getchar(), c >= '0' && c <= '9')
{
res = res * 10 + (c - '0');
}
// One character is gobbled here
#ifdef USE_SIGN
return sign ? -res : res;
#else
return res;
#endif
}
inline void scanint(int &x)
{
register int c = gc();
x = 0;
int neg = 0;
for(;((c<48 || c>57) && c != '-');c = gc());
if(c=='-') {neg=1;c=gc();}
for(;c>47 && c<58;c = gc()) {x = (x<<1) + (x<<3) + c - 48;}
if(neg) x=-x;
}
int arr[MAX];
int block;
struct node
{
int s,e,ind;
} que[20*MAX];
bool mo(node a, node b)
{
int block_a = a.s/block;
int block_b = b.s/block;
if(block_a != block_b)
return a.e < b.e;
return block_a < block_b;
}
int mark[MAX];
int answer[MAX*20];
int main()
{
optimizar_io
int n;
int i;
int q;
int mo_left,mo_right,left,right;
int ind;
int dis = 0;
int a_idx;
scanint(n);
for(i=0;i<n;++i)
scanint(arr[i]);
scanint(q);
for(i=0;i<q;++i)
{
scanint(que[i].s);
scanint(que[i].e);
que[i].ind = i;
}
block = 555;
sort(que,que+q,mo);
mo_left = 0;
mo_right = -1;
memset(mark,0,sizeof(mark));
for(i=0;i<q;++i)
{
left = que[i].s-1;
right = que[i].e-1;
a_idx = que[i].ind;
while(mo_right < right)
{
mo_right++;
mark[arr[mo_right]]++;
if(mark[arr[mo_right]] == 1) dis++;
}
while(mo_right > right)
{
mark[arr[mo_right]]--;
if(mark[arr[mo_right]] == 0) dis--;
mo_right--;
}
while(mo_left < left)
{
mark[arr[mo_left]]--;
if(mark[arr[mo_left]] == 0) dis--;
mo_left++;
}
while(mo_left > left)
{
mo_left--;
mark[arr[mo_left]]++;
if(mark[arr[mo_left]] == 1) dis++;
}
answer[a_idx] = dis;
}
for(i=0;i<q;++i)
{
write_int(answer[i]);
putchar('\n');
}
}