HDU - 3874 Necklace (线段树 + 离线处理)

欢迎参加——每周六晚的BestCoder(有米!)

Necklace

Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 4003    Accepted Submission(s): 1330

Problem Description

Mery has a beautiful necklace. The necklace is made up of N magic balls. Each ball has a beautiful value. The balls with the same beautiful value look the same, so if two or more balls have the same beautiful value, we just count it once. We define the beautiful
value of some interval [x,y] as F(x,y). F(x,y) is calculated as the sum of the beautiful value from the xth ball to the yth ball and the same value is ONLY COUNTED ONCE. For example, if the necklace is 1 1 1 2 3 1, we have F(1,3)=1, F(2,4)=3, F(2,6)=6.

Now Mery thinks the necklace is too long. She plans to take some continuous part of the necklace to build a new one. She wants to know each of the beautiful value of M continuous parts of the necklace. She will give you M intervals [L,R] (1<=L<=R<=N) and you
must tell her F(L,R) of them.

Input

The first line is T(T<=10), representing the number of test cases.

For each case, the first line is a number N,1 <=N <=50000, indicating the number of the magic balls. The second line contains N non-negative integer numbers not greater 1000000, representing the beautiful value of the N balls. The third line has a number
M, 1 <=M <=200000, meaning the nunber of the queries. Each of the next M lines contains L and R, the query.

Output

For each query, output a line contains an integer number, representing the result of the query.

Sample Input

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

Sample Output

3
7
14
1
3
6

对于这道题,大家可以看我博客里http://blog.csdn.net/qq_18661257/article/details/47419441提供的离线处理教程后,基本能够理解离线处理的机制了,然后我们就要对照理解题目,不能出现重复的数字,所以可以离线最右边的值,将前面的值一一删除就可以得到正确答案,当然,大家还需注意的是数值取值范围为long long ,我就是在这个上面看了接近一个小时,query()函数的返回值应该也是long long ,坑爹的地方就是这里了,其他的,大家看代码基本能够秒懂的





#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
using namespace std;
typedef long long LL;
#define lson rt << 1, l, mid
#define rson rt << 1|1, mid + 1, r
#define root 1, 1, N
const int MAXN = 5e4 + 5;
const int MAXM = 2e5 + 5;
const int MAXS = 1e6 + 5;
int N, M, T, pre[MAXS];
LL Ans[MAXM],Sum[MAXN << 2];

struct node {
    int l, r, id;
    bool operator < (const node & object) const {
        return r < object.r;
    }
} Node[MAXM];

void pushup(int rt) {
    Sum[rt] = Sum[rt << 1] + Sum[rt << 1|1];
}

void build(int rt,int l,int r) {
    if(l == r) {
        scanf("%I64d", &Sum[rt]);
        return;
    }
    int mid = (l + r) >> 1;
    build(lson);
    build(rson);
    pushup(rt);
}

void update(int p,int rt, int l, int r) {
    if(l == r) {
        Sum[rt] = 0;
        return;
    }
    int mid = (l + r) >> 1;
    if(p <= mid) update(p, lson);
    else update(p, rson);
    pushup(rt);
}

LL query(int L, int R, int rt, int l, int r) {
    if(L <= l && r <= R) {
        return Sum[rt];
    }
    int mid = (l + r) >> 1;
    LL ret = 0;
    if(L <= mid) ret += query(L, R, lson);
    if(R > mid) ret += query(L, R, rson);
    return ret;
}

