hdu 5057 Argestes and Sequence (离散的树状数组)

来源:互联网 发布:奢侈品辨别真假软件 编辑:程序博客网 时间:2024/05/22 17:02

Argestes and Sequence

Time Limit: 5000/2500 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 786    Accepted Submission(s): 204


Problem Description
Argestes has a lot of hobbies and likes solving query problems especially. One day Argestes came up with such a problem. You are given a sequence a consisting of N nonnegative integers, a[1],a[2],...,a[n].Then there are M operation on the sequence.An operation can be one of the following:
S X Y: you should set the value of a[x] to y(in other words perform an assignment a[x]=y).
Q L R D P: among [L, R], L and R are the index of the sequence, how many numbers that the Dth digit of the numbers is P.
Note: The 1st digit of a number is the least significant digit.
 

Input
In the first line there is an integer T , indicates the number of test cases.
For each case, the first line contains two numbers N and M.The second line contains N integers, separated by space: a[1],a[2],...,a[n]—initial value of array elements.
Each of the next M lines begins with a character type.
If type==S,there will be two integers more in the line: X,Y.
If type==Q,there will be four integers more in the line: L R D P.

[Technical Specification]
1<=T<= 50
1<=N, M<=100000
0<=a[i]<=231 - 1
1<=X<=N
0<=Y<=231 - 1
1<=L<=R<=N
1<=D<=10
0<=P<=9
 

Output
For each operation Q, output a line contains the answer.
 

Sample Input
15 710 11 12 13 14Q 1 5 2 1Q 1 5 1 0Q 1 5 1 1Q 1 5 3 0Q 1 5 3 1S 1 100Q 1 5 3 1
 

Sample Output
511501
 

Source
BestCoder Round #11 (Div. 2)
题目大意:就是单点修改,区间查询
题目分析:直接做的话数组要开c[MAX][10][10],超内存,所以要用时间换空间,减少一维,离线操作
#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>#define MAX 100007using namespace std;int t,n,m;int a[MAX],back[MAX];int c[MAX][10];int pow ( int n ){    if ( n == 0 ) return 1;    int temp = pow ( n/2 );    if ( n&1 ) return temp*temp*10;    else return temp*temp;}int lowbit ( int x ){    return x&-x;}void add ( int x , int y , int v ){    while ( x <= n )    {        c[x][y] += v;        x += lowbit (x);    }}int sum ( int x , int y ){    int ret = 0;    while ( x )    {        ret += c[x][y];        x -= lowbit (x);    }    return ret;}int query ( int l , int r , int num ){    return sum( r , num ) - sum ( l-1 , num );}int digit ( int value , int j ){    return value/pow(j-1)%10;}void init ( int j ){    memset ( c , 0 , sizeof ( c ) );    for ( int i = 1 ; i <= n ; i++ )    {        a[i] = back[i];        add ( i , digit( a[i] ,j ) , 1 );    }}struct Query {    int x,y,l,r,d,p,type;}q[MAX];int ans[MAX];char s[5];int main ( ){    scanf ( "%d" , &t );    while ( t-- )    {        scanf ( "%d%d" , &n , &m );        for ( int i = 1 ; i <= n ; i++ )            scanf ( "%d" , &back[i] );        for ( int i = 1 ; i <= m ; i++ )        {            scanf ( "%s" , s );            if ( s[0] == 'Q' )            {                scanf ( "%d%d%d%d" , &q[i].l , &q[i].r , &q[i].d , &q[i].p );                q[i].type = 1;            }            else            {                scanf ( "%d%d" , &q[i].x , &q[i].y );                q[i].type = 0;            }        }       // cout << "sdasdasdsdsadsa" << endl;        for ( int j = 1 ; j <= 10 ; j++ )        {            init ( j );           // cout << "Okay " << endl;            for ( int i = 1 ; i <= m ; i++ )            {               // cout << i << endl;                if ( q[i].type == 0 )                {                    add ( q[i].x , digit ( a[q[i].x] , j )  , -1 );                    add ( q[i].x , digit ( q[i].y , j ) , 1 );                    a[q[i].x] = q[i].y;                }                else                {                    if ( q[i].d == j )                        ans[i] = query ( q[i].l , q[i].r , q[i].p );                 }            }        }       // cout << "Yes " << endl;        for ( int i = 1 ; i <= m ; i++ )            if ( q[i].type == 1 )                printf ( "%d\n" , ans[i] );    }}


 
0 0