首页 > 代码库 > [CSU1809] Parenthesis(RMQ)
[CSU1809] Parenthesis(RMQ)
题目链接:http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1809
题意:给一个长为n的匹配好的括号串,q个询问,每次询问交换一对括号。问交换后是否依然匹配。
维护一个数组b和一个数,表示当前字符串下左右匹配过后左括号剩余的数量。更新的时候遇到左括号则++cnt,否则--cnt。交换l,r则在区间内查询[l,r-1],看看最小值是否是<2。
1 /* 2 ━━━━━┒ギリギリ♂ eye! 3 ┓┏┓┏┓┃キリキリ♂ mind! 4 ┛┗┛┗┛┃\○/ 5 ┓┏┓┏┓┃ / 6 ┛┗┛┗┛┃ノ) 7 ┓┏┓┏┓┃ 8 ┛┗┛┗┛┃ 9 ┓┏┓┏┓┃ 10 ┛┗┛┗┛┃ 11 ┓┏┓┏┓┃ 12 ┛┗┛┗┛┃ 13 ┓┏┓┏┓┃ 14 ┃┃┃┃┃┃ 15 ┻┻┻┻┻┻ 16 */ 17 #include <algorithm> 18 #include <iostream> 19 #include <iomanip> 20 #include <cstring> 21 #include <climits> 22 #include <complex> 23 #include <fstream> 24 #include <cassert> 25 #include <cstdio> 26 #include <bitset> 27 #include <vector> 28 #include <deque> 29 #include <queue> 30 #include <stack> 31 #include <ctime> 32 #include <set> 33 #include <map> 34 #include <cmath> 35 using namespace std; 36 #define fr first 37 #define sc second 38 #define cl clear 39 #define BUG puts("here!!!") 40 #define W(a) while(a--) 41 #define pb(a) push_back(a) 42 #define Rint(a) scanf("%d", &a) 43 #define Rll(a) scanf("%I64d", &a) 44 #define Rs(a) scanf("%s", a) 45 #define Cin(a) cin >> a 46 #define FRead() freopen("in", "r", stdin) 47 #define FWrite() freopen("out", "w", stdout) 48 #define Rep(i, len) for(int i = 0; i < (len); i++) 49 #define For(i, a, len) for(int i = (a); i < (len); i++) 50 #define Cls(a) memset((a), 0, sizeof(a)) 51 #define Clr(a, x) memset((a), (x), sizeof(a)) 52 #define Full(a) memset((a), 0x7f7f7f, sizeof(a)) 53 #define lrt rt << 1 54 #define rrt rt << 1 | 1 55 #define pi 3.14159265359 56 #define RT return 57 #define lowbit(x) x & (-x) 58 #define onecnt(x) __builtin_popcount(x) 59 typedef long long LL; 60 typedef long double LD; 61 typedef unsigned long long ULL; 62 typedef pair<int, int> pii; 63 typedef pair<string, int> psi; 64 typedef pair<LL, LL> pll; 65 typedef map<string, int> msi; 66 typedef vector<int> vi; 67 typedef vector<LL> vl; 68 typedef vector<vl> vvl; 69 typedef vector<bool> vb; 70 71 #define lson l,m,lrt 72 #define rson m+1,r,rrt 73 const int maxn = 200100; 74 int n, q; 75 char s[maxn]; 76 int b[maxn]; 77 int sum[maxn<<2]; 78 79 void pushUP(int rt) { 80 sum[rt] = min(sum[rt<<1], sum[rt<<1|1]); 81 } 82 83 void build(int l, int r, int rt) { 84 if(l == r) { 85 sum[rt] = b[l]; 86 return; 87 } 88 int m = (l + r) >> 1; 89 build(lson); 90 build(rson); 91 pushUP(rt); 92 } 93 94 int query(int L, int R, int l, int r, int rt) { 95 if(L <= l && r <= R) return sum[rt]; 96 int m = (l + r) >> 1; 97 int ret = 0x7f7f7f7f; 98 if(L <= m) ret = min(ret, query(L, R, lson)); 99 if(R > m) ret = min(ret, query(L, R, rson));100 return ret;101 }102 103 signed main() {104 // FRead();105 int u, v;106 while(~Rint(n)&&~Rint(q)) {107 Cls(b); Rs(s+1);108 int cnt = 0;109 For(i, 1, n+1) {110 if(s[i] == ‘(‘) b[i] = ++cnt;111 else b[i] = --cnt;112 }113 build(1, n, 1);114 W(q) {115 Rint(u); Rint(v);116 if(u > v) swap(u, v);117 if((s[u]==‘)‘&&s[v]==‘(‘)||(s[u]==s[v])) {118 puts("Yes");119 continue;120 }121 int ret = query(u, v-1, 1, n, 1);122 if(ret < 2) puts("No");123 else puts("Yes");124 }125 }126 RT 0;127 }
[CSU1809] Parenthesis(RMQ)
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。