日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

nyoj116士兵杀敌2

發布時間:2025/3/16 24 豆豆
生活随笔 收集整理的這篇文章主要介紹了 nyoj116士兵杀敌2 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
題目鏈接: 士兵殺敵2
鏈接二

解法線段樹,線段樹有些像是特殊的二叉平衡樹,不知道的可以搜搜二叉平衡樹,可能并沒有你想象中的那樣難。

AC代碼:

#include <stdio.h> #include <stdlib.h> struct node{int data, min, max, mid;//由min、max得到mid,小于mid的部分在左子樹,大于mid的部分都在右子樹。node *leftchild, *rightchild;//左右子節點。 }; typedef node * nodePoint;nodePoint CreateTree(int l, int r) {nodePoint newNode = (nodePoint)malloc(sizeof(node));//if(newNode == NULL)newNode->min = l;newNode->max = r;newNode->leftchild = NULL;newNode->rightchild = NULL; newNode->data = 0;//初始化數據。 int m = (l+r)/2;newNode->mid = m;if(m >= l && l != r) newNode->leftchild = CreateTree(l, m);//如果沒有&&l!=r這個條件,m==l且l==r時將是死循環。if(m < r) newNode->rightchild = CreateTree(m+1, r);return newNode; }void addData(nodePoint ct, int n, int d) {while(ct->min < ct->max) {ct->data += d;//從跟節點到目標葉節點路徑都加上dif(n <= ct->mid) ct = ct->leftchild;else ct = ct->rightchild;}ct->data += d; }int queryData(nodePoint ct, int i, int j) {if(ct->min == i && ct->max == j) return ct->data;if(i > ct->mid) return queryData(ct->rightchild, i, j);//要詢問的區間在右子樹。if(j <= ct->mid) return queryData(ct->leftchild, i, j);return queryData(ct->leftchild, i, ct->mid)+queryData(ct->rightchild, ct->mid+1, j);//要詢問的區間包含mid時。 }int main() {int n, m, x, i;char str[9];scanf("%d%d", &n, &m);nodePoint Tree = CreateTree(1,n);for(i = 1; i <= n; i++) {scanf("%d", &x);addData(Tree, i, x);}while(m--) {scanf("%s%d%d", str, &i, &x);if(str[0] == 'A') addData(Tree, i, x);else printf("%d\n", queryData(Tree, i, x));}return 0; }
---BY HYK--

總結

以上是生活随笔為你收集整理的nyoj116士兵杀敌2的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。