摘要:第四棵线段树,其实推倒重写了好几遍。说实话一点也不simple……题目中写的增量c在-1e4到1e4之间,操作最多1e5,那c应该不会超过1e9才对,结果不用__int64存增量就会一直wa……基本上还是线段树,开始建树的时候最好先把每个点的值接收进来再建,免得多更新一次。然后用一个变量来存增量,一个变量来存区间和。执行加操作的时候只要加到对应的区间就可以了,把增量存在该区间的增量上就返回,避免每次都加到根节点浪费时间。执行查询操作的时候每次向下传递增量信息,查到对应区间返回即可。变量名起的太长,自己看着都觉得恶心了…… 1 #include <stdio.h> 2 typedef
阅读全文
摘要:继续线段树。开始直接统计无悬念超时,后来看了某大牛的代码用了位运算把颜色的种类存到一个整形变量里才过。 1 #include <stdio.h> 2 #include <string.h> 3 typedef struct{ 4 int left,right,color; 5 bool cover; 6 }SegTree; 7 SegTree board[450000]; 8 void createSegTree(int root, int left, int right){ 9 int mid;10 board[root].left = left;11 board[r
阅读全文
摘要:第二个线段树,用malloc申请果断超内存,自己写了个销毁树的函数果断超时。最后还是默默地用数组实现了。2174ms 慢的可怕…… 1 #include <stdio.h> 2 3 typedef struct{ 4 int left,right,max; 5 int lChild,rChild; 6 }SegTree; 7 SegTree list[800000]; 8 9 int max(int a, int b){10 return a > b? a:b;11 }12 void createSegTree(int root, int left, int right){1
阅读全文
摘要:第一次学线段树,从头到尾敲了快一小时。其实add和sub是没必要写的,不过想想反正当练手,就敲上去了。 1 #include <stdio.h> 2 #include <string.h> 3 #include <malloc.h> 4 5 typedef struct Seg_Tree{ 6 int left,right; 7 int amount; 8 struct Seg_Tree *lChild, *rChild; 9 }SegTreeNode,* SegTree; 10 11 SegTree createSegTree(int left, int
阅读全文
摘要:基本上就是深搜……利用3个数组分别来表示行列宫有哪些数存在,用1个数组记录所有需要填数的位置,深搜过去即可 1 #include <stdio.h> 2 #include <string.h> 3 #include <time.h> 4 int rowok[9][10],colok[9][10],miyaok[9][10]; //用来记录行列宫有哪些数字 5 int sudoku[9][9],place[81][2],p,n; //place用来记录哪些位置需要填数 6 int search(int x, int y){ 7 int i,j; 8 if(p
阅读全文
摘要:结点名是字符串的最短路,处理好了用dijkstra即可自己写了个非常丑陋的处理……起点和终点是同一点的时候还得判断。#include <stdio.h>#include <string.h>#define INF 999999999#define MAX 155int N,map[MAX][MAX],stationnum;char station[MAX][35];int input(){ int j; char stationname[35]; scanf("%s",stationname); for(j = 0; j < stationnu
阅读全文
摘要:多点到一点的最短距离,因为点的编号是从1到n所以把0作为新的起点将所有可能的起点跟0连接,转换为求单源最短路径用dijkstra即可。 1 #include <stdio.h> 2 #include <string.h> 3 4 #define INF 999999999 5 #define MAX 1007 6 7 int n,map[MAX][MAX]; 8 int dijkstra(int str, int end){ 9 bool hash[MAX];10 int i,min,path[MAX];11 12 for(i = 0; i <= n; i++){
阅读全文