[BZOJ3781]小B的询问
生活随笔
收集整理的這篇文章主要介紹了
[BZOJ3781]小B的询问
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
[BZOJ3781]小B的詢問
試題描述
小B有一個(gè)序列,包含N個(gè)1~K之間的整數(shù)。他一共有M個(gè)詢問,每個(gè)詢問給定一個(gè)區(qū)間[L..R],求Sigma(c(i)^2)的值,其中i的值從1到K,其中c(i)表示數(shù)字i在[L..R]中的重復(fù)次數(shù)。小B請(qǐng)你幫助他回答詢問。輸入
第一行,三個(gè)整數(shù)N、M、K。 第二行,N個(gè)整數(shù),表示小B的序列。 接下來的M行,每行兩個(gè)整數(shù)L、R。輸出
M行,每行一個(gè)整數(shù),其中第i行的整數(shù)表示第i個(gè)詢問的答案。輸入示例
6 4 3 1 3 2 1 1 3 1 4 2 6 3 5 5 6輸出示例
6 9 5 2數(shù)據(jù)規(guī)模及約定
對(duì)于全部的數(shù)據(jù),1<=N、M、K<=50000
題解
莫隊(duì)裸題。。。
就我的莫隊(duì)跑得慢(大概是因?yàn)橛昧?vector?)。。。
#include <iostream> #include <cstdio> #include <cstdlib> #include <cstring> #include <cctype> #include <algorithm> #include <vector> #include <cmath> using namespace std;int read() {int x = 0, f = 1; char c = getchar();while(!isdigit(c)){ if(c == '-') f = -1; c = getchar(); }while(isdigit(c)){ x = x * 10 + c - '0'; c = getchar(); }return x * f; }#define maxn 50010 #define maxbl 233 #define LL long longint A[maxn]; struct Que {int l, r;Que() {}Que(int _, int __): l(_), r(__) {} } qs[maxn];bool cmp(int a, int b) { return qs[a].r < qs[b].r; } vector <int> qid[maxbl]; int tot[maxn]; LL Ans[maxn];LL sqr(int x) { return (LL)x * x; }int main() {int n = read(), q = read(), K = read();for(int i = 1; i <= n; i++) A[i] = read();int m = sqrt(n + .5), cntb = 0;for(int i = 1; i <= q; i++) {int l = read(), r = read();qs[i] = Que(l, r);qid[(l-1)/m+1].push_back(i);cntb = max(cntb, (l - 1) / m + 1);}for(int i = 1; i <= cntb; i++) {sort(qid[i].begin(), qid[i].end(), cmp);memset(tot, 0, sizeof(tot));int l = 1, r = 0; LL ans = 0;for(vector <int> :: iterator j = qid[i].begin(); j != qid[i].end(); j++) {while(r < qs[*j].r) tot[A[++r]]++, ans = ans - sqr(tot[A[r]] - 1) + sqr(tot[A[r]]);while(l < qs[*j].l) tot[A[l]]--, ans = ans - sqr(tot[A[l]] + 1) + sqr(tot[A[l]]), l++;while(l > qs[*j].l) tot[A[--l]]++, ans = ans - sqr(tot[A[l]] - 1) + sqr(tot[A[l]]);Ans[*j] = ans;}}for(int i = 1; i <= q; i++) printf("%lld\n", Ans[i]);return 0; }?
轉(zhuǎn)載于:https://www.cnblogs.com/xiao-ju-ruo-xjr/p/6747021.html
總結(jié)
以上是生活随笔為你收集整理的[BZOJ3781]小B的询问的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 再谈节奏与动力---平淡与枯燥的力量
- 下一篇: 两种计算器小程序对比