Advertisement
bibaboba12345

Untitled

Jan 14th, 2023
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.44 KB | None | 0 0
  1. // clang-format off
  2. #define _CRT_SECURE_NO_WARNINGS
  3. #include <iostream>
  4. #include <iomanip>
  5. #include <bitset>
  6. #include <vector>
  7. #include <algorithm>
  8. #include <random>
  9. #include <map>
  10. #include <string>
  11. #include <set>
  12. #include <deque>
  13. #include <string>
  14. #include <cassert>
  15.  
  16. #pragma GCC optimize("Ofast")
  17.  
  18. using namespace std;
  19. const int N = 4e5 + 7, MOD = 1e9 + 7;
  20. const long long INF = 1e18;
  21. const long double EPS = 1e-9;
  22. int C, C2 = 430;
  23. int lb(int a) {
  24. return a & (-a);
  25. }
  26.  
  27. vector<int> s;
  28. int bro[8];
  29.  
  30. struct dq {
  31. int mas[4 * N], L , R;
  32. void clear() {
  33. L = 2*N;
  34. R = 2*N - 1;
  35. }
  36. int size() {
  37. return R - L + 1;
  38. }
  39. int front() {
  40. return mas[L];
  41. }
  42. int back() {
  43. return mas[R];
  44. }
  45. void pop_front() {
  46. L++;
  47. }
  48. void pop_back() {
  49. R--;
  50. }
  51. void push_front(int x) {
  52. L--;
  53. mas[L] = x;
  54. }
  55. void push_back(int x) {
  56. R++;
  57. mas[R] = x;
  58. }
  59. };
  60.  
  61. struct Change {
  62. int tp; // seti push_back pop_back push_front pop_front
  63. int val;
  64. int* target;
  65. };
  66.  
  67. struct Mo {
  68. dq indexes;
  69. int L, R;
  70. vector<Change> c;
  71. void process(Change a) {
  72. if (a.tp == 2) {
  73. indexes.push_back(a.val);
  74. }
  75. if (a.tp == 3) {
  76. indexes.pop_back();
  77. }
  78. if (a.tp == 4) {
  79. indexes.push_front(a.val);
  80. }
  81. if (a.tp == 5) {
  82. indexes.pop_front();
  83. }
  84. if (a.tp == 1) {
  85. *a.target = a.val;
  86. }
  87. }
  88. void otkat() {
  89. reverse(c.begin(), c.end());
  90. for (auto u : c) {
  91. process(u);
  92. }
  93. c.clear();
  94. }
  95. void init(int l) {
  96. c.clear();
  97. L = l;
  98. R = l;
  99. indexes.clear();
  100. indexes.push_back(s[l]);
  101. }
  102. void MoveL() {
  103. c.push_back({ 1, L,&L });
  104. L--;
  105. int b1 = int(s[L]);
  106. int b2 = bro[b1];
  107. if (indexes.size() == 0) {
  108. c.push_back({ 5, L, &L });
  109. indexes.push_front(b1);
  110. return;
  111. }
  112. int b = indexes.front();
  113. if (b == b2) {
  114. c.push_back({ 4, b, &L });
  115. indexes.pop_front();
  116. return;
  117. }
  118. c.push_back({ 5, L, &L });
  119. indexes.push_front(b1);
  120. return;
  121. }
  122. void MoveR() {
  123. c.push_back({ 1, R,&R });
  124. R++;
  125. int b1 = int(s[R]);
  126. int b2 = bro[b1];
  127. if (indexes.size() == 0) {
  128. c.push_back({ 3, R, &R });
  129. indexes.push_front(b1);
  130. return;
  131. }
  132. int b = indexes.back();
  133. if (b == b2) {
  134. c.push_back({ 2, b, &R });
  135. indexes.pop_back();
  136. return;
  137. }
  138. c.push_back({ 3, R, &R });
  139. indexes.push_back(b1);
  140. return;
  141. }
  142. bool get() {
  143. return indexes.size() == 0;
  144. }
  145. };
  146.  
  147. Mo M;
  148.  
  149. struct Fenwick {
  150. int f[N];
  151. void add(int i, int x) {
  152. for (i; i < N; i += lb(i)) {
  153. f[i] += x;
  154. }
  155. }
  156. int get(int i) {
  157. int answ = 0;
  158. for (i; i > 0; i -= lb(i)) {
  159. answ += f[i];
  160. }
  161. return answ;
  162. }
  163. int get(int l, int r) {
  164. return get(r) - get(l - 1);
  165. }
  166. };
  167.  
  168. Fenwick cnt[8];
  169.  
  170. struct Query {
  171. int t, l, r;
  172. int ind;
  173. };
  174.  
  175. vector <Query> q, q2;
  176.  
  177. int n, m;
  178. bool first_only = 1;
  179. bool ask_only = 1, ask[N];
  180. bool answ[N];
  181.  
  182.  
  183.  
  184. char transf(char c) {
  185. if (c == '(') {
  186. c = 0;
  187. }
  188. if (c == ')') {
  189. c = 1;
  190. }
  191. if (c == '[') {
  192. c = 2;
  193. }
  194. if (c == ']') {
  195. c = 3;
  196. }
  197. if (c == '{') {
  198. c = 4;
  199. }
  200. if (c == '}') {
  201. c = 5;
  202. }
  203. if (c == '<') {
  204. c = 6;
  205. }
  206. if (c == '>') {
  207. c = 7;
  208. }
  209. return c;
  210. }
  211.  
  212.  
  213.  
  214. bool stupid_check(int l, int r) {
  215. if ((r - l + 1) % 2) {
  216. return 0;
  217. }
  218. for (int i = 0; i < 8; i += 2) {
  219. if (cnt[i].get(l + 1, r + 1) != cnt[bro[i]].get(l + 1, r + 1)) {
  220. return 0;
  221. }
  222. }
  223. vector<int> indexes;
  224. indexes.clear();
  225. if (first_only) {
  226. return 1;
  227. }
  228. int total_sum = 0;
  229. for (int i = l; i <= r; i++) {
  230. auto u = s[i];
  231. int c = int(u);
  232. int b = bro[c];
  233. if (indexes.size() == 0 || indexes.back() != b) {
  234. indexes.push_back(c);
  235. }
  236. else {
  237. indexes.pop_back();
  238. }
  239. }
  240. return (indexes.size() == 0);
  241. }
  242.  
  243.  
  244. void stupid_solve() {
  245. for (int i = 0; i < m; i++) {
  246. if (q[i].t == 1) {
  247. cnt[s[q[i].l - 1]].add(q[i].l, -1);
  248. s[q[i].l - 1] = q[i].r;
  249. cnt[s[q[i].l - 1]].add(q[i].l, 1);
  250. }
  251. else {
  252. if ((q[i].r - q[i].l + 1) % 2) {
  253. cout << "No\n";
  254. continue;
  255. }
  256. if (stupid_check(q[i].l - 1, q[i].r - 1)) {
  257. cout << "Yes\n";
  258. }
  259. else {
  260. cout << "No\n";
  261. }
  262. }
  263. }
  264. }
  265.  
  266. bool cmp(Query a, Query b) {
  267. if (a.l / C == b.l / C) {
  268. return a.r < b.r;
  269. }
  270. return a.l / C < b.l / C;
  271. }
  272.  
  273. void solve_asks() {
  274. for (auto u : q) {
  275. if ((u.r - u.l + 1) % 2) {
  276. answ[u.ind] = 0;
  277. continue;
  278. }
  279. if (u.r - u.l + 1 <= C+1) {
  280. answ[u.ind] = stupid_check(u.l -1 , u.r - 1);
  281. continue;
  282. }
  283. q2.push_back({ u.t, u.l - 1, u.r - 1, u.ind });
  284. }
  285. sort(q2.begin(), q2.end(), cmp);
  286. for (int i = 0; i < q2.size(); i++) {
  287. if (i == 0 || q2[i].l / C != q2[i - 1].l / C) {
  288. M.init(C * (q2[i].l / C) + C - 1);
  289. }
  290. while (M.R < q2[i].r) {
  291. M.MoveR();
  292. }
  293. while (M.L > q2[i].l) {
  294. M.MoveL();
  295. }
  296. answ[q2[i].ind] = M.get();
  297. M.otkat();
  298. }
  299. for (int i = 0; i < m; i++) {
  300. if (answ[i]) {
  301. cout << "Yes\n";
  302. }
  303. else {
  304. cout << "No\n";
  305. }
  306. }
  307. }
  308.  
  309.  
  310. signed main() {
  311. #ifdef _DEBUG
  312. freopen("input.txt", "r", stdin);
  313. C2 = 2;
  314. #else
  315. std::ios::sync_with_stdio(false);
  316. cin.tie(0);
  317. #endif
  318. bro[0] = 1;
  319. bro[1] = 0;
  320. bro[2] = 3;
  321. bro[3] = 2;
  322. bro[4] = 5;
  323. bro[5] = 4;
  324. bro[6] = 7;
  325. bro[7] = 6;
  326. cin >> n;
  327. C = min(C2, n);
  328. s.resize(n);
  329. q.clear();
  330. for (int i = 0; i < n; i++) {
  331. char c;
  332. cin >> c;
  333. s[i] = transf(c);
  334. cnt[s[i]].add(i + 1, 1);
  335. if (s[i] > 1) {
  336. first_only = 0;
  337. }
  338. }
  339. cin >> m;
  340. for (int i = 0; i < m; i++) {
  341. int t;
  342. cin >> t;
  343. if (t == 1) {
  344. ask_only = 0;
  345. int ind;
  346. char c;
  347. cin >> ind >> c;
  348. c = transf(c);
  349. if (c > 1) {
  350. first_only = 0;
  351. }
  352. q.push_back({ t, ind, int(c), i });
  353. }
  354. else {
  355. int l, r;
  356. cin >> l >> r;
  357. ask[l] = 1;
  358. q.push_back({ t,l,r, i });
  359. }
  360.  
  361. }
  362. if (first_only) {
  363. stupid_solve();
  364. return 0;
  365. }
  366. if (ask_only) {
  367. solve_asks();
  368. return 0;
  369. }
  370. if (n * m <= 100000000) {
  371. stupid_solve();
  372. return 0;
  373. }
  374.  
  375.  
  376.  
  377. stupid_solve();
  378. }
  379.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement