mo +离散化 HDU3333(听说还有离线线段树的做法 )
http://acm.hdu.edu.cn/showproblem.php?pid=3333
mo套map会T,卡了一个logN,所以要先离散化处理
#define _CRT_SECURE_NO_WARNINGS
#include<cmath>
#include<iostream>
#include<stdio.h>
#include<algorithm>
#include<map>
using namespace std;
#define rep(i,t,n) for(int i =(t);i<=(n);++i)
#define per(i,n,t) for(int i =(n);i>=(t);--i)
#define mmm(a,b) memset(a,b,sizeof(a))
const int maxn = 1e5+ ;
map<int, int> mmp; struct node {
int l, r, id; }Q[maxn];
long long ans[maxn]; int a[maxn], b[maxn],cnt[maxn],pos[maxn];
int n, m, k;
int L = , R = ;
long long Ans = ;
bool cmp(node a, node b) {
if (pos[a.l] == pos[b.l])
return a.r < b.r;
return pos[a.l] < pos[b.l]; }
void add(int x) { if (cnt[b[x]]>)cnt[b[x]]++;
else Ans += a[x],cnt[b[x]] = ; }
void del(int x) {
cnt[b[x]]--;
if (cnt[b[x]] == ) Ans -= a[x];
} int main() {
int t; scanf("%d", &t);
while (t--) {
Ans = ;
mmp.clear();
mmm(cnt, );
L = , R = ;
scanf("%d", &n);
int sz = sqrt(n);
for (int i = ; i <= n; i++) {
scanf("%d", &a[i]);
pos[i] = i / sz;
}
int id = ;
rep(i, , n) {
if (!mmp.count(a[i]))mmp[a[i]] = ++id;
b[i] = mmp[a[i]];
}
scanf("%d", &m);
for (int i = ; i <= m; i++) {
scanf("%d%d", &Q[i].l, &Q[i].r);
Q[i].id = i;
}
sort(Q + , Q + + m, cmp); for (int i = ; i <= m; i++) {
while (R < Q[i].r) {
R++;
add(R);
}
while (L > Q[i].l) {
L--;
add(L);
}
while (R > Q[i].r) {
del(R);
R--; }
while (L < Q[i].l) {
del(L);
L++;
}
ans[Q[i].id] = Ans;
//
}
for (int i = ; i <= m; i++)
//cout << ans[i] << endl;
printf("%lld\n", ans[i]);
//cin >> n;
}
cin >> t;
return ;
}
mo +离散化 HDU3333(听说还有离线线段树的做法 )的更多相关文章
- hdu 4288 离线线段树+间隔求和
Coder Time Limit: 20000/10000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Su ...
- bzoj2333 离线 + 线段树
https://www.lydsy.com/JudgeOnline/problem.php?id=2333 有N个节点,标号从1到N,这N个节点一开始相互不连通.第i个节点的初始权值为a[i],接下来 ...
- HDU 5700 区间交 离线线段树
区间交 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5700 Description 小A有一个含有n个非负整数的数列与m个区间.每个区间可以表示为 ...
- BZOJ 3626 [LNOI2014]LCA 树剖+(离线+线段树 // 在线+主席树)
BZOJ 4012 [HNOI2015]开店 的弱化版,离线了,而且没有边权(长度). 两种做法 1 树剖+离线+线段树 这道题求的是一个点zzz与[l,r][l,r][l,r]内所有点的lcalca ...
- HDU 4288 Coder ( 离散化 + 离线 + 线段树 )
这题跟ZOJ 3606的解题思路很相似. 题意:有3中操作:1.向集合中增加一个数x(1≤x≤1e9):2.从集合中删去一个数x(保证这个数存在):3.查询集合中所有位置满足i%5==3的数a[i]的 ...
- 求余区间的求和类问题 离线+线段树 HDU4228
题目大意:给一个数组a,他的顺序是严格的单调增,然后有如下三个操作 ①加入一个val到a数组里面去,加入的位置就是a[i-1]<val<a[i+1] ②删除一个a[i]=val的值 ③查询 ...
- HDU 6162 Ch’s gift (树剖 + 离线线段树)
Ch’s gift Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total S ...
- HDU3333 Turing Tree(线段树)
题目 Source http://acm.hdu.edu.cn/showproblem.php?pid=3333 Description After inventing Turing Tree, 3x ...
- HDU 4031 Attack(离线+线段树)(The 36th ACM/ICPC Asia Regional Chengdu Site —— Online Contest)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4031 Problem Description Today is the 10th Annual of ...
随机推荐
- StringUtils类中isEmpty与isBlank的区别
org.apache.commons.lang.StringUtils类提供了String的常用操作,最为常用的判空有如下两种isEmpty(String str)和isBlank(String st ...
- tensorflow 笔记8:RNN、Lstm源码,训练代码输入输出,维度分析
tensorflow 官网信息:https://www.tensorflow.org/api_docs/python/tf/contrib/rnn/BasicLSTMCell tensorflow 版 ...
- pip离线安装软件包
1. 首先一台主机上安装所有python包,然后运行如下命令下载依赖包: pip freeze > requirements pip download -r requirements 当然可以在 ...
- Java编程的逻辑 (81) - 并发同步协作工具
本系列文章经补充和完善,已修订整理成书<Java编程的逻辑>,由机械工业出版社华章分社出版,于2018年1月上市热销,读者好评如潮!各大网店和书店有售,欢迎购买,京东自营链接:http: ...
- 聊聊模板方法模式,装饰器模式以及AOP
在软件系统设计的时候,我们需要把一个大的系统按照业务功能进行拆分,做到高内聚.低耦合. 但是呢,拆分之后会产生一些通用性的东西,比如日志,安全,事务,性能统计等,这些非功能性需求,横跨多个模块.最lo ...
- Git 修正错误
大部分的人都会犯错.所以每VCS提供了一个功能,修正错误,直到特定的点. Git提供功能使用,我们可以撤销已作出的修改到本地资源库. 假设用户不小心做了一些更改,以他的本地的仓库,现在他要扔掉这些变化 ...
- LeetCode_832. Flipping an Image_Solution
原题链接 原题中文链接 一.题目描述 二.解题思路 题目所描述的意思是对每个数组先进行取反,并且对数组中的每个元素进行取反转换,所以一共要执行两个操作. 使用reverse函数解决水平翻转的操作: 由 ...
- SQL-----------------------之ON和WHERE的区别
SQL中ON和WHERE的区别 数据库在通过连接两张或多张表来返回记录时,都会生成一张中间的临时表,然后再将这张临时表返回给用户.在使用left jion时,on和where条件的区别如下:1. on ...
- Orchard CMS -Migration文件更新后数据库不更新的问题 new properties not updating after migrationData migration is not working?
Orchard CMS - new properties not updating after migrationData migration is not working? If your mapp ...
- 阿里云oss上传文件如何支持https?
let client = new OSS.Wrapper({ accessKeyId: res.data.accessKeyId, accessKeySecret: res.data.accessKe ...