标签:fine style png 传送门 color ide ++i bool getchar
http://www.lydsy.com/JudgeOnline/problem.php?id=4012
1 #include <cstdio> 2 #include <cstring> 3 #include <algorithm> 4 5 #define REP(i, l, r) for(int i=(l); i!=(r); ++i) 6 #define FOR(i, l, r) for(int i=(l); i<=(r); ++i) 7 #define DRP(i, l, r) for(int i=(l); i!=(r); --i) 8 #define DFR(i, l, r) for(int i=(l), i>=(r); --i) 9 10 typedef long long LL; 11 12 template<class T>T Min(const T &a, const T &b) {return a < b ? a : b;} 13 template<class T>T Max(const T &a, const T &b) {return a > b ? a : b;} 14 template<class T>bool Chkmin(T &a, const T &b) {return a > b ? a=b, 1 : 0;} 15 template<class T>bool Chkmax(T &a, const T &b) {return a < b ? a=b, 1 : 0;} 16 17 const int SN = 150000 + 47; 18 const int SM = SN << 1 | 1; 19 const int ST = 10000000 + 47; 20 21 int head[SN], nxt[SM], to[SM], wei[SM], tot; 22 int size[SN], fa[SN], deep[SN], son[SN], val[SN], dis[SN]; 23 int top[SN], f[SN], g[SN], rnk; 24 int a[SN], b[SN], c[SN], sum[SN], n, m, p; 25 int rt[ST], lson[ST], rson[ST], data[ST], cnt; 26 LL segs[ST], sd[SN], lstans; 27 28 void NxtInt(int &); 29 bool Cmp(const int &, const int &); 30 void Add(int, int, int); 31 void DFS(int); 32 void DFS(int, int); 33 void Get(int); 34 int Modify(int, int, int, int, int); 35 void Query(int, int, int); 36 LL Query(int, int, int, int, int, int); 37 38 int main() { 39 40 freopen("shop.in", "r", stdin); 41 freopen("shop.out", "w", stdout); 42 43 int x, y, z, r, s; 44 NxtInt(n), NxtInt(m), NxtInt(p); 45 FOR(i, 1, n) NxtInt(a[i]), b[i] = i; 46 std::sort(b+1, b+n+1, Cmp); 47 FOR(i, 1, n) c[b[i]] = i; 48 std::sort(a+1, a+n+1); 49 50 REP(i, 1, n) NxtInt(x), NxtInt(y), NxtInt(z), Add(c[x], c[y], z); 51 deep[1]=1, DFS(1), DFS(1, 1); 52 FOR(i, 1, n) sum[i] = sum[i-1]+val[i]; 53 FOR(i, 1, n) sd[i] = sd[i-1]+dis[i]; 54 FOR(i, 1, n) Get(i); 55 56 while(m--) { 57 NxtInt(x), NxtInt(y), NxtInt(z); 58 y = (y+lstans)%p, z = (z+lstans)%p; 59 if(Min(y, z)>a[n] || Max(y, z)<a[0]) {puts("0"), lstans = 0; continue;} 60 r = std::lower_bound(a+1, a+n+1, Min(y, z)) - a; 61 s = std::upper_bound(a+1, a+n+1, Max(y, z)) - a - 1; 62 Query(r, s, c[x]); 63 printf("%lld\n", lstans=sd[s]-sd[r-1]+1LL*(s-r+1)*dis[c[x]]-2*lstans); 64 } 65 66 return 0; 67 68 } 69 70 void NxtInt(int &in) { 71 char ch; 72 for(ch=getchar(); ch>‘9‘||ch<‘0‘; ch=getchar()) ; 73 for(in=0; ch>=‘0‘&&ch<=‘9‘; ch=getchar()) in=in*10+ch-‘0‘; 74 } 75 76 bool Cmp(const int &x, const int &y) { 77 return a[x] < a[y]; 78 } 79 80 void Add(int u, int v, int w) { 81 nxt[++tot]=head[u]; head[u]=tot; to[tot]=v; wei[tot]=w; 82 nxt[++tot]=head[v]; head[v]=tot; to[tot]=u; wei[tot]=w; 83 } 84 85 void DFS(int u) { 86 size[u] = 1; 87 for(int i=head[u]; i; i=nxt[i]) 88 if(to[i] != fa[u]) { 89 deep[to[i]]=deep[u]+1, fa[to[i]] = u; 90 val[to[i]]=wei[i], dis[to[i]]=dis[u]+wei[i]; 91 DFS(to[i]), size[u]+=size[to[i]]; 92 if(size[to[i]]>size[son[u]]) son[u] = to[i]; 93 } 94 } 95 96 void DFS(int u, int v) { 97 top[u] = v, g[f[u]=++rnk] = u; 98 if(son[u]) DFS(son[u], v); 99 for(int i=head[u]; i; i=nxt[i]) 100 if(to[i]!=son[u] && to[i]!=fa[u]) 101 DFS(to[i], to[i]); 102 } 103 104 void Get(int x) { 105 int y = x; 106 rt[x] = rt[x-1]; 107 while(top[y] != 1) 108 rt[x] = Modify(rt[x], 1, n, f[top[y]], f[y]), y = fa[top[y]]; 109 if(y != 1) rt[x] = Modify(rt[x], 1, n, 1, f[y]); 110 } 111 112 int Modify(int rt, int l, int r, int L, int R) { 113 int x = ++cnt, mid = l+r>>1; 114 if(l==L && r==R) { 115 lson[x] = lson[rt], rson[x] = rson[rt]; 116 data[x]=data[rt]+1, segs[x]=segs[rt]+dis[g[r]]-dis[fa[g[l]]]; 117 return x; 118 } 119 if(R <= mid) { 120 lson[x] = Modify(lson[rt], l, mid, L, R); 121 rson[x] = rson[rt]; 122 } 123 else if(L > mid) { 124 lson[x] = lson[rt]; 125 rson[x] = Modify(rson[rt], mid+1, r, L, R); 126 } 127 else { 128 lson[x] = Modify(lson[rt], l, mid, L, mid); 129 rson[x] = Modify(rson[rt], mid+1, r, mid+1, R); 130 } 131 data[x] = data[rt]; 132 segs[x]=segs[lson[x]]+segs[rson[x]]+1LL*data[rt]*(dis[g[r]]-dis[fa[g[l]]]); 133 return x; 134 } 135 136 void Query(int x, int y, int z) { 137 lstans = 0; 138 while(top[z] != 1) 139 lstans += Query(rt[x-1], rt[y], 1, n, f[top[z]], f[z]), z = fa[top[z]]; 140 if(z != 1) lstans += Query(rt[x-1], rt[y], 1, n, 1, f[z]); 141 } 142 143 LL Query(int rt1, int rt2, int l, int r, int L, int R) { 144 if(l==L && r==R) return segs[rt2]-segs[rt1]; 145 int mid = l+r>>1; 146 LL x = 1LL * (data[rt2]-data[rt1]) * (dis[g[R]]-dis[fa[g[L]]]); 147 if(R <= mid) return Query(lson[rt1], lson[rt2], l, mid, L, R) + x; 148 else if(L > mid) return Query(rson[rt1], rson[rt2], mid+1, r, L, R) + x; 149 else return Query(lson[rt1], lson[rt2], l, mid, L, mid) + 150 Query(rson[rt1], rson[rt2], mid+1, r, mid+1, R) + x; 151 }
标签:fine style png 传送门 color ide ++i bool getchar
原文地址:http://www.cnblogs.com/Cai2016/p/6697475.html