Guest User

Untitled

a guest
Jul 12th, 2020
149
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.88 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. vector <long long int> update(int x,long long int val,vector <long long int> b,int n)
  6. {
  7. b[x]+=(val);
  8. x+=(x&-x);
  9. while(x<=n)
  10. {
  11. b[x]+=(val);
  12. x+=(x&-x);
  13. }
  14. return b;
  15. }
  16.  
  17. long long int getsum(int i,int j,vector <long long int> b)
  18. {
  19. i=i-1;
  20. long long int sum1=b[i];
  21. i-=(i&-i);
  22. while(i>0)
  23. {
  24. sum1+=(b[i]);
  25. i-=(i&-i);
  26. }
  27. long long int sum2=b[j];
  28. j-=(j&-j);
  29. while(j>0)
  30. {
  31. sum2+=b[j];
  32. j-=(j&-j);
  33. }
  34. return(sum2-sum1);
  35. }
  36.  
  37. int main()
  38. {
  39. int n,q;
  40. ios_base::sync_with_stdio(false);
  41. cin.tie(NULL);
  42. cin>>n>>q;
  43. vector <int> h(n);
  44. for(int i=0;i<n;i++)
  45. {
  46. cin>>h[i];
  47. }
  48. vector <long long int> a(n);
  49. for(int i=0;i<n;i++)
  50. {
  51. cin>>a[i];
  52. }
  53. vector <int> Pl(n,-1);
  54. vector <int> Pr(n,-1);
  55. stack <int> s;
  56. s.push(0);
  57. for(int i = 1; i < n; i++)
  58. {
  59.  
  60. if (s.empty())
  61. {
  62. s.push(i);
  63. continue;
  64. }
  65. while (s.empty() == false && h[s.top()] < h[i])
  66. {
  67. Pl[s.top()] = i;
  68. s.pop();
  69. }
  70. s.push(i);
  71. }
  72. while(s.empty()==false)
  73. {
  74. s.pop();
  75. }
  76.  
  77. s.push(n-1);
  78. // iterate for rest of the elements
  79. for(int i = n-2; i >= 0; i--)
  80. {
  81. if (s.empty())
  82. {
  83. s.push(i);
  84. continue;
  85. }
  86. while (s.empty() == false && h[s.top()] < h[i])
  87. {
  88. Pr[s.top()] = i;
  89. s.pop();
  90. }
  91. s.push(i);
  92. }
  93. // for(auto i:Pr)
  94. // {
  95. // cout<<i<<" ";
  96. // }
  97. // cout<<endl;
  98. while(s.empty()==false)
  99. {
  100. s.pop();
  101. }
  102. vector <pair <int,int>> timel(n);
  103. vector <int> arrayl;
  104. s.push(0);
  105. int t = 1;
  106. timel[0].first = t;
  107. t++;
  108. arrayl.push_back(0);
  109. arrayl.push_back(a[0]);
  110. int i = 1;
  111. while(i<n)
  112. {
  113. if(s.empty())
  114. {
  115. s.push(i);
  116. timel[i].first = t;
  117. t++;
  118. arrayl.push_back(a[i]);
  119. i++;
  120. if(i==n)
  121. break;
  122. }
  123. if(h[i]<h[s.top()])
  124. {
  125. s.push(i);
  126. timel[i].first = t;
  127. t++;
  128. arrayl.push_back(a[i]);
  129. i++;
  130. }
  131. else
  132. {
  133. timel[s.top()].second = t;
  134. t++;
  135. arrayl.push_back(-a[s.top()]);
  136. s.pop();
  137. }
  138. }
  139. while(s.empty()==false)
  140. {
  141. timel[s.top()].second = t;
  142. t++;
  143. arrayl.push_back(-a[s.top()]);
  144. s.pop();
  145. }
  146. vector <pair <int,int>> timer(n);
  147. vector <int> arrayr;
  148. s.push(n-1);
  149. t = 1;
  150. timer[n-1].first = t;
  151. t++;
  152. arrayr.push_back(0);
  153. arrayr.push_back(a[n-1]);
  154. i = n-2;
  155. while(i>=0)
  156. {
  157. if(s.empty())
  158. {
  159. s.push(i);
  160. timer[i].first = t;
  161. t++;
  162. arrayr.push_back(a[i]);
  163. i--;
  164. if(i==0)
  165. break;
  166. }
  167. if(h[i]<h[s.top()])
  168. {
  169. s.push(i);
  170. timer[i].first = t;
  171. t++;
  172. arrayr.push_back(a[i]);
  173. i--;
  174. }
  175. else
  176. {
  177. timer[s.top()].second = t;
  178. t++;
  179. arrayr.push_back(-a[s.top()]);
  180. s.pop();
  181. }
  182. }
  183. while(s.empty()==false)
  184. {
  185. timer[s.top()].second = t;
  186. t++;
  187. arrayr.push_back(-a[s.top()]);
  188. s.pop();
  189. }
  190. vector <long long int> bitr(arrayr.size(),0);
  191. vector <long long int> bitl(arrayl.size(),0);
  192. for(int i=1;i<arrayl.size();i++)
  193. {
  194. bitl = update(i,arrayl[i],bitl, arrayl.size()-1);
  195. bitr = update(i,arrayr[i],bitr, arrayr.size()-1);
  196. }
  197. // for(auto i:arrayl)
  198. // cout<<i<<" ";
  199. // cout<<endl;
  200. for(int i=0;i<q;i++)
  201. {
  202. int type;
  203. cin>>type;
  204. if(type==2)
  205. {
  206. int s,d;
  207. cin>>s>>d;
  208. s--;
  209. d--;
  210. int x = s;
  211. int y = d;
  212. if(s>d)
  213. {
  214. while (Pl[x] != Pl[y])
  215. {
  216. if (x < y)
  217. x = Pl[x];
  218. else
  219. y = Pl[y];
  220. }
  221. if(x != s)
  222. {
  223. cout<<-1<<endl;
  224. }
  225. else
  226. {
  227. long long int ans = getsum(timer[s].first,timer[d].first,bitr);
  228. cout<<ans<<endl;
  229. }
  230. }
  231. else
  232. {
  233. while (Pr[x] != Pr[y])
  234. {
  235. if (x > y)
  236. x = Pr[x];
  237. else
  238. y = Pr[y];
  239. }
  240. // y = Pr[y];
  241. // cout<<s<<" "<<d<<endl;
  242. // cout<<x<<endl;
  243. if(y != s)
  244. {
  245. cout<<-1<<endl;
  246. }
  247. else
  248. {
  249. long long int ans = getsum(timel[s].first,timel[d].first,bitl);
  250. cout<<ans<<endl;
  251. }
  252. }
  253. }
  254. else
  255. {
  256. int s,k;
  257. cin>>s>>k;
  258. long long int val = k-a[s-1];
  259. bitl = update(timel[s-1].first,val,bitl,arrayl.size()-1);
  260. bitl = update(timel[s-1].second,-val,bitl,arrayl.size()-1);
  261. bitr = update(timer[s-1].first,val,bitr,arrayr.size()-1);
  262. bitr = update(timer[s-1].second,-val,bitr,arrayr.size()-1);
  263. a[s-1] = k;
  264. }
  265. }
  266. return 0;
  267. }
Add Comment
Please, Sign In to add comment