洛谷模板题
#include<bits/stdc++.h>
#include<unordered_map>
#include<math.h>
#include<forward_list>
#include<unordered_set>
using namespace std;
#pragma warning(disable:4996)
const double pi = acos(-1);
#define pdd pair<double,double>
#define re register
#define ll long long
#define endl '\n'
#define pii pair<ll,ll>
#define IOS std::ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define fi first
#define se second
#define ull unsigned ll
#define eb emplace_back
#define pb push_back
#define power qpow
#define rad read
#define mp make_pair
#define ls (o<<1)
#define rs (o<<1|1)
#define mid (l+r>>1)
#define fpn(x) fp(x),putchar('\n')
ll mod = 1e9 + 7;
inline ll lowbit(ll x) { return x & (-x); }
inline ll gcd(ll a, ll b) {
return b ? gcd(b, a % b) : a;
}
inline ll exgcd(ll a, ll b, ll& x, ll& y) {
//ax+by=1; 返回值为a,b最大公约数
if (!b) {
x = 1, y = 0;
return a;
}
ll g = exgcd(b, a % b, y, x);
y -= (a / b) * x;
return g;
}
inline ll ksc(ll a, ll b, ll mod) {
ll ans = 0;
while (b) {
if (b & 1) ans = (ans + a) % mod;
a = (a + a) % mod;
b >>= 1;
}
return ans;
}
ll qpow(ll a, ll b, ll modd = mod) {
ll ans = 1;
while (b) {
if (b & 1)
ans = ksc(ans, a, modd);
a = ksc(a, a, modd);
b >>= 1;
}
return ans;
}
inline ll read() {
ll s = 0; bool w = true;
char c = getchar();
for (; c < '0' || c>'9'; c = getchar())if (c == '-')w = false;
for (; c >= '0' && c <= '9'; c = getchar())s = (s << 1) + (s << 3) + (c ^ 48);
return w ? s : -s;
}
inline void fp(ll n) {
if (n < 0) putchar('-'), n = -n;
if (n > 9) fp(n / 10);
putchar(n % 10 + 48);
}
const int N = 1e5 + 10;
struct node {
int l, r, id, pos;
//l,r,左右边界,id为询问编号,pos为分块所在位置
bool operator<(const node& x) {
if (pos == x.pos)return r < x.r;
return pos < x.pos;
}
}a[N];
int b[N];
ll cnt[N], mm[N];
void solve() {
ll n = read(), m = read(), k = read();
ll size = sqrt(n);
for (int i = 0; i < n; ++i)b[i] = read();
for (int i = 0; i < m; ++i) {
a[i].l = read()-1;
a[i].r = read()-1;
a[i].id = i;
a[i].pos = (a[i].l - 1) / size + 1;
}
sort(a, a + m);
ll l = 0, r = -1,sum=0;
//双指针移动
for (int i = 0; i < m; ++i) {
while (l > a[i].l)--l, cnt[b[l]]++, sum += 2 * cnt[b[l]] - 1;
while (r < a[i].r)++r, cnt[b[r]]++, sum += 2 * cnt[b[r]] - 1;
while (l < a[i].l)cnt[b[l]]--, sum -= 2 * cnt[b[l]] + 1, ++l;
while (r > a[i].r)cnt[b[r]]--, sum -= 2 * cnt[b[r]] + 1, --r;
mm[a[i].id] = sum;
}
for (int i = 0; i < m; ++i)fpn(mm[i]);
}
signed main()
{
ll T = 1;
//T = read();
while (T--) {
solve();
}
return 0;
}
/**
*
* ┏┓ WQJ ┏┓+ +
* ┏┛┻━━━┛┻┓ +
* ┃ ┃
* ┃ ━ ┃ ++ + + +
* ████━████+
* ◥██◤ ◥██◤ +
* ┃ ┻ ┃
* ┃ ┃ + +
* ┗━┓ ┏━┛
* ┃ ┃ + + + +Code is far away from
* ┃ ┃ + bug with the animal protecting
* ┃ ┗━━━┓ 神兽保佑,代码无bug
* ┃ ┣┓>>>
* ┃ - ┏┛
* ┗┓┓┏━┳┓┏┛ + + + +
* ┃┫┫ ┃┫┫
* ┗┻┛ ┗┻┛+ + + +
*/
Atcoder293_补题
#include<bits/stdc++.h>
#include<unordered_map>
#include<math.h>
#include<forward_list>
#include<unordered_set>
using namespace std;
#pragma warning(disable:4996)
const double pi = acos(-1);
#define pdd pair<double,double>
#define re register
#define ll long long
#define endl '\n'
#define pii pair<ll,ll>
#define IOS std::ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define fi first
#define se second
#define ull unsigned ll
#define eb emplace_back
#define pb push_back
#define power qpow
#define rad read
#define mp make_pair
#define ls (o<<1)
#define rs (o<<1|1)
#define mid (l+r>>1)
#define fpn(x) fp(x),putchar('\n')
ll mod = 1e9 + 7;
inline ll lowbit(ll x) { return x & (-x); }
inline ll gcd(ll a, ll b) {
return b ? gcd(b, a % b) : a;
}
inline ll exgcd(ll a, ll b, ll& x, ll& y) {
//ax+by=1; 返回值为a,b最大公约数
if (!b) {
x = 1, y = 0;
return a;
}
ll g = exgcd(b, a % b, y, x);
y -= (a / b) * x;
return g;
}
inline ll ksc(ll a, ll b, ll mod) {
ll ans = 0;
while (b) {
if (b & 1) ans = (ans + a) % mod;
a = (a + a) % mod;
b >>= 1;
}
return ans;
}
ll qpow(ll a, ll b, ll modd = mod) {
ll ans = 1;
while (b) {
if (b & 1)
ans = ksc(ans, a, modd);
a = ksc(a, a, modd);
b >>= 1;
}
return ans;
}
inline ll read() {
ll s = 0; bool w = true;
char c = getchar();
for (; c < '0' || c>'9'; c = getchar())if (c == '-')w = false;
for (; c >= '0' && c <= '9'; c = getchar())s = (s << 1) + (s << 3) + (c ^ 48);
return w ? s : -s;
}
inline void fp(ll n) {
if (n < 0) putchar('-'), n = -n;
if (n > 9) fp(n / 10);
putchar(n % 10 + 48);
}
const int N = 2e5 + 10;
struct node {
int l, r, id, pos;
//l,r,左右边界,id为询问编号,pos为分块所在位置
bool operator<(const node& x) {
if (pos == x.pos)
if (pos & 1)return r < x.r;
else return r > x.r;
return pos < x.pos;
}
}a[N];
ll q, b[N], n;
ll ans[N], sum = 0, cnt[N];
void add(int x) {
x = b[x];
sum -= cnt[x] * (cnt[x] - 1) * (cnt[x] - 2) / 6;
++cnt[x];
sum += cnt[x] * (cnt[x] - 1) * (cnt[x] - 2) / 6;
}
void del(int x) {
x = b[x];
//cout << cnt[x] << endl;
sum -= cnt[x] * (cnt[x] - 1) * (cnt[x] - 2) / 6;
--cnt[x];
sum += cnt[x] * (cnt[x] - 1) * (cnt[x] - 2) / 6;
}
void solve() {
n = read();
q = read();
ll sn = ceil(1. * n / sqrt(q));
for (int i = 1; i <= n; ++i) {
b[i] = read();
}
for (int i = 1; i <= q; ++i) {
a[i].l = read(), a[i].r = read();
a[i].id = i;
//关键点
a[i].pos = (a[i].l - 1) / sn + 1;
}
sort(a + 1, a + 1 + q);
ll l = 1, r = 0;
for (int i = 1; i <= q; ++i) {
while (l < a[i].l)del(l++);
while (l > a[i].l)add(--l);
while (r < a[i].r)add(++r);
while (r > a[i].r)del(r--);
ans[a[i].id] = sum;
}
for (int i = 1; i <= q; ++i) {
fpn(ans[i]);
}
}
signed main()
{
ll T = 1;
//T = read();
while (T--) {
solve();
}
return 0;
}
/**
*
* ┏┓ WQJ ┏┓+ +
* ┏┛┻━━━┛┻┓ +
* ┃ ┃
* ┃ ━ ┃ ++ + + +
* ████━████+
* ◥██◤ ◥██◤ +
* ┃ ┻ ┃
* ┃ ┃ + +
* ┗━┓ ┏━┛
* ┃ ┃ + + + +Code is far away from
* ┃ ┃ + bug with the animal protecting
* ┃ ┗━━━┓ 神兽保佑,代码无bug
* ┃ ┣┓>>>
* ┃ - ┏┛
* ┗┓┓┏━┳┓┏┛ + + + +
* ┃┫┫ ┃┫┫
* ┗┻┛ ┗┻┛+ + + +
*/