看到大家都队列模拟,但前缀和也可以吧。代码如下:
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 10;
typedef long long ll;
ll a[N], b[N];
int main()
{
int n, m;
cin >> n >> m;
for (int i = 1; i <= n; i++)
{
ll tmp;
cin >> tmp;
a[i] = a[i - 1] + tmp;
}
for (int i = 1; i <= m; i++)
{
ll tmp;
cin >> tmp;
b[i] = b[i - 1] + tmp;
}
ll res = 0;
int index1 = 1, index2 = 1;
int pre_index1 = 0, pre_index2 = 0;
while (index1 <= n && index2 <= m)
{
if (a[index1] - a[pre_index1] == b[index2] - b[pre_index2])
{
res += index1 + index2 - pre_index1 - pre_index2 - 2;
pre_index1 = index1;
pre_index2 = index2;
index2++;
index1++;
}
else if (a[index1] - a[pre_index1] < b[index2] - b[pre_index2])
{
index1++;
}
else
{
index2++;
}
}
cout << res;
return 0;
}
1 回复
0 转发
0 喜欢
5 阅读



