↓ Skip to main content
  1. Posts/

hdoj 1754 I hate it

·581 words·2 mins
Note: This article is available in Chinese only. 本文暂无英文版本。 View original

http://acm.hdu.edu.cn/showproblem.php?pid=1754 题意:给定一个区间,有m组操作,操作可以是改变单点,或者查询区间最大值。对于每组查询,输出。 思路:分块。这篇博客说得很不错。http://www.cnblogs.com/sweetsc/archive/2012/08/15/2639395.html

代码实现
  1/* ***********************************************
  2Author :111qqz
  3Created Time :2015年12月15日 星期二 11时25分17秒
  4File Name :code/hdu/1754.cpp
  5************************************************ */
  6
  7#include <cstdio>
  8#include <cstring>
  9#include <iostream>
 10#include <algorithm>
 11#include <vector>
 12#include <queue>
 13#include <set>
 14#include <map>
 15#include <string>
 16#include <cmath>
 17#include <cstdlib>
 18#include <ctime>
 19#define fst first
 20#define sec second
 21#define lson l,m,rt<<1
 22#define rson m+1,r,rt<<1|1
 23#define ms(a,x) memset(a,x,sizeof(a))
 24typedef long long LL;
 25#define pi pair < int ,int >
 26#define MP make_pair
 27
 28using namespace std;
 29const double eps = 1E-8;
 30const int dx4[4]={1,0,0,-1};
 31const int dy4[4]={0,-1,1,0};
 32const int inf = 0x3f3f3f3f;
 33const int N=2E5+7;
 34int a[N];
 35int mx[N];
 36int n ,m;
 37int magic;
 38
 39void pre()
 40{
 41    magic = int(sqrt(n));
 42    ms(mx,-1); //把i%magic去掉试试。
 43    for ( int i = 0 ; i <n ; i++ )  //从0开始比较方便。
 44    {
 45//	if (a[i]>mx[i/magic])
 46//	    mx[i/magic] = a[i];
 47	mx[i/magic] = max(mx[i/magic],a[i]);
 48    }
 49
 50}
 51
 52int query( int l,int r)
 53{
 54    int ret = a[l];
 55    for ( int j = l ; j <= r;)
 56    {
 57	if (j%magic==0&&j+magic-1<=r)
 58	{
 59	    ret = max(ret,mx[j/magic]);
 60	 //   if (mx[j/magic]>ret) ret= mx[j/magic];
 61	    j+= magic;
 62	}
 63	else  //首尾两段不够magic的部分直接暴力
 64	{
 65	    ret = max(a[j],ret);
 66	    //if (a[j]>ret) ret = a[j];
 67	    j++;
 68	}
 69    }
 70    return ret;
 71}
 72
 73void update( int x,int delta)
 74{
 75    a[x] = delta;
 76    int l = x/magic*magic;
 77    int r = l+magic;  //找到x对应的哪个块。
 78    for ( int i = l ; i < r ; i++)
 79    {
 80	//	if (i%magic==0||a[i]>mx[i/magic]) mx[i/magic] = a[i];
 81	    mx[i/magic] = max(mx[i/magic],a[i]);
 82    }
 83}
 84
 85int main()
 86{
 87	#ifndef  ONLINE_JUDGE
 88	freopen("code/in.txt","r",stdin);
 89  #endif
 90
 91	while (scanf("%d%d",&n,&m)!=EOF)
 92	{
 93//	    ms(a,0);
 94//	    ms(mx,0);
 95	    for ( int i = 0 ; i < n ; i++) scanf("%d",&a[i]);
 96	    getchar();
 97	    pre();
 98	    while (m--)
 99	    {
100		char opt;
101		scanf("%c ",&opt);
102		if (opt=='Q')
103		{
104		    int l,r;
105		    scanf("%d%d",&l,&r);
106		    getchar();
107		    printf("%d\n",query(l-1,r-1));
108		}
109		else
110		{
111		    int x,delta;
112		    scanf("%d%d",&x,&delta);
113		    getchar(); //又忘记读掉回车符了.....
114		    update(x-1,delta);
115		}
116	    }
117
118	}
119
120  #ifndef ONLINE_JUDGE
121  fclose(stdin);
122  #endif
123    return 0;
124}

Related

