Codeforces 292e copying data (line segment tree)

Source: Internet
Author: User

Http://codeforces.com/problemset/problem/292/E

Two operations: A and B:

1: copy the range [x, x + k-1] of array A to the range [y, Y + k-1] of array B.

2: Ask the current value of B [X.

 

Idea: You can use the line segment tree to maintain the following values in the online segment tree: STA and STB. These two values are meaningful only for the leaf nodes of the online segment tree. For a position Po, (that is, the line segment [Po, po], which is the leaf node of the Line Segment tree). If the value of STA is 0, the position is not covered by a, and the original value is kept. Otherwise, sta indicates that the position Po has been overwritten and is a [sta] ~ A [sta + k-1] covered. The value of STB is similar to that of STB. If the value of STB Is not 0, the position is overwritten from Stb. The following is an example.

For example, if we perform operation 1, convert a [x] ~ The value of a [x + k-1] is copied to the range of array B [y, Y + k-1], then we divide the range in the line tree [y, the STA value in Y + k-1] is X, and the STB value is Y.

Then for the operation 1,: 1, x, y, K, we will range [y, Y + k-1] In the STA, STB respectively assigned to X, Y.

For operation 2: 2, we get the position Po value as STA and STB. If the sta is 0, then output B [po, otherwise, output a [po-STB + sta.

CodeAs follows:

# Include <iostream> # include <string. h> # include <algorithm> # include <stdio. h> # define maxn 100010 # define mid (T [p]. L + T [p]. r)> 1) # define ls (P <1) # define RS (LS | 1) Using namespace STD; struct tree {int L, R; int STA, STB;} t [maxn <2]; int A [maxn], B [maxn]; void Pushdown (INT p) {If (T [p]. sta) {T [ls]. sta = T [p]. sta; t [ls]. STB = T [p]. STB; t [RS]. sta = T [p]. sta; t [RS]. STB = T [p]. STB; t [p]. sta = 0 ;}} void build (int p, int L, int R) {T [p]. L = L, t [p]. R = r, t [p]. sta = T [p]. STB = 0; If (L = r) return; build (LS, L, mid); Build (RS, Mid + 1, R);} void change (int p, int L, int R, int STA, int STB) {If (T [p]. L = L & T [p]. R = r) {T [p]. sta = sta; t [p]. STB = STB; return;} Pushdown (p); If (r <= mid) Change (LS, L, R, STA, STB); else if (L> mid) change (RS, L, R, STA, STB); else {change (LS, L, mid, STA, STB); change (RS, Mid + 1, R, STA, STB) ;}} struct node {int STA, STB ;}; node query (int p, int X) {node TMP; If (T [p]. L = T [p]. r) {TMP. sta = T [p]. STA, TMP. STB = T [p]. STB; return TMP;} Pushdown (p); If (x> mid) return query (RS, x); Return query (LS, x);} int main () {freopen ("dd.txt", "r", stdin); int N, Q, I; scanf ("% d", & N, & Q ); for (I = 1; I <= N; I ++) scanf ("% d", & A [I]); for (I = 1; I <= N; I ++) scanf ("% d", & B [I]); Build (1,1, n); While (Q --) {int t, x, y, z; scanf ("% d", & T); If (t = 1) {scanf ("% d", & X, & Y, & Z ); change (1, Y, Y + Z-1, x, y);} else {scanf ("% d", & X); node TMP = query (1, x ); if (TMP. STB = 0) printf ("% d \ n", B [x]); else printf ("% d \ n", a [x-tmp.stb + TMP. sta]) ;}} 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.