莫队算法+离散化

1.map会TLE,必须离散化做

2.long long会WA,__int64定义 %I64d输出输出能AC

3.注意输入的序列会爆int

#include<cstdio>
#include<cstring>
#include<cmath>
#include<queue>
#include<map>
#include<algorithm>
using namespace std; const int maxn = + ;
int n, m;
__int64 tmp[maxn], a[maxn], lsh[maxn];
int cnt;
int pos[maxn];
__int64 c[maxn];
__int64 ans[maxn], Ans;
int L, R;
struct X
{
int l, r, id;
}s[maxn]; bool cmp(const X&a, const X&b)
{
if (pos[a.l] == pos[b.l]) return a.r < b.r;
return a.l < b.l;
} int f(__int64 x)
{
int l = , r = cnt;
while (l <= r)
{
int mid = (l + r) / ;
if (lsh[mid] < x) l = mid + ;
else if (lsh[mid]>x) r = mid - ;
else return mid;
}
} void LSH()
{
sort(tmp + , tmp + + n);
cnt = , lsh[++cnt] = tmp[];
for (int i = ; i <= n; i++)
{
if (tmp[i] == tmp[i - ]) continue;
lsh[++cnt] = tmp[i];
}
for (int i = ; i <= n; i++) a[i] = (__int64)f(a[i]);
} int main()
{
while (~scanf("%d", &n))
{
memset(c, , sizeof c);
int sz = sqrt(1.0*n);
for (int i = ; i <= n; i++)
{
pos[i] = i / sz;
scanf("%I64d", &tmp[i]);
a[i] = tmp[i];
}
LSH(); scanf("%d", &m);
for (int i = ; i <= m; i++) {
scanf("%d%d", &s[i].l, &s[i].r);
s[i].id = i;
}
sort(s + , s + + m, cmp);
Ans = ;
for (int i = s[].l; i <= s[].r; i++)
{
Ans = Ans - c[a[i]] * c[a[i]] * c[a[i]];
c[a[i]]++;
Ans = Ans + c[a[i]] * c[a[i]] * c[a[i]];
}
ans[s[].id] = Ans; L = s[].l; R = s[].r; for (int i = ; i <= m; i++)
{
while (L < s[i].l)
{
Ans = Ans - c[a[L]] * c[a[L]] * c[a[L]];
c[a[L]]--;
Ans = Ans + c[a[L]] * c[a[L]] * c[a[L]];
L++;
}
while (L > s[i].l)
{
L--;
Ans = Ans - c[a[L]] * c[a[L]] * c[a[L]];
c[a[L]]++;
Ans = Ans + c[a[L]] * c[a[L]] * c[a[L]];
}
while (R < s[i].r)
{
R++;
Ans = Ans - c[a[R]] * c[a[R]] * c[a[R]];
c[a[R]]++;
Ans = Ans + c[a[R]] * c[a[R]] * c[a[R]];
}
while (R > s[i].r)
{
Ans = Ans - c[a[R]] * c[a[R]] * c[a[R]];
c[a[R]]--;
Ans = Ans + c[a[R]] * c[a[R]] * c[a[R]];
R--;
}
ans[s[i].id] = Ans;
} for (int i = ; i <= m; i++) printf("%I64d\n", ans[i]);
}
return ;
}

