Advertisement
Guest User

Untitled

a guest
Oct 20th, 2014
194
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.56 KB | None | 0 0
  1. #include <functional>
  2. #include <algorithm>
  3. #include <iostream>
  4. #include <climits>
  5. #include <fstream>
  6. #include <sstream>
  7. #include <iomanip>
  8. #include <numeric>
  9. #include <cstring>
  10. #include <cassert>
  11. #include <cstdio>
  12. #include <string>
  13. #include <vector>
  14. #include <bitset>
  15. #include <queue>
  16. #include <stack>
  17. #include <cmath>
  18. #include <ctime>
  19. #include <list>
  20. #include <set>
  21. #include <map>
  22. #include <unistd.h>
  23.  
  24. using namespace std;
  25.  
  26. typedef long long LL;
  27. typedef pair<int, int> pii;
  28. typedef pair<int, pii> piii;
  29. typedef vector<int> vi;
  30. typedef vector<pii> vii;
  31. typedef vector<piii> viii;
  32.  
  33. #ifdef _WIN32
  34. #define getchar_unlocked getchar
  35. #endif
  36. inline void inpint( int &n ) {
  37. n=0; register int ch = getchar_unlocked(); bool sign = 0;
  38. while(ch < 48 || ch > 57) { if(ch == '-') sign = 1; ch = getchar_unlocked(); }
  39. while(ch >= 48 && ch <= 57) { n = (n << 3) + (n << 1) + ch - 48, ch = getchar_unlocked(); }
  40. if(sign) n = -n;
  41. }
  42.  
  43. inline int sqr(int x){return x * x;}
  44. inline int cube(int x){return x * x * x;}
  45. inline LL sqrLL(LL x){return x * x;}
  46. inline LL cubeLL(LL x){return x * x * x;}
  47.  
  48. const LL LLINF = 9223372036854775807LL;
  49. const LL LLINF17 = 100000000000000000LL;
  50. const int INF = 2147483647;
  51. const int INF9 = 1000000000;
  52. const int MOD = 1000000007;
  53. const double eps = 1e-7;
  54. const double PI = acos(-1.0);
  55.  
  56. #define FOR(a,b,c) for (int (a)=(b); (a)<(c); (a)++)
  57. #define FORN(a,b,c) for (int (a)=(b); (a)<=(c); (a)++)
  58. #define FORD(a,b,c) for (int (a)=(b); (a)>=(c); (a)--)
  59. #define REP(i,n) FOR(i,0,n)
  60. #define REPN(i,n) FORN(i,1,n)
  61. #define REPD(i,n) FORD(i,n,1)
  62.  
  63. #define RESET(a,b) memset(a,b,sizeof(a))
  64. #define SYNC ios_base::sync_with_stdio(0);
  65. #define SIZE(a) (int)(a.size())
  66. #define MIN(a,b) (a) = min((a),(b))
  67. #define MAX(a,b) (a) = max((a),(b))
  68. #define ALL(a) a.begin(),a.end()
  69. #define RALL(a) a.rbegin(),a.rend()
  70. #define SIZE(a) (int)(a.size())
  71. #define LEN(a) (int)(a.length())
  72.  
  73. #define fi first
  74. #define se second
  75. #define pb push_back
  76. #define mp make_pair
  77. class FastInput {
  78. public:
  79. FastInput() {
  80. m_dataOffset = 0;
  81. m_dataSize = 0;
  82. m_v = 0x80000000;
  83. }
  84. inline uint32_t ReadNext() {
  85. if (m_dataOffset == m_dataSize) {
  86. int r = read(0, m_buffer, sizeof(m_buffer));
  87. if (r <= 0) return m_v;
  88. m_dataOffset = 0;
  89. m_dataSize = 0;
  90. int i = 0;
  91. if (m_buffer[0] < '0') {
  92. if (m_v != 0x80000000) {
  93. m_data[m_dataSize++] = m_v;
  94. m_v = 0x80000000;
  95. }
  96. for (; (i < r) && (m_buffer[i] < '0'); ++i);
  97. }
  98. for (; i < r;) {
  99. if (m_buffer[i] >= '0') {
  100. m_v = m_v * 10 + m_buffer[i] - 48;
  101. ++i;
  102. } else {
  103. m_data[m_dataSize++] = m_v;
  104. m_v = 0x80000000;
  105. for (i = i + 1; (i < r) && (m_buffer[i] < '0'); ++i);
  106. }
  107. }
  108. }
  109. return m_data[m_dataOffset++];
  110. }
  111. public:
  112. uint8_t m_buffer[32768];
  113. uint32_t m_data[16384];
  114. size_t m_dataOffset, m_dataSize;
  115. uint32_t m_v;
  116. };
  117.  
  118. int n, m, arr[200005], root, bit[200005], compress[200005], compress1[200005], bitmax;
  119. piii queries[200005];
  120. LL ans[200005], inversions, tot;
  121. int l = 0, r = 0;
  122. inline bool cf(piii a, piii b){
  123. int lo = a.se.fi / root, hi = b.se.fi / root;
  124. if(lo != hi) return lo < hi;
  125. return a.se.se < b.se.se;
  126. }
  127. inline void update(int pos, int val){
  128. for(int i = pos; i <= bitmax; i += i & -i) bit[i] += val;
  129. }
  130. inline int query(int pos){
  131. int ret = 0;
  132. for(int i = pos; i; i ^= i & -i) ret += bit[i];
  133. return ret;
  134. }
  135. inline int queryBIT(int lo, int hi){
  136. int ret1 = (hi == bitmax) ? (r - l + 1) : query(hi);
  137. int ret2 = (lo == 1) ? 0 : query(lo - 1);
  138. return ret1 - ret2;
  139. }
  140. FastInput FI;
  141. inline void go(){
  142. sort(compress+1,compress+1+n);
  143. int prev = -1;
  144. for(int i = 1; i <= n; i++){
  145. if(compress[i] != prev){
  146. ++bitmax;
  147. compress1[bitmax] = compress[i];
  148. prev = compress[i];
  149. }
  150. }
  151. }
  152. int main(){
  153. n = FI.ReadNext(); m = FI.ReadNext();
  154. for(int i = 1; i <= n; i++){
  155. arr[i] = FI.ReadNext();
  156. compress[i] = arr[i];
  157. }
  158. go();
  159. for(int i = n; i >= 1; i--){
  160. arr[i] = lower_bound(compress1+1,compress1+1+bitmax,arr[i]) - compress1;
  161. inversions += query(arr[i]-1);
  162. update(arr[i],1);
  163. }
  164. for(int i = 1; i <= bitmax; i++) bit[i] = 0;
  165.  
  166. for(int i = 1; i <= m; i++){
  167. queries[i].se.fi = FI.ReadNext();
  168. queries[i].se.se = FI.ReadNext();
  169. if(queries[i].se.fi > queries[i].se.se) swap(queries[i].se.fi, queries[i].se.se);
  170. queries[i].fi = i;
  171. }
  172.  
  173. root = (int)sqrt(n*1.0);
  174. sort(queries+1,queries+1+m,cf);
  175.  
  176. for(int i = 1; i <= m; i++){
  177. int nowl = queries[i].se.fi, nowr = queries[i].se.se;
  178.  
  179. tot = inversions + (arr[nowr] > arr[nowl]) - (arr[nowl] > arr[nowr]);
  180. if(nowl == nowr){
  181. ans[queries[i].fi] = inversions;
  182. }
  183. else if(nowl == nowr - 1){
  184. ans[queries[i].fi] = tot;
  185. }
  186. else{
  187. int tmpl = nowl + 1, tmpr = nowr - 1;
  188.  
  189. while(r < tmpr){
  190. r++;
  191. if(r) update(arr[r],1);
  192. }
  193. while(l > tmpl){
  194. l--;
  195. if(l) update(arr[l],1);
  196. }
  197. while(r > tmpr){
  198. if(r) update(arr[r],-1);
  199. r--;
  200. }
  201. while(l < tmpl){
  202. if(l) update(arr[l],-1);
  203. l++;
  204. }
  205.  
  206. ans[queries[i].fi] = tot - queryBIT(1, arr[nowl] - 1) + queryBIT(arr[nowl] + 1, bitmax)
  207. + queryBIT(1, arr[nowr] - 1) - queryBIT(arr[nowr] + 1, bitmax);
  208. }
  209. }
  210.  
  211. for(int i = 1; i <= m; i++){
  212. printf("%lld\n",ans[i]);
  213. }
  214. return 0;
  215. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement