CF 292E, 线段树

题目大意:给你a,b两个数组,两个操作,一个是把a里连续的一段复制到b中,另一个是单点查询b。

解:可以看到其实a是不修改的,本质我们要维护b到a的一个映射,而赋值又是连续的,所以本质又是维护公差1的等差序列,那么这个等差序列只要一个首项就能表达他自己的性质。所以我们每个线段存一个首项,lazytag维护即可。

 

  1 #include <cstdio>
  2 #include <string>
  3 #include <iostream>
  4 #include <algorithm>
  5 #include <cmath>
  6 #include <cstring>
  7 #include <complex>
  8 #include <set>
  9 #include <vector>
 10 #include <map>
 11 #include <queue>
 12 #include <deque>
 13 #include <ctime>
 14 
 15 using namespace std;
 16 
 17 const double EPS = 1e-8;
 18 
 19 #define ABS(x) ((x)<0?(-(x)):(x))
 20 #define SQR(x) ((x)*(x))
 21 #define MIN(a,b) ((a)<(b)?(a):(b))
 22 #define MAX(a,b) ((a)>(b)?(a):(b))
 23 
 24 #define LSON(x) ((x)<<1)
 25 #define RSON(x) (((x)<<1)+1)
 26 #define LOWBIT(x) ((x)&(-(x)))
 27 #define MAXS 1111
 28 #define MAXN 222222
 29 #define VOIDPOINT 0
 30 #define LL long long
 31 #define OO 214748364
 32 
 33 int a[MAXN], b[MAXN];
 34 
 35 struct segTree{
 36     int tag[MAXN*4], num[MAXN*4], l[MAXN*4], r[MAXN*4], mid[MAXN*4];
 37     int gx, gy, gz;
 38     inline void updata(const int &kok) {
 39     }
 40     inline void pushdown(int kok) {
 41         if (l[kok] == r[kok]) num[kok] = a[tag[kok]];
 42         else {
 43             tag[LSON(kok)] = tag[kok];
 44             tag[RSON(kok)] = tag[kok] + (r[LSON(kok)] - l[LSON(kok)] + 1);
 45         }
 46         tag[kok] = 0;
 47     }
 48     void build(int kok, int ll, int rr, int *a) {
 49         l[kok] = ll; r[kok] = rr; tag[kok] = 0;
 50         if (ll == rr) {
 51             num[kok] = a[ll]; 
 52             return ;
 53         }
 54         int m = mid[kok] = (ll + rr) >> 1;
 55         build(LSON(kok), ll, m, a); build(RSON(kok), m+1, rr, a);
 56     }
 57     void insert(int kok) {
 58 //        cout << "insert ::  " << l[kok] << ' '<< r[kok] << endl;
 59 
 60         if (tag[kok]) pushdown(kok);
 61 
 62         if (gx <= l[kok] && r[kok] <= gy) {
 63             tag[kok] = gz + l[kok] - gx; return ;    
 64         }
 65         if (gx <= mid[kok]) insert(LSON(kok));
 66         if (gy > mid[kok]) insert(RSON(kok));
 67     }
 68     int query(int kok) {
 69         if (tag[kok]) pushdown(kok);
 70 
 71         if (l[kok] == r[kok]) {
 72             return num[kok];
 73         }
 74         if (gx <= mid[kok]) 
 75             return query(LSON(kok));
 76         else if (gx > mid[kok]) 
 77             return query(RSON(kok));
 78     }
 79     void set(int a = 0, int b = 0, int c = 0) {
 80         gx = a; gy = b; gz = c;
 81     }
 82 } Tree;
 83 
 84 int n, m;
 85 
 86 int main() {
 87 //    freopen("test.txt", "r", stdin);    
 88     scanf("%d%d", &n, &m);
 89     for (int i = 1; i <= n; ++i) scanf("%d", &a[i]);
 90     for (int i = 1; i <= n; ++i) scanf("%d", &b[i]);
 91     int cas, x, y, k;
 92     Tree.build(1, 1, n, b);
 93     while (m--) {
 94         scanf("%d", &cas);
 95         if (cas == 1) {
 96             scanf("%d%d%d", &x, &y, &k);
 97             Tree.set(y, y+k-1, x);
 98             Tree.insert(1);
 99         } else {
100             scanf("%d", &x);
101             Tree.gx = x;
102             printf("%d\n", Tree.query(1));
103 
104         }
105     }
106 
107     return 0;
108 }
CF 292E

 

posted @ 2016-07-16 10:58  F.D.His.D  阅读(264)  评论(0编辑  收藏  举报