NBUT 1457 Sona的更多相关文章

  1. NBUT 1457 Sona(莫队算法+离散化)

    [1457] Sona 时间限制: 5000 ms 内存限制: 65535 K 问题描述 Sona, Maven of the Strings. Of cause, she can play the ...

  2. NBUT 1457 Sona (莫队算法)

    题目大意: 求一段区间内 出现的数字的次数的三次方的和 思路分析: 这要水过去的题目真是难,各种优化. 不能用map , 要离散化之后 先处理lowerbound. 优化输入. . . 时间卡的非常紧 ...

  3. NBUT 1457 莫队算法 离散化

    Sona Time Limit:5000MS     Memory Limit:65535KB     64bit IO Format: Submit Status Practice NBUT 145 ...

  4. bzoj 2038 小z的袜子 莫队例题

    莫队,利用可以快速地通过一个问题的答案得到另一问题的答案这一特性,合理地组织问题的求解顺序,将已解决的问题帮助解决当前问题,来优化时间复杂度. 典型用法:处理静态(无修改)离线区间查询问题. 线段树也 ...

  5. ACM: NBUT 1107 盒子游戏 - 简单博弈

     NBUT 1107  盒子游戏 Time Limit:1000MS     Memory Limit:65535KB     64bit IO Format:  Practice  Appoint ...

  6. ACM: NBUT 1105 多连块拼图 - 水题 - 模拟

    NBUT 1105  多连块拼图 Time Limit:1000MS     Memory Limit:65535KB     64bit IO Format:  Practice  Appoint ...

  7. ACM: NBUT 1646 Internet of Lights and Switches - 二进制+map+vector

    NBUT 1646 Internet of Lights and Switches Time Limit:5000MS     Memory Limit:65535KB     64bit IO Fo ...

  8. NBUT 1525 Cow Xor(01字典树+前缀思想)

    [1525] Cow Xor 时间限制: 2000 ms 内存限制: 65535 K 问题描述 农民约翰在喂奶牛的时候被另一个问题卡住了.他的所有N(1 <= N <= 100,000)个 ...

  9. NBUT 1186 Get the Width(DFS求树的宽度,水题)

    [1186] Get the Width 时间限制: 1000 ms 内存限制: 65535 K 问题描述 It's an easy problem. I will give you a binary ...

随机推荐

  1. 编译在android 平台上跑的C应用程序

    Android 用的是 Bionic C, 而不是通常的glibc,因此简单使用交叉工具链并不能够编译出适合运行在android 设备上的 C/C++ 程序. 交叉工具链可以很轻松在 Android ...

  2. SQL语句创建access表

    CREATE TABLE Persons(ID AutoIncrement primary key,Id_P int NOT NULL,LastName varchar(255) NOT NULL,s ...

  3. navicat连接oracle时发现 ORA-12737 set CHS16GBK

    oracle安装目录下找到目录:\product\11.2.0\dbhome_1\BIN, 将箭头标注的三个文件(截图中为navicat中的目录,已经测试成功,亲们可以参考)从目录中拷贝纸navica ...

  4. iOS开发工具——统计Crash的工具Crashlytics

    简介 Crashlytic 成立于2011年,是专门为移动应用开者发提供的保存和分析应用崩溃信息的工具.Crashlytics的使用者包括:支付工具Paypal, 点评应用Yelp, 照片分享应用Pa ...

  5. Swift --> Map & FlatMap

    转载自:https://segmentfault.com/a/1190000004050907 Map map函数能够被数组调用,它接受一个闭包作为参数,作用于数组中的每个元素.闭包返回一个变换后的元 ...

  6. struts2中的<s:select>默认选项

    //... public class SelectAction extends ActionSupport{ private List<String> searchEngine; priv ...

  7. [转]ubuntu的which、whereis、locate和find命令

     which 只能寻找执行文件 ,并在PATH变量里面寻找. whereis 从linux文件数据库(/var/lib/slocate/slocate.db)寻找,所以有可能找到刚刚删除,或者没有发现 ...

  8. 【转载】关于SetWindowOrgEx、SetViewportOrgEx、SetViewportExtEx 和SetWindowExtEx 详解

    关于SetWindowOrgEx.SetViewportOrgEx.SetViewportExtEx 和SetWindowExtEx 详解 1.  SetWindowOrgEx是设置窗口的原点坐标. ...

  9. HDU 1532 Drainage Ditches (最大网络流)

    Drainage Ditches Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other) To ...

  10. php csv操作

    csv的写入数据: $data = array( array('qq号','登录时间','名称'), array('123456','2012-08-21 15:21:10'.chr(1),'我是来测 ...