90分超时求调!还可以优化哪里呢?
#include<iostream>
#include<algorithm>
using namespace std;
typedef long long ll;
struct q {
ll id;
ll num;
ll col;
}p[100010];
bool cmp(q x, q y) {
if (x.col < y.col)return 0;
else if (x.col == y.col)return x.id < y.id;
return 1;
}
ll n, m, ans = 0;
bool f[100010];
int main() {
std::cin >> n >> m;
for (ll i = 0; i < n; i++) {
p[i].id = i + 1;
ll num;
std::cin >> num;
p[i].num = num;
}
for (ll i = 0; i < n; i++) {
ll col;
std::cin >> col;
p[i].col = col;
}
sort(p, p + n, cmp);
for (ll i = 0; i < n; i++) {
for (ll j = i + 1; j < n;j++) {
if (p[i].col == p[j].col) {
if ((p[i].id + p[j].id) % 2 == 0) {
ans += (p[i].id + p[j].id) * (p[i].num + p[j].num);
ans %= 10007;
}
}
else break;
}
}
std::cout << ans;
}