1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
| #include<bits/stdc++.h> #define int long long #define ull unsigned long long #define maxn 200005 #define put() putchar('\n') #define Tp template<typename Ty> #define Ts template<typename Ty,typename... Ar> using namespace std; void read(int &x){ int f=1;x=0;char c=getchar(); while (c<'0'||c>'9') {if (c=='-') f=-1;c=getchar();} while (c>='0'&&c<='9') {x=x*10+c-'0';c=getchar();} x*=f; } namespace Debug{ Tp void _debug(char* f,Ty t){cerr<<f<<'='<<t<<endl;} Ts void _debug(char* f,Ty x,Ar... y){while(*f!=',') cerr<<*f++;cerr<<'='<<x<<",";_debug(f+1,y...);} Tp ostream& operator<<(ostream& os,vector<Ty>& V){os<<"[";for(auto& vv:V) os<<vv<<",";os<<"]";return os;} #define gdb(...) _debug((char*)#__VA_ARGS__,__VA_ARGS__) }using namespace Debug; #define fi first #define se second #define mk make_pair const int mod=1e9+7; int power(int x,int y=mod-2) { int sum=1; while (y) { if (y&1) sum=sum*x%mod; x=x*x%mod;y>>=1; } return sum; } mt19937 rd(time(0)); int n,m; struct yyy { int l,r,x; }g[maxn],e[maxn]; struct node { int ls,rs,rnd,val,lazy,x,tag,fa,id; }a[maxn*2]; vector<int>O[maxn],Q[maxn]; int root,id[maxn],Ans[maxn],cnt; int clone(int x,int id) { ++cnt; a[cnt].val=0;a[cnt].x=x;a[cnt].rnd=rd();a[cnt].id=id; return cnt; } void cover(int k,int tmp1,int tmp2) { a[k].x+=tmp1,a[k].lazy+=tmp1; a[k].val+=tmp2,a[k].tag+=tmp2; } void pushdown(int k) { if (a[k].ls) cover(a[k].ls,a[k].lazy,a[k].tag); if (a[k].rs) cover(a[k].rs,a[k].lazy,a[k].tag); a[k].lazy=a[k].tag=0; } void pushup(int k) {a[a[k].ls].fa=a[a[k].rs].fa=k;a[k].fa=0;} void split(int k,int x,int &tmp1,int &tmp2) { if (!k) return tmp1=tmp2=0,void(); pushdown(k); if (a[k].x<x) split(a[k].rs,x,a[k].rs,tmp2),tmp1=k,pushup(k); else split(a[k].ls,x,tmp1,a[k].ls),tmp2=k,pushup(k); } int merge(int x,int y) { if (!x||!y) return x+y; if (a[x].rnd<a[y].rnd) return pushdown(x),a[x].rs=merge(a[x].rs,y),pushup(x),x; else return pushdown(y),a[y].ls=merge(x,a[y].ls),pushup(y),y; } int rt; void dfs(int k) { if (!k) return ; pushdown(k); dfs(a[k].ls);dfs(a[k].rs); int tmp1,tmp2; a[k].ls=a[k].rs=0;a[k].fa=0; split(rt,a[k].x,tmp1,tmp2); rt=merge(tmp1,merge(k,tmp2)); } void print(int x) {if (!x) return ;print(a[x].fa);pushdown(x);} void pt(int x) { if (!x) return ; pushdown(x); pt(a[x].ls);printf("x=%lld : %lld %lld %lld %lld %lld\n",x,a[x].x,a[x].val,a[x].id,a[x].ls,a[x].rs);pt(a[x].rs); } signed main(void){ int i,tmp1,tmp2,tmp3; read(n);read(m); for (i=1;i<=n;i++) { read(g[i].l),read(g[i].r),read(g[i].x); } for (i=1;i<=m;i++) { read(e[i].l),read(e[i].r),read(e[i].x); O[e[i].l].push_back(i),Q[e[i].r].push_back(i); } for (i=1;i<=n;i++) { for (auto tmp:O[i]) { id[tmp]=clone(e[tmp].x,tmp); split(root,e[tmp].x,tmp1,tmp2); root=merge(tmp1,merge(id[tmp],tmp2)); } if (g[i].r==-1) { split(root,g[i].l+1,tmp1,tmp2); split(tmp2,g[i].l*2,tmp2,tmp3); cover(tmp2,-g[i].l,g[i].x); rt=tmp1;dfs(tmp2); cover(tmp3,-g[i].l,g[i].x); root=merge(rt,tmp3); } else { split(root,g[i].l,tmp1,tmp2); split(tmp2,g[i].r+1,tmp2,tmp3); cover(tmp2,0,g[i].x); root=merge(tmp1,merge(tmp2,tmp3)); } for (auto tmp:Q[i]) { print(id[tmp]); Ans[tmp]=a[id[tmp]].val; } } for (i=1;i<=m;i++) printf("%lld\n",Ans[i]); return 0; }
|