暑期嗨八~(E题还要加强理解!!!)

CodeForces - 1234D

题意:在这里插入图片描述
一串给你多次这俩命令 2命令是需要输出东西的。
思路:自己刚开始还以为普通的做就行,然后就TL了。
用26个set来存储每个字母在字符串中出现的位置,因为STL中set是排序的,所以每次查询时,只需要遍历26个字母,查询其在字符串中第一次出现的在l之后的位置,若该位置比r要小,说明这个字符存在于这个区间之内,答案加1。更新时只需要在原串位置的字符对应的set中去除这个位置,并在新替换的字符对应的set中存入该位置即可。

#include <iostream>
#include <cstdio>
#include <cmath>
#include <string>
#include <cstring>
#include <stack>
#include <algorithm>
#include <iomanip>
#include <map>
#include <queue>
#include <vector>
#include <set>
#define inf 0x3f3f3f3f
typedef long long ll;
using namespace std;
char s[200000];
int q;
set<int>sett[26];
int main()
{
	cin >> (s + 1);
	cin >> q;
	int n = strlen(s + 1);
	for (int i = 1; i <= n; i++)
		sett[s[i] - 'a'].insert(i);
	while (q--)
	{
		int ch; cin >> ch;
		if (ch == 1)
		{
			int p; char c; cin >> p >> c;
			int cur = s[p] - 'a';
			if (sett[cur].count(p))
				sett[cur].erase(sett[cur].find(p));
			s[p] = c;
			sett[c - 'a'].insert(p);
		}
		else
		{
			int l, r, ans = 0;
			cin >> l >> r;
			for (int i = 0; i < 26; i++)
			{
				int cur = *sett[i].lower_bound(l);//第一个大于或等于被查数的值的下标
				if (cur >= l && cur <= r)
					ans++;
			}
			cout << ans << endl;
		}
		
	}
	
}

CodeForces - 1234E

题意:
定义pi(n)为1到n的排列,第i位移动到第一个位置,下面将pi(n)简写为p
例如:
p1(4)=[1,2,3,4]
p2(4)=[2,1,3,4]
定义pos(p,val)为上面p中值为val的元素所在位置,例如pos(p1(4),3)=3,因为p1(4)=[1,2,3,4],元素3在第3个位置
给一个长度为m的序列x
定义f(p ):

在这里插入图片描述
看看样例的解释就很清楚了。
思路:
在这里插入图片描述

#include <iostream>
#include <cstdio>
#include <cmath>
#include <string>
#include <cstring>
#include <stack>
#include <algorithm>
#include <iomanip>
#include <map>
#include <queue>
#include <vector>
#include <set>
#define inf 0x3f3f3f3f
typedef long long ll;
const int maxn = 200100;
ll a[maxn], b[maxn], c[maxn], ans[maxn];
using namespace std;
int main()
{
	int n, m, i, j, k; cin >> n >> m;
	for (i = 0; i < m; i++)
		cin >> a[i];
	for (i = 1; i < m; i++)
	{
		j = max(a[i], a[i - 1]);
		k = min(a[i], a[i - 1]);
		ans[1] += j - k;
		if (j == k) continue;
		b[k] += k - 1; b[j] += k - (j - k);
		c[k + 1]--; c[j]++;
	}
	for (i = 2; i <= n; i++)
		c[i] += c[i - 1];
	for (i = 2; i <= n; i++)
		ans[i] = ans[1] + b[i] + c[i];
	for (i = 1; i <= n; i++)
		cout << ans[i] << " ";


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值