codeforces #319 div 2 E C. Points on Plane (分块)

·453 words·1 min
初识分快. 引一段题解: Let’s split rectangle 106 × 106 by vertical lines into 1000 rectangles 103 × 106. Let’s number them from left to right. We’re going to pass through points rectangle by rectangle. Inside the rectangle we’re going to pass the points in increasing order of y-coordinate if the number of rectangle is even and in decreasing if it’s odd. Let’s calculate the maximum length of such a way. The coordinates are independent. By y-coordinate we’re passing 1000 rectangles from0 to 106, 109 in total. By x-coordinate we’re spending 1000 to get to the next point of current rectangle and 2000 to get to next rectangle. That means, 2 * 109 + 2000000 in total, which perfectly fits.

codeforces 519 C. A and B and Team Training

·271 words·1 min
http://codeforces.com/problemset/problem/519/C 题意:两种组队方式,3人一组,1个大牛+2个蒟蒻或者1个蒟蒻+2个大牛。给定大牛和蒟蒻的个数。问最多能组多少队。 思路:线性规划。设两种队分别有x,y个即可。 突然发现这题以前做过。。。比当时的代码简单了一些。还不错。

codeforces 522 A. Vanya and Table

·482 words·1 min
http://codeforces.com/problemset/problem/552/A 题意:一个100*100的网格。然后给n个矩形。每个格子中填上包含这个格子的矩形的个数。最后问所有格子的和。 思路:树状数组搞得…然而..直接求所有矩形面积的和就可以啊喂。。o(n)。。。111qqz你个炒鸡大菜鸡。

codeforces 1 B. Spreadsheets

·632 words·2 mins
http://codeforces.com/problemset/problem/1/B 题意:给出了两种表格的表示方法。要求互相转化。 思路:直接模拟即可。注意和一般的进制转化不同的是,26进制对应的是1到26而不是0到25,所以要记得处理下借位。 代码实现 1/* *********************************************** 2Author :111qqz 3Created Time :2015年12月13日 星期日 19时46分09秒 4File Name :code/cf/problem/1B.cpp 5************************************************ */ 6 7#include <cstdio> 8#include <cstring> 9#include <iostream> 10#include <algorithm> 11#include <vector> 12#include <queue> 13#include <set> 14#include <map> 15#include <string> 16#include <cmath> 17#include <cstdlib> 18#include <ctime> 19#define fst first 20#define sec second 21#define lson l,m,rt<<1 22#define rson m+1,r,rt<<1|1 23#define ms(a,x) memset(a,x,sizeof(a)) 24typedef long long LL; 25#define pi pair < int ,int > 26#define MP make_pair 27 28using namespace std; 29const double eps = 1E-8; 30const int dx4[4]={1,0,0,-1}; 31const int dy4[4]={0,-1,1,0}; 32const int inf = 0x3f3f3f3f; 33 34char st[100]; 35int a26[100]; 36int a10[100]; 37 38void pre() 39{ 40 a26[0]=1; 41 for ( int i = 1 ;i<=5; i++) 42 { 43 a26[i] = a26[i-1]*26; 44// cout<<"i:"<<i<<" "<<a26[i]<<endl; 45 } 46 a10[0] = 1; 47 for ( int i = 1 ; i<=7 ; i++) 48 { 49 a10[i] = a10[i-1]*10; 50 } 51} 52void solve() 53{ 54 cin>>st; 55 int len = strlen(st); 56 int p1=-1,p2=-1; 57 for ( int i = 0 ; i < len ; i++) 58 { 59 char ch = st[i]; 60 if (ch>='A'&&ch<='Z') 61 { 62 if (p1==-1) 63 { 64 p1 = i ; 65 } 66 else 67 { 68 p2 = i; 69 break; 70 } 71 } 72 } 73 if (p2-p1==1||p2==-1) 74 { 75 int dig = 0 ; 76 int alp = 0; 77 int sum = 0 ; 78 int sum2 = 0 ; 79 for ( int i = len -1 ; i>= 0 ; i--) 80 { 81 char ch = st[i]; 82 if (!isdigit(ch)) 83 { 84 sum += (ch-'A'+1)*a26[alp]; 85 alp++; 86 } 87 else 88 { 89 sum2+= (ch-'0')*a10[dig]; 90 dig++; 91 } 92 } 93 printf("R%d\n",sum2,sum); 94 } 95 else 96 { 97 int dig1 = 0 ; 98 int dig2 = 0; 99 int sum1 = 0 ; 100 int sum2 = 0 ; 101 int p = 1; 102 for ( int i = len-1 ; i>= 0 ; i-- ) 103 { 104 char ch = st[i]; 105 if (isdigit(ch)) 106 { 107 // cout<<"ch:"<<ch<<endl; 108 if (p) 109 { 110 sum1+=(ch-'0')*a10[dig1]; 111 // cout<<"sum1:"<<sum1<<endl; 112 dig1++; 113 } 114 else 115 { 116 sum2+=(ch-'0')*a10[dig2]; 117 dig2++; 118 } 119 120 121 } 122 else 123 { 124 p = 0 ; 125 } 126 } 127// cout<<"sum1:"<<sum1<<" sum2:"<<sum2<<endl; 128 int cnt = 0 ; 129 int b[50]; 130 while(sum1) 131 { 132 cnt++; 133 b[cnt] = sum1; 134 sum1/=26; 135// cout<<"sum1::::"<<sum1<<endl; 136 } 137// cout<<"cnt:"<<cnt<<endl; 138 for ( int i = 1 ; i <= cnt -1 ; i ++) 139 { 140 if (b[i]<=0) 141 { 142 b[i]+=26; 143 b[i+1]--; 144 } 145 } 146 while (b[cnt]==0) cnt--; 147 148 for ( int i = cnt ; i >=1 ; i--) 149 { 150 151 // cout<<"b[i]:"<<b[i]<<endl; 152 cout<<char(b[i]+'A'-1); 153 } 154 cout<<sum2<<endl; 155 156 157 158 159 } 160 161 162} 163int main() 164{ 165 #ifndef ONLINE_JUDGE 166 freopen("code/in.txt","r",stdin); 167 #endif 168 169 pre(); 170 int T; 171 cin>>T; 172 while (T--) 173 { 174 solve(); 175 } 176 177 #ifndef ONLINE_JUDGE 178 fclose(stdin); 179 #endif 180 return 0; 181}

codeforces 526 B Om Nom and Dark Park

·281 words·1 min
http://codeforces.com/contest/526/problem/B 题意:有一棵完全二叉树。每条边上有一定数量的路灯。问最少需要添加多少个路灯。使得根节点道叶子节点的每一条路径上的路灯数量一样。 思路:同叶子节点网上更新即可。 代码实现 1/* *********************************************** 2Author :111qqz 3Created Time :2015年12月11日 星期五 16时57分27秒 4File Name :code/cf/problem/526B.cpp 5************************************************ */ 6 7#include <cstdio> 8#include <cstring> 9#include <iostream> 10#include <algorithm> 11#include <vector> 12#include <queue> 13#include <set> 14#include <map> 15#include <string> 16#include <cmath> 17#include <cstdlib> 18#include <ctime> 19#define fst first 20#define sec second 21#define lson l,m,rt<<1 22#define rson m+1,r,rt<<1|1 23#define ms(a,x) memset(a,x,sizeof(a)) 24typedef long long LL; 25#define pi pair < int ,int > 26#define MP make_pair 27 28using namespace std; 29const double eps = 1E-8; 30const int dx4[4]={1,0,0,-1}; 31const int dy4[4]={0,-1,1,0}; 32const int inf = 0x3f3f3f3f; 33const int N=3E3+7; 34int n; 35int a[N]; 36int main() 37{ 38 #ifndef ONLINE_JUDGE 39 freopen("code/in.txt","r",stdin); 40 #endif 41 cin>>n; 42 LL res = 0 ; 43 n = (2<<n)-2; 44 for ( int i = 1 ; i <= n ; i++) 45 { 46 cin>>a[i]; 47 } 48 for ( int i = n ; i >= 1 ; i-=2) 49 { 50 res +=abs(a[i]-a[i-1]); 51 if (i!=2) a[i/2-1] += max(a[i],a[i-1]); 52 } 53 cout<<res<<endl; 54 55 56 #ifndef ONLINE_JUDGE 57 fclose(stdin); 58 #endif 59 return 0; 60}