20200724T2 【NOIP2015模拟10.28B组】圣章-精灵使的魔法语

Description

【背景介绍】

“魔法???算了吧,这种东西我肯定学不了的啦!”明明是个剑士,却被眼前这位洋洋自得的精灵使——弗洛莉拖出去学魔法,真是个没事找茬的家伙……
“没事啦。作为一名冒险者会发生很多情况,中毒啦,受伤啦,被咒语束缚之类的,没有魔法就很难办的呀!”她到是好像一副什么都懂的样子,真是令人火大。
“都说我是个人类了,魔法这种东西学起来很困难的吧!”我只好找个看似靠谱的借口。
然而,她那不屈不挠的声音又响了起来:“人类虽然与自然的共鸣,也就是魔法的连接较少,但如果认真训练的话还是可以做到的呢!总之,试试看吧!念念咒语之类的!”弗洛莉把魔法书一把拍在了我面前。
我没兴趣地瞟了一眼,“哼。这种东西我不看也会,伦福萨——密西卡!”才刚刚念完不知道从哪里偷学来的魔法咒语。随即,便听到弗洛莉的一声尖叫,使得整个酒店的人的视线都往这边看来。喂喂喂,别往我这边看啊,我有视线恐惧症啊!!!!况且,我只是把她正在吃的面包的样子变成虫子而已,谁会料到这种情况啊啊啊!!
“真是的,弗洛莉才是老拖我的后腿呢!”我没好气地笑道……
“里修!你……”她从牙缝里挤出了一个字。我顿感不妙,见到了那张比魔鬼还可怕的扭曲的面孔。“真是个魔法的天才哪!”她一扫之前不愉快的表情,想我露出大拇指,好像是在夸奖我的样子。
咦?她竟然没有打我,那真是我福大命大。我这样想着,便一屁股坐在了凳子上,松了口气……

【题目描述】

“伦福萨”【即" ( "】和“密西卡”【即" ) "】是两种不同的精灵咒语,已知一个成功的咒语符合如下的规定:
每一个密西卡之前都可以对应匹配到一个伦福萨,即为一个合法的精灵魔法咒语。
方便的是,我们将“伦福萨”视为" ( ",“密西卡”视为" ) ",合法的精灵魔法咒语即为一个合法的括号序列。
如:" ( ( ( ) ) ) "" ( ( ) ( ) ) "" ( ) ( ) ( ) "均为合法的魔法咒语," ) ( "" ( ) ) ( "" ( ( "均为不合法的魔法咒语。
现在弗洛莉给我一个长长的“伦福萨”【即" ( "】和“密西卡”【即" ) "】的片段,每次给我一个l和r,让我判断需要在这个片段前最少添多少个“伦福萨”【即" ( "】,以及最少添多少个“密西卡”【即" ) "】可以成为一个合法的魔法咒语,更令人不爽的是,弗洛莉有的时候还会把一个“伦福萨”【即" ( "】变成“密西卡”【即" ) "】,或把一个“密西卡”【即" ) "】变为“伦福萨”【即" ( "】。

Input

第一行两个正整数n,m,表示我现在含有的咒语元素(“伦福萨”【即" ( "】和“密西卡”【即" ) "】)的个数以及弗洛莉给我的任务个数,
第二行包含n个字符(“伦福萨”【即" ( "】或“密西卡”【即" ) "】)表示一开始弗洛莉给我的咒语片段。
以下m行包括两种任务:
Change x,表示弗洛莉将位置为x上的咒语片段进行一次变换(原来是“伦福萨”【即" ( "】变为“密西卡”【即" ) "】,原来是“密西卡”【即" ) "】变为“伦福萨”【即" ( "】)。
Query l r,询问从l到r的区间的片段,在这个片段前最少添上多少个伦福萨”【即" ( "】,在这个片段后最少添上多少个“密西卡”【即" ) "】可以成为合法的魔法序列。

Output

每个询问对应一行答案,每行包括两个整数,表示在这个片段前最少添上多少个伦福萨”【即" ( "】,在这个片段后最少添上多少个“密西卡”【即" ) "】可以成为合法的魔法序列。

Sample Input

