Description
In the game of DotA, Pudge’s meat hook is actually the most horrible thing for most of the heroes. The hook is made up of several consecutive metallic sticks which are of the same length.
Now Pudge wants to do some Z??http://www.2cto.com/kf/ware/vc/" target="_blank" class="keylink">vcGVyYXRpb25zIG9uIHRoZSBob29rLiA8YnI+Cjxicj4KTGV0IHVzIG51bWJlciB0aGUgY29uc2VjdXRpdmUgbWV0YWxsaWMgc3RpY2tzIG9mIHRoZSBob29rIGZyb20gMSB0byBOLiBGb3IgZWFjaCBvcGVyYXRpb24sIFB1ZGdlIGNhbiBjaGFuZ2UgdGhlIGNvbnNlY3V0aXZlIG1ldGFsbGljIHN0aWNrcywgbnVtYmVyZWQgZnJvbSBYIHRvIFksIGludG8gY3VwcmVvdXMgc3RpY2tzLCBzaWx2ZXIgc3RpY2tzIG9yIGdvbGRlbiBzdGlja3MuCjxicj4KVGhlIHRvdGFsIHZhbHVlIG9mIHRoZSBob29rIGlzIGNhbGN1bGF0ZWQgYXMgdGhlIHN1bSBvZiB2YWx1ZXMgb2YgTiBtZXRhbGxpYyBzdGlja3MuIE1vcmUgcHJlY2lzZWx5LCB0aGUgdmFsdWUgZm9yIGVhY2gga2luZCBvZiBzdGljayBpcyBjYWxjdWxhdGVkIGFzIGZvbGxvd3M6Cjxicj4KPGJyPgpGb3IgZWFjaCBjdXByZW91cyBzdGljaywgdGhlIHZhbHVlIGlzIDEuIDxicj4KRm9yIGVhY2ggc2lsdmVyIHN0aWNrLCB0aGUgdmFsdWUgaXMgMi4gPGJyPgpGb3IgZWFjaCBnb2xkZW4gc3RpY2ssIHRoZSB2YWx1ZSBpcyAzLiA8YnI+Cjxicj4KUHVkZ2Ugd2FudHMgdG8ga25vdyB0aGUgdG90YWwgdmFsdWUgb2YgdGhlIGhvb2sgYWZ0ZXIgcGVyZm9ybWluZyB0aGUgb3BlcmF0aW9ucy4gPGJyPgpZb3UgbWF5IGNvbnNpZGVyIHRoZSBvcmlnaW5hbCBob29rIGlzIG1hZGUgdXAgb2YgY3VwcmVvdXMgc3RpY2tzLiA8YnI+CgogIAoKCgo8cCBjbGFzcz0="pst">Input
The input consists of several test cases. The first line of the input is the number of the cases. There are no more than 10 cases.
For each case, the first line contains an integer N, 1<=N<=100,000, which is the number of the sticks of Pudge’s meat hook and the second line contains an integer Q, 0<=Q<=100,000, which is the number of the operations.
Next Q lines, each line contains three integers X, Y, 1<=X<=Y<=N, Z, 1<=Z<=3, which defines an operation: change the sticks numbered from X to Y into the metal kind Z, where Z=1 represents the cupreous kind, Z=2 represents the silver kind and Z=3 represents the golden kind.
Output
For each case, print a number in a line representing the total value of the hook after the operations. Use the format in the example.
Sample Input
1
10
2
1 5 2
5 9 3
Sample Output
Case 1: The total value of the hook is 24.
题意:让你修改[l, r]的类型为v, v的种类不同,分数不同,求所有操作后的总和,默认值是1
思路:线段树的区间修改,不算太难
#include
#include
#include
#include
#define lson(x) ((x) << 1) #define rson(x) ((x) << 1 | 1) using namespace std; const int maxn = 100005; struct seg { int w; int v; }; struct segment_tree { seg node[maxn<<2]; void update(int pos) { node[pos].w = node[lson(pos)].w + node[rson(pos)].w; } void push(int pos, int len) { if (node[pos].v > 0) { node[lson(pos)].v = node[rson(pos)].v = node[pos].v; node[lson(pos)].w = node[pos].v * (len - (len >> 1)); node[rson(pos)].w = node[pos].v * (len >> 1); node[pos].v = 0; } } void build(int l, int r, int pos) { node[pos].v = 0; if (l == r) { node[pos].w = 1; return; } int m = l + r >> 1; build(l, m, lson(pos)); build(m+1, r, rson(pos)); update(pos); } void modify(int l, int r, int pos, int x, int y, int z) { if (x <= l && y >= r) { node[pos].w = (r-l+1) * z; node[pos].v = z; return; } push(pos, r-l+1); int m = l + r >> 1; if (x <= m) modify(l, m, lson(pos), x, y, z); if (y > m) modify(m+1, r, rson(pos), x, y, z); update(pos); } int query(int l, int r, int pos, int x, int y) { if (x <= l && y >= r) return node[pos].w; push(pos, r-l+1); int m = l + r >> 1; if (x <= m) query(l, m, lson(pos), x, y); if (y > m) query(m+1, r, rson(pos), x, y); } } tree; int main() { int n, q, cas = 1; int t; scanf("%d", &t); while (t--) { scanf("%d", &n); tree.build(1, n, 1); scanf("%d", &q); int x, y, z; for (int i = 0; i < q; i++) { scanf("%d%d%d", &x, &y, &z); tree.modify(1, n, 1, x, y, z); } printf("Case %d: The total value of the hook is %d.\n", cas++, tree.node[1].w); } return 0; }