水线段树。

 /* 380C */
#include <iostream>
#include <string>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>
#include <deque>
#include <algorithm>
#include <cstdio>
#include <cmath>
#include <ctime>
#include <cstring>
#include <climits>
#include <cctype>
#include <cassert>
#include <functional>
#include <iterator>
#include <iomanip>
using namespace std;
//#pragma comment(linker,"/STACK:102400000,1024000") #define sti set<int>
#define stpii set<pair<int, int> >
#define mpii map<int,int>
#define vi vector<int>
#define pii pair<int,int>
#define vpii vector<pair<int,int> >
#define rep(i, a, n) for (int i=a;i<n;++i)
#define per(i, a, n) for (int i=n-1;i>=a;--i)
#define clr clear
#define pb push_back
#define mp make_pair
#define fir first
#define sec second
#define all(x) (x).begin(),(x).end()
#define SZ(x) ((int)(x).size())
#define lson l, mid, rt<<1
#define rson mid+1, r, rt<<1|1 const int maxn = 1e6+;
char s[maxn];
int ln[maxn<<];
int rn[maxn<<];
int L, R; inline void PushUp(int rt) {
int lb = rt<<;
int rb = (rt<<)|;
int tmp = min(ln[lb], rn[rb]); ln[rt] = ln[lb] + ln[rb] - tmp;
rn[rt] = rn[lb] + rn[rb] - tmp;
} void build(int l, int r, int rt) {
if (l == r) {
if (s[l] == '(')
ln[rt] = ;
else
rn[rt] = ;
return ;
} int mid = (l+r)>>;
build(lson);
build(rson);
PushUp(rt);
} pii query(int l, int r, int rt) {
if (L<=l && R>=r) {
return mp(ln[rt], rn[rt]);
}
int mid = (l+r)>>;
if (mid < L) {
return query(rson);
} else if (mid >= R) {
return query(lson);
} else {
pii p1 = query(lson);
pii p2 = query(rson);
pii ret;
int tmp = min(p1.fir, p2.sec);
ret.fir = p1.fir + p2.fir - tmp;
ret.sec = p1.sec + p2.sec - tmp;
return ret;
}
} int main() {
ios::sync_with_stdio(false);
#ifndef ONLINE_JUDGE
freopen("data.in", "r", stdin);
freopen("data.out", "w", stdout);
#endif scanf("%s", s+); int n = strlen(s+), m;
pii p;
int ans; build(, n, );
scanf("%d", &m);
while (m--) {
scanf("%d %d", &L, &R);
p = query(, n, );
ans = R-L+-p.fir-p.sec;
printf("%d\n", ans);
} #ifndef ONLINE_JUDGE
printf("time = %d.\n", (int)clock());
#endif return ;
}

【CF】223 Div.1 C Sereja and Brackets的更多相关文章

  1. Codeforces Round #223 (Div. 2) E. Sereja and Brackets 线段树区间合并

    题目链接:http://codeforces.com/contest/381/problem/E  E. Sereja and Brackets time limit per test 1 secon ...

  2. 【CF】121 Div.1 C. Fools and Roads

    题意是给定一棵树.同时,给定如下k个查询: 给出任意两点u,v,对u到v的路径所经过的边进行加计数. k个查询后,分别输出各边的计数之和. 思路利用LCA,对cnt[u]++, cnt[v]++,并对 ...

  3. 【CF】310 Div.1 C. Case of Chocolate

    线段树的简单题目,做一个离散化,O(lgn)可以找到id.RE了一晚上,额,后来找到了原因. /* 555C */ #include <iostream> #include <str ...

  4. 【CF】110 Div.1 B. Suspects

    这题目乍眼一看还以为是2-sat.其实很水的,O(n)就解了.枚举每个人,假设其作为凶手.观察是否满足条件.然后再对满足的数目分类讨论,进行求解. /* 156B */ #include <io ...

  5. 【CF】222 Div.1 B Preparing for the Contest

    这样类似的题目不少,很多都是一堆优化条件求最优解,这个题的策略就是二分+贪心.对时间二分, 对费用采用贪心. /* 377B */ #include <iostream> #include ...

  6. 【CF】207 Div.1 B.Xenia and Hamming

    这题目一看很牛逼,其实非常easy.求求最小公倍数,最大公约数,均摊复杂度其实就是O(n). /* 356B */ #include <iostream> #include <str ...

  7. 【CF】142 Div.1 B. Planes

    SPFA.注意状态转移条件,ans的求解需要在bfs中间求解.因为只要到了地点n,则无需等待其他tourist.还是蛮简单的,注意细节. /* 229B */ #include <iostrea ...

  8. 【CF】196 Div.2 D. Book of Evil

    显然这个图是一课树,看着题目首先联想到LCA(肯定是可以解的).但是看了一下数据大小,应该会TLE.然后,忽然想到一个前面做过的题目,大概是在一定条件下树中某结点旋转成为根后查询最长路径.结果灵感就来 ...

  9. 【CF】259 Div.1 B Little Pony and Harmony Chest

    还蛮有趣的一道状态DP的题目. /* 435B */ #include <iostream> #include <string> #include <map> #i ...

随机推荐

  1. VS2015 Cordova Ionic移动开发(一)

    一.Windows环境配置 1.如果已经安装VS2015,打开[工具]-[选项]找到Cordova选项: 运行依赖关系查看器,用来检测开发环境是否完整. 如果检测显示: 那么就是环境配置完成了.可以直 ...

  2. 关于bootstrap的datepicker在meteor应用中的使用(不包含bootstrap框架)

    1.安装bootstrap3-datepicker包 meteor add rajit:bootstrap3-datepicker 2.使用方法 Example In your handlebars ...

  3. c常用字符串函数

    获取字符串长度 : size_t strlen(const char *str); 字符串拷贝函数: 把src中内容拷贝到dest中,它会覆盖原来的内容,它会把src中的\0,没有覆盖内容不变 如果s ...

  4. PHP学习笔记(六)

    <Wordpress 50个过滤钩子> 1-10 过滤钩子是一类函数,wordpress执行传递和处理数据的过程中,在针对这些数据做出某些动作之前的特定点执行.本质上,就是在wordpre ...

  5. 九度OJ 1066 字符串排序

    题目地址:http://ac.jobdu.com/problem.php?pid=1066 题目描述: 输入一个长度不超过20的字符串,对所输入的字符串,按照ASCII码的大小从小到大进行排序,请输出 ...

  6. Google Map 自定义 infowindow

    最近做的东西用到了infowindow,不过google提供的样式太难看了,于是想改变一下样式.看了一下好像infowindow的样式不太好改. 查了半天资料,看到一个infobox,感觉真的挺好用. ...

  7. [转] HTML5终极备忘大全(图片版+文字版)---张鑫旭

    by zhangxinxu from http://www.zhangxinxu.com本文地址:http://www.zhangxinxu.com/wordpress/?p=1544 一.前言兼图片 ...

  8. H5小内容(四)

    SVG   基本内容     SVG并不属于HTML5专有内容       HTML5提供有关SVG原生的内容     在HTML5出现之前,就有SVG内容     SVG,简单来说就是矢量图     ...

  9. 【转】PFILE和SPFILE介绍

    原文:http://blog.sina.com.cn/s/blog_77bba23901017xcl.html 一.PFILE Pfile(Parameter File,参数文件)是基于文本格式的参数 ...

  10. php练习5——简单的学生管理系统(隐藏控件的使用)

    要求:    程序:gradeManage.html和gradeManage.php          结果       注意:   1.使用隐藏控件时,得在不同表单下,不能在同一个表单下:   2. ...