Advertisement
osipyonok

Mo's algorithm example

Jan 30th, 2017
203
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.51 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2.  
  3. #define INF 1000010000
  4. #define nl '\n'
  5. #define pb push_back
  6. #define ppb pop_back
  7. #define mp make_pair
  8. #define fi first
  9. #define se second
  10. #define pii pair<int,int>
  11. #define pdd pair<double,double>
  12. #define all(c) (c).begin(), (c).end()
  13. #define SORT(c) sort(all(c))
  14. #define sz(c) (c).size()
  15. #define rep(i,n) for( int i = 0; i < n; ++i )
  16. #define repi(i,n) for( int i = 1 ; i <= n; ++i )
  17. #define repn(i,n) for( int i = n - 1 ; i >= 0 ; --i )
  18. #define repf(j,i,n) for( int j = i ; j < n ; ++j )
  19. #define die(s) {std::cout << s << nl;}
  20. #define dier(s) {std::cout << s; return 0;}
  21. #define vi vector<int>
  22. typedef long long ll;
  23. #define MAXN 505050
  24. #define LEN (int)(sqrt(MAXN) + 1)
  25. using namespace std;
  26.  
  27. struct Query{
  28.     int l , r , i;
  29.     Query(int _l , int _r , int _i){
  30.         l = _l;
  31.         r = _r;
  32.         i = _i;
  33.     }
  34. };
  35.  
  36. class Mo{
  37.     vector<Query> q;
  38.     public:
  39.     Mo(int query_count , int maxn){
  40.         int l , r;
  41.         rep(kk , query_count){
  42.             cin >> l >> r;
  43.             q.pb(Query(l , r , kk));
  44.         }
  45.         struct{
  46.             bool operator()(Query & a , Query & b){  
  47.                 return a.l / LEN < b.l / LEN || (a.l / LEN == b.l / LEN && a.r / LEN < b.r / LEN);
  48.             }  
  49.         }comp;
  50.         sort(all(q) , comp);
  51.     }
  52.     Query & getQuery(int index){
  53.         assert(index < sz(q));
  54.         return q[index];
  55.     }
  56. };
  57.  
  58.  
  59. vi a(MAXN , 0);
  60. map<int , int> m;
  61. vi cnt(MAXN , 0);
  62. vi ans(MAXN , 0);
  63.  
  64. int main() {
  65.     ios_base::sync_with_stdio(false);
  66.     cin.tie(NULL);
  67.     cout.precision(0);
  68.     int num = 1;
  69.     int n , k;
  70.     cin >> n >> k;
  71.     repi(i , n){
  72.         cin >> a[i];
  73.         if(!m[a[i]]){
  74.             m[a[i]] = num;
  75.             ++num;
  76.         }
  77.         a[i] = m[a[i]];
  78.     }
  79.     Mo mo = Mo(k , n);
  80.     cnt[a[1]] = 1;
  81.     int x = 1 , y = 1;
  82.     int t = 0;
  83.     rep(i , k){
  84.         Query q = mo.getQuery(i);
  85.         int l = q.l;
  86.         int r = q.r;
  87.         while(x > l){
  88.             --x;
  89.             if(cnt[a[x]] == 2)--t;
  90.             ++cnt[a[x]];
  91.             if(cnt[a[x]] == 2)++t;
  92.         }
  93.         while(x < l){
  94.             if(cnt[a[x]] == 2)--t;
  95.             --cnt[a[x]];
  96.             if(cnt[a[x]] == 2)++t;
  97.             ++x;
  98.         }
  99.         while(y < r){
  100.             ++y;
  101.             if(cnt[a[y]] == 2)--t;
  102.             ++cnt[a[y]];
  103.             if(cnt[a[y]] == 2)++t;
  104.         }
  105.         while(y > r){
  106.             if(cnt[a[y]] == 2)--t;
  107.             --cnt[a[y]];
  108.             if(cnt[a[y]] == 2)++t;
  109.             --y;
  110.         }
  111.         ans[q.i] = t;
  112.     }  
  113.     rep(i , k){
  114.         die(ans[i]);
  115.     }
  116.     return 0;
  117. }
  118.  
  119. /*
  120. task Poklon from COCI 2016/2017 Round #5, January 21st, 2017
  121. Little Mirko is a very simple man. Mirko’s friend Darko has given him an array of N natural
  122. integers and asked him Q queries about the array that Mirko must answer.
  123. Each query consists of two integers, the positions of the left and right end of an interval in
  124. the array. The answer to the query is the number of different values that appear exactly
  125. twice in the given interval.
  126. INPUT
  127. The first line of input contains the integers N and Q (1 ≤ N, Q ≤ 500 000).
  128. The second line of input contains N natural integers less than 1 000 000 000, the elements
  129. of the array.
  130. Each of the following Q lines contains two integers, L and R (1 ≤ L ≤ R ≤ N), from the task.
  131. OUTPUT
  132. The output must consist of Q lines, each line containing the answer to a query, respectively.
  133. SCORING
  134. In test cases worth 56 points in total, the numbers N and Q will not be larger than 5000.
  135. Input:
  136. 5 2
  137. 1 1 2 2 3
  138. 1 1
  139. 1 5
  140. Output:
  141. 0
  142. 2
  143. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement