[UESTC1059]秋实大哥与小朋友(线段树, 离散化)

题目链接:http://acm.uestc.edu.cn/#/problem/show/1059

普通线段树+离散化,关键是……离散化后建树和查询都要按照基本法!!!RE了不知道多少次………………我真是个沙茶……

  1 /*
  2 ━━━━━┒ギリギリ♂ eye!
  3 ┓┏┓┏┓┃キリキリ♂ mind!
  4 ┛┗┛┗┛┃\○/
  5 ┓┏┓┏┓┃ /
  6 ┛┗┛┗┛┃ノ)
  7 ┓┏┓┏┓┃
  8 ┛┗┛┗┛┃
  9 ┓┏┓┏┓┃
 10 ┛┗┛┗┛┃
 11 ┓┏┓┏┓┃
 12 ┛┗┛┗┛┃
 13 ┓┏┓┏┓┃
 14 ┃┃┃┃┃┃
 15 ┻┻┻┻┻┻
 16 */
 17 #include <algorithm>
 18 #include <iostream>
 19 #include <iomanip>
 20 #include <cstring>
 21 #include <climits>
 22 #include <complex>
 23 #include <fstream>
 24 #include <cassert>
 25 #include <cstdio>
 26 #include <bitset>
 27 #include <vector>
 28 #include <deque>
 29 #include <queue>
 30 #include <stack>
 31 #include <ctime>
 32 #include <set>
 33 #include <map>
 34 #include <cmath>
 35 
 36 using namespace std;
 37 
 38 #define fr first
 39 #define sc second
 40 #define cl clear
 41 #define BUG puts("here!!!")
 42 #define W(a) while(a--)
 43 #define pb(a) push_back(a)
 44 #define Rint(a) scanf("%d", &a)
 45 #define Rll(a) scanf("%lld", &a)
 46 #define Rs(a) scanf("%s", a)
 47 #define Cin(a) cin >> a
 48 #define FRead() freopen("in", "r", stdin)
 49 #define FWrite() freopen("out", "w", stdout)
 50 #define Rep(i, len) for(LL i = 0; i < (len); i++)
 51 #define For(i, a, len) for(LL i = (a); i < (len); i++)
 52 #define Cls(a) memset((a), 0, sizeof(a))
 53 #define Clr(a, x) memset((a), (x), sizeof(a))
 54 #define Full(a) memset((a), 0x7f7f, sizeof(a))
 55 #define lrt rt << 1
 56 #define rrt rt << 1 | 1
 57 typedef long long LL;
 58 
 59 const int maxn = 800010;
 60 LL sum[maxn<<2];
 61 LL add[maxn<<2];
 62 int n, m;
 63 int hcnt;
 64 LL h[maxn];
 65 LL cmd[maxn], a[maxn], b[maxn], c[maxn];
 66 
 67 void pushUP(int rt) {
 68     sum[rt] = sum[lrt] + sum[rrt];
 69 }
 70 
 71 void pushDOWN(int rt, int m) {
 72     if(add[rt]) {
 73         add[lrt] += add[rt];
 74         add[rrt] += add[rt];
 75         sum[lrt] += (m - (m >> 1)) * add[rt];
 76         sum[rrt] += (m >> 1) * add[rt];
 77         add[rt] = 0;
 78     }
 79 }
 80 
 81 void build(int l, int r, int rt) {
 82     add[rt] = sum[rt] = 0;
 83     if(l == r) return;
 84     int m = (l + r) >> 1;
 85     build(l, m, lrt);
 86     build(m+1, r, rrt);
 87     pushUP(rt);
 88 }
 89 
 90 void update(int L, int R, int x, int l, int r, int rt) {
 91     if(l >= L && R >= r) {
 92         add[rt] += x;
 93         sum[rt] += (r - l + 1) * x;
 94         return;
 95     }
 96     pushDOWN(rt, r-l+1);
 97     int m = (l + r) >> 1;
 98     if(m >= L) update(L, R, x, l, m, lrt);
 99     if(m < R) update(L, R, x, m+1, r, rrt);
100     pushUP(rt);
101 }
102 
103 LL query(int p, int l, int r, int rt) {
104     if(l == r && p == l) return sum[rt];
105     pushDOWN(rt, r-l+1);
106     int m = (l + r) >> 1;
107     if(p <= m) {
108         LL ret = query(p, l, m, lrt);
109         pushUP(rt);
110         return ret;
111     }
112     else {
113         LL ret = query(p, m+1, r, rrt);
114         pushUP(rt);
115         return ret;
116     }
117 }
118 
119 int getid(int x) {
120     return lower_bound(h, h+hcnt, x) - h + 1;
121 }
122 
123 int main() {
124     // FRead();
125     while(~Rint(n) && ~Rint(m)) {
126         hcnt = 0;
127         Rep(i, m) {
128             Rint(cmd[i]);
129             if(cmd[i] == 0) {
130                 Rll(a[i]); Rll(b[i]); Rll(c[i]);
131                 h[hcnt++] = a[i]; h[hcnt++] = b[i];
132             }
133             if(cmd[i] == 1) {
134                 Rll(a[i]);
135                 h[hcnt++] = a[i];
136             }
137         }
138         sort(h, h+hcnt); hcnt = unique(h, h+hcnt) - h;
139         build(1, hcnt, 1);
140         Rep(i, m) {
141             if(cmd[i] == 0) update(getid(a[i]), getid(b[i]), c[i], 1, hcnt, 1);
142             if(cmd[i] == 1) printf("%lld\n", query(getid(a[i]), 1, hcnt, 1));
143         }
144     }
145     return 0;
146 }

 

转载于:https://www.cnblogs.com/kirai/p/5509922.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值