|
| 1 | +#include <bits/stdc++.h> |
| 2 | +#include <algorithm> |
| 3 | +#define MIN(a,b) (a<b?a:b) |
| 4 | +#define MAX(a,b) (a>b?a:b) |
| 5 | +#define pi 3.14159265358979323846264338327950288419716939937510582097494459230 |
| 6 | + |
| 7 | +using namespace std; |
| 8 | +typedef unsigned long long ull; |
| 9 | +typedef long double ld; |
| 10 | +typedef pair<int, int> pii; |
| 11 | +int blocksize; |
| 12 | +int count_x_x[1000000] = {0}; |
| 13 | +int a[30000]; |
| 14 | +typedef struct |
| 15 | +{ |
| 16 | + int left; |
| 17 | + int right; |
| 18 | + int query_num; |
| 19 | +}Q; |
| 20 | +bool sortQuery (Q i, Q j) |
| 21 | +{ |
| 22 | + if(i.left/blocksize != j.left/blocksize) |
| 23 | + { |
| 24 | + //sort by left index |
| 25 | + return i.left/blocksize < j.left/blocksize; |
| 26 | + } |
| 27 | + return i.right < j.right; |
| 28 | +} |
| 29 | +void add(int index, int &answer) |
| 30 | +{ |
| 31 | + count_x_x[a[index]]++; |
| 32 | + if(count_x_x[a[index]]==1) |
| 33 | + answer++; |
| 34 | +} |
| 35 | +void remove(int index, int &answer) |
| 36 | +{ |
| 37 | + //if(count_x_x[a[index]]) |
| 38 | + { |
| 39 | + count_x_x[a[index]]--; |
| 40 | + if(count_x_x[a[index]]==0) |
| 41 | + answer--; |
| 42 | + } |
| 43 | +} |
| 44 | +int main(int argc, char*argv[]) |
| 45 | +{ |
| 46 | + //ios_base::sync_with_stdio(false); |
| 47 | + //cin.tie(NULL); |
| 48 | + int N, t; |
| 49 | + scanf("%d",&N);; |
| 50 | + |
| 51 | + for (int i=0;i<N;i++) |
| 52 | + scanf("%d",&a[i]); |
| 53 | + int Qn; |
| 54 | + scanf("%d",&Qn); |
| 55 | + Q q[Qn]; |
| 56 | + for (int i=0; i<Qn;i++) |
| 57 | + { |
| 58 | + scanf("%d %d",&q[i].left,&q[i].right); |
| 59 | + q[i].query_num = i; |
| 60 | + // Comment if using 0 based indexing |
| 61 | + q[i].left--; |
| 62 | + q[i].right--; |
| 63 | + } |
| 64 | + |
| 65 | + blocksize = sqrt(N); |
| 66 | + ////cout<<"going to sort"<<blocksize<<endl; |
| 67 | + sort(q, q+Qn, sortQuery); |
| 68 | +#if 0 |
| 69 | + for (auto i : q) // access by value, the type of i is int |
| 70 | + std:://cout << i.left << i.right <<i.query_num<<endl; |
| 71 | +#endif |
| 72 | + int answer[Qn] ={0}; |
| 73 | + int currentL = 0; |
| 74 | + int currentR = 0; |
| 75 | + |
| 76 | + for(int i=0; i<Qn;i++) |
| 77 | + { |
| 78 | + cout <<"Index"<<i<<endl; |
| 79 | + if(i!=0) |
| 80 | + answer[q[i].query_num] = answer[q[i-1].query_num]; |
| 81 | + while(currentL < q[i].left) |
| 82 | + { |
| 83 | + remove(currentL, answer[q[i].query_num]); |
| 84 | + currentL++; |
| 85 | + } |
| 86 | + cout<<answer[q[i].query_num]<<count_x_x[7]<<endl; |
| 87 | + while(currentL > q[i].left) |
| 88 | + { |
| 89 | + add(currentL-1, answer[q[i].query_num]); |
| 90 | + currentL--; |
| 91 | + } |
| 92 | + cout<<answer[q[i].query_num]<<count_x_x[7]<<endl; |
| 93 | + while(currentR <= q[i].right) |
| 94 | + { |
| 95 | + add(currentR, answer[q[i].query_num]); |
| 96 | + currentR++; |
| 97 | + } |
| 98 | + cout<<answer[q[i].query_num]<<count_x_x[7]<<endl; |
| 99 | + while(currentR > q[i].right+1) |
| 100 | + { |
| 101 | + remove(currentR-1, answer[q[i].query_num]); |
| 102 | + currentR--; |
| 103 | + } |
| 104 | + cout<<answer[q[i].query_num]<<count_x_x[7]<<endl; |
| 105 | + } |
| 106 | + for (auto i : answer) // access by value, the type of i is int |
| 107 | + printf("%d\n",i); |
| 108 | +} |
0 commit comments