6 4
(()()(
Query 1 3
Query 3 6
Change 6
Query 1 6

Sample Output

0 1
1 1
0 0
【样例解释】
1.片段为“ ( ( ) ”最右边填1个 ) 即可。
2.片段为“ ) ( ) ( ”最左边添1个 ( 最右边添1个 ) 即可。
3.片段为“ ( ( ) ( ) ) ”已经是合法片段。不需添加。

Data Constraint

对于20%的数据,1 ≤ n,m ≤ 100
对于40%的数据,1 ≤ n,m ≤ 3000
另外含有30%的数据,数据中不包含修改操作。
对于100%的数据,1 ≤ n,m ≤ 150,000

solution

显然线段树

不过update不太好处理

所以copy zjy的

时间复杂度为O(m log n)

code

  1 #include<iostream>
  2 #include<cstdio>
  3 #include<cmath>
  4 #include<algorithm>
  5 #include<cstring>
  6 #include<queue>
  7 #include<vector>
  8 #include<stack>
  9 #include<set>
 10 #include<deque>
 11 #include<map>
 12 using namespace std;
 13 
 14 template <typename T> void read(T &x) {
 15     x = 0; int f = 1; char c;
 16     for (c = getchar(); c < '0' || c > '9'; c = getchar()) if (c == '-') f = -f;
 17     for (; c >= '0' && c <= '9'; c = getchar()) x = 10 * x + c - '0' ;
 18     x *= f;
 19 }
 20 template <typename T> void write(T x){
 21     if (x < 0) putchar('-'), x = -x;
 22     if (x > 9) write(x / 10);
 23     putchar(x % 10 + '0');
 24 }
 25 template <typename T> void writeln(T x) { write(x); putchar('
'); }
 26 template <typename T> void writesn(T x) { write(x); putchar(' '); }
 27 
 28 #define ll long long
 29 #define inf 1234567890
 30 #define next net
 31 #define P 2147483647
 32 #define N 150010
 33 #define mid ((l+r)>>1)
 34 #define lson (o<<1)
 35 #define rson (o<<1|1)
 36 #define Re register
 37 #define debug puts("zxt")
 38 
 39 int n , m , x, y;
 40 char s[N ],qwq[10];
 41 int a[N ];
 42 struct node{
 43     int left,right;
 44 }tree[N << 2], ans;
 45 node update(node a, node b)
 46 {
 47     node kkk;
 48     kkk.left = (a.left > b.right ? a.left - b.right : 0) + b.left;
 49     kkk.right = (a.left < b.right ? b.right - a.left : 0)+ a.right;
 50     return kkk;
 51 }
 52 void build(int o, int l, int r)
 53 {
 54     if(l == r)
 55     {
 56         if(a[l]) tree[o].left++;
 57         else tree[o].right++;
 58         return;
 59     }
 60     build(lson, l, mid);
 61     build(rson, mid + 1, r);
 62     tree[o] = update(tree[lson], tree[rson]);
 63     return;
 64 }
 65 void change(int o, int l, int r, int x)
 66 {
 67     if(l == r)
 68     {
 69         a[l] ^= 1;
 70         if(a[l] == 1) tree[o].left++, tree[o].right--;
 71         else tree[o].left--, tree[o].right++;
 72         return;
 73     }
 74     if(x <= mid) change(lson, l, mid, x);
 75     else change(rson, mid + 1, r, x);
 76     tree[o] = update(tree[lson], tree[rson]);
 77     return;
 78 }
 79 node query(int o, int l, int r, int L, int R )
 80 {
 81     if(l == L && R == r)
 82     {
 83         return tree[o];
 84     }
 85     if(R <= mid) return query(lson, l, mid, L, R);
 86     if(L > mid) return query(rson, mid + 1, r, L, R);
 87     return update(query(lson, l, mid, L, mid), query(rson, mid + 1, r, mid + 1, R));
 88 }
 89 signed main()
 90 {
 91     //freopen("elf.in","r",stdin);
 92     //freopen("elf.out","w",stdout);
 93     read(n); read(m );
 94     scanf("%s", s + 1);
 95     for(Re int i = 1; i <= n; i++) a[i] = (s[i] == '(' ? 1 : 0);
 96     build(1, 1, n);
 97     while(m --)
 98     {
 99         scanf("%s", qwq);
100         if(qwq[0] == 'Q')
101         {
102             read(x); read(y);
103             ans = query(1, 1, n, x, y);
104             writesn(ans.right); writeln(ans.left);
105         }
106         else
107         {
108             read(x);
109             change(1, 1, n, x);
110         }
111     }
112     return 0;
113 }
原文地址:https://www.cnblogs.com/e-e-thinker/p/13374057.html