Advertisement
Korotkodul

CF D 6

Aug 15th, 2022 (edited)
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.47 KB | None | 0 0
  1. #include <iostream>
  2. #include <cmath>
  3. #include <vector>
  4. #include <queue>
  5. #include <algorithm>
  6. #include <string>
  7. #include <stack>
  8. #include <set>
  9. #include <map>
  10. #define pii pair <int,int>
  11. #define vec vector
  12. using namespace std;
  13. using ll = long long;
  14. using ld = long double;
  15. using db = double;
  16. void cv(vector <int> &v){
  17. for (auto x: v) cout<<x<<' ';
  18. cout<<"\n";
  19. }
  20.  
  21. void cvl(vector <ll> &v){
  22. for (auto x: v) cout<<x<<' ';
  23. cout<<"\n";
  24. }
  25.  
  26.  
  27. void cvv(vector <vector <int> > &v){
  28. for (auto x: v) cv(x);
  29. cout<<"\n";
  30. }
  31.  
  32. void cvb(vector <bool> v){
  33. for (bool x: v) cout<<x<<' ';
  34. cout<<"\n";
  35. }
  36.  
  37. void cvs(vector <string> v){
  38. for (auto a: v){
  39. cout<<a<<"\n";
  40. }
  41. }
  42.  
  43. void cvp(vector <pii> a){
  44. for (auto p: a){
  45. cout<<p.first<<' '<<p.second<<"\n";
  46. }
  47. cout<<"\n";
  48. }
  49.  
  50. bool sh=1;
  51.  
  52.  
  53. int n,N,k;
  54.  
  55. vector <int> a,a1;
  56.  
  57. const int inf = 1e9;
  58.  
  59. int up2(int x){
  60. int y = log2(x);
  61. if (pow(2, y) < x){
  62. y++;
  63. }
  64. y = pow(2, y);
  65. return y;
  66. }
  67.  
  68. void seep(pii p){
  69. cout<<"("<<p.first<<","<<p.second<<") ";
  70. }
  71.  
  72. struct tree{
  73. vector <pii> MN; //{mn, id}
  74. vector <pii> fr0, fr1; //макс мин на отрезках длины 2 {max mn, id}
  75.  
  76. void showMN(){
  77. //cout<<"SHOW\n";
  78. cout<<"MN\n";
  79. int id=-1;
  80. for (int lg = 0; lg <= log2(N); ++lg){
  81. for (int i = 0; i < pow(2, lg); ++i){
  82. id++;
  83. seep(MN[id]);
  84. }cout<<"\n";
  85. }cout<<"\n";
  86. }
  87.  
  88. void showfr0(){
  89. cout<<"fr0\n";
  90. int id=-1;
  91. for (int lg = 0; lg <= log2(N) - 1; ++lg){
  92. for (int i = 0; i < pow(2, lg); ++i){
  93. id++;
  94. seep(fr0[id]);
  95. }cout<<"\n";
  96. }cout<<"\n";
  97. }
  98.  
  99. void showfr1(){
  100. cout<<"fr1\n";
  101. int id = -1;
  102. for (int lg = 0; lg <= log2(N) - 1; ++lg){
  103. for (int i = 0; i < pow(2, lg); ++i){
  104. id++;
  105. seep(fr1[id]);
  106. }cout<<"\n";
  107. }cout<<"\n";
  108. }
  109.  
  110. void bld(){
  111. for (int i = 1; i < n; ++i){
  112. if (n % 2 == 0 && i == n - 1) continue;
  113. a1.push_back(a[i]);
  114. }
  115. //while (a1.size() < a.size()) a1.push_back(inf);
  116.  
  117. N = up2(n);
  118. MN.assign(2 * N - 1, {inf,-1});
  119. for (int i = N - 1; i < N - 1 + n; ++i){
  120. int id = i - ( N - 1);
  121. MN[i] = {a[id], id};
  122. }
  123.  
  124. for (int i = N - 2;i >= 0; --i){
  125. if (MN[2*i + 1] < MN[2 * i + 2]){
  126. MN[i] = MN[2*i + 1];
  127. }
  128. else{
  129. MN[i] = MN[2 * i + 2];
  130. }
  131. }
  132.  
  133. fr0.assign(N - 1, {0,-1});
  134. fr1.assign(N - 1, {0,-1});
  135.  
  136. for (int i = 0; i + 1 < n; i += 2){
  137. int mn = a[i], id = i;
  138. if (a[i] > a[i+1]){
  139. mn = a[i+1];
  140. id++;
  141. }
  142. fr0[i/2 + N/2 - 1] = {mn, id};
  143. }
  144.  
  145.  
  146.  
  147. for (int i = 0; i + 1 < n; i += 2){
  148. if (n % 2 == 0 && i + 1 == n - 1) continue;
  149. int mn = a1[i], id = i;
  150. if (a1[i] > a1[i + 1]){
  151. mn = a1[i + 1];
  152. id++;
  153. }
  154. fr1[i/2 + N/2 - 1] = {mn, id+1};
  155. }
  156.  
  157. for (int i = N/2 - 2; i >= 0; --i){
  158.  
  159. /*fr0[i] = max(fr0[i*2 + 1], fr0[i * 2 + 2]);
  160. fr1[i] = max(fr1[i*2 + 1], fr1[i * 2 + 2]);*/
  161. pii p = fr0[i*2 + 1];
  162. if (fr0[i*2 + 2].first > fr0[i*2 + 1].first){
  163. p = fr0[i*2 + 2];
  164. }
  165. fr0[i] = p;
  166. }
  167.  
  168. for (int i = N/2 - 2; i >= 0; --i){
  169. pii p = fr1[i*2 + 1];
  170. if (fr1[i*2 + 2].first > fr1[i*2 + 1].first){
  171. p = fr1[i*2 + 2];
  172. }
  173. fr1[i] = p;
  174. }
  175.  
  176.  
  177. if (sh){
  178. //showMN();
  179. //showfr0();
  180. }
  181. }
  182.  
  183. void updMN(int id){
  184. //MN
  185. id += N - 1;
  186. MN[id].first = inf;
  187. while (id > 0){
  188. id--;
  189. id /= 2;
  190. pii p = MN[2 * id + 1];
  191. if (MN[2 * id + 1].first > MN[2 * id + 2].first){
  192. p = MN[2 * id + 2];
  193. }
  194. MN[id] = p;
  195. }
  196. }
  197.  
  198.  
  199. void updfr0(int id){
  200. //!!!
  201. a[id] = inf;
  202.  
  203. //if id % 2 == 0
  204.  
  205.  
  206. if (sh){
  207. cout<<"id = "<<id<<"\n";
  208. }
  209. //соседи по паре в fr0 и fr1
  210.  
  211.  
  212. if (n % 2 == 1 && id == n - 1){
  213. if (sh){
  214. cout<<"C\n";
  215. }
  216. return;
  217. }
  218.  
  219.  
  220.  
  221. int to;
  222.  
  223. if (id % 2 == 0){
  224. if (sh){
  225. cout<<"D\n";
  226. }
  227. to = id+1;
  228. }
  229. else if (id % 2 == 1){
  230. if (sh){
  231. cout<<"E\n";
  232. }
  233. to = id-1;
  234. }
  235.  
  236.  
  237.  
  238. //fr0
  239. if (sh){
  240. cout<<"F\n";
  241. cout<<"to = "<<to<<"\n";
  242.  
  243. }
  244. fr0[(id + N - 1 - 1) / 2] = {a[id], id};
  245.  
  246. if (a[to] < a[id]){
  247. fr0[(id + N - 1 - 1) / 2] = {a[to], to};
  248. }
  249.  
  250. if (sh){
  251. cout<<"(id + N - 1 - 1) / 2 = "<<((id + N - 1 - 1) / 2)<<"\n";
  252. cout<<"fr0[(id + N - 1 - 1) / 2] = "; seep(fr0[(id + N - 1 - 1) / 2]); cout<<"\n";
  253. }
  254.  
  255. id = (id + N - 1);
  256. if (sh){
  257. cout<<"id = "<<id<<"\n";
  258. }
  259. id--;
  260. id /= 2;
  261.  
  262. if (sh){
  263. cout<<"id = "<<id<<"\n";
  264. }
  265.  
  266. while (id > 0){
  267.  
  268. id--;
  269. id /= 2;
  270. if (sh){
  271. cout<<"id = "<<id<<"\n";
  272. }
  273. pii p = fr0[id * 2 + 1];
  274. if (fr0[id * 2 + 2].first > fr0[id * 2 + 1].first){
  275. p = fr0[id * 2 + 2];
  276. }
  277. if (sh){
  278. cout<<"p = "; seep(p); cout<<"\n";
  279. }
  280. fr0[id] = p;
  281. }
  282.  
  283.  
  284.  
  285. /*if (sh){
  286. cout<<"a\n"; cv(a);
  287. cout<<"upd0 upd1 = "<<upd0<<" "<<upd1<<"\n";
  288. cout<<"to0 to1 = "<<to0<<" "<<to1<<"\n";
  289. cout<<"id0 id1 = "<<id0<<" "<<id1<<"\n";
  290. if (upd0){
  291. cout<<"fr0[id0] = "; seep(fr0[id0]);
  292. }
  293. if (upd1){
  294. cout<<"fr1[id1] = "; seep(fr1[id1]);
  295. }
  296. cout<<"\n";
  297. }*/
  298.  
  299.  
  300.  
  301.  
  302. }
  303.  
  304. void updfr1(int id){
  305. if (id == 0){
  306. if (sh){
  307. cout<<"A\n";
  308. }
  309. return;
  310. }
  311. else if (n % 2 == 0 && id == n - 1){
  312. if (sh){
  313. cout<<"B\n";
  314. }
  315. return;
  316. }
  317.  
  318. int to;
  319. if (id % 2 == 0){
  320. if (sh){
  321. cout<<"D\n";
  322. }
  323. to = id-1;
  324. }
  325. else if (id % 2 == 1){
  326. if (sh){
  327. cout<<"E\n";
  328. }
  329. to = id+1;
  330. }
  331. id--;
  332.  
  333. a1[id] = inf;
  334.  
  335. //fr1
  336. if (sh){
  337. cout<<"G\n";
  338. }
  339. fr1[(id + N - 1 - 1) / 2] = {a1[id], id};
  340. if (a1[to] < a1[id]){
  341. fr1[(id + N - 1 - 1) / 2] = {a1[to], to};
  342. }
  343.  
  344. id = (id + N - 1);
  345. id--;
  346. id /= 2;
  347.  
  348. while (id > 0){
  349. id--;
  350. id /= 2;
  351. pii p = fr1[id * 2 + 1];
  352. if (fr1[id * 2 + 2].first > fr1[id * 2 + 1].first){
  353. p = fr1[id * 2 + 2];
  354. }
  355. fr1[id] = p;
  356. }
  357. }
  358.  
  359. void upd(int id){
  360. updMN(id);
  361. updfr0(id);
  362. updfr1(id);
  363. }
  364. };
  365.  
  366. void slv(){
  367. tree T;
  368. T.bld();
  369. if (sh){
  370. cout<<"a\n"; cv(a);
  371. T.showMN();
  372. T.showfr0();
  373. T.showfr1();
  374. }
  375. if (sh){
  376. int q; cin>>q;
  377. for (int i = 0; i < q; ++i){
  378. int id; cin>>id;
  379. T.upd(id);
  380. if (sh){
  381. cout<<"a\n"; cv(a);
  382. T.showMN();
  383. T.showfr0();
  384. T.showfr1();
  385. }
  386.  
  387. //T.showfr1();
  388. }
  389. }
  390. }
  391.  
  392.  
  393. int main()
  394. {
  395. /*ios::sync_with_stdio(0);
  396. cin.tie(0);
  397. cout.tie(0);*/
  398. int t=1; if (!sh) cin>>t;
  399. for (int go = 0; go < t; ++go){
  400. cin>>n>>k; a.resize(n); for (int &i: a) cin>>i;
  401. slv();
  402. }
  403. }
  404. /*
  405. 20 20
  406. 1 4 3 6 3
  407. 1 4 7 4 7
  408. 1 4 3 6 4
  409. 1 4 3 2 3
  410. */
  411.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement