Advertisement
Guest User

Untitled

a guest
Apr 20th, 2019
374
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.19 KB | None | 0 0
  1. /**********************************************************************************/  
  2. /*  Problem: z033 "吃點心" from 2017 NPSC 國中組初試                                      */  
  3. /*  Language: C++                                                                 */  
  4. /*  Result: AC (1690ms, 26197KB) on ZeroJudge                                     */  
  5. /*  Author: sifmelcara at 2019-04-21 10:43:15                                     */  
  6. /**********************************************************************************/  
  7.  
  8.  
  9. #include <cstdio>  
  10. #include <iostream>  
  11. #include <cstdlib>  
  12. #include <cstring>  
  13. #include <map>  
  14.  
  15. using namespace std ;  
  16.  
  17. // https://www.boost.org/doc/libs/1_64_0/boost/functional/hash/hash.hpp  
  18. template <typename SizeT>  
  19. inline void hash_combine_impl(SizeT& seed, SizeT value)  
  20. {  
  21.     seed ^= value + 0x9e3779b9 + (seed<<6) + (seed>>2);  
  22. }  
  23.  
  24. uint64_t hashPair(uint64_t v1, uint64_t v2) {  
  25.     hash_combine_impl(v1, v2);  
  26.     return v1;  
  27. }  
  28.  
  29. #define VAL_SIZE ((1<<21)+11)  
  30. uint64_t val[VAL_SIZE];  
  31.  
  32. void build(int now, int l, int r) {  
  33.     if (l+1 == r) return;  
  34.     int mid = (l+r)/2;  
  35.     build(now*2, l, mid);  
  36.     build(now*2+1, mid, r);  
  37.     val[now] = hashPair(val[now*2], val[now*2+1]);  
  38. }  
  39.  
  40. void toggle(int now, int l, int r, int loc) {  
  41.     if (loc < l) return;  
  42.     if (loc >= r) return;  
  43.     if (loc == l && l+1 == r) {  
  44.         val[now] ^= 0x123456789ULL;  
  45.         return;  
  46.     }  
  47.     int mid = (l+r)/2;  
  48.     toggle(now*2, l, mid, loc);  
  49.     toggle(now*2+1, mid, r, loc);  
  50.     val[now] = hashPair(val[now*2], val[now*2+1]);  
  51. }  
  52.  
  53. int main()  
  54. {  
  55.     for (int i = 0 ; i != VAL_SIZE ; i++)  
  56.         val[i] = rand() + (rand() << 30);  
  57.     int N;  
  58.     build(1, 0, 1<<20);  
  59.     while (~scanf("%d", &N)) {  
  60.         long long ans = 0;  
  61.         map<uint64_t, int> stat;  
  62.         stat[val[1]]++;  
  63.         for (int i = 0 ; i != N ; i++) {  
  64.             int a;  
  65.             scanf("%d", &a);  
  66.             toggle(1, 0, 1<<20, a);  
  67.             ans += stat[val[1]];  
  68.             stat[val[1]]++;  
  69.         }  
  70.         cout << ans << endl;  
  71.     }  
  72.     return 0;  
  73. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement