H - Sum---(2015 summer training #12(Team_Qualifying))
来源:互联网 发布:神奇宝贝坂木老大知乎 编辑:程序博客网 时间:2024/06/05 07:16
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.
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".
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.
样例输入
13 32 2 31 1 3 41 2 3 6
样例输出
70
分析:一开始,题意直接理解错误,后来明白了。写了个线段树,然而一直TLE,看了网上的题解,叫啥容斥原理,不会。
CODE:(TLE)
#include <iostream>#include <cmath>#include <cstdio>#include<stdio.h>#include <string.h>#define ll long longusing 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;}
0 0
- H - Sum---(2015 summer training #12(Team_Qualifying))
- D - A very hard Aoshu problem----(2015 summer training #12(Team_Qualifying))
- H - Load Balancing---(2015 NEUQ_ACM summer training #1)
- H - Hurdles of 110m----(2015 summer training #4 (Qualifying))
- F - Stone-----(2015 summer training #11)
- J - Flyer------(2015 summer training #11)
- C - We Love MOE Girls---(2015 summer training #9)
- B - And Or---(2015 NEUQ_ACM summer training #1)
- A - Faulty Odometer---(2015 summer training #3)
- J - Intelligent IME---(2015 summer training #3)
- C - Colorful Rainbows----(2015 summer training #4 (Qualifying))
- K - Kinds of Fuwas----(2015 summer training #4 (Qualifying))
- Problem A. Automated Telephone Exchange----(2015 summer training #5)
- Problem F. Flat----(2015 summer training #5)
- B - Median-----(2015 summer training #8(Qualifying))
- D - Minimum palindrome----(2015 summer training #9)
- J - A Bit Fun-----(2015 summer training #9)
- D - Cut the Cake----(2015 summer training #11)
- MySQL查看和修改wait_timeout
- CentOS更改yum源与更新系统
- servletFileUpload.parseRequest(request)解析为空获取不到数据问题
- 如何构建一个高效的敏捷团队
- java与mysql乱码的问题
- H - Sum---(2015 summer training #12(Team_Qualifying))
- Java I/O
- 数据结构实验之求二叉树后序遍历和层次遍历
- linux系统下无法创建mysql存储过程问题
- cmake 生成 CodeBlocks 配置
- TiXml使用详解
- 吸血鬼日记第七季
- spring_jar包的详解
- Java 四大域对象总结