CodeForces 145E :Lucky Queries 线段树

传送门

题意

n代表字符串的长度,m代表操作的次数。两种操作:count:输出该字符串中非递减子序列的最大长度(子序列不是子串,不需要连续)switch:表示将从l 到 r的所有字符改变,4变成7,7变成4

分析

想搜线段树合并来着,不知道为啥搜出来这道题
区间问题首先想到线段树,一般这种 s w a p swap swap类型的题目,我们都要去维护 s w a p swap swap前后的数值变化,我们维护一下区间内 4 , 7 , 47 , 74 4,7,47,74 4,7,47,74这四种类型的数的长度为多少即可,区间修改的话,两两之间 s w a p swap swap即可

代码

#pragma GCC optimize(3)
#include <bits/stdc++.h>
#define debug(x) cout<<#x<<":"<<x<<endl;
#define dl(x) printf("%lld\n",x);
#define di(x) printf("%d\n",x);
#define _CRT_SECURE_NO_WARNINGS
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
#define SZ(x) ((int)(x).size())
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> PII;
typedef vector<int> VI;
const int INF = 0x3f3f3f3f;
const int N = 1e6 + 10;
const ll mod = 1000000007;
const double eps = 1e-9;
const double PI = acos(-1);
template<typename T>inline void read(T &a) {
	char c = getchar(); T x = 0, f = 1; while (!isdigit(c)) {if (c == '-')f = -1; c = getchar();}
	while (isdigit(c)) {x = (x << 1) + (x << 3) + c - '0'; c = getchar();} a = f * x;
}
int gcd(int a, int b) {return (b > 0) ? gcd(b, a % b) : a;}
char str[N];
int n,m;

struct Node{
	int l,r;
	int cnt1,cnt2,cnt3,cnt4; // 4,7,47,74;
	int add;
}tr[N * 4];

void push(int u){
	tr[u].cnt1 = tr[u << 1].cnt1 + tr[u << 1 | 1].cnt1;
	tr[u].cnt2 = tr[u << 1].cnt2 + tr[u << 1 | 1].cnt2;
	tr[u].cnt3 = max(tr[u << 1].cnt1 + tr[u << 1 | 1].cnt3,tr[u << 1].cnt3 + tr[u << 1 | 1].cnt2);
	tr[u].cnt4 = max(tr[u << 1].cnt2 + tr[u << 1 | 1].cnt4,tr[u << 1].cnt4 + tr[u << 1 | 1].cnt1);
}

void down(int u){
	if(tr[u].add){
		swap(tr[u << 1].cnt1,tr[u << 1].cnt2);
		swap(tr[u << 1].cnt3,tr[u << 1].cnt4);
		swap(tr[u << 1 | 1].cnt1,tr[u << 1 | 1].cnt2);
		swap(tr[u << 1 | 1].cnt3,tr[u << 1 | 1].cnt4);
		tr[u].add ^= 1;
		tr[u << 1].add ^= 1;
		tr[u << 1 | 1].add ^= 1;
	}
}

void build(int u,int l,int r){
	tr[u] = {l,r,0,0,0,0,0};
	if(l == r){
		tr[u].cnt3 = tr[u].cnt4 = 1;
		if(str[l] == '4') tr[u].cnt1++;
		else tr[u].cnt2++;
		return;
	}
	int mid = (l + r) >> 1;
	build(u << 1,l,mid),build(u << 1 | 1,mid + 1,r);
	push(u);
}

void modify(int u,int l,int r){
	if(tr[u].l >= l && tr[u].r <= r){
		swap(tr[u].cnt1,tr[u].cnt2);
		swap(tr[u].cnt3,tr[u].cnt4);
		tr[u].add ^= 1;
		return;
	}
	down(u);
	int mid = (tr[u].l + tr[u].r) >> 1;
	if(l <= mid) modify(u << 1,l,r);
	if(r > mid) modify(u << 1 | 1,l,r);
	push(u);
}

int main() {
	read(n),read(m);
	scanf("%s",str + 1);
	build(1,1,n);
	while(m--){
		char op[10];
		int l,r;
		scanf("%s",op);
		if(*op == 'c') {di(max(tr[1].cnt3,max(tr[1].cnt2,tr[1].cnt1)));}
		else{
			read(l),read(r);
			modify(1,l,r);
		}
	}	
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值