Advertisement
Guest User

TRUE_VERSION

a guest
Feb 19th, 2020
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 15.18 KB | None | 0 0
  1.  
  2.  
  3. #include <math.h>
  4. #include <map>
  5. #include <vector>
  6. #include <set>
  7. #include <deque>
  8. #include <algorithm>
  9. #include <list>
  10.  
  11. using namespace std;
  12.  
  13. #ifdef LOCAL
  14. #include <fstream>
  15. ifstream cin("tests.txt");
  16. ofstream cout("output1.txt");
  17. #else // LOCAL
  18. #include <iostream>
  19. #endif
  20.  
  21.  
  22.  
  23.  
  24.  
  25. #define ull unsigned long long
  26. #define ll long long
  27. #define pii pair<int,int>
  28.  
  29. #define DEBUG false
  30. #define CHECKER false
  31.  
  32. struct block {
  33. deque<int> data;
  34. bool rev;
  35. int val;
  36. block(int val = -1,bool rev = false) {
  37. this->rev = rev;
  38. this->val = val;
  39. }
  40.  
  41. int get_less(int x) {
  42. if(this->val != -1)
  43. if(this->val >= x)
  44. return data.size();
  45. else
  46. return 0;
  47.  
  48. int ans = this->data.size() - (lower_bound(this->data.begin(), this->data.end(), x) - this->data.begin());
  49. return ans;
  50. }
  51.  
  52. void add_element(int x,bool to_l = false) {
  53. to_l ^= this->rev;
  54. if(to_l)
  55. this->data.push_front(x);
  56. else
  57. this->data.push_back(x);
  58. }
  59.  
  60.  
  61. pair<block,block> split(int pos) {
  62. //without the pos element
  63. if(this->rev==true) {
  64. pos = this->data.size()-pos-1;
  65. }
  66.  
  67. block lb;
  68. for(int i = 0; i < pos; i++) {
  69. lb.add_element(this->data[i]);
  70. }
  71. block rb;
  72. for(int i = pos+1; i < this->data.size(); i++) {
  73. rb.add_element(this->data[i]);
  74. }
  75.  
  76. if(this->rev) {
  77. swap(lb,rb);
  78. lb.rev = true;
  79. rb.rev = true;
  80. }
  81.  
  82. lb.val = this->val;
  83. rb.val = this->val;
  84.  
  85. return {lb,rb};
  86. }
  87.  
  88. int get_el(int pos) {
  89. if(this->rev==true) {
  90. pos = this->data.size()-pos-1;
  91. }
  92. return this->data[pos];
  93. }
  94. };
  95.  
  96. list<block> sqr;
  97.  
  98. void coutsqr();
  99.  
  100. void set_val(int l,int r,int x) {
  101. int sl = 0;
  102. int sr = -1;
  103.  
  104. bool dbg = false;
  105. if(DEBUG && l==4&&r==4&&x==74209) {
  106. cout << "\n____________IN_SET___\n";
  107. coutsqr();
  108. int klsdjfsd = 123;
  109. dbg = true;
  110.  
  111. }
  112.  
  113. for(auto i = sqr.begin(); i!= sqr.end(); i++) {
  114. block & curblock = *i;
  115. if(curblock.data.size()==0)
  116. continue;
  117. sl = sr+1;
  118. sr = sl + curblock.data.size()-1;
  119. if(sr<sl)
  120. continue;
  121.  
  122. if(sr < l)
  123. continue;
  124. if(sl > r)
  125. continue;
  126. if(l <= sl && sr <= r) {
  127. curblock.val = x;
  128. continue;
  129. }
  130. if(sl<=l && l<=sr && sl<=r && r<=sr) {
  131. pair<block,block> blocks1 = curblock.split(l-sl);
  132. block & lb1 = blocks1.first;
  133. block & rb1 = blocks1.second;
  134.  
  135. rb1.add_element(curblock.get_el(l-sl),true);
  136.  
  137. sl = sl + lb1.data.size();
  138.  
  139. pair<block,block> blocks2 = rb1.split(r-sl);
  140. block & lb2 = blocks2.first;
  141. block & rb2 = blocks2.second;
  142. lb2.add_element(rb1.get_el(r-sl));
  143.  
  144. lb2.val = x;
  145.  
  146.  
  147. if(dbg) {
  148. cout << "\ncurblock:\n";
  149. for(auto j:curblock.data)
  150. cout << j << ' ';
  151.  
  152. cout << "\nlb1:\n";
  153. for(auto j:lb1.data)
  154. cout << j << ' ';
  155. cout << "\nrb1:\n";
  156. for(auto j:rb1.data)
  157. cout << j << ' ';
  158. cout << "\nlb2:\n";
  159. for(auto j:lb2.data)
  160. cout << j << ' ';
  161. cout << "\nrb2:\n";
  162. for(auto j:rb2.data)
  163. cout << j << ' ';
  164. }
  165.  
  166. i = sqr.erase(i);
  167. sqr.insert(i,lb1);
  168. sqr.insert(i,lb2);
  169. sqr.insert(i,rb2);
  170. break;
  171. }
  172. if(sl<=l && l<=sr) {
  173. pair<block,block> blocks = curblock.split(l-sl);
  174. block & lb = blocks.first;
  175. block & rb = blocks.second;
  176. rb.add_element(curblock.get_el(l-sl),true);
  177. rb.val = x;
  178.  
  179. i = sqr.erase(i);
  180. sqr.insert(i,lb);
  181. sqr.insert(i,rb);
  182. i--;
  183. i--;
  184. sr = sl + lb.data.size()-1;
  185. continue;
  186. }
  187. if(sl<=r && r<=sr) {
  188. pair<block,block> blocks = curblock.split(r-sl);
  189. block & lb = blocks.first;
  190. block & rb = blocks.second;
  191. lb.add_element(curblock.get_el(r-sl));
  192. lb.val = x;
  193. i = sqr.erase(i);
  194. sqr.insert(i,lb);
  195. sqr.insert(i,rb);
  196. i--;
  197. i--;
  198. continue;
  199. }
  200. }
  201. }
  202.  
  203.  
  204. int get_val(int l,int r,int x) {
  205. if(DEBUG && l==6&&r==6)
  206. int skladjfds = 2;
  207. int sl = 0;
  208. int sr = -1;
  209. int ans = 0;
  210. for(auto i = sqr.begin(); i!= sqr.end(); i++) {
  211. block & curblock = *i;
  212. if(curblock.data.size()==0)
  213. continue;
  214. sl = sr+1;
  215. sr = sl + curblock.data.size()-1;
  216. if(sr<sl)
  217. continue;
  218.  
  219. if(sr < l)
  220. continue;
  221. if(sl > r)
  222. continue;
  223. if(l <= sl && sr <= r) {
  224. ans += curblock.get_less(x);
  225. continue;
  226. }
  227. if(sl<=l && l<=sr && sl<=r && r<=sr) {
  228. pair<block,block> blocks1 = curblock.split(l-sl);
  229. block & lb1 = blocks1.first;
  230. block & rb1 = blocks1.second;
  231. rb1.add_element(curblock.get_el(l-sl),true);
  232.  
  233. sl = sl + lb1.data.size();
  234.  
  235. pair<block,block> blocks2 = rb1.split(r-sl);
  236. block & lb2 = blocks2.first;
  237. block & rb2 = blocks2.second;
  238. lb2.add_element(rb1.get_el(r-sl));
  239.  
  240. ans += lb2.get_less(x);
  241. break;
  242. }
  243. if(sl<=l && l<=sr) {
  244. pair<block,block> blocks = curblock.split(l-sl);
  245. block & lb = blocks.first;
  246. block & rb = blocks.second;
  247. rb.add_element(curblock.get_el(l-sl),true);
  248.  
  249. ans += rb.get_less(x);
  250. continue;
  251. }
  252. if(sl<=r && r<=sr) {
  253. pair<block,block> blocks = curblock.split(r-sl);
  254. block & lb = blocks.first;
  255. block & rb = blocks.second;
  256. lb.add_element(curblock.get_el(r-sl));
  257.  
  258. ans += lb.get_less(x);
  259. continue;
  260. }
  261. }
  262. return ans;
  263. }
  264. void rreverse(int l,int r) {
  265. int sl = 0;
  266. int sr = -1;
  267.  
  268. if(DEBUG && l==1&&r==1) {
  269. cout << "\n____________IN_REVERSE___\n";
  270. coutsqr();
  271. int klsdjfsd = 123;
  272.  
  273. }
  274.  
  275. vector <block *> vect;
  276. auto first_it = sqr.end();
  277. bool first_it_b = true;
  278. auto last_it = sqr.end();
  279.  
  280. for(auto i = sqr.begin(); i!= sqr.end(); i++) {
  281. block & curblock = *i;
  282. if(curblock.data.size()==0)
  283. continue;
  284. sl = sr+1;
  285. sr = sl + curblock.data.size()-1;
  286. if(sr<sl)
  287. continue;
  288.  
  289. if(sr < l)
  290. continue;
  291. if(sl > r)
  292. continue;
  293. if(sl<=l && l<=sr && sl<=r && r<=sr) {
  294. pair<block,block> blocks1 = curblock.split(l-sl);
  295. block & lb1 = blocks1.first;
  296. block & rb1 = blocks1.second;
  297. rb1.add_element(curblock.get_el(l-sl),true);
  298.  
  299. sl = sl + lb1.data.size();
  300.  
  301. pair<block,block> blocks2 = rb1.split(r-sl);
  302. block & lb2 = blocks2.first;
  303. block & rb2 = blocks2.second;
  304. lb2.add_element(rb1.get_el(r-sl));
  305.  
  306. lb2.rev ^= 1;
  307.  
  308. i = sqr.erase(i);
  309. sqr.insert(i,lb1);
  310. sqr.insert(i,lb2);
  311. sqr.insert(i,rb2);
  312.  
  313. break;
  314. }
  315. if(l <= sl && sr <= r) {
  316. curblock.rev ^= 1;
  317. if(first_it_b) {
  318. first_it_b = false;
  319. first_it = i;
  320. }
  321. last_it = i;
  322. continue;
  323. }
  324. if(sl<=l && l<=sr) {
  325. pair<block,block> blocks = curblock.split(l-sl);
  326. block & lb = blocks.first;
  327. block & rb = blocks.second;
  328.  
  329. //DEBUG
  330. // cout << "\n-----START_DEBUG\nlb:\n";
  331. // for(auto i:lb.data)
  332. // cout << i << ' ';
  333. // cout << "\nrb\n";
  334. // for(auto i:rb.data)
  335. // cout << i << ' ';
  336. // cout << "\n-----END_DEBUG\n\n";
  337. //exit(0);
  338.  
  339.  
  340. rb.add_element(curblock.get_el(l-sl),true);
  341.  
  342. i = sqr.erase(i);
  343. sqr.insert(i,lb);
  344. sqr.insert(i,rb);
  345. i--;
  346. i--;
  347. sr = sl + lb.data.size()-1;
  348. continue;
  349. }
  350. if(sl<=r && r<=sr) {
  351. pair<block,block> blocks = curblock.split(r-sl);
  352. block & lb = blocks.first;
  353. block & rb = blocks.second;
  354. lb.add_element(curblock.get_el(r-sl));
  355.  
  356. i = sqr.erase(i);
  357. lb.rev ^= 1;
  358. sqr.insert(i,lb);
  359. sqr.insert(i,rb);
  360.  
  361. i--;
  362. i--;
  363. last_it = i;
  364. continue;
  365. }
  366. }
  367. // cout << "\n\n\n___AFTER_REVERSE\n";
  368. // coutsqr();
  369. // cout << "\n___AFTER_REVERSE\n\n\n";
  370. reverse(first_it,++last_it);
  371. }
  372.  
  373.  
  374.  
  375. void build(vector<int> & a) {
  376. int curr = 0;
  377. int ss = sqrt(a.size());
  378. for(int i = 0; i < ss; i++) {
  379. block curblock;
  380. for(int j = 0; j < ss; j++) {
  381. curblock.add_element(a[curr++]);
  382. }
  383. sqr.push_back(curblock);
  384. }
  385. if(curr!=a.size()) {
  386. block curblock;
  387. while(curr != a.size()) {
  388. curblock.add_element(a[curr++]);
  389. }
  390. sqr.push_back(curblock);
  391. }
  392. }
  393.  
  394.  
  395. void coutsqr() {
  396. for(auto i:sqr) {
  397.  
  398. for(auto j:i.data) {
  399. cout << j << ' ';
  400. }
  401. cout << " == " << i.val;
  402. cout << " == " << i.rev;
  403. cout << '\n';
  404. }
  405.  
  406.  
  407. }
  408.  
  409. struct to_face {
  410. vector<int> a;
  411. to_face(vector<int> a) {
  412. this->a = a;
  413. }
  414.  
  415. void sset(int l,int r, int x) {
  416. for(int i = l; i <=r; i++) {
  417. this->a[i] = x;
  418. }
  419. }
  420. void rreverse(int l,int r) {
  421. reverse(a.begin()+l,a.begin()+r+1);
  422. }
  423. int gget(int l, int r, int x) {
  424. int ans = 0;
  425. for(int i = l; i <=r; i++) {
  426. if(this->a[i] >= x)
  427. ans++;
  428. }
  429. return ans;
  430. }
  431. };
  432. to_face facer({0});
  433.  
  434. void rebuild() {
  435. if(DEBUG){
  436. cout << "\n\n\n";
  437. coutsqr();
  438. }
  439. vector<int> a;
  440. for(auto i:sqr) {
  441. if(i.data.size()==0)
  442. continue;
  443. if(i.val != -1)
  444. for(int t = 0; t < i.data.size(); t++)
  445. a.push_back(i.val);
  446.  
  447. else {
  448. if(i.rev)
  449. reverse(i.data.begin(),i.data.end());
  450. for(auto j:i.data) {
  451. a.push_back(j);
  452. }
  453.  
  454. }
  455. }
  456. sqr.clear();
  457. build(a);
  458. if(DEBUG){
  459. cout << "\n\n\n";
  460. coutsqr();
  461.  
  462. cout << "\n\n\n\n";
  463. for(auto &i:facer.a) {
  464. cout << i << " ";
  465. }
  466.  
  467. cout << "\n\n\n";
  468. }
  469. }
  470.  
  471.  
  472. bool checker() {
  473. vector<int> a;
  474. //cout << "CHECKER_WORKING:\n";
  475. for(auto i:sqr) {
  476. if(i.data.size()==0)
  477. continue;
  478. if(i.val != -1)
  479. for(int t = 0; t < i.data.size(); t++) {
  480. if(DEBUG){
  481. cout << "PUSHED_TO_A: " << i.val;
  482. cout << "\n";
  483. }
  484. a.push_back(i.val);
  485. }
  486.  
  487. else {
  488. if(i.rev)
  489. for(int j = i.data.size()-1; j>=0; j--) {
  490. if(DEBUG){
  491. cout << "PUSHED_TO_A: " << i.data[j];
  492. cout << "\n";
  493. }
  494. a.push_back(i.data[j]);
  495. } else
  496. for(auto j:i.data) {
  497. if(DEBUG){
  498. cout << "PUSHED_TO_A: " << j;
  499. cout << "\n";
  500. }
  501. a.push_back(j);
  502. }
  503.  
  504. }
  505. }
  506. for(int i = 0; i < a.size(); i++) {
  507. if(a[i] != facer.a[i]) {
  508. {
  509. cout << "\n\n__ERROR__\n\na:\n";
  510. for(auto &i:a)
  511. cout << i << ' ';
  512. cout << "\nFACER:\n";
  513. for(auto i:facer.a)
  514. cout << i << ' ';
  515. cout << "\nFASTER:\n";
  516. coutsqr();
  517. cout << "\n\n";
  518. exit(0);
  519. }
  520. }
  521. }
  522. }
  523.  
  524.  
  525.  
  526. int main() {
  527. //#define int long long
  528. int n;
  529. cin >> n;
  530. vector<int> a(n);
  531. for(auto &i:a)
  532. cin >> i;
  533. // to_face facer({0});
  534. if(DEBUG || CHECKER)
  535. facer = to_face(a);
  536. build(a);
  537. int t;
  538. cin >> t;
  539. int sqqq = sqrt(t);
  540. while(t--) {
  541. string s="\n";
  542.  
  543. cin >> s;
  544. //cout <<s << ' ' << s.size() << endl;
  545. if(s=="get") {
  546. int l,r,x;
  547. cin >> l >> r >> x;
  548. int fast_ans = get_val(--l,--r,x);
  549. int fc_ans;
  550. if(DEBUG)
  551. fc_ans = facer.gget(l,r,x);
  552. cout << fast_ans << '\n';
  553. if(DEBUG)
  554. if(fast_ans!=fc_ans) {
  555. cout << "\n\n__ERROR__\n\n";
  556. cout << "get " << l << ' ' << r << ' ' << x << " ZAPROS " << t << "\n";
  557. cout << fast_ans << " " << fc_ans << "\n\n";
  558. cout << "FACER:\n";
  559. for(auto i:facer.a)
  560. cout << i << ' ';
  561. cout << "\nFASTER:\n";
  562. coutsqr();
  563. cout << "\n\n";
  564. exit(0);
  565. }
  566. }
  567. if(s=="set") {
  568. int l,r,val;
  569. cin >> l >> r >> val;
  570. set_val(--l,--r,val);
  571. if(CHECKER){
  572. facer.sset(l,r,val);
  573. checker();
  574. }
  575. if(DEBUG) {
  576. facer.sset(l,r,val);
  577. cout << "set " << l << ' ' << r << ' ' << val;
  578. cout << '\n';
  579. checker();
  580. }
  581. }
  582. if(s=="reverse") {
  583. int l,r;
  584. cin >> l >> r;
  585. //coutsqr();
  586. //cout << "\n\n\n\n";
  587. rreverse(--l,--r);
  588. if(CHECKER){
  589. facer.rreverse(l,r);
  590. checker();
  591. }
  592. if(DEBUG) {
  593. cout << "\n\nREVERSE " << l << ' ' << r;
  594. if(l==1 && r == 1) {
  595. cout << "\n";
  596. coutsqr();
  597. cout << "\n\n\n\n";
  598. }
  599. facer.rreverse(l,r);
  600. if(l==2 && r == 7) {
  601. coutsqr();
  602. cout << "\n\n\n\n";
  603. }
  604.  
  605. cout << '\n';
  606. checker();
  607. }
  608. //coutsqr();
  609. //cout << "\n\n\n\n";
  610. }
  611. if(t%sqqq==0)
  612. rebuild();
  613. }
  614. return 0;
  615. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement