WA on #35 莫队求助
查看原帖
WA on #35 莫队求助
359485
best_lover楼主2024/8/22 16:24
#include <bits/stdc++.h>
#define int long long

using namespace std;

const int N = 4e5 + 5;
int n, m, k, a[N], pos[N];
int ans[N], now, cnt[N];
struct query {
  int l, r, id;
}q[N];

inline bool cmp(query a, query b){
	if(pos[a.l] == pos[b.l]){
		if(pos[a.l] & 1) return a.r < b.r;
		return a.r > b.r;
	}
	return a.l < b.l;
}

void add(int x) {
  cnt[a[x]]++;
  now += (2 * cnt[a[x]] - 1) * a[x];
}

void del (int x) {
  cnt[a[x]]--;
  now -= (2 * cnt[a[x]] + 1) * a[x];
}

signed main() {

  ios::sync_with_stdio(0);
  cin.tie(0), cout.tie(0);

  cin >> n >> m; 
  
  k = n/sqrt(m*2/3) + 1;
  
  for(int i = 1; i <= n; i++) {
    cin >> a[i];
  }

  for(int i = 1; i <= m; i++) {
    cin >> q[i].l >> q[i].r;
    q[i].id = i;
    pos[i]=(i-1) / k + 1;
  }
  
  sort(q + 1, q + 1 + m, cmp);
  int l = 1, r = 0;
  
  for(int i = 1;i <= m; i++) {
    while(q[i].l < l) add(--l);
    while(q[i].r > r) add(++r);
    while(q[i].l > l) del(l++);
    while(q[i].r < r) del(r--);
    ans[q[i].id] = now;
  }
  
  for(int i = 1; i <= m; i++) {
    cout << ans[i] << "\n";
  } 
  
  return 0;
}

Rt

2024/8/22 16:24
加载中...