HDU 4027 Can you answer these queries?(線段樹區間開方)
阿新 • • 發佈:2017-06-10
sizeof sqrt .cn swap %d nes nts following clr
You are asked to answer the queries that the sum of the endurance of a consecutive part of the battleship line.
Notice that the square root operation should be rounded down to integer.
For each test case, the first line contains a single integer N, denoting there are N battleships of evil in a line. (1 <= N <= 100000)
The second line contains N integers Ei, indicating the endurance value of each battleship from the beginning of the line to the end. You can assume that the sum of all endurance value is less than 263.
The next line contains an integer M, denoting the number of actions and queries. (1 <= M <= 100000)
For the following M lines, each line contains three integers T, X and Y. The T=0 denoting the action of the secret weapon, which will decrease the endurance value of the battleships between the X-th and Y-th battleship, inclusive. The T=1 denoting the query of the commander which ask for the sum of the endurance value of the battleship between X-th and Y-th, inclusive.
Can you answer these queries?
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65768/65768 K (Java/Others)
Total Submission(s): 16260 Accepted Submission(s): 3809
Notice that the square root operation should be rounded down to integer.
Input The input contains several test cases, terminated by EOF.
For each test case, the first line contains a single integer N, denoting there are N battleships of evil in a line. (1 <= N <= 100000)
The next line contains an integer M, denoting the number of actions and queries. (1 <= M <= 100000)
Output For each test case, print the case number at the first line. Then print one line for each query. And remember follow a blank line after each test case.
Sample Input 10 1 2 3 4 5 6 7 8 9 10 5 0 1 10 1 1 10 1 1 5 0 5 8 1 4 8
Sample Output Case #1: 19 7 6 題目鏈接:HDU 4027 一開始用分塊做的,可惜無限超時,這題如果用分塊寫,更新用lazy標記可以做到$sqrt(N)$,但是求和的復雜度不是$sqrt(N)$,因為區間開方之和不等於和的開方。只能用線段樹了,顯然一個數被向下取整開方太多次只能為0或者1,因此記錄一下一個區間被開方的次數即可,小於$2^{63}$的非負數數開7次一定為0或者1,因此如果一個區間被開方次數大於等於7次就不用再繼續往下更新了,當然前提是每一次都更新到葉子節點,因為區間只是記錄了開方次數,sum得更新到葉子才算更新 代碼:
#include <stdio.h> #include <iostream> #include <algorithm> #include <cstdlib> #include <sstream> #include <numeric> #include <cstring> #include <bitset> #include <string> #include <deque> #include <stack> #include <cmath> #include <queue> #include <set> #include <map> using namespace std; #define INF 0x3f3f3f3f #define LC(x) (x<<1) #define RC(x) ((x<<1)+1) #define MID(x,y) ((x+y)>>1) #define CLR(arr,val) memset(arr,val,sizeof(arr)) #define FAST_IO ios::sync_with_stdio(false);cin.tie(0); typedef pair<int, int> pii; typedef long long LL; const double PI = acos(-1.0); const int N = 100010; struct seg { int l, mid, r; LL sum; int cnt; }; seg T[N << 2]; LL arr[N]; void pushup(int k) { T[k].sum = T[LC(k)].sum + T[RC(k)].sum; } void build(int k, int l, int r) { T[k].l = l; T[k].r = r; T[k].mid = MID(l, r); T[k].sum = T[k].cnt = 0; if (l == r) T[k].sum = arr[l]; else { build(LC(k), l, T[k].mid); build(RC(k), T[k].mid + 1, r); pushup(k); } } void SQ(int k, int l, int r) { if (l <= T[k].l && T[k].r <= r) ++T[k].cnt; if (T[k].cnt >= 7) return; if (T[k].l == T[k].r) { T[k].sum = sqrt(T[k].sum); return ; } if (r <= T[k].mid) SQ(LC(k), l, r); else if (l > T[k].mid) SQ(RC(k), l, r); else SQ(LC(k), l, T[k].mid), SQ(RC(k), T[k].mid + 1, r); pushup(k); } LL query(int k, int l, int r) { if (l <= T[k].l && T[k].r <= r) return T[k].sum; else { if (r <= T[k].mid) return query(LC(k), l, r); else if (l > T[k].mid) return query(RC(k), l, r); else return query(LC(k), l, T[k].mid) + query(RC(k), T[k].mid + 1, r); } } int main(void) { int i; int tcase = 1; int n, m; while (~scanf("%d", &n)) { for (i = 1; i <= n; ++i) scanf("%I64d", &arr[i]); build(1, 1, n); scanf("%d", &m); printf("Case #%d:\n", tcase++); while (m--) { int l, r, ops; scanf("%d%d%d", &ops, &l, &r); if (l > r) swap(l, r); if (!ops) SQ(1, l, r); else printf("%I64d\n", query(1, l, r)); } puts(""); } return 0; }
HDU 4027 Can you answer these queries?(線段樹區間開方)