fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. #define fast ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL)
  4. #define mofile(s) freopen(s,"r",stdin)
  5. #define outfile(s) freopen(s,"w",stdout)
  6. #define ll long long
  7. #define ii pair<ll,ll>
  8. #define iii pair<ll,ii>
  9. #define fi first
  10. #define se second
  11. #define tf bool
  12. #define ST stack
  13. #define Q deque
  14. #define Q queue
  15. #define S string
  16. #define Ma map
  17. #define UM unormideremid_map
  18. #define SE set
  19. #define str(x) to_string(x)
  20. #define all(a) (a).begin(),(a).end()
  21. #define FOR(i,l,r,mid) for(int i=l;i<=r;i+=mid)
  22. #define FOD(i,l,r,mid) for(int i=r;i>=l;i-=mid)
  23. #define xuong cout<<"\n"
  24. #define midebug(x) cout<<(x)<<" "
  25. #define ppcnt(x) __builtin_popcountll(x)
  26. #define parity(x) __builtin_parityll(x)
  27. #define leamid0(x) __builtin_clzll(x)
  28. #define LOG2 __lg(x)
  29. #define tr0(x) __builtin_ctzll(x)
  30. #define fiset(x) __builtin_ffsll(x)
  31. #define MASK(k) (1LL<<(k))
  32. #define BIT(x,k) ((x)>>(k)&1)
  33. #define pb push_back
  34. #define tron(x) setprecision(x)
  35. #define het return 0
  36. #define base_ 1000000000
  37. template<class X, class Y>
  38. bool maximize(X &x, const Y &y){return (x < y) ? x = y, 1 : 0;}
  39. template<class X, class Y>
  40. bool minimize(X &x, const Y &y){return (x > y) ? x = y, 1 : 0;}
  41. const int maxn=1e6+5;
  42. const ll tle=2e8;
  43. const ll INF=1e9+9;
  44. const int base=31;
  45. string bcc="abcmidefghijklmnopqrstuvwxyz";
  46. int midx[]={-1,0,1,0};
  47. int midy[]={0,1,0,-1};
  48. bool sang[10000005];
  49. ll pref[1005][1005],mt[1005][1005];
  50. void sieve(){
  51. for(int i=1;i<=10000000;++i) sang[i]=1;
  52. sang[0]=sang[1]=0;
  53. for(int i=2;i*i<=10000000;++i){
  54. if(sang[i]){
  55. for(int j=i*i;j<=10000000;j+=i) sang[j]=0;
  56. }
  57. }
  58. }
  59. void lis(){
  60. vector<int>t;
  61. vector<int>a;
  62. int n; cin>>n;
  63. for(int i=1;i<=n;++i){
  64. int ai; cin>>ai;
  65. a.pb(ai);
  66. }
  67. for(int x:a){
  68. auto it=lower_bound(all(t),x);
  69. if(it==t.end()) t.pb(x);
  70. else *it=x;
  71. }
  72. }
  73. void pfs2mid(){
  74. int n,m,k; cin>>n>>k; m=n;
  75. for(int i=1;i<=n;++i){
  76. for(int j=1;j<=m;++j) cin>>mt[i][j];
  77. }
  78. for(int i=1;i<=n;++i){
  79. for(int j=1;j<=m;++j) pref[i][j]=mt[i][j]+pref[i-1][j]+pref[i][j-1]-pref[i-1][j-1];
  80. }
  81. }
  82. ll qu2mid(int x1,int y1,int x2,int y2){
  83. return pref[x2][y2]-pref[x1-1][y2]-pref[x2][y1-1]+pref[x1-1][y1-1];
  84. }
  85. void open(){
  86. if(fopen("mideptrai.INP","r")){
  87. mofile("mideptrai.INP");
  88. outfile("mideptrai.OUT");
  89. }
  90. }
  91. const int LIM = 1e5 + 5;
  92.  
  93. int n, q, shelf[LIM];
  94. map <int, vector <int>> f;
  95.  
  96. signed main() {
  97.  
  98. ios::sync_with_stdio(false);
  99. cin.tie(nullptr);
  100.  
  101. cin >> n >> q;
  102. for (int i = 1; i <= n; ++i) {
  103.  
  104. cin >> shelf[i];
  105. int num = shelf[i];
  106.  
  107. if (f[num].empty()) f[num].push_back(0);
  108. f[num].push_back(i);
  109. }
  110.  
  111. int qq = q;
  112. while (q--) {
  113.  
  114. if (q < qq - 1) cout << endl;
  115.  
  116. int l, r, x;
  117. cin >> l >> r >> x;
  118.  
  119. int L, R, M, lll, rr;
  120.  
  121. L = 1, R = f[x].size() - 1;
  122. while (L <= R) {
  123.  
  124. M = (L + R) / 2;
  125.  
  126. if (f[x][M] >= l) R = M - 1;
  127. else L = M + 1;
  128. }
  129. lll = L;
  130.  
  131. L = 1, R = f[x].size() - 1;
  132. while (L <= R) {
  133.  
  134. M = (L + R) / 2;
  135.  
  136. if (f[x][M] <= r) L = M + 1;
  137. else R = M - 1;
  138. }
  139. rr = R;
  140.  
  141. cout << (lll > rr ? 0 : (rr - lll + 1));
  142. }
  143. }
Success #stdin #stdout 0s 5540KB
stdin
7 3
1 2 1 4 2 4 2
1 4 1
2 7 2
3 5 4
stdout
2
3
1