日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

Sereja and Brackets CodeForces - 380C (树状数组+离线)

發(fā)布時(shí)間:2023/12/18 编程问答 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Sereja and Brackets CodeForces - 380C (树状数组+离线) 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

Sereja and Brackets

題目鏈接: CodeForces - 380C

Sereja has a bracket sequence s1,?s2,?...,?*s**n, or, in other words, a string s* of length n, consisting of characters "(" and ")".

Sereja needs to answer m queries, each of them is described by two integers li,?ri(1?≤?li?≤?ri?≤?n). The answer to the i-th query is the length of the maximum correct bracket subsequence of sequence sli,?sli?+?1,?...,?sri. Help Sereja answer all queries.

You can find the definitions for a subsequence and a correct bracket sequence in the notes.

Input

The first line contains a sequence of characters s1,?s2,?...,?*s**n* (1?≤?n?≤?106) without any spaces. Each character is either a "(" or a ")". The second line contains integer m (1?≤?m?≤?105) — the number of queries. Each of the next m lines contains a pair of integers. The i-th line contains integers li,?ri (1?≤?li?≤?ri?≤?n) — the description of the i-th query.

Output

Print the answer to each question on a single line. Print the answers in the order they go in the input.

Examples

Input

())(())(())(71 12 31 21 128 125 112 10

Output

00210466

Note

A subsequence of length |x| of string s?=?s1s2... s|s| (where |s| is the length of string s) is string x?=?sk1sk2... *s**k|x| (1?≤?k1?<?k2?<?...?<?k|x|?≤?|s*|).

A correct bracket sequence is a bracket sequence that can be transformed into a correct aryphmetic expression by inserting characters "1" and "+" between the characters of the string. For example, bracket sequences "()()", "(())" are correct (the resulting expressions "(1)+(1)", "((1+1)+1)"), and ")(" and "(" are not.

For the third query required sequence will be ?()?.

For the fourth query required sequence will be ?()(())(())?.

題意:

給你一個(gè)只含有'(' 和')' 的字符串,

以及q個(gè)詢(xún)問(wèn),每一個(gè)詢(xún)問(wèn)給你兩個(gè)整數(shù)l和r,代表一個(gè)區(qū)間。對(duì)于每一個(gè)詢(xún)問(wèn),讓你輸出區(qū)間中能選出最長(zhǎng)的子序列是合法的括號(hào)序列的長(zhǎng)度。

思路:

對(duì)詢(xún)問(wèn)區(qū)間進(jìn)行離線(xiàn)保存,以右端點(diǎn)升序來(lái)排序,

然后從坐向右掃描,用stack來(lái)維護(hù)每一個(gè)還沒(méi)被匹配到的(字符的下標(biāo),

當(dāng)來(lái)一個(gè))字符的時(shí)候,將其和棧頂?shù)哪莻€(gè)(匹配,同時(shí)用樹(shù)狀數(shù)組在(的下標(biāo)的數(shù)值上+2

然后走到每一個(gè)區(qū)間的右端點(diǎn)時(shí)對(duì)區(qū)間的答案進(jìn)行更新。

為什么這樣寫(xiě)可以呢?

因?yàn)槲覀兛梢酝ㄟ^(guò)分析發(fā)現(xiàn),字符串中每一個(gè))字符在區(qū)間中可以固定匹配到一個(gè)(使其區(qū)間答案最右。

細(xì)節(jié)見(jiàn)代碼:

#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <cmath> #include <queue> #include <stack> #include <map> #include <set> #include <vector> #include <iomanip> #define ALL(x) (x).begin(), (x).end() #define sz(a) int(a.size()) #define all(a) a.begin(), a.end() #define rep(i,x,n) for(int i=x;i<n;i++) #define repd(i,x,n) for(int i=x;i<=n;i++) #define pii pair<int,int> #define pll pair<long long ,long long> #define gbtb ios::sync_with_stdio(false),cin.tie(0),cout.tie(0) #define MS0(X) memset((X), 0, sizeof((X))) #define MSC0(X) memset((X), '\0', sizeof((X))) #define pb push_back #define mp make_pair #define fi first #define se second #define eps 1e-6 #define gg(x) getInt(&x) #define chu(x) cout<<"["<<#x<<" "<<(x)<<"]"<<endl using namespace std; typedef long long ll; ll gcd(ll a, ll b) {return b ? gcd(b, a % b) : a;} ll lcm(ll a, ll b) {return a / gcd(a, b) * b;} ll powmod(ll a, ll b, ll MOD) {ll ans = 1; while (b) {if (b % 2) { ans = ans * a % MOD; } a = a * a % MOD; b /= 2;} return ans;} inline void getInt(int *p); const int maxn = 1000010; const int inf = 0x3f3f3f3f; /*** TEMPLATE CODE * * STARTS HERE ***/ struct node {int l, r;int id; } a[maxn]; char s[maxn]; int n; int m; bool cmp(node aa, node bb) {return aa.r < bb.r; } int tree[maxn]; int lowbit(int x) {return x & (-1 * x); } void add(int x, int val) {while (x <= n) {tree[x] += val;x += lowbit(x);} } int ask(int x) {int res = 0;while (x) {res += tree[x];x -= lowbit(x);}return res; } int ans[maxn]; int main() {//freopen("D:\\code\\text\\input.txt","r",stdin);//freopen("D:\\code\\text\\output.txt","w",stdout);gbtb;cin >> s + 1;n=strlen(s+1);cin >> m;repd(i, 1, m ) {cin >> a[i].l >> a[i].r;a[i].id = i;}sort(a + 1, a + 1 + m, cmp);stack<int> st;while (sz(st)) {st.pop();}int pos = 1;repd(i, 1, m) { // chu(pos);repd(j, pos, a[i].r) {if (s[j] == '(') {st.push(j);} else {if (sz(st)) {add(st.top(), 2);st.pop();}}}ans[a[i].id] = ask(a[i].r) - ask(a[i].l - 1);pos = a[i].r + 1;}repd(i,1,m){printf("%d\n",ans[i] );}return 0; }inline void getInt(int *p) {char ch;do {ch = getchar();} while (ch == ' ' || ch == '\n');if (ch == '-') {*p = -(getchar() - '0');while ((ch = getchar()) >= '0' && ch <= '9') {*p = *p * 10 - ch + '0';}} else {*p = ch - '0';while ((ch = getchar()) >= '0' && ch <= '9') {*p = *p * 10 + ch - '0';}} }

轉(zhuǎn)載于:https://www.cnblogs.com/qieqiemin/p/11491979.html

總結(jié)

以上是生活随笔為你收集整理的Sereja and Brackets CodeForces - 380C (树状数组+离线)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。