hdoj 3874 —— 树状数组单点修改+区间求和

Necklace

Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 2145    Accepted Submission(s): 769


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
 

Source
2011 Multi-University Training Contest 4 - Host by SDU
 

Recommend
lcy
 
挺水的一道题现在还写了好久,TAT;
题意是给你一个数组,问你L,R区间内数值不同的数之和;
因为是离线查询,我们可以先把查询按照右端点的大小排序,然后用map记录是否这个值出现过,出现过就把该点减此数值,否则加此数值,最后区间求和输出即可。
PS:hdoj要用I64d,不要用lld
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
using namespace std;
///#define Online_Judge
#define outstars cout << "***********************" << endl;
#define clr(a,b) memset(a,b,sizeof(a))
#define lson l , mid  , rt << 1
#define rson mid + 1 , r , rt << 1 | 1
#define mk make_pair
#define FOR(i , x , n) for(int i = (x) ; i < (n) ; i++)
#define FORR(i , x , n) for(int i = (x) ; i <= (n) ; i++)
#define REP(i , x , n) for(int i = (x) ; i > (n) ; i--)
#define REPP(i ,x , n) for(int i = (x) ; i >= (n) ; i--)
const int MAXN = 50010 + 50;
const int sigma_size = 26;
const long long LLMAX = 0x7fffffffffffffffLL;
const long long LLMIN = 0x8000000000000000LL;
const int INF = 0x7fffffff;
const int IMIN = 0x80000000;
#define eps 1e-8
const int MOD = (int)1e9 + 7;
typedef long long LL;
const double PI = acos(-1.0);
typedef double D;
typedef pair pi;
///#pragma comment(linker, "/STACK:102400000,102400000")
struct Node
{
    int l , r;
    int id;
}a[200010];
__int64 c[MAXN];
__int64 ans[200010];
int b[MAXN];
int lowbit(int x)
{
    return x & ( -x);
}
void update(int x , int add)
{
    while(x < MAXN)
    {
        c[x] += add;
        x += lowbit(x);
    }
}
__int64 getsum(int x)
{
    __int64 sum = 0;
    while(x > 0)
    {
        sum += c[x];
        x -= lowbit(x);
    }
    return sum;
}
int cmp(Node a , Node b)
{
    if(a.r == b.r)return a.l < b.l;
    return a.r < b.r;
}
int main()
{
//    freopen("heritage.in","r",stdin);
//    freopen("heritage.out","w",stdout);
    int t;
    mapmp;
    cin >> t;
    while(t--)
    {
        int n , m;
        scanf("%d" , &n);
        clr(c , 0);
        clr(ans , 0);
        mp.clear();
        clr(b , 0);
        FORR(i , 1 , n)scanf("%d" , &b[i]);
        scanf("%d" , &m);
        FOR(i , 0 , m)
        {
            scanf("%d%d" , &a[i].l , &a[i].r);
            a[i].id = i;
        }
        sort(a , a + m , cmp);
        int p = 1;
        FOR(i , 0 , m)
        {
            while(p <= a[i].r)
            {
                int x = b[p];
                if(mp[x] != 0)
                {
                    update(mp[x] , -x);
                }
                update(p ,x);
                mp[x] = p;
                p++;
            }
            ans[a[i].id] = getsum(a[i].r) - getsum(a[i].l - 1);
        }
        FOR(i , 0 , m)printf("%I64d\n" , ans[i]);
    }
    return 0;
}


你可能感兴趣的:(HDOJ)