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 ...
随机推荐
- 12.翻译系列:EF 6 中配置一对多的关系【EF 6 Code-First系列】
原文链接:https://www.entityframeworktutorial.net/code-first/configure-one-to-many-relationship-in-code-f ...
- connect by prior start with 语句实现树递归查询[百度经验]
TART WITH CONNECT BY PRIOR子句实现递归查询 TART WITH CONNECT BY PRIOR这个语法主要用于查询数据包中的树型结构关系.先看下原始数据时怎么样的吧! 表中 ...
- 30. Substring with Concatenation of All Words
题目: You are given a string, s, and a list of words, words, that are all of the same length. Find all ...
- JavaScript高级用法二之内置对象
综述 本篇的主要内容来自慕课网,内置对象,主要内容如下 1 什么是对象 2 Date 日期对象 3 返回/设置年份方法 4 返回星期方法 5 返回/设置时间方法 6 String 字符串对象 7 返回 ...
- Nginx 实现负载均衡
.安装nginx和tomcat 我这里是使用docker安装的.安装流程可参照 dockerfile 这里安装了两个tomcat,端口分别是42000和42001.第二个tomcat的首页随便加了些代 ...
- Ant与Proguard集中
示例: <taskdef resource="proguard/ant/task.properties" classpath="/usr/local ...
- 阿里云中ssl配置(nginx安装https服务)
1.配置 a.阿里云服务器 b.安装了nginx,php等 2.申请免费ssl证数 a. b. c.产看ssl证数 d.下载证数 e,这里我下载的是nginx(crt与key文件) f.服务器上配置 ...
- DedeCMS织梦文章页图片地址为绝对路径实现方法
{dede:field.body function='replaceurl(@me)'/} 余斗博客改版后增加了一个m站点即手机站点,用二级域名实现,在做手机站的过程中发现一个问题,手机站和pc站都是 ...
- [Bayes] Multinomials and Dirichlet distribution
From: https://www.cs.cmu.edu/~scohen/psnlp-lecture6.pdf 不错的PPT,图示很好. 伯努利分布 和 多项式分布 Binomial Distribu ...
- [AWS] Deploy react project on EC2
如何在aws部署项目 申请到亚马逊AWS免费账户后,我们可以拥有很多的免费云服务产品项目,其中包括: EC2云服务器. Amazon S3存储. Amazon RDS数据库. Amazon Cloud ...