跳到正文

个人记录

树状数组&线段树总结

the133lvlone约 2 分钟
原文

这是 2025年10月8日 存档的第 1 版,不是当前版本。查看当前版本

文章信息与版本

文章信息

收录
2025年12月12日
更新
2025年12月12日
篇幅
约 2 分钟

版本

  1. v2当前
  2. v1

总结

T1

C++
#include"bits/stdc++.h"
#define con continue
#define pb push_back
#define en length
//#define int long long
//#define double long double
using namespace std;
string a;
int b[26],d;
signed main()
{
	cin>>a;
	for(int i=0;i<a.en();i++)
	{
		memset(b,0,sizeof(b));
		for(int j=i;j<a.en();j++)
		{
			b[a[j]-97]++;
			for(int k=0;k<26;k++)
			{
				if(b[k]>((j-i+1)>>1))
				{
					goto c;
				}
			}
			d++;
			c:;
		}
	}
	cout<<d;
	return 0;
} 

T2

C++
#include"bits/stdc++.h"
#define con continue
#define pb push_back
#define en length
//#define int long long
//#define double long double
using namespace std;
string a;
int b[100002][26],d,e[201602],q;
void A(int p)
{
    while(p<(n<<1)+3)
    {
        e[p]++;
        p+=(p&(-p));
    }
    return;
}
int B(int p)
{
    while(p)
    {
        q+=e[p];
        p-=(p&(-p));
    }
    return q;
}
signed main()
{
	cin>>a;
	for(int i=0;i<a.en();i++)
	{
        for(int j=0;j<26;j++)
        {
            if(i==0)
            {
                break;
            }
            b[i][j]=b[i-1][j];
        }
		b[i][a[j]-97]++;
	}
  //树状数组获得众数串数量,进树状数组加&查的数+n+2就是正数
	cout<<((a.en()*(a.en()-1))>>1)-d;
	return 0;
}
代码
树状数组获得某个字符多于一半长度的子串的数量
暴力的方式O(n^2)枚举起始点,随后一个一个字符的加