Description

https://loj.ac/problem/6468


Solution

zjp的题,orzorz

先考虑l=1,r=n的部分分。
对于所有同色的水晶,其中两个相邻的为a,b,mid=(a+b)/2,那么如果a<=x<=mid,那么位置为x的法师会选择a,否则选择b。
设位置为p的点,左边选择的水晶有k1,位置和为sum1,右边分别为k2、sum2,那么答案为sum2 - k2 * p + k1 * p - sum1。
我们将同色的水晶一起计算,用一棵树状数组维护sum,一棵树状数组维护k即可。

至于正解,从小到大依次插入。对于询问l, r拆开,用r的答案减去l-1的答案即可。


Code

/************************************************
 * Au: Hany01
 * Prob: magic

************************************************/

#include<bits/stdc++.h>

using namespace std;

typedef long long LL;
typedef pair<int, int> PII;
#define File(a) freopen(a".in", "r", stdin), freopen(a".out", "w", stdout)
#define rep(i, j) for (register int i = 0, i##_end_ = (j); i < i##_end_; ++ i)
#define For(i, j, k) for (register int i = (j), i##_end_ = (k); i <= i##_end_; ++ i)
#define Fordown(i, j, k) for (register int i = (j), i##_end_ = (k); i >= i##_end_; -- i)
#define Set(a, b) memset(a, b, sizeof(a))
#define Cpy(a, b) memcpy(a, b, sizeof(a))
#define x first
#define y second
#define pb(a) push_back(a)
#define mp(a, b) make_pair(a, b)
#define ALL(a) (a).begin(), (a).end()
#define SZ(a) ((int)(a).size())
#define INF (0x3f3f3f3f)
#define INF1 (2139062143)
#define debug(...) fprintf(stderr, __VA_ARGS__)
#define y1 wozenmezhemecaia

template <typename T> inline bool chkmax(T &a, T b) { return a < b ? a = b, 1 : 0; }
template <typename T> inline bool chkmin(T &a, T b) { return b < a ? a = b, 1 : 0; }

inline int read() {
    static int _, __; static char c_;
    for (_ = 0, __ = 1, c_ = getchar(); c_ < '0' || c_ > '9'; c_ = getchar()) if (c_ == '-') __ = -1;
    for ( ; c_ >= '0' && c_ <= '9'; c_ = getchar()) _ = (_ << 1) + (_ << 3) + (c_ ^ 48);
    return _ * __;
}

const int maxn = 2e5 + 5;

int n, q, sz;
LL ans[maxn];
vector<int> List[maxn];

struct Qry { int p, op, id; };
vector<Qry> Q[maxn];

struct FenwickTree {
    LL c[maxn];
#define lb(x) ((x) & -(x))
    inline LL query(int x) { LL ans; for (ans = 0; x; x -= lb(x)) ans += c[x]; return ans; }
    inline void update(int x, int dt) { for ( ; x <= n; x += lb(x)) c[x] += dt; }
    inline void update(int x, int y, int dt) { update(x, dt), update(y + 1, -dt); }
}FT1, FT2;

int main()
{
    File("magic");

    n = read(), q = read();
    For(i, 1, n) List[read()].pb(i);

    For(i, 1, q) {
        register int p = read(), l = read(), r = read();
        Q[l - 1].push_back(Qry{p, -1, i}), Q[r].push_back(Qry{p, 1, i});
    }

    For(i, 1, n) {
        if (sz = SZ(List[i])) {
            FT1.update(1, List[i][0], -1), FT2.update(1, List[i][0], -List[i][0]);
            rep(j, sz - 1) {
                register int l = List[i][j], r = List[i][j + 1], mid = (l + r) >> 1;
                FT1.update(l, mid, 1), FT2.update(l, mid, l), FT1.update(mid + 1, r, -1), FT2.update(mid + 1, r, -r);
            }
            FT1.update(List[i][sz - 1], n, 1), FT2.update(List[i][sz - 1], n, List[i][sz - 1]);
        }
        rep(j, SZ(Q[i]))
            ans[Q[i][j].id] += Q[i][j].op * (Q[i][j].p * FT1.query(Q[i][j].p) - FT2.query(Q[i][j].p));
    }
    For(i, 1, q) printf("%lld\n", ans[i]);

    return 0;
}