A. 简单题
#include<bits/stdc++.h>
const int N=100010;
#define LL long long
#define MOD 1000000007
using namespace std;
int a[N];
int L[N],R[N];
int M1[N],M2[N];
LL work(LL l, LL r, LL k,int n)
{
LL ans;
l++;
r--;
ans=((k-l+1)*(r-k+1))%MOD;
return ans;
}
int main()
{
int n,l,r;
LL ans;
while(scanf("%d",&n)!=EOF)
{
ans=0;
//map<int,int> M1, M2;
for (int i=1;i<=n;i++) M1[i]=M2[i]=0;
for (int i=1;i<=n;i++) scanf("%d",&a[i]);
for (int i=1;i<=n;i++)
{
l=0;
for (int j=1;j<=(int)sqrt(a[i]+0.5);j++)
if (a[i]%j==0)
{
int j1=j,j2=a[i]/j;
//if (M1.find(j1)!=M1.end()) l=max(M1[j1],l);
//if (M1.find(j2)!=M1.end()) l=max(M1[j2],l);
if (M1[j1]) l=max(M1[j1],l);
if (M1[j2]) l=max(M1[j2],l);
}
//if (M1.find(a[i])==M1.end()) M1.insert(make_pair(a[i],i)); else M1[a[i]]=i;
M1[a[i]]=i;
L[i]=l;
}
for (int i=n;i>=1;i--)
{
r=n+1;
for (int j=1;j<=(int)sqrt(a[i]+0.5);j++)
if (a[i]%j==0)
{
int j1=j,j2=a[i]/j;
//if (M2.find(j1)!=M2.end()) r=min(M2[j1],r);
//if (M2.find(j2)!=M2.end()) r=min(M2[j2],r);
if (M2[j1]) r=min(M2[j1],r);
if (M2[j2]) r=min(M2[j2],r);
}
//if (M2.find(a[i])==M2.end()) M2.insert(make_pair(a[i],i)); else M2[a[i]]=i;
M2[a[i]]=i;
R[i]=r;
}
for (int i=1;i<=n;i++) ans=(ans+work((LL)L[i],(LL)R[i],(LL)i,n))%MOD;
printf("%I64d\n",ans);
}
return 0;
}
B. 数据结构题目
#include<bits/stdc++.h>
#define LL long long
using namespace std;
struct node
{
LL value;
int index;
node(LL value,int index):value(value),index(index){}
bool operator<(const node &b)const {
return (this->value>b.value);
}
};
//set<node> S;
set<LL> S;
int j,n;
LL k;
LL a[100010];
void work(set<LL>::iterator now, LL value,int index,int flag)
{
//set<node>::iterator tmp;
set<LL>::iterator tmp;
if (flag==1) now--;
//while (abs((*now).value-value)>=k)
while (abs((*now)/1000000-value)>=k)
{
//int val = (*now).index;
int val = (*now)%1000000;
while (j != val)
{
//S.erase(node(a[j],j));
S.erase(a[j]*1000000+j);
j++;
}
tmp=now;
if (flag==1) now--; else now++;
S.erase(tmp);
j++;
if (S.size()==0)
{
if (flag==-1) S.clear();
return;
}
}
}
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
LL ans=0;
S.clear();
j=0;
scanf("%d%I64d",&n,&k);
for (int i=0;i<n;i++)
{
scanf("%I64d",&a[i]);
if (S.size()>0) work(S.end(),a[i],i,1);
if (S.size()>0) work(S.begin(),a[i],i,-1);
ans=ans+S.size()+1;
//S.insert(node(a[i],i));
S.insert(a[i]*1000000+i);
}
printf("%I64d\n", ans);
}
return 0;
}
/*
1
10 5
0 3 4 5 2 1 6 7 8 9
*/