https://acm.hdu.edu.cn/showproblem.php?pid=7020
题目大意:求出区间众数严格大于区间一半大小的子区间的数量。
思路:先对每一个数求出对应的位置排列,S[j]代表1-有多少个i,易得s[r]-s[l]>r-l-(s[r]-s[l])的时候子区间{l+1,r}是合法的,然后呢,我赛时就不知道怎么处理二阶的前缀和,数据结构菜鸡。
可以参考:P4062 [Code+#1]Yazid 的新生舞会
洛谷上的一道一模一样的题
洛谷上的题解:https://www.luogu.com.cn/problem/solution/P4062,HDU的数据比较大 要n*logn
#include <cstdio>
#include <cstring>
#include <string>
#include <cmath>
#include <iostream>
#include <algorithm>
#include <queue>
#include <cstdlib>
#include <stack>
#include <vector>
#include <set>
#include <map>
#include <bitset>
#include <complex>
#define INF 0x3f3f3f3f3f3f3f3f
#define inf 0x3f3f3f3f
#define FILL(a,b) (memset(a,b,sizeof(a)))
#define lson rt<<1
#define rson rt<<1|1
#define lowbit(a) ((a)&-(a))
#define ios std::ios::sync_with_stdio(false);std::cin.tie(0);std::cout.tie(0);
#define fi first
#define sc second
#define pb push_back
#define all(x) (x).begin(),(x).end()
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;
const ll mod=1e9+7;
const ll N =6e6+10;
const double eps = 1e-6;
const double PI=acos(-1);
ll gcd(ll a,ll b){return !b?a:gcd(b,a%b);}
ll re(){ll x;scanf("%lld",&x);return x;}
int dx[8]= {1,0,-1,0,1,1,-1,-1}, dy[8] = {0,1,0,-1,1,-1,1,-1};
int n;
int a[N];
vector<int>b[N];
ll c1[N],c2[N],c3[N];
ll q(int x){ll ans=0;for(int i=x;i>0;i-=lowbit(i)){ans+=c1[i]*(x+2)*(x+1)-c2[i]*(2*x+3)+c3[i];}return ans/2;
}
void add(int x,ll v,int n){//二阶前缀和for(int i=x;i<=n;i+=lowbit(i)){c1[i]+=v;c2[i]+=v*x;c3[i]+=v*x*x;}
}
void solve(){cin>>n;int maxx=0;for(int i=1;i<=n;i++) {cin>>a[i];maxx=max(maxx,a[i]);b[a[i]].pb(i);}ll ans=0;int wc=n+1;for(int i=0;i<=maxx;i++){//if(!b[i].size()) continue;b[i].pb(n+1);int last=0;for(int j=0;j<b[i].size();j++){int x=2*j-(b[i][j]-1)+wc;int y=2*j-last+wc;ans+=q(y-1);if(x>=3) ans-=q(x-2);add(x,1,2*n+1);add(y+1,-1,2*n+1);last=b[i][j];}last=0;for(int j=0;j<b[i].size();j++){int x=2*j-(b[i][j]-1)+wc;int y=2*j-last+wc;add(x,-1,2*n+1);add(y+1,1,2*n+1);last=b[i][j];}b[i].clear();}cout<<ans<<endl;
}
int main()
{iosint t;cin>>t;while(t--) solve();return 0;
}