int main() {
    //freopen("D://imput.txt","r",stdin);
    scanf("%d", &T);
    while(T --) {
        scanf("%d", &N);
        build(root);
        scanf("%d", &M);
        for(int i = 1 ; i <= M ; i ++) {
            scanf("%d %d", &Node[i].l, &Node[i].r);
            Node[i].id = i;
        }
        memset(pre, -1, sizeof(pre));
        sort(Node + 1, Node + M + 1);
        for(int i = 1,j = 1; i <= N; i++) {
            int tmp = query(i, i, root);
            if(tmp != 0 && pre[tmp] != -1) {//如果前面存在重复的数字则删除他
                update(pre[tmp],root);
            }
            pre[tmp] = i;
            while(j <= M && Node[j].r == i) {//如果右边的值等于当前的值,则进行求和,大家可以参考我博客里的教程
                Ans[Node[j].id] = query(Node[j].l, Node[j].r, root);
                j ++;
            }
        }
        for(int i = 1; i <= M ; i ++) {
            printf("%I64d\n", Ans[i]);
        }
    }
    return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-10-09 07:58:43

HDU - 3874 Necklace (线段树 + 离线处理)的相关文章

Necklace HDU - 3874 (线段树/树状数组 + 离线处理)

Necklace HDU - 3874 Mery has a beautiful necklace. The necklace is made up of N magic balls. Each ball has a beautiful value. The balls with the same beautiful value look the same, so if two or more balls have the same beautiful value, we just count

HDU 4638-Group(线段树+离线处理)

题意: 给n个编号,m个查询每个查询l,r,求下标区间[l,r]中能分成标号连续的组数(一组内的标号是连续的) 分析: 我们认为初始,每个标号为一个组(线段树维护区间组数),从左向右扫序列,当前标号,要考虑和他相邻的标号的位置,若前面位置出现了和它相邻的标号, 则前面位置组数减一(因为可以合并成一组),查询区间离线处理,保证了查询的正确. #include <map> #include <set> #include <list> #include <cmath&g

hdu 4288 Coder (线段树+离线)

题意: 刚开始有一个空集合.有三种操作: 1.往集合中加入一个集合中不存在的数 x 2.从集合中删除一个已经存在的数 x 3.计算集合的digest sum并输出.       digest sum求法:将集合中所有数从小到大排序,得到a1<a2<...<an. digest sum = sum(ai) where i mod 5 = 3 数据范围: N ( 1 <= N <= 105 ) 1 <= x <= 109. For any “add x” it is

HDU 3874 Necklace(树状数组)

Problem Description Mery has a beautiful necklace. The necklace is made up of N magic balls. Each ball has a beautiful value. The balls with the same beautiful value look the same, so if two or more balls have the same beautiful value, we just count

HDU 3874 Necklace (线段树单点更新+区间查询+离线操作)

Problem Description Mery has a beautiful necklace. The necklace is made up of N magic balls. Each ball has a beautiful value. The balls with the same beautiful value look the same, so if two or more balls have the same beautiful value, we just count

gcd(线段树离线处理)——HDU 4630

对应HDU题目:点击打开链接 No Pain No Game Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 1801    Accepted Submission(s): 770 Problem Description Life is a game,and you lose it,so you suicide. But you can

HDU3874 线段树 + 离线处理

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3874 , 线段树(或树状数组) + 离线处理 下午做了第一道离线处理的题目(HDU4417),多少有点感觉,顺便就把这道题也给做了. 这道题就是要求某个区间内不重复数的和,自己在网上百度后参考别人的方法才AC的.参考来源:http://www.cnblogs.com/gj-Acit/p/3249827.html 这道题还是需要先离线处理数据,具体方法: ① 先用线段树把树给建立起来: ② 先把所有要

HDU 1542 Atlantis 线段树+离散化+扫描线

题意:给出一些矩形的最上角坐标和右下角坐标,求这些矩形的面积并. NotOnlySuccess 线段树专辑中扫描线模板题,弱智的我对着大大的代码看了一下午才搞懂. 具体见思路见注释=.= #include <cstdio> #include <cstring> #include <algorithm> #include <vector> #define lson rt<<1,l,mid #define rson rt<<1|1,mid

Super Mario(线段树离线区间k值)

以前见过这题,没做出来,知道是离线处理,这次仔细想了下, 首先把出现的高度都map离散化一下,以离散化出来的数目g建树,把每个位置都开俩个vector,一个存以这个位置为L的询问,一个存以这个位置为R的询问. 然后从1-g 进行更新,假如当前i是以第j个区间的开始位置,那么这时就可以询问一下<=p[j].h的个数s,显然这时第J个区间多加的,需要减掉,p[j].sum-=s; 然后更新第i个数,update(a[i],1,g,1);再找到某第k区间是以i结尾的,那么依旧询问一下,得出s,p[k]