Shandong Province competition question neu OJ 1444 line segment tree dual mark

Source: Internet
Author: User

Http://acm.neu.edu.cn/hustoj/problem.php? Id = 1444

OJ issue forum post http://t.cn/zjBp4jd FAQ http://t.cn/zjHKbmN Linux problem see http://t.cn/aWnP1n
1444: hour ur magic time limit: 1 sec memory limit: 256 MB
Submitted: 129 solution: 21
[Submit] [Status] [discussion version] Description

In Warcraft III, destroyer is a large flying unit that must consume magic to sustain its mana. breaking free of the Obsidian stone that holds them, these monstrous creatures roar into battle, swallowing magic to feed their insatiable hunger as they move between battles and rain destruction down upon their foes. has Spell Immunity. attacks land and air units.

The core skill of the destroyer is so called hour ur magic, it takes all mana from all units in a area and gives it to the Destroyer.

Now to simplify the problem, assume you haveNUnits in a line, all unit start with 0 mana and can increase to infinity maximum mana. All unit within t the destroyer have mana regeneration 1 in per unit time.

The destroyer haveMInstructionsT L R, It means, in timeT, The destroyer use hour ur magic on unit fromLToR. We give you allMInstructions in time order, count how many mana the destroyer have hour ur altogether.

Input

The first line contains one integerT, Indicating the test case. For each test case, the first contains two integerN,?M(1? ≤?N,?M? ≤? (105). The nextMLine each line contains a instructionT lR. (1? ≤?T? ≤? 105 ,? 1? ≤?L? ≤?R? ≤?N)

Output

For each test case, output the conrespornding result.

Sample Input
110 51 1 102 3 103 5 104 7 105 9 10
Sample output
30
Prompt

Source

Shandong Provincial Competition



I wrote several times, modified several times, and finally determined a method:
For double-label classes, consider the following:
1. Multiple variables are marked with different modifications;

2. The order in which tags are modified is different. The order of the tags for the reset and add operations is wrong when Pushdown is always wa ,,,

There seems to be a bit of doubt, but for this question, the previous operations of each query must be the add operation. Therefore, the operations must be cleared first and then added. But what about other questions ??

In conclusion

# Include <cstdio> # include <cstring> # include <algorithm> # include <string> # include <iostream> # include <cmath> # include <map> # include <queue> using namespace STD; # define ls (RT) rt * 2 # define RS (RT) rt * 2 + 1 # define ll long # define rep (I, S, E) for (INT I = s; I <E; I ++) # define repe (I, S, e) for (INT I = s; I <= E; I ++) # define Cl (a, B) memset (a, B, sizeof (A) # define in (s) freopen (S, "r", stdin) # define out (s) freopen (S, "W", stdin) const int maxn = 100000 + 10; struct node {int L, R; int add; // mark the incremental int V; ll sum; int Len () {return r-L + 1 ;}} nodes [maxn * 4]; void build (int rt, int l, int R) {// printf ("# Build # RT = % d l = % d r = % d \ n ", RT, L, R); // nodes [RT]. L = L; nodes [RT]. R = r; nodes [RT]. sum = nodes [RT]. add = 0; nodes [RT]. V = 0; If (L = r) return; int mid = (L + r)/2; build (LS (RT), L, mid ); build (RS (RT), Mid + 1, R); nodes [RT]. sum = nodes [ls (RT)]. sum + nodes [RS (RT)]. SUM;} void Pushdown (int rt) {// printf ("# push # RT = % d l = % d r = % d sum = % LLD add = % d V = % d \ n ", RT, nodes [RT]. l, nodes [RT]. r, nodes [RT]. sum, nodes [RT]. add, nodes [RT]. v); If (nodes [RT]. v) // put it next to it because of the high priority {nodes [ls (RT)]. add = nodes [RS (RT)]. add = 0; nodes [RS (RT)]. sum = nodes [ls (RT)]. sum = 0; nodes [ls (RT)]. V = nodes [RS (RT)]. V = 1; nodes [RT]. V = 0;} If (nodes [RT]. add) {nodes [ls (RT)]. add + = nodes [RT]. add; nodes [RS (RT)]. add + = nodes [RT]. add; nodes [ls (RT)]. sum + = nodes [RT]. add * nodes [ls (RT)]. len (); nodes [RS (RT)]. sum + = nodes [RT]. add * nodes [RS (RT)]. len (); nodes [RT]. add = 0 ;}// RT updates are completed before Pushdown. Pushdown only updates the subnode void Update (int rt, int L, int R, int OP) {If (L = nodes [RT]. L & nodes [RT]. R = r) {If (OP = 1) // increase {nodes [RT]. add ++; nodes [RT]. sum + = nodes [RT]. len (); /// // printf ("# update # RT = % d l = % d r = % d sum = % LLD add = % d \ n ", RT, L, R, nodes [RT]. sum, nodes [RT]. add);} If (OP = 2) // clear {nodes [RT]. add = nodes [RT]. sum = 0; nodes [RT]. V = 1;} return;} Pushdown (RT); int mid = (nodes [RT]. L + nodes [RT]. r)/2;/* If (L <= mid) Update (LS (RT), L, R, OP); If (r> mid) update (RS (RT), L, R, OP); */If (r <= mid) Update (LS (RT), L, R, OP ); else {If (L> mid) Update (RS (RT), L, R, OP); else {Update (LS (RT), L, mid, OP ); update (RS (RT), Mid + 1, R, OP) ;}} nodes [RT]. sum = nodes [RS (RT)]. sum + nodes [ls (RT)]. SUM;} ll query (int rt, int L, int R) {If (L = nodes [RT]. L & nodes [RT]. R = r) {// printf ("RT = % d l = % d r = % d sum = % LLD \ n", RT, L, R, nodes [RT]. sum); return nodes [RT]. SUM;} Pushdown (RT); int mid = (nodes [RT]. L + nodes [RT]. r)/2; ll tmp1 = 0;/* If (L <= mid) tmp1 = query (LS (RT), L, R); If (r> mid) tmp2 = query (RS (RT), L, R); nodes [RT]. sum = nodes [RS (RT)]. sum + nodes [ls (RT)]. SUM; */If (r <= mid) tmp1 = query (LS (RT), L, R); else {If (L> mid) tmp1 = query (RS (RT), L, R); else {tmp1 = query (LS (RT), L, mid) + query (RS (RT ), mid + 1, R) ;}} nodes [RT]. sum = nodes [RS (RT)]. sum + nodes [ls (RT)]. SUM; return tmp1;} struct edge {int L, R; int t;} e [maxn]; bool CMP (const edge & A, const edge & B) {return. T <B. t;} int main () {int ncase, N, M, L, R, T; ll ans; scanf ("% d", & ncase); While (ncase --) {scanf ("% d", & N, & M); Build (1,1, n); For (INT I = 0; I <m; I ++) {scanf ("% d", & E [I]. t, & E [I]. l, & E [I]. r);} Sort (E, E + M, CMP); int time = 0; ans = 0; For (INT I = 0; I <m; I ++) {If (E [I]. t> time) {update (1, 1, n, 1); time = E [I]. t;} ans + = query (1, E [I]. l, E [I]. r); Update (1, E [I]. l, E [I]. r, 2);} printf ("% LLD \ n", ANS);} return 0 ;}



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.