Codevs4919 line segment tree Exercise 4, codevs4919 Line Segment
Time Limit: 1 s space limit: 128000 KB title level: GoldQuestionView running resultsDescriptionDescription
There are two operations for giving you N numbers.
1: Add X to all numbers in the range [a, B ].
2: Number of [a, B] divisible by 7
Input description
Input Description
The first line is a positive integer n, the next n rows are n integers, And the next positive integer Q represents the number of operations. The next row is a number of integers. If the first number is add, followed by three positive integers a, B, X, it indicates that each number in the range [a, B] is increased by X. If it is count, indicates the number of [a, B] divisible by 7 in the statistical interval.
Output description
Output Description
Output one answer line for each query
Sample Input
Sample Input
3 2 3 46count 1 3count 1 2add 1 3 2count 1 3add 1 3 3count 1 3
Sample output
Sample Output
0
0
0
1
Data range and prompt
Data Size & Hint
10%: 1 <N <= 10, 1 <Q <= 10
30%: 1 <N <= random, 1 <Q <= 10000
100%: 1 <N <= 100000,1 <Q <= 100000
CATEGORY tag
Tags click here to expand
Record the remaining lines of % 7
Brute force statistics
1 # include <iostream> 2 # include <cstdio> 3 # include <cstring> 4 # include <cmath> 5 # include <algorithm> 6 # define ls k <1 7 # define rs k <1 | 1 8 using namespace std; 9 const int MAXN = 2000050; 10 const int INF = 0x7fffff; 11 void read (int & n) 12 {13 char c = '+'; int x = 0; bool flag = 0; 14 while (c <'0' | c> '9') {c = getchar (); if (c = '-') flag = 1;} 15 while (c> = '0' & c <= '9') {x = x * 10 + (c-48 ); c = getchar ();} 16 flag = 1? N =-x: n = x; 17} 18 struct node 19 {20 int l, r, f, mod [7]; // Number of remainder of mod7 21} tree [MAXN]; 22 int ans = 0; 23 int p [7]; 24 inline void update (int k) 25 {26 for (int I = 0; I <7; I ++) tree [k]. mod [I] = tree [ls]. mod [I] + tree [rs]. mod [I]; 27} 28 int down (int k) 29 {30 for (int I = 0; I <7; I ++) p [(I + tree [k]. f) % 7] = tree [ls]. mod [I]; 31 for (int I = 0; I <7; I ++) tree [ls]. mod [I] = p [I]; 32 for (int I = 0; I <7; I ++) p [(I + tree [k]. f) % 7] = tree [rs]. mod [I]; 33 for (int I = 0; I <7; I ++) tree [rs]. mod [I] = p [I]; 34 tree [ls]. f = tree [k]. f; tree [rs]. f = tree [k]. f; 35 tree [k]. f = 0; 36} 37 void Build_Tree (int k, int ll, int rr) 38 {39 tree [k]. l = ll; tree [k]. r = rr; 40 if (tree [k]. l = tree [k]. r) 41 {42 int p; read (p); 43 ++ tree [k]. mod [p % 7]; 44 return; 45} 46 if (tree [k]. f) down (k); 47 int mid = tree [k]. l + tree [k]. r> 1; 48 Build_Tree (ls, ll, mid); Build_Tree (rs, mid + 1, rr); 49 update (k ); 50} 51 void Interval_Count (int k, int ll, int rr) 52 {53 if (ll <= tree [k]. l & tree [k]. r <= rr) 54 {55 ans + = tree [k]. mod [0]; 56 return; 57} 58 if (tree [k]. f) down (k); 59 int mid = tree [k]. l + tree [k]. r> 1; 60 if (ll <= mid) Interval_Count (ls, ll, rr); 61 if (rr> mid) Interval_Count (rs, ll, rr ); 62} 63 void Interval_Add (int k, int ll, int rr, int val) 64 {65 if (ll <= tree [k]. l & tree [k]. r <= rr) 66 {67 for (int I = 0; I <7; I ++) p [(I + val) % 7] = tree [k]. mod [I]; 68 for (int I = 0; I <7; I ++) tree [k]. mod [I] = p [I]; 69 tree [k]. f = val; 70 return; 71} 72 if (tree [k]. f) down (k); 73 int mid = tree [k]. l + tree [k]. r> 1; 74 if (ll <= mid) Interval_Add (ls, ll, rr, val); 75 if (rr> mid) Interval_Add (rs, ll, rr, val); 76 update (k); 77} 78 int main () 79 {80 int n, m; 81 read (n); 82 Build_Tree (1, 1, n ); 83 read (m); 84 for (int I = 1; I <= m; I ++) 85 {86 char s [10]; 87 scanf ("% s ", s); 88 if (s [0] = 'C') // count the number of divisible values by 7: 89 {90 int x, y; read (x ); read (y); ans = 0; 91 Interval_Count (1, x, y); 92 printf ("% d \ n", ans ); 93} 94 else 95 {96 int x, y, val; read (x); read (y); read (val); 97 Interval_Add (1, x, y, val); 98} 99} 100 return 0; 101}