求助莫队卡常
查看原帖
求助莫队卡常
406941
Register_int-std=c++14楼主2022/10/13 22:53

rt.人傻常数大

#include <bits/stdc++.h>

using namespace std;

// using fread
#define INPUT_OPTIMIZE

// using fwrite
#define OUTPUT_OPTIMIZE

namespace IO {
	
#ifdef INPUT_OPTIMIZE
	static char buf[1 << 21], *p1 = buf, *p2 = buf;
	#define getchar() p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1 << 21, stdin), p1 == p2) ? EOF : *p1++
#endif
	
	inline  
	bool read(char *t) {
		memset(t, 0, sizeof t);
		char *p = t, c = getchar();
		while (isspace(c)) c = getchar();
		while (!isspace(c)) *p++ = c, c = getchar();
		return c == EOF;
	}
	
	template 
	<typename T> 
	inline 
	bool read(T &t) {
		t = 0;
    	char c = getchar(); bool f = 1;
    	while (isspace(c)) c = getchar(); 
		if (c == '-') f = 0, c = getchar();
    	while (isdigit(c)) t = (t << 3) + (t << 1) + (c ^ 48), c = getchar();
		t *= f ? 1 : -1;
		return c == EOF;
	}

	template 
	<typename T, typename... Args> 
	inline 
	bool read(T &t, Args&... args) {
		return read(t) ? 1 : read(args...);
	}

#ifdef OUTPUT_OPTIMIZE
	static char outbuf[1 << 24], *out = outbuf;
	#define putchar(x) *out++ = x
	#define flush() fwrite(outbuf, 1, out - outbuf, stdout)
#else 
   #define flush() 0
#endif

	inline 
	void write(const char* s) {
		int l = strlen(s);
		for (int i = 0; i < l; i++) putchar(s[i]);
	}

	template 
	<typename T> 
	inline 
	void write(T x) {
		if (x < 0) putchar('-'), x = -x;
		if (!x) return putchar('0'), void();
		static char t[20], p = 0;
		while (x) t[++p] = (x % 10) ^ 48, x /= 10;
		while (p) putchar(t[p--]);
	}
	
	template 
	<typename T, typename... Args> 
	inline 
	void write(T &t, Args&... args) {
		write(t), write(args...);
	}
	
}

using namespace IO;

typedef long long ll;

const int MAXN = 5e4 + 10;

int pos[MAXN], c[MAXN];

struct query {
	int l, r, id;
	bool operator < (const query &rhs) const {
		if (pos[l] == pos[rhs.l]) return pos[l] & 1 ? r < rhs.r : r > rhs.r;
		return l < rhs.l;
	}
} q[MAXN];

ll cnt[MAXN], ans;

ll a[MAXN], b[MAXN];

inline void add(int p) { ans -= cnt[p] * cnt[p], cnt[p]++, ans += cnt[p] * cnt[p]; }
inline void del(int p) { ans -= cnt[p] * cnt[p], cnt[p]--, ans += cnt[p] * cnt[p]; }

int n, m, len;

int l = 1, r = 0;

int main() {
	read(n, m);
	for (int i = 1; i <= n; i++) read(c[i]);
	len = sqrt(n);
	for (int i = 1; i <= n; i++) pos[i] = (i - 1) / len + 1;
	for (int i = 1; i <= m; i++) read(q[i].l, q[i].r), q[i].id = i;
	sort(a + 1, a + m + 1);
	for (int i = 1; i <= m; i++) {
		while (r < q[i].r) add(c[++r]);
		while (r > q[i].r) del(c[r--]);
		while (l < q[i].l) del(c[l++]);
		while (l > q[i].l) add(c[--l]);
		if (q[i].l == q[i].r) { b[q[i].id] = 1; continue; }
		a[q[i].id] = ans - (q[i].r - q[i].l + 1);
		b[q[i].id] = (q[i].r - q[i].l + 1) * (q[i].r - q[i].l);
		ll g = __gcd(a[q[i].id], b[q[i].id]);
		a[q[i].id] /= g, b[q[i].id] /= g;
	}
	for (int i = 1; i <= m; i++) write(a[i]), putchar('/'), write(b[i]), putchar('\n');
	flush();
}
2022/10/13 22:53
加载中...