Http://acm.hdu.edu.cn/showproblem.php? PID = 1, 4407
At first there were N numbers 1 ~ N. Here there are m operations, two types. Operation 1: Obtain the sum of the numbers of mutual qualities with P in the [x, y] range. Operation 2: change the number at position X to Y. Output results for each query.
Because there are only 1000 operations, and they are sequential at first. For the I-th query, we ignore all the 2nd operations before I, that is, we think that the number of N is 1 ~ N. Calculate the sum of the numbers in the range [x, y] and P in accordance with the volume exclusion principle, traverse operation 2 before I, and adjust the answer. Wa has been changed for countless times. It has been changed for a long time because I ignore that the number of X locations may be modified multiple times. We only need to consider the last modification. Therefore, you can use map to save operation 2.
# Include <stdio. h> # include <iostream> # include <map> # include <set> # include <bitset> # include <list> # include <stack> # include <vector> # include <math. h> # include <string. h> # include <queue> # include <string> # include <stdlib. h >#include <algorithm> # define ll _ int64 // # define ll long # define EPS 1e-9 # define PI ACOs (-1.0) using namespace STD; const int maxn = 400010; int n, m; bool flag [maxn]; int prime [maxn]; In T FAC [100]; int faccnt; ll ans; Map <int, int> m; int gcd (int A, int B) {If (B = 0) return; return gcd (B, A % B);} void getprime () {memset (flag, false, sizeof (FLAG); prime [0] = 0; for (INT I = 2; I <maxn; I ++) {If (flag [I] = false) prime [++ prime [0] = I; for (Int J = 1; j <= prime [0] & prime [J] * I <maxn; j ++) {flag [prime [J] * I] = true; if (I % prime [J] = 0) Break ;}} void getfac (INT num) {int TMP = num; FAC CNT = 0; For (INT I = 1; I <= prime [0] & prime [I] * prime [I] <= TMP; I ++) {If (TMP % prime [I] = 0) {FAC [faccnt ++] = prime [I]; while (TMP % prime [I] = 0) TMP/= prime [I];} If (TMP = 1) break;} If (TMP> 1) FAC [faccnt ++] = TMP ;} ll getsum (INT t) {return (1 + 1ll * t) * 1ll * t)> 1;} // evaluate [1, r: The sum of the numbers that are mutually dependent on the number. Ll CAL (int r) {ll ANW = getsum (r); ll tmp = 0; For (INT I = 1; I <(1 <faccnt); I ++) {ll Mul = 1; int CNT = 0; For (Int J = 0; j <faccnt; j ++) {if (I & (1 <j )) {Mul * = FAC [J]; CNT ++ ;}} if (CNT & 1) TMP + = Mul * getsum (LL) r/MUL ); else TMP-= Mul * getsum (LL) r/MUL);} return ANW-TMP;} int main () {getprime (); int test; scanf ("% d", & Test); While (test --) {M. clear (); int op, x, y, z; scanf ("% d", & N, & M ); For (INT I = 1; I <= m; I ++) {scanf ("% d", & OP); If (OP = 1) {scanf ("% d", & X, & Y, & Z); getfac (z); ans = CAL (y)-cal (x-1 ); // printf ("Ans: % i64d \ n", ANS); Map <int, int >:: iterator it; for (IT = m. begin (); it! = M. end (); It ++) {If (IT-> first> = x & IT-> first <= y) {If (gcd (z, IT-> first) = 1) ans-= it-> first; If (gcd (z, IT-> second) = 1) ans + = it-> second ;}} printf ("% i64d \ n", ANS);} else {scanf ("% d", & X, & Y ); M [x] = y ;}} return 0 ;}
HDU 4407 sum)