H - Sum---(2015 summer training #12(Team_Qualifying))

H - Sum
时限:1000MS     内存:32768KB     64位IO格式:%I64d & %I64u

问题描述

XXX is puzzled with the question below: 

1, 2, 3, ..., n (1<=n<=400000) are placed in a line. There are m (1<=m<=1000) operations of two kinds. 

Operation 1: among the x-th number to the y-th number (inclusive), get the sum of the numbers which are co-prime with p( 1 <=p <= 400000). 
Operation 2: change the x-th number to c( 1 <=c <= 400000). 

For each operation, XXX will spend a lot of time to treat it. So he wants to ask you to help him. 
 

输入

There are several test cases. 
The first line in the input is an integer indicating the number of test cases. 
For each case, the first line begins with two integers --- the above mentioned n and m. 
Each the following m lines contains an operation. 
Operation 1 is in this format: "1 x y p". 
Operation 2 is in this format: "2 x c".
 

输出

For each operation 1, output a single integer in one line representing the result. 
 

样例输入

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

样例输出

     
     
7 0

分析:一开始,题意直接理解错误,后来明白了。写了个线段树,然而一直TLE,看了网上的题解,叫啥容斥原理,不会。


CODE:(TLE)

#include <iostream>
#include <cmath>
#include <cstdio>
#include<stdio.h>
#include <string.h>
#define ll long long
using namespace std;

const ll maxind=400005;
ll segTree[maxind*4+10];
ll array[maxind];

ll gcd(ll a,ll b){
    return b==0?a:gcd(b,a%b);
}

void buildTree(int node,int begin,int end,int c)
{
    if(begin==end){
        if(gcd(array[begin],c)==1)
            segTree[node]=array[begin];
        else
            segTree[node]=0;
    }
    else{
        buildTree(2*node,begin,(begin+end)/2,c);
        buildTree(2*node+1,(begin+end)/2+1,end,c);
        segTree[node]=segTree[2*node]+segTree[2*node+1];
    }
    return ;
}

ll query(int node,int begin,int end,int left,int right)
{
    ll p1, p2;

    if(left>end||right<begin)
        return -1;

    if(begin>=left&&end<=right)
        return segTree[node];

    p1=query(2*node,begin,(begin+end)/2,left,right);
    p2=query(2*node+1,(begin+end)/2+1,end,left,right);

    if(p1==-1)
        return p2;
    if(p2==-1)
        return p1;
    return p1+p2;
}

void Updata(int node,int begin,int end,int ind,int add)
{

    if(begin==end){
        segTree[node]=add;
        return ;
    }
    int m=(begin+end)>>1;
    if(ind<=m)
        Updata(node*2,begin,m,ind,add);
    else
        Updata(node*2+1,m+1,end,ind,add);

    segTree[node]=segTree[node*2]+segTree[node*2+1];
    return ;
}

int main()
{
    int t,n,m,tmp;
    cin>>t;
    while(t--){
        scanf("%d%d",&n,&m);
        memset(segTree,0,sizeof(segTree));
        for(int i=0;i<n;i++)
            array[i]=i+1;
        while(m--){
            scanf("%d",&tmp);
            int a,b,c;
            if(tmp==1){
                scanf("%d%d%d",&a,&b,&c);
                buildTree(1,0,n-1,c);
                ll sum=query(1,0,n-1,a-1,b-1);
                printf("%lld\n",sum);
            }
            else{
                scanf("%d%d",&a,&b);
                array[a-1]=b;
            }
        }
    }
    return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值