zoukankan      html  css  js  c++  java
  • 【BZOJ】3781: 小B的询问(莫队算法)

    http://www.lydsy.com/JudgeOnline/problem.php?id=3781

    还能不能再裸点。。

    #include <cstdio>
    #include <cstring>
    #include <cmath>
    #include <string>
    #include <iostream>
    #include <algorithm>
    #include <queue>
    #include <set>
    #include <map>
    using namespace std;
    typedef long long ll;
    #define rep(i, n) for(int i=0; i<(n); ++i)
    #define for1(i,a,n) for(int i=(a);i<=(n);++i)
    #define for2(i,a,n) for(int i=(a);i<(n);++i)
    #define for3(i,a,n) for(int i=(a);i>=(n);--i)
    #define for4(i,a,n) for(int i=(a);i>(n);--i)
    #define CC(i,a) memset(i,a,sizeof(i))
    #define read(a) a=getint()
    #define print(a) printf("%d", a)
    #define dbg(x) cout << (#x) << " = " << (x) << endl
    #define error(x) (!(x)?puts("error"):0)
    #define rdm(x, i) for(int i=ihead[x]; i; i=e[i].next)
    inline const int getint() { int r=0, k=1; char c=getchar(); for(; c<'0'||c>'9'; c=getchar()) if(c=='-') k=-1; for(; c>='0'&&c<='9'; c=getchar()) r=r*10+c-'0'; return k*r; }
    
    const int N=50005;
    struct dat { int l, r, id; }q[N];
    int a[N], n, m, k, pos[N];
    ll sum, ans[N], s[N];
    
    inline bool cmp(const dat &a, const dat &b) { return pos[a.l]==pos[b.l]?a.r<b.r:a.l<b.l; }
    inline void fix(const int &x, const int &f) {
    	sum-=s[x]*s[x];
    	s[x]+=f;
    	sum+=s[x]*s[x];
    }
    void init() {
    	int sz=sqrt(0.5+n);
    	for1(i, 1, n) pos[i]=i/sz;
    	sort(q+1, q+1+m, cmp);
    }
    int main() {
    	read(n); read(m); read(k);
    	for1(i, 1, n) read(a[i]);
    	for1(i, 1, m) read(q[i].l), read(q[i].r), q[i].id=i;
    	init();
    	int l=1, r=0;
    	for1(i, 1, m) {
    		int xl=q[i].l, xr=q[i].r, id=q[i].id;
    		while(l<xl) fix(a[l++], -1);
    		while(l>xl) fix(a[--l], 1);
    		while(r<xr) fix(a[++r], 1);
    		while(r>xr) fix(a[r--], -1);
    		ans[id]=sum;
    	}
    	for1(i, 1, m) printf("%lld
    ", ans[i]);
    	return 0;
    }
    

      


    Description

    小B有一个序列,包含N个1~K之间的整数。他一共有M个询问,每个询问给定一个区间[L..R],求Sigma(c(i)^2)的值,其中i的值从1到K,其中c(i)表示数字i在[L..R]中的重复次数。小B请你帮助他回答询问。

    Input

    第一行,三个整数N、M、K。
    第二行,N个整数,表示小B的序列。
    接下来的M行,每行两个整数L、R。

    Output

    M行,每行一个整数,其中第i行的整数表示第i个询问的答案。
     
     

    Sample Input

    6 4 3
    1 3 2 1 1 3
    1 4
    2 6
    3 5
    5 6

    Sample Output

    6
    9
    5
    2

    HINT

    对于全部的数据,1<=N、M、K<=50000


    Source

  • 相关阅读:
    wordpress (net::ERR_TOO_MANY_REDIRECTS):重定向过多。
    Windows2003 IIS6完美实现WordPress伪静态的方法
    显示当前时间
    禁止搜索引擎收录的几种方法(全)
    MSClass 的使用(Class Of Marquee Scroll通用不间断滚动JS封装类)
    go to Top 返回顶部 例子
    电机位置和速度信号检测电路
    matlab polyfit 多项式拟合 (转自 百科)
    matlab contour
    DTC学习
  • 原文地址:https://www.cnblogs.com/iwtwiioi/p/4148157.html
Copyright © 2011-2022 走看看