## 【Codeforces 781E】Andryusha and Nervous Barriers

### Code

#include<bits/stdc++.h>
#define LL long long
using namespace std;

const int N = 5000000;
const int M = 200009;
const int MOD = 1000000007;

int hit,wid,n,cnt;
struct Pack{int h,sum,del;}p[N];
struct Wall{int h,l,r,s;}wall[M];

class Segment_Tree{
int ans_tmp,tot,root,ch[M][2];
stack<int> s[M];
public:
inline void insert(int p, int id) {
insert(root, 1, wid, p, id);
}
inline int query(int h, int l, int r) {
ans_tmp = 0;
query(root, 1, wid, l, r, h);
return ans_tmp;
}
private:
void insert(int &w, int l, int r, int p, int v) {
if (!w) w = ++tot; s[w].push(v);
if (l < r) {
int mid = l + r + 1 >> 1;
if (p < mid) insert(ch[w][0], l, mid-1, p, v);
else insert(ch[w][1], mid, r, p, v);
}
}
void query(int w, int l, int r, int L, int R, int h) {
if (L <= l && r <= R) {
while (!s[w].empty() && p[s[w].top()].h <= h) {
if (!p[s[w].top()].del) {
p[s[w].top()].del = 1;
(ans_tmp += p[s[w].top()].sum) %= MOD;
} s[w].pop();
}
} else {
int mid = l + r + 1 >> 1;
if (L < mid) query(ch[w][0], l, mid-1, L, R, h);
if (mid <= R) query(ch[w][1], mid, r, L, R, h);
}
}
}SGT;

char c=getchar(); int ret=0,f=1;
while (c<'0'||c>'9') {if(c=='-')f=-1;c=getchar();}
while (c<='9'&&c>='0') {ret=ret*10+c-'0';c=getchar();}
return ret*f;
}

int main() {
for (int i=1;i<=n;i++) {
}
for (int i=1;i<=wid;i++) {
p[++cnt].h = hit + 1;
p[cnt].sum = 1;
SGT.insert(i, cnt);
}
sort(wall+1, wall+1+n, [](const Wall &A, const Wall &B){return A.h > B.h;});
for (int i=1,h_mx,tmp;i<=n;i++) {
tmp = SGT.query(wall[i].h + wall[i].s, wall[i].l, wall[i].r);
p[++cnt].sum = tmp; p[cnt].h = wall[i].h;
p[++cnt].sum = tmp; p[cnt].h = wall[i].h;
if (wall[i].l == 1) SGT.insert(wall[i].r+1, cnt-1);
else SGT.insert(wall[i].l-1, cnt-1);
if (wall[i].r == wid) SGT.insert(wall[i].l-1, cnt);
else SGT.insert(wall[i].r+1, cnt);
}
int vout = 0;
for (int i=1;i<=cnt;i++)
(vout += (p[i].del ^ 1) * p[i].sum) %= MOD;
printf("%d\n",vout);
return 0;
}


## 【UOJ 218】[UNR #1] 火车管理

™我的自定义测试，那么大的数据都没有wa

std的做法是：

#include<iostream>
#include<cstdio>
using namespace std;

const int MAXN = 500000+9;

int n,m,ty,last_ans,arr[MAXN];

char c=getchar(); int buf=0,f=1;
while (c<'0'||c>'9'){if(c=='-')f=-1;c=getchar();}
while (c<='9'&&c>='0'){buf=buf*10+c-'0';c=getchar();}
return (buf*f+last_ans*ty) % n + 1;
}

namespace Segment_Tree{
#define SEG Segment_Tree
const int N = MAXN*4;
int tag[N],sum[N],tm[N],ans_tmp,L,R,DEL;

inline void push_down(int w, int l, int r, int mid){
tag[w*2] = tag[w*2+1] = tag[w];
sum[w*2] = (mid-l)*arr[tag[w]];
sum[w*2+1] = (r-mid+1)*arr[tag[w]];
tag[w] = 0;
}

void query(int w, int l, int r){
if (L <= l && r <= R) ans_tmp += sum[w];
else {
int mid = (l + r + 1) / 2;
if (tag[w]) push_down(w,l,r,mid);
if (L < mid) query(w*2,l,mid-1);
if (R >= mid) query(w*2+1,mid,r);
}
}inline int query(int l, int r){ans_tmp=0;L=l,R=r;query(1,1,n);return ans_tmp;}

void Modify(int w, int l, int r){
if (L <= l && r <= R) tag[w] = DEL, sum[w] = (r-l+1)*arr[DEL];
else {
int mid = (l + r + 1) / 2;
if (tag[w]) push_down(w,l,r,mid);
if (L < mid) Modify(w*2,l,mid-1);
if (R >= mid) Modify(w*2+1,mid,r);
sum[w] = sum[w*2] + sum[w*2+1];
}
}inline void modify(int l, int r, int del){L=l,R=r,DEL=del;Modify(1,1,n);}

inline int index(int pos){
int w = 1, l = 1, r = n, mid;
while (l < r) {
if (tag[w]) return tag[w];
else {
mid = (l + r + 1) / 2;
if (pos < mid) w = w*2, r = mid-1;
else w = w*2+1, l = mid;
}
}
return tag[w];
}
};

namespace Persistent_Segment_Tree{
#define PST Persistent_Segment_Tree
const int N = 20000000;
int tag[N],root[N],ch[N][2],tot,cnt,L,R,DEL;

inline int query(int tm, int pos) {
if (tm < 1) return 0;
else {
int w = root[tm], l=1, r=n, mid;
while (l < r) {
if (tag[w]) return tag[w];
else {
mid = (l + r + 1) / 2;
if (pos < mid) w = ch[w][0], r = mid-1;
else w = ch[w][1], l = mid;
}
}
return tag[w];
}
}

inline void push_down(int w){
for (int i=0;i<=1;i++) if (!ch[w][i]) ch[w][i] = ++cnt;
for (int i=0;i<=1;i++) tag[ch[w][i]] = tag[w];
tag[w] = 0;
}

void Modify(int pre, int &w, int l, int r, int tg){
w = ++cnt; ch[w][1] = ch[pre][1];
ch[w][0] = ch[pre][0]; tag[w] = tg?tg:tag[pre];
if (L <= l && r <= R) tag[w] = DEL;
else {
int mid = (l + r + 1) / 2;
if (L < mid) Modify(ch[pre][0],ch[w][0],l,mid-1,tag[w]);
else if (tag[w]) ch[w][0] = ++cnt, tag[cnt] = tag[w];
if (mid <= R) Modify(ch[pre][1],ch[w][1],mid,r,tag[w]);
else if (tag[w]) ch[w][1] = ++cnt, tag[cnt] = tag[w];
tag[w] = 0;
}
}

inline int modify(int l, int r, int val, bool type){
L = l, R = r, DEL = val;
Modify(root[tot], root[(tot+type)], 1, n, 0);
}
};

int main(){
scanf("%d%d%d",&n,&m,&ty);
for (int i=1,type,l,r,del;i<=m;i++){
scanf("%d",&type);
if (type == 1) {
if (l > r) swap(l, r);
printf("%d\n",last_ans=SEG::query(l,r));
} else if (type == 2) {
int tmp = SEG::index(l);
int nv = PST::query(tmp-1,l);
PST::modify(l,l,nv,0);
SEG::modify(l,l,nv);
} else {