[2, 1438] Get Up, Soldier! Noj (Ningbo)

Source: Internet
Author: User

Http://ac.nbutoj.com/Problem/view.xhtml? Id = 1438

  • [2, 1438] Get Up, Soldier!
  • Time Limit: 1000 MS memory limit: 65535 K
  • Problem description
  • Japan is becoming more and more arrogant, but our army is not a vegetarian. In order to be able to kill Japan, our army decided to hold a military exercise recently. The Acting method was that the Red Army and Blue Army confronted each other on both sides of the Great Wall, and both sides wanted to win their positions. The two sides are currently deadlocked on the Great Wall. As the Blue Force Commander, you are eager to know the situation on the Great Wall. For simplicity, assume that the Great Wall is a one-dimensional coordinate ranging from 1 to n. Each time your subordinates send an intelligence: the situation from X to Y has been reversed. This reversal means that if this position is occupied by the Blue Army, it is now occupied by the Red Army, and vice versa. In addition, you always want to know what the situation is at a certain position. As the ACMer commander, of course, it is necessary to take advantage of the point, so at first the Great Wall line was fully occupied by the blue force.
  • Input
  • Multiple Input groups exist. The first behavior in each group contains two numbers: n (1 <= n <= 80000) and m (1 <= m <= 40000), representing the length of the Great Wall and m-times of inquiry. Next is line m. Each line starts with q or s. s represents the Report of the soldiers and q represents the inquiry of the Commander. For the specific numerical meaning, see the description.
  • Output
  • For each q query in each group, please output the result. 0 represents the Blue Army, and 1 represents the Red Army.
  • Sample Input
  • 3 3q 1s 1 2q 2
  • Sample output
  • 01
  • 1 # include <math. h> 2 # include <algorithm> 3 # include <string. h> 4 # include <string> 5 # include <ctime> 6 # include <queue> 7 # include <list> 8 # include <map> 9 # include <set> 10 # include <vector> 11 # include <stack> 12 # include <iostream> 13 # define INF 999999999 14 # define N 80010 15 using namespace std; 16 struct Nod 17 {18 int l, r; 19 int flag; 20} node [N <2]; // mark the Left and Right nodes with 21 22 void buildingTree (int l, int r, Int p) // build 23 {24 Nod & nd = node [p]; 25 nd. l = l; 26 nd. r = r; 27 nd. flag = 0; 28 if (l = r) 29 return; 30 int mid = (l + r)> 1; 31 buildingTree (l, mid, p * 2 ); 32 buildingTree (mid + 1, r, p * 2 + 1); 33} 34 35 void change (int p) // change the flag 36 {37 Nod & nd = node [p]; 38 nd. flag = 1-nd.flag; 39} 40 41 void updateTree (int l, int r, int p) 42 {43 Nod & nd = node [p]; 44 if (nd. l = l & nd. r = r) 45 {46 change (p); 47 return; 48} 49 if (nd. f Lag) // if the parent node is marked, 50 {51 change (2 * p); 52 change (2 * p + 1 ); 53 nd. flag = 1-nd.flag; // after the transfer is complete, change the flag back 54} 55 int mid = (nd. l + nd. r)> 1; 56 if (r <= mid) 57 updateTree (l, r, p * 2); 58 else if (l> mid) 59 updateTree (l, r, p * 2 + 1); 60 else 61 {62 updateTree (l, mid, p * 2); 63 updateTree (mid + 1, r, p * 2 + 1); 64} 65} 66 67 int queryTree (int idx, int p) // query 68 {69 Nod & nd = node [p]; 70 if (nd. l = idx & nd. r = idx) 71 retur N nd. flag; 72 if (nd. flag) // same as 73 {74 change (p * 2); 75 change (p * 2 + 1); 76 nd. flag = 1-nd.flag; 77} 78 int mid = (nd. l + nd. r)> 1; 79 if (mid> = idx) 80 return queryTree (idx, p * 2); 81 else if (mid <idx) 82 return queryTree (idx, p * 2 + 1); 83} 84 85 int main () 86 {87 int n, m; 88 while (~ Scanf ("% d", & n, & m) 89 {90 char str [4]; 91 buildingTree (1, n, 1 ); 92 while (m --) 93 {94 scanf ("% s", str); 95 int x, y; 96 if (str [0] ='s ') 97 {98 scanf ("% d", & x, & y); 99 updateTree (x, y, 1 ); 100} 101 else102 {103 scanf ("% d", & x); 104 printf ("% d \ n", queryTree (x, 1 )); 105} 106} 107} 108 return 0; 109}

     

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.