P3372 "Template" segment tree 1
- 153 through
- 525 Submit
- Topic provider Hansbug
- Label
- Difficulty popularity +/ Improve
Submit a Discussion
Recent discussions
- "Template" segment tree 1 (aaaaaaaaa ...
- "Template" segment tree 1
- Is there something wrong with the Rokua evaluator?
Title Description
Title, you need to perform the following two operations, known as a sequence:
1. Add x to each number in a range
2. Find out the amount of each number in a range and
Input/output format
Input format:
The first line contains two integers n, M, respectively, indicating the number of numbers and the total number of operations.
The second line contains n space-delimited integers, where the I number represents the initial value of item I of a sequence.
The next M line contains 3 or 4 integers, representing an operation, as follows:
Action 1: Format: 1 x y k meaning: Add K to each number in the interval [x, y]
Action 2: Format: 2 x y meaning: the and of each number in the output interval [x, y]
Output format:
The output contains several line integers, which is the result of all Operation 2.
Input/Output sample
Input Sample #1 :
5 5
1 5 4) 2 3
2 2 4
1 2 3 2
2 3 4
1 1 5 1
2 1 4
Output Sample #1 :
11
8
20
Description
Time limit: 1000ms,128m
Data size:
For 30% data: n<=8,m<=10
For 70% data: n<=1000,m<=10000
For 100% data: n<=100000,m<=100000
(data has been strengthened ^_^, guaranteed within Int64/long Long data range)
Analysis: involves the interval operation, then uses the Lazy-tag thought, when needs to deal with this interval, does not need to go down, marking, when needs to use the time passes the mark to be able.
#include <cstdio>#include<cstring>#include<iostream>#include<algorithm>using namespacestd;Long LongN, m,sum[3000000],tag[3000000];voidPushdown (intLintRinto) { if(Tag[o]) {intMid = (L + r) >>1; Tag[o*2] +=Tag[o]; Tag[o*2+1] +=Tag[o]; Sum[o*2] + = tag[o] * (Mid-l +1); Sum[o*2+1] + = tag[o] * (R-mid); Tag[o]=0; }}voidBuildintLintRinto) { if(L = =r) {scanf ("%lld", &Sum[o]); return; } intMid = (L + r) >>1; Build (L, Mid, O*2); Build (Mid+1, R, O *2+1); Sum[o]= Sum[o *2] + Sum[o *2+1];}voidUpdateintLintRintVintLintRinto) { if(l <= l && R <=R) {Tag[o]+=v; Sum[o]+ = v * (r-l +1); return; } pushdown (L, R, O); intMid = (L + r) >>1; if(L <=mid) Update (L, R, V, L, Mid, O*2); if(R >mid) Update (L, R, V, mid+1, R, O *2+1); Sum[o]= Sum[o *2] + Sum[o *2+1];}Long LongQueryintLintRintLintRinto) { if(l <= l && R <=R)returnSum[o]; if(L > R | | R <l)return 0; Pushdown (L, R, O); intMid = (L + r) >>1; returnQuery (L, R, L, Mid, O *2) + query (L, R, Mid +1, R, O *2+1);}intMain () {scanf ("%lld%lld", &n, &m); Build (1N1); for(inti =1; I <= m; i++) { intID, x, y, K; scanf ("%d", &ID); if(id = =1) {scanf ("%d%d%d", &x, &y, &k); Update (x, Y, K,1N1); } if(id = =2) {scanf ("%d%d", &x, &y); printf ("%lld\n", query (x, Y,1N1)); } } return 0;}
Rokua P3372 "Template" segment tree 1