Turing Tree HDU - 3333 (树状数组,离线求区间元素种类数)
After inventing Turing Tree, 3xian always felt boring when solving problems about intervals, because Turing Tree could easily have the solution. As well, wily 3xian made lots of new problems about intervals. So, today, this sick thing happens again...
Now given a sequence of N numbers A1, A2, ..., AN and a number of Queries(i, j) (1≤i≤j≤N). For each Query(i, j), you are to caculate the sum of distinct values in the subsequence Ai, Ai+1, ..., Aj.
给定一个长度为n的序列,给定m个查询,每次查询区间[L,R]范围内不同元素的和。
Input
The first line is an integer T (1 ≤ T ≤ 10), indecating the number of testcases below.
For each case, the input format will be like this:
- Line 1: N (1 ≤ N ≤ 30,000).
- Line 2: N integers A1, A2, ..., AN (0 ≤ Ai ≤ 1,000,000,000).
- Line 3: Q (1 ≤ Q ≤ 100,000), the number of Queries.
- Next Q lines: each line contains 2 integers i, j representing a Query (1 ≤ i ≤ j ≤ N). 第一个整数T,表示数据组数。
对于每组数据,第一行一个整数n,表示序列中元素个数。
第二行n个元素。
第三行一个整数q,表示询问的组数。
接下来q行,每行两个整数L和R
Output
For each Query, print the sum of distinct values of the specified subsequence in one line.
对于每个询问,输出结果。
Sample Input
2
3
1 1 4
2
1 2
2 3
5
1 1 2 1 3
3
1 5
2 4
3 5
Sample Output
1
5
6
3
6
题意:
英文下面有中文翻译
思路:
先把区间信息读进来,然后以右端点排序,然后离线处理每一个区间信息,同时用一个map来维护一个数值x最后一次出现的位置。
细节见代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <bits/stdc++.h>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <vector>
#include <iomanip>
#define ALL(x) (x).begin(), (x).end()
#define sz(a) int(a.size())
#define all(a) a.begin(), a.end()
#define rep(i,x,n) for(int i=x;i<n;i++)
#define repd(i,x,n) for(int i=x;i<=n;i++)
#define pii pair<int,int>
#define pll pair<long long ,long long>
#define gbtb ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define MS0(X) memset((X), 0, sizeof((X)))
#define MSC0(X) memset((X), '\0', sizeof((X)))
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define eps 1e-6
#define gg(x) getInt(&x)
#define chu(x) cout<<"["<<#x<<" "<<(x)<<"]"<<endl
using namespace std;
typedef long long ll;
ll gcd(ll a, ll b) {return b ? gcd(b, a % b) : a;}
ll lcm(ll a, ll b) {return a / gcd(a, b) * b;}
ll powmod(ll a, ll b, ll MOD) {ll ans = 1; while (b) {if (b % 2)ans = ans * a % MOD; a = a * a % MOD; b /= 2;} return ans;}
inline void getInt(int* p);
const int maxn = 30010;
const int inf = 0x3f3f3f3f;
/*** TEMPLATE CODE * * STARTS HERE ***/
int q;
int n;
ll tree[maxn];
int lowbit(int x)
{
return -x&x;
}
void add(int x,ll val)
{
while(x<maxn)
{
tree[x]+=val;
x+=lowbit(x);
}
}
ll ask(int x)
{
ll res=0;
while(x)
{
res+=tree[x];
x-=lowbit(x);
}
return res;
}
ll a[maxn];
map<ll,int> vis;
struct node
{
int l,r;
int id;
}b[100010];
bool cmp(node aa,node bb)
{
return aa.r<bb.r;
}
bool cmp2(node aa,node bb)
{
return aa.id<bb.id;
}
ll ans[100010];
int main()
{
//freopen("D:\\code\\text\\input.txt","r",stdin);
//freopen("D:\\code\\text\\output.txt","w",stdout);
gbtb;
int t;
cin>>t;
while(t--)
{
MS0(tree);
vis.clear();
cin>>n;
repd(i,1,n)
{
cin>>a[i];
}
int q;
cin>>q;
repd(i,1,q)
{
cin>>b[i].l;
cin>>b[i].r;
b[i].id=i;
}
sort(b+1,b+1+q,cmp);
int p=1;
repd(i,1,q)
{
while(p<=b[i].r)
{
if(vis[a[p]])
{
add(vis[a[p]],-a[p]);
add(p,a[p]);
vis[a[p]]=p;
}else
{
vis[a[p]]=p;
add(p,a[p]);
}
p++;
}
ans[b[i].id]=ask(b[i].r)-ask(b[i].l-1);
}
sort(b+1,b+1+q,cmp2);
repd(i,1,q)
{
cout<<ans[b[i].id]<<endl;
}
}
return 0;
}
inline void getInt(int* p) {
char ch;
do {
ch = getchar();
} while (ch == ' ' || ch == '\n');
if (ch == '-') {
*p = -(getchar() - '0');
while ((ch = getchar()) >= '0' && ch <= '9') {
*p = *p * 10 - ch + '0';
}
}
else {
*p = ch - '0';
while ((ch = getchar()) >= '0' && ch <= '9') {
*p = *p * 10 + ch - '0';
}
}
}
Turing Tree HDU - 3333 (树状数组,离线求区间元素种类数)的更多相关文章
- hdu 3333 树状数组+离线处理
http://acm.hdu.edu.cn/showproblem.php?pid=3333 不错的题,想了非常久不知道怎么处理,并且答案没看懂,然后找个样例模拟下别人的代码立即懂了---以后看不懂的 ...
- HDU 3333 树状数组离线查询
题目大意: 询问区间内不同种类的数的数值之和 这里逐个添加最后在线查询,会因为相同的数在区间内导致冲突 我们总是希望之后添加的数不会影响前面,那么我们就在添加到第i个数的时候,把所有在1~i 的区间的 ...
- HDU 1394 树状数组+离散化求逆序数
对于求逆序数问题,学会去利用树状数组进行转换求解方式,是很必要的. 一般来说我们求解逆序数,是在给定一串序列里,用循环的方式找到每一个数之前有多少个比它大的数,算法的时间复杂度为o(n2). 那么我们 ...
- hdu 3333(树状数组 + 离线操作)
Turing Tree Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...
- hdu 3333 树状数组
思路:定义一个map容器用来记录数ai上次出现的位置.将查询区间按右边界升序进行排序,当插入第i个数ai时,pre[ai]+1---->i的区间就会多一个不同的数,其值就是ai,那么可以用upd ...
- SPOJ DQUERY树状数组离线or主席树
D-query Time Limit: 227MS Memory Limit: 1572864KB 64bit IO Format: %lld & %llu Submit Status ...
- HDU - 1166 树状数组模板(线段树也写了一遍)
题意: 汉语题就不说题意了,用到单点修改和区间查询(树状数组和线段树都可以) 思路: 树状数组的单点查询,单点修改和区间查询. 树状数组是巧妙运用二进制的规律建树,建树就相当于单点修改.这里面用到一个 ...
- Necklace HDU - 3874 (线段树/树状数组 + 离线处理)
Necklace HDU - 3874 Mery has a beautiful necklace. The necklace is made up of N magic balls. Each b ...
- 2016 Multi-University Training Contest 5 1012 World is Exploding 树状数组+离线化
http://acm.hdu.edu.cn/showproblem.php?pid=5792 1012 World is Exploding 题意:选四个数,满足a<b and A[a]< ...
随机推荐
- Java 程序员必备的 Intellij IDEA 插件
以下是我用过不错的Intellij插件,分享给大家希望能帮到大家. 1. ignore 地址: https://plugins.jetbrains.com/plugin/7495--ignore 生成 ...
- 一个好的关于计算文件大小,及使其便于阅读的方法(php)
public function getReadableFileSize($retstring=null){ $sizes = array('byte','kB','MB','GB','TB','PB' ...
- C# 反转单向链表
static void Main(string[] args) { Mynode mynode0 = new Mynode(); Mynode mynode1 = new Mynode(); Myno ...
- iOS开发之点击tabBarItem进行Push一个页面
使用场景: 实现底部Tabbar两个TabBarItem,点击第二个跳转push到个人信息页面: 实现步骤: 首先设置UITabBarController的代理为appdelegate如下:myTab ...
- Appium-实战之启动App 获取信息说明
如下为启动 格来云游戏APP启动信息 代码如下: from appium import webdriver caps = {} caps["platformName"] = &qu ...
- UOJ#495晒被子
#include<iostream> #include<cstdio> #include<cstring> #include<cstdlib> #inc ...
- vue 导出JSON数据为Excel
1. 安装三个依赖 npm install file-saver --save npm install xlsx --save npm install script-loader --save-dev ...
- 【Python开发】urllib2异常处理
一.urllib2模块回顾 urllib2模块中最重要的函数是urlopen()函数,用于获取URLs资源(Uniform Resorce Locators).urlopen函数不仅可以用于简单的情况 ...
- c++ string详解 assign
assign方法可以理解为先将原字符串清空,然后赋予新的值作替换. 返回类型为 string类型的引用.其常用的重载也有下列几种: a. string& assign ( const stri ...
- [ZJOI2007]捉迷藏(动态点分治/(括号序列)(线段树))
题目描述 Jiajia和Wind是一对恩爱的夫妻,并且他们有很多孩子.某天,Jiajia.Wind和孩子们决定在家里玩捉迷藏游戏.他们的家很大且构造很奇特,由N个屋子和N-1条双向走廊组成,这N-1条 ...