Advertisement
Guest User

Untitled

a guest
Feb 29th, 2016
762
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.94 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. #define rep(i,s,n) for(i=(s);i<(n);i++)
  3. #define pb push_back
  4. #define mp make_pair
  5. using namespace std;
  6. typedef long long ll;
  7. const int SZ = 3e4+8;
  8. int M;
  9. int fibs[SZ],vals[SZ],ar[SZ];
  10. int st[4*SZ][2],lazy[4*SZ];
  11. void preprocess()
  12. {
  13. fibs[0] = 0;
  14. fibs[1] = 1;
  15. for(int i=2; i<SZ; i++)
  16. fibs[i] = (fibs[i-1] + fibs[i-2]) % M;
  17. for(int i=2; i<SZ; i+=2)
  18. fibs[i] = -fibs[i];
  19. }
  20. inline int modulo(int x,int m)
  21. {
  22. x%=m;
  23. if(x<0)x+=m;
  24. return x;
  25. }
  26. // initially s = 0, e = N-1, x = position where added(overall), i=0,
  27. // n = index of fibo to be added, toadd = true if element to be added, false to remove
  28. // val is the array from which ai is taken
  29. void update(int s,int e,int x,int i,int n,bool toadd)
  30. {
  31. if(lazy[i]!=0)
  32. {
  33. int a,b,c,d;
  34. if(lazy[i]>0)
  35. {
  36. a = abs(fibs[lazy[i]+1]);
  37. b = c = abs(fibs[lazy[i]]);
  38. d = abs(fibs[lazy[i]-1]);
  39. }
  40. else if(lazy[i]<0)
  41. {
  42. a = fibs[-(lazy[i]+1)];
  43. b = c = fibs[-lazy[i]];
  44. d = fibs[-(lazy[i]-1)];
  45. }
  46. a = a*st[i][0];
  47. b = b*st[i][1];
  48. c = c*st[i][0];
  49. d = d*st[i][1];
  50. st[i][0] = modulo(a+b,M);
  51. st[i][1] = modulo(c+d,M);
  52. if(s!=e)
  53. lazy[2*i+1]+=lazy[i], lazy[2*i+2] += lazy[i];
  54. lazy[i] = 0;
  55. }
  56. if(x>e)
  57. return;
  58. if(x<s)
  59. {
  60. int a = st[i][0];
  61. int b = st[i][1];
  62. if(toadd)
  63. {
  64. st[i][0] = modulo(a+b,M);
  65. st[i][1] = a;
  66. if(s!=e)
  67. lazy[2*i+1]++,lazy[2*i+2]++;
  68. }
  69. else
  70. {
  71. st[i][0] = b;
  72. st[i][1] = modulo(a-b,M);
  73. if(s!=e)
  74. lazy[2*i+1]--,lazy[2*i+2]--;
  75. }
  76. return;
  77. }
  78. if(s==e)
  79. {
  80. if(toadd)
  81. {
  82. int xr = vals[s];
  83. st[i][0] = (xr*abs(fibs[n]))%M;
  84. st[i][1] = (xr*abs(fibs[n-1]))%M;
  85. }
  86. else
  87. st[i][0] = st[i][1] = 0;
  88. return;
  89. }
  90. int mid = (s+e)/2;
  91. update(s,mid,x,2*i+1,n,toadd);
  92. update(mid+1,e,x,2*i+2,n,toadd);
  93. int a = st[2*i+1][0],b = st[2*i+1][1];
  94. int c = st[2*i+2][0],d = st[2*i+2][1];
  95. st[i][0] = (a+c)%M;
  96. st[i][1] = (b+d)%M;
  97. }
  98. int sqn;
  99. inline bool sub(pair<int,int> x,pair<int,int> y)
  100. {
  101. double v1 = x.first;
  102. v1=ceil(v1/sqn);
  103. double v2 = y.first;
  104. v2=ceil(v2/sqn);
  105. if(v1==v2)
  106. return x.second<y.second;
  107. else
  108. return v1<v2;
  109. }
  110. int bit[SZ];
  111. int l = SZ;
  112.  
  113. void bit_update(int i,int val)
  114. {
  115. while(i<=l)
  116. {
  117. bit[i]=bit[i]+val;
  118. i=i+(i&(-i));
  119. }
  120. }
  121.  
  122. int bit_summ(int a)
  123. {
  124. int s=0;
  125. while(a>0)
  126. {
  127. s+=bit[a];
  128. a=a-(a&(-a));
  129. }
  130. return s;
  131. }
  132. int ans[SZ],num[SZ];
  133. int main()
  134. {
  135. ios::sync_with_stdio(false);
  136. cin.tie(NULL);
  137. int N,Q,l,r;
  138. cin>>N>>M;
  139. preprocess();
  140. sqn = sqrt(N);
  141. for(int i=1;i<=N;i++)
  142. {
  143. cin>>ar[i];
  144. vals[i-1] = ar[i];
  145. }
  146. sort(vals,vals+N);
  147. for(int i=1;i<=N;i++)
  148. ar[i] = lower_bound(vals,vals+N,ar[i]) - vals;
  149. for(int i=0;i<N;i++)
  150. vals[i]%=M;
  151. cin>>Q;
  152. vector<pair<pair<int,int>,int> > queries;
  153. for(int i=0;i<Q;i++)
  154. {
  155. cin>>l>>r;
  156. queries.push_back(make_pair(make_pair(l,r),i));
  157. }
  158. sort(queries.begin(),queries.end(),[&](pair<pair<int,int>,int> a,pair<pair<int,int>,int> b){
  159. return sub(a.first,b.first);
  160. });
  161. l=r=1;
  162. num[ar[1]]++;
  163. bit_update(ar[1]+1,1);
  164. update(0,N-1,ar[1],0,1,true);
  165. for(int i=0; i<queries.size(); i++)
  166. {
  167. pair<int,int> p = queries[i].first;
  168. int ix = queries[i].second,pos;
  169. while(l<p.first)
  170. {
  171. num[ar[l]]--;
  172. if(num[ar[l]]==0)
  173. {
  174. bit_update(ar[l]+1,-1);
  175. update(0,N-1,ar[l],0,0,false);
  176. }
  177. l++;
  178. }
  179. while(l>p.first)
  180. {
  181. l--;
  182. num[ar[l]]++;
  183. if(num[ar[l]]==1)
  184. {
  185. pos = bit_summ(ar[l]) + 1;
  186. bit_update(ar[l]+1,1);
  187. update(0,N-1,ar[l],0,pos,true);
  188. }
  189. }
  190. while(r<p.second)
  191. {
  192. r++;
  193. num[ar[r]]++;
  194. if(num[ar[r]]==1)
  195. {
  196. pos = bit_summ(ar[r]) + 1;
  197. bit_update(ar[r]+1,1);
  198. update(0,N-1,ar[r],0,pos,true);
  199. }
  200. }
  201. while(r>p.second)
  202. {
  203. num[ar[r]]--;
  204. if(num[ar[r]]==0)
  205. {
  206. bit_update(ar[r]+1,-1);
  207. update(0,N-1,ar[r],0,0,false);
  208. }
  209. r--;
  210. }
  211. ans[ix] = st[0][0];
  212. }
  213. for(int i=0; i<Q; i++)
  214. cout<<ans[i]<<"\n";
  215. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement