[hdu5217]线段树

题意:给定一个只含‘(‘,‘)‘的括号序列,有m个操作,改变某个位置的括号或者询问区间[L,R]内的括号进行配对后剩下的第K个括号的位置(配对的括号从原序列中删掉)。

思路:首先对于一个括号序列,进行配对后剩下的括号序列必定是")))...)((...(("这种形式的,令(x,y)表示当前区间的剩下的右括号数为x,左括号数为y这个状态,不难发现它可以通过子区间来合并,因此考虑用线段树解决。对于单点更新比较容易,直接在叶子节点改变一下,然后向上更新即可,对于L,R,K这个询问,首先进行一次区间[L,R]的查找,得到最终的左括号和右括号数目,如果数目和小于K,则答案为-1,否则可以确定第K个括号的是哪种类型的括号,假设第K个括号是右括号,则问题转化为求第K个右括号的位置,在线段树上二分即可,二分的时候需要注意,假定答案在右区间,且左区间有x个右括号y个左括号,则在右区间应查找第K-x+y个右括号。

  1 #pragma comment(linker, "/STACK:102400000,102400000")
  2
  3 #include <iostream>
  4 #include <cstdio>
  5 #include <algorithm>
  6 #include <cstdlib>
  7 #include <cstring>
  8 #include <map>
  9 #include <queue>
 10 #include <deque>
 11 #include <cmath>
 12 #include <ctime>
 13 #include <cctype>
 14 #include <set>
 15 #include <bitset>
 16 #include <functional>
 17 #include <numeric>
 18 #include <stdexcept>
 19 #include <utility>
 20 #include <vector>
 21
 22 using namespace std;
 23
 24 #define mem0(a) memset(a, 0, sizeof(a))
 25 #define mem_1(a) memset(a, -1, sizeof(a))
 26 #define lson l, m, rt << 1
 27 #define rson m + 1, r, rt << 1 | 1
 28 #define define_m int m = (l + r) >> 1
 29 #define rep_up0(a, b) for (int a = 0; a < (b); a++)
 30 #define rep_up1(a, b) for (int a = 1; a <= (b); a++)
 31 #define rep_down0(a, b) for (int a = b - 1; a >= 0; a--)
 32 #define rep_down1(a, b) for (int a = b; a > 0; a--)
 33 #define all(a) (a).begin(), (a).end()
 34 #define lowbit(x) ((x) & (-(x)))
 35 #define constructInt4(name, a, b, c, d) name(int a = 0, int b = 0, int c = 0, int d = 0): a(a), b(b), c(c), d(d) {}
 36 #define constructInt3(name, a, b, c) name(int a = 0, int b = 0, int c = 0): a(a), b(b), c(c) {}
 37 #define constructInt2(name, a, b) name(int a = 0, int b = 0): a(a), b(b) {}
 38 #define pchr(a) putchar(a)
 39 #define pstr(a) printf("%s", a)
 40 #define sstr(a) scanf("%s", a)
 41 #define sint(a) scanf("%d", &a)
 42 #define sint2(a, b) scanf("%d%d", &a, &b)
 43 #define sint3(a, b, c) scanf("%d%d%d", &a, &b, &c)
 44 #define pint(a) printf("%d\n", a)
 45 #define test_print1(a) cout << "var1 = " << a << endl
 46 #define test_print2(a, b) cout << "var1 = " << a << ", var2 = " << b << endl
 47 #define test_print3(a, b, c) cout << "var1 = " << a << ", var2 = " << b << ", var3 = " << c << endl
 48
 49 typedef long long LL;
 50 typedef pair<int, int> pii;
 51 typedef vector<int> vi;
 52
 53 const int dx[8] = {0, 0, -1, 1, 1, 1, -1, -1};
 54 const int dy[8] = {-1, 1, 0, 0, 1, -1, 1, -1 };
 55 const int maxn = 2e5 + 7;
 56 const int md = 10007;
 57 const int inf = 1e9 + 7;
 58 const LL inf_L = 1e18 + 7;
 59 const double pi = acos(-1.0);
 60 const double eps = 1e-6;
 61
 62 template<class T>T gcd(T a, T b){return b==0?a:gcd(b,a%b);}
 63 template<class T>bool max_update(T &a,const T &b){if(b>a){a = b; return true;}return false;}
 64 template<class T>bool min_update(T &a,const T &b){if(b<a){a = b; return true;}return false;}
 65 template<class T>T condition(bool f, T a, T b){return f?a:b;}
 66 template<class T>void copy_arr(T a[], T b[], int n){rep_up0(i,n)a[i]=b[i];}
 67 int make_id(int x, int y, int n) { return x * n + y; }
 68
 69 int ans;
 70 char s[maxn];
 71
 72 struct SegTree {
 73     struct Node {
 74         int cl, cr;
 75     } tree[maxn << 2];
 76
 77     Node merge(Node b, Node c) {
 78         Node a;
 79         a.cl = c.cl;
 80         a.cr = b.cr;
 81         if (b.cl <= c.cr) a.cr += c.cr - b.cl;
 82         else a.cl += b.cl - c.cr;
 83         return a;
 84     }
 85
 86     void build(int l, int r, int rt) {
 87         if (l == r) {
 88             char ch = s[l - 1];
 89             tree[rt].cl = ch == ‘(‘;
 90             tree[rt].cr = ch == ‘)‘;
 91             return ;
 92         }
 93         define_m;
 94         build(lson);
 95         build(rson);
 96         tree[rt] = merge(tree[rt << 1], tree[rt << 1 | 1]);
 97     }
 98
 99     void update(int u, int l, int r, int rt) {
100         if (l == r) {
101             swap(tree[rt].cl, tree[rt].cr);
102             return ;
103         }
104         define_m;
105         if (u <= m) update(u, lson);
106         else update(u, rson);
107         tree[rt] = merge(tree[rt << 1], tree[rt << 1 | 1]);
108     }
109
110     Node query(int L, int R, int l, int r, int rt) {
111         if (L <= l && r <= R)  return tree[rt];
112         define_m;
113         if (R <= m) return query(L, R, lson);
114         if (L > m) return query(L, R, rson);
115         return merge(query(L, R, lson), query(L, R, rson));
116     }
117
118     Node find1(int L, int R, int k, int l, int r, int rt) {
119         if (L <= l && r <= R && (k > tree[rt].cr || k <= 0 || ans)) return tree[rt];
120         if (l == r) {
121             ans = l;
122             return tree[rt];
123         }
124         define_m;
125         if (R <= m) return find1(L, R, k, lson);
126         if (L > m) return find1(L, R, k, rson);
127         Node buf = find1(L, R, k, lson);
128         if (ans) return buf;
129         return merge(buf, find1(L, R, k - buf.cr + buf.cl, rson));
130     }
131
132      Node find2(int L, int R, int k, int l, int r, int rt) {
133         if (L <= l && r <= R && (k > tree[rt].cl || k <= 0 || ans)) return tree[rt];
134         if (l == r) {
135             ans = l;
136             return tree[rt];
137         }
138         define_m;
139         if (R <= m) return find2(L, R, k, lson);
140         if (L > m) return find2(L, R, k, rson);
141         Node buf = find2(L, R, k, rson);
142         if (ans) return buf;
143         return merge(find2(L, R, k - buf.cl + buf.cr, lson), buf);
144     }
145 };
146 SegTree ST;
147 int main() {
148     //freopen("in.txt", "r", stdin);
149     int T, n, m;
150     cin >> T;
151     while (T --) {
152         cin >> n >> m;
153         scanf("%s", s);
154         ST.build(1, n, 1);
155         rep_up0(i, m) {
156             int id;
157             sint(id);
158             if (id == 1) {
159                 int u;
160                 sint(u);
161                 ST.update(u, 1, n, 1);
162             }
163             else {
164                 int u, v, k;
165                 sint3(u, v, k);
166                 SegTree::Node buf = ST.query(u, v, 1, n, 1);
167                 if (buf.cl + buf.cr < k) {
168                     puts("-1");
169                     continue;
170                 }
171                 ans = 0;
172                 if (buf.cr >= k) ST.find1(u, v, k, 1, n, 1);
173                 else ST.find2(u, v, buf.cl + buf.cr - k + 1, 1, n, 1);
174                 printf("%d\n", ans);
175             }
176         }
177     }
178     return 0;
179 }

时间: 2024-08-03 05:46:14

[hdu5217]线段树的相关文章

[poj2104]可持久化线段树入门题(主席树)

解题关键:离线求区间第k小,主席树的经典裸题: 对主席树的理解:主席树维护的是一段序列中某个数字出现的次数,所以需要预先离散化,最好使用vector的erase和unique函数,很方便:如果求整段序列的第k小,我们会想到离散化二分和线段树的做法, 而主席树只是保存了序列的前缀和,排序之后,对序列的前缀分别做线段树,具有差分的性质,因此可以求任意区间的第k小,如果主席树维护索引,只需要求出某个数字在主席树中的位置,即为sort之后v中的索引:若要求第k大,建树时反向排序即可 1 #include

【BZOJ4942】[Noi2017]整数 线段树+DFS(卡过)

[BZOJ4942][Noi2017]整数 题目描述去uoj 题解:如果只有加法,那么直接暴力即可...(因为1的数量最多nlogn个) 先考虑加法,比较显然的做法就是将A二进制分解成log位,然后依次更新这log位,如果最高位依然有进位,那么找到最高位后面的第一个0,将中间的所有1变成0,那个0变成1.这个显然要用到线段树,但是复杂度是nlog2n的,肯定过不去. 于是我在考场上yy了一下,这log位是连续的,我们每次都要花费log的时间去修改一个岂不是很浪费?我们可以先在线段树上找到这段区间

bzoj1798: [Ahoi2009]Seq 维护序列seq 线段树

题目传送门 这道题就是线段树 先传乘法标记再传加法 #include<cstdio> #include<cstring> #include<algorithm> #define LL long long using namespace std; const int M=400010; LL read(){ LL ans=0,f=1,c=getchar(); while(c<'0'||c>'9'){if(c=='-') f=-1; c=getchar();}

Vijos P1066 弱弱的战壕【多解,线段树,暴力,树状数组】

弱弱的战壕 描述 永恒和mx正在玩一个即时战略游戏,名字嘛~~~~~~恕本人记性不好,忘了-_-b. mx在他的基地附近建立了n个战壕,每个战壕都是一个独立的作战单位,射程可以达到无限(“mx不赢定了?!?”永恒[email protected][email protected]). 但是,战壕有一个弱点,就是只能攻击它的左下方,说白了就是横纵坐标都不大于它的点(mx:“我的战壕为什么这么菜”ToT).这样,永恒就可以从别的地方进攻摧毁战壕,从而消灭mx的部队. 战壕都有一个保护范围,同它的攻击

luogu 1712 区间(线段树+尺取法)

题意:给出n个区间,求选择一些区间,使得一个点被覆盖的次数超过m次,最小的花费.花费指的是选择的区间中最大长度减去最小长度. 坐标值这么大,n比较小,显然需要离散化,需要一个技巧,把区间转化为半开半闭区间,然后线段树的每一个节点表示一个半开半闭区间. 接着我们注意到需要求最小的花费,且这个花费只与选择的区间集合中的最大长度和最小长度有关. 这意味着如果最大长度和最小长度一定,我们显然是需要把中间长度的区间尽量的选择进去使答案不会变的更劣. 不妨把区间按长度排序,枚举每个最小长度区间,然后最大区间

【BZOJ】1382: [Baltic2001]Mars Maps (线段树+扫描线)

1382: [Baltic2001]Mars Maps Time Limit: 5 Sec  Memory Limit: 64 MB Description 给出N个矩形,N<=10000.其坐标不超过10^9.求其面积并 Input 先给出一个数字N,代表有N个矩形. 接下来N行,每行四个数,代表矩形的坐标. Output 输出面积并 Sample Input 2 10 10 20 20 15 15 25 30 Sample Output 225 本以为是傻逼题,没想到不容易啊- 线段树+扫描

BZOJ 1012: [JSOI2008]最大数maxnumber(线段树)

012: [JSOI2008]最大数maxnumber Time Limit: 3 Sec  Memory Limit: 162 MB Description 现在请求你维护一个数列,要求提供以下两种操作:1. 查询操作.语法:Q L 功能:查询当前数列中末尾L个数中的最大的数,并输出这个数的值.限制:L不超过当前数列的长度.2. 插入操作.语法:A n 功能:将n加上t,其中t是最近一次查询操作的答案(如果还未执行过查询操作,则t=0),并将所得结果对一个固定的常数D取模,将所得答案插入到数列

HDU 1754 I Hate It(线段树之单点更新,区间最值)

I Hate It Time Limit: 9000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 70863    Accepted Submission(s): 27424 Problem Description 很多学校流行一种比较的习惯.老师们很喜欢询问,从某某到某某当中,分数最高的是多少. 这让很多学生很反感.不管你喜不喜欢,现在需要你做的是,就是按照老师的

线段树入门总结

线段树的入门级 总结   线段树是一种二叉搜索树,与区间树相似,它将一个区间划分成一些单元区间,每个单元区间对应线段树中的一个叶结点.      对于线段树中的每一个非叶子节点[a,b],它的左儿子表示的区间为[a,(a+b)/2],右儿子表示的区间为[(a+b)/2+1,b].因此线段树是平衡二叉树,最后的子节点数目为N,即整个线段区间的长度.      使用线段树可以快速的查找某一个节点在若干条线段中出现的次数,时间复杂度为O(logN).而未优化的空间复杂度为2N,因此有时需要离散